|
| 1 | +<template> |
| 2 | + <h2>Using CKEditor 5 from CDN in Vue 3</h2> |
| 3 | + |
| 4 | + <ckeditor |
| 5 | + v-if="TestEditor" |
| 6 | + v-model="data" |
| 7 | + tag-name="textarea" |
| 8 | + :disable-two-way-data-binding="isTwoWayDataBindingDisabled" |
| 9 | + :editor="TestEditor" |
| 10 | + :config="config" |
| 11 | + :disabled="disabled" |
| 12 | + @ready="onReady" |
| 13 | + @focus="onFocus" |
| 14 | + @blur="onBlur" |
| 15 | + @input="onInput" |
| 16 | + @destroy="onDestroy" |
| 17 | + /> |
| 18 | + |
| 19 | + <button @click="toggleEditorDisabled"> |
| 20 | + {{ disabled ? 'Enable' : 'Disable' }} editor |
| 21 | + </button> |
| 22 | + |
| 23 | + <button @click="toggleTwoWayBinding"> |
| 24 | + {{ isTwoWayDataBindingDisabled ? 'Enable' : 'Disable' }} two way binding |
| 25 | + </button> |
| 26 | + |
| 27 | + <button |
| 28 | + v-if="isTwoWayDataBindingDisabled" |
| 29 | + @click="setEditorData" |
| 30 | + > |
| 31 | + Set editor data |
| 32 | + </button> |
| 33 | + |
| 34 | + <h2>Live editor data</h2> |
| 35 | + |
| 36 | + <textarea v-model="data" /> |
| 37 | +</template> |
| 38 | + |
| 39 | +<script setup lang="ts"> |
| 40 | +import { ref, reactive, computed } from 'vue'; |
| 41 | +import useCKEditorCloud from '../../src/useCKEditorCloud'; |
| 42 | +import type { EventInfo, ClassicEditor } from 'https://cdn.ckeditor.com/typings/ckeditor5.d.ts'; |
| 43 | +
|
| 44 | +// Load CKEditor from the CDN |
| 45 | +const cloud = useCKEditorCloud( { |
| 46 | + version: '43.0.0' |
| 47 | +} ); |
| 48 | +
|
| 49 | +const TestEditor = computed<typeof ClassicEditor | null>( () => { |
| 50 | + if ( !cloud.data.value ) { |
| 51 | + return null; |
| 52 | + } |
| 53 | +
|
| 54 | + const { |
| 55 | + ClassicEditor: BaseEditor, Paragraph, |
| 56 | + Essentials, Heading, Bold, Italic |
| 57 | + } = cloud.data.value.CKEditor; |
| 58 | +
|
| 59 | + return class TestEditor extends BaseEditor { |
| 60 | + static builtinPlugins = [ |
| 61 | + Essentials, |
| 62 | + Paragraph, |
| 63 | + Heading, |
| 64 | + Bold, |
| 65 | + Italic |
| 66 | + ]; |
| 67 | + }; |
| 68 | +} ); |
| 69 | +
|
| 70 | +// State |
| 71 | +const editorInstance = ref<ClassicEditor | null>( null ); |
| 72 | +const data = ref( '<p>Hello world!</p>' ); |
| 73 | +const disabled = ref( false ); |
| 74 | +const isTwoWayDataBindingDisabled = ref( false ); |
| 75 | +const config = reactive( { |
| 76 | + toolbar: [ 'heading', '|', 'bold', 'italic' ] |
| 77 | +} ); |
| 78 | +
|
| 79 | +// Methods |
| 80 | +function setEditorData() { |
| 81 | + data.value = editorInstance.value?.getData() ?? ''; |
| 82 | +} |
| 83 | +
|
| 84 | +function toggleTwoWayBinding() { |
| 85 | + isTwoWayDataBindingDisabled.value = !isTwoWayDataBindingDisabled.value; |
| 86 | +} |
| 87 | +
|
| 88 | +function toggleEditorDisabled() { |
| 89 | + disabled.value = !disabled.value; |
| 90 | +} |
| 91 | +
|
| 92 | +function onReady( editor: ClassicEditor ) { |
| 93 | + editorInstance.value = editor; |
| 94 | +
|
| 95 | + console.log( 'Editor is ready.', { editor } ); |
| 96 | +} |
| 97 | +
|
| 98 | +function onFocus( event: EventInfo, editor: ClassicEditor ) { |
| 99 | + console.log( 'Editor focused.', { event, editor } ); |
| 100 | +} |
| 101 | +
|
| 102 | +function onBlur( event: EventInfo, editor: ClassicEditor ) { |
| 103 | + console.log( 'Editor blurred.', { event, editor } ); |
| 104 | +} |
| 105 | +
|
| 106 | +function onInput( data: string, event: EventInfo, editor: ClassicEditor ) { |
| 107 | + console.log( 'Editor data input.', { event, editor, data } ); |
| 108 | +} |
| 109 | +
|
| 110 | +function onDestroy() { |
| 111 | + console.log( 'Editor destroyed.' ); |
| 112 | +} |
| 113 | +</script> |
| 114 | + |
| 115 | +<style> |
| 116 | +body { |
| 117 | + max-width: 800px; |
| 118 | + margin: 20px auto; |
| 119 | +} |
| 120 | +
|
| 121 | +textarea { |
| 122 | + width: 100%; |
| 123 | + height: 100px; |
| 124 | + font-family: monospace; |
| 125 | +} |
| 126 | +
|
| 127 | +button { |
| 128 | + margin-top: 10px; |
| 129 | +} |
| 130 | +</style> |
0 commit comments