|
1 | 1 | <template> |
2 | 2 | <div class="tinymce-container" :style="{ width: containerWidth }"> |
3 | | - <tinymce-editor |
4 | | - :id="id" |
5 | | - :init="initOptions" |
6 | | - :modelValue="tinymceContent" |
7 | | - @update:modelValue="handleChange" |
8 | | - :tinymceScriptSrc="tinymceScriptSrc" |
9 | | - ></tinymce-editor> |
| 3 | + <textarea :id="tinymceId" visibility="hidden" ref="elRef"></textarea> |
10 | 4 | </div> |
11 | 5 | </template> |
12 | 6 |
|
13 | 7 | <script lang="ts"> |
14 | | - import TinymceEditor from './lib'; // TinyMCE vue wrapper |
15 | | - import { defineComponent, computed } from 'vue'; |
| 8 | + import { |
| 9 | + defineComponent, |
| 10 | + computed, |
| 11 | + onMounted, |
| 12 | + nextTick, |
| 13 | + ref, |
| 14 | + unref, |
| 15 | + watch, |
| 16 | + onUnmounted, |
| 17 | + onDeactivated, |
| 18 | + } from 'vue'; |
16 | 19 | import { basicProps } from './props'; |
17 | 20 | import toolbar from './toolbar'; |
18 | 21 | import plugins from './plugins'; |
| 22 | + import { getTinymce } from './getTinymce'; |
| 23 | + import { useScript } from '/@/hooks/web/useScript'; |
| 24 | + import { snowUuid } from '/@/utils/uuid'; |
| 25 | + import { bindHandlers } from './helper'; |
19 | 26 |
|
20 | 27 | const CDN_URL = 'https://cdn.bootcdn.net/ajax/libs/tinymce/5.5.1'; |
| 28 | +
|
21 | 29 | const tinymceScriptSrc = `${CDN_URL}/tinymce.min.js`; |
22 | 30 |
|
23 | 31 | export default defineComponent({ |
24 | 32 | name: 'Tinymce', |
25 | | - components: { TinymceEditor }, |
26 | 33 | props: basicProps, |
27 | | - setup(props, { emit }) { |
| 34 | + emits: ['change', 'update:modelValue'], |
| 35 | + setup(props, { emit, attrs }) { |
| 36 | + const editorRef = ref<any>(null); |
| 37 | + const elRef = ref<Nullable<HTMLElement>>(null); |
| 38 | +
|
| 39 | + const tinymceId = computed(() => { |
| 40 | + return snowUuid('tiny-vue'); |
| 41 | + }); |
| 42 | +
|
28 | 43 | const tinymceContent = computed(() => { |
29 | | - return props.value; |
| 44 | + return props.modelValue; |
30 | 45 | }); |
31 | | - function handleChange(value: string) { |
32 | | - emit('change', value); |
33 | | - } |
| 46 | +
|
34 | 47 | const containerWidth = computed(() => { |
35 | 48 | const width = props.width; |
36 | | - // Test matches `100`, `'100'` |
37 | 49 | if (/^[\d]+(\.[\d]+)?$/.test(width.toString())) { |
38 | 50 | return `${width}px`; |
39 | 51 | } |
40 | 52 | return width; |
41 | 53 | }); |
| 54 | +
|
42 | 55 | const initOptions = computed(() => { |
43 | | - const { id, height, menubar } = props; |
| 56 | + const { height, menubar } = props; |
44 | 57 | return { |
45 | | - selector: `#${id}`, |
| 58 | + selector: `#${unref(tinymceId)}`, |
46 | 59 | height: height, |
47 | 60 | toolbar: toolbar, |
| 61 | + theme: 'silver', |
48 | 62 | menubar: menubar, |
49 | 63 | plugins: plugins, |
50 | 64 | // 语言包 |
51 | 65 | language_url: 'resource/tinymce/langs/zh_CN.js', |
52 | 66 | // 中文 |
53 | 67 | language: 'zh_CN', |
| 68 | + default_link_target: '_blank', |
| 69 | + link_title: false, |
| 70 | + advlist_bullet_styles: 'square', |
| 71 | + advlist_number_styles: 'default', |
| 72 | + object_resizing: false, |
| 73 | + setup: (editor: any) => { |
| 74 | + editorRef.value = editor; |
| 75 | + editor.on('init', (e: Event) => initSetup(e)); |
| 76 | + }, |
54 | 77 | }; |
55 | 78 | }); |
56 | | - return { containerWidth, initOptions, tinymceContent, handleChange, tinymceScriptSrc }; |
| 79 | +
|
| 80 | + const { toPromise } = useScript({ |
| 81 | + src: tinymceScriptSrc, |
| 82 | + }); |
| 83 | +
|
| 84 | + watch( |
| 85 | + () => attrs.disabled, |
| 86 | + () => { |
| 87 | + const editor = unref(editorRef); |
| 88 | + if (!editor) return; |
| 89 | + editor.setMode(attrs.disabled ? 'readonly' : 'design'); |
| 90 | + } |
| 91 | + ); |
| 92 | +
|
| 93 | + onMounted(() => { |
| 94 | + nextTick(() => { |
| 95 | + init(); |
| 96 | + }); |
| 97 | + }); |
| 98 | +
|
| 99 | + onUnmounted(() => { |
| 100 | + destory(); |
| 101 | + }); |
| 102 | +
|
| 103 | + onDeactivated(() => { |
| 104 | + destory(); |
| 105 | + }); |
| 106 | +
|
| 107 | + function destory() { |
| 108 | + if (getTinymce() !== null) { |
| 109 | + getTinymce().remove(unref(editorRef)); |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + function init() { |
| 114 | + toPromise().then(() => { |
| 115 | + initEditor(); |
| 116 | + }); |
| 117 | + } |
| 118 | +
|
| 119 | + function initEditor() { |
| 120 | + getTinymce().init(unref(initOptions)); |
| 121 | + } |
| 122 | +
|
| 123 | + function initSetup(e: Event) { |
| 124 | + const editor = unref(editorRef); |
| 125 | + if (!editor) return; |
| 126 | + const value = props.modelValue || ''; |
| 127 | +
|
| 128 | + editor.setContent(value); |
| 129 | + bindModelHandlers(editor); |
| 130 | + bindHandlers(e, attrs, unref(editorRef)); |
| 131 | + } |
| 132 | +
|
| 133 | + function bindModelHandlers(editor: any) { |
| 134 | + const modelEvents = attrs.modelEvents ? attrs.modelEvents : null; |
| 135 | + const normalizedEvents = Array.isArray(modelEvents) ? modelEvents.join(' ') : modelEvents; |
| 136 | + watch( |
| 137 | + () => props.modelValue, |
| 138 | + (val: string, prevVal: string) => { |
| 139 | + if ( |
| 140 | + editor && |
| 141 | + typeof val === 'string' && |
| 142 | + val !== prevVal && |
| 143 | + val !== editor.getContent({ format: attrs.outputFormat }) |
| 144 | + ) { |
| 145 | + editor.setContent(val); |
| 146 | + } |
| 147 | + } |
| 148 | + ); |
| 149 | +
|
| 150 | + editor.on(normalizedEvents ? normalizedEvents : 'change keyup undo redo', () => { |
| 151 | + emit('update:modelValue', editor.getContent({ format: attrs.outputFormat })); |
| 152 | + }); |
| 153 | + } |
| 154 | +
|
| 155 | + function handleChange(value: string) { |
| 156 | + emit('change', value); |
| 157 | + } |
| 158 | + return { |
| 159 | + containerWidth, |
| 160 | + initOptions, |
| 161 | + tinymceContent, |
| 162 | + handleChange, |
| 163 | + tinymceScriptSrc, |
| 164 | + elRef, |
| 165 | + tinymceId, |
| 166 | + }; |
57 | 167 | }, |
58 | 168 | }); |
59 | 169 | </script> |
|
0 commit comments