Commit fb43fad555b093af23194bdb3670bc1347c0010f

Authored by 无木
1 parent 8e013774

fix(tinymce): fixed `tinymce` destory method

修复tinymce销毁方法可能出现异常的问题
src/components/Tinymce/src/Editor.vue
@@ -19,7 +19,7 @@ @@ -19,7 +19,7 @@
19 </template> 19 </template>
20 20
21 <script lang="ts"> 21 <script lang="ts">
22 - import type { RawEditorSettings } from 'tinymce'; 22 + import type { Editor, RawEditorSettings } from 'tinymce';
23 import tinymce from 'tinymce/tinymce'; 23 import tinymce from 'tinymce/tinymce';
24 import 'tinymce/themes/silver'; 24 import 'tinymce/themes/silver';
25 import 'tinymce/icons/default/icons'; 25 import 'tinymce/icons/default/icons';
@@ -60,8 +60,8 @@ @@ -60,8 +60,8 @@
60 ref, 60 ref,
61 unref, 61 unref,
62 watch, 62 watch,
63 - onUnmounted,  
64 onDeactivated, 63 onDeactivated,
  64 + onBeforeUnmount,
65 } from 'vue'; 65 } from 'vue';
66 import ImgUpload from './ImgUpload.vue'; 66 import ImgUpload from './ImgUpload.vue';
67 import { toolbar, plugins } from './tinymce'; 67 import { toolbar, plugins } from './tinymce';
@@ -114,9 +114,9 @@ @@ -114,9 +114,9 @@
114 components: { ImgUpload }, 114 components: { ImgUpload },
115 inheritAttrs: false, 115 inheritAttrs: false,
116 props: tinymceProps, 116 props: tinymceProps,
117 - emits: ['change', 'update:modelValue'], 117 + emits: ['change', 'update:modelValue', 'inited', 'init-error'],
118 setup(props, { emit, attrs }) { 118 setup(props, { emit, attrs }) {
119 - const editorRef = ref(); 119 + const editorRef = ref<Nullable<Editor>>(null);
120 const fullscreen = ref(false); 120 const fullscreen = ref(false);
121 const tinymceId = ref<string>(buildShortUUID('tiny-vue')); 121 const tinymceId = ref<string>(buildShortUUID('tiny-vue'));
122 const elRef = ref<Nullable<HTMLElement>>(null); 122 const elRef = ref<Nullable<HTMLElement>>(null);
@@ -165,7 +165,7 @@ @@ -165,7 +165,7 @@
165 content_css: 165 content_css:
166 publicPath + 'resource/tinymce/skins/ui/' + skinName.value + '/content.min.css', 166 publicPath + 'resource/tinymce/skins/ui/' + skinName.value + '/content.min.css',
167 ...options, 167 ...options,
168 - setup: (editor) => { 168 + setup: (editor: Editor) => {
169 editorRef.value = editor; 169 editorRef.value = editor;
170 editor.on('init', (e) => initSetup(e)); 170 editor.on('init', (e) => initSetup(e));
171 }, 171 },
@@ -194,9 +194,7 @@ @@ -194,9 +194,7 @@
194 ); 194 );
195 195
196 onMountedOrActivated(() => { 196 onMountedOrActivated(() => {
197 - if (initOptions.value.inline) {  
198 - tinymceId.value = unref(initOptions).selector!;  
199 - } else { 197 + if (!initOptions.value.inline) {
200 tinymceId.value = buildShortUUID('tiny-vue'); 198 tinymceId.value = buildShortUUID('tiny-vue');
201 } 199 }
202 nextTick(() => { 200 nextTick(() => {
@@ -206,7 +204,7 @@ @@ -206,7 +204,7 @@
206 }); 204 });
207 }); 205 });
208 206
209 - onUnmounted(() => { 207 + onBeforeUnmount(() => {
210 destory(); 208 destory();
211 }); 209 });
212 210
@@ -216,7 +214,7 @@ @@ -216,7 +214,7 @@
216 214
217 function destory() { 215 function destory() {
218 if (tinymce !== null) { 216 if (tinymce !== null) {
219 - tinymce?.remove?.(tinymceId.value as string); 217 + tinymce?.remove?.(unref(initOptions).selector!);
220 } 218 }
221 } 219 }
222 220
@@ -225,7 +223,14 @@ @@ -225,7 +223,14 @@
225 if (el) { 223 if (el) {
226 el.style.visibility = ''; 224 el.style.visibility = '';
227 } 225 }
228 - tinymce.init(unref(initOptions)); 226 + tinymce
  227 + .init(unref(initOptions))
  228 + .then((editor) => {
  229 + emit('inited', editor);
  230 + })
  231 + .catch((err) => {
  232 + emit('init-error', err);
  233 + });
229 } 234 }
230 235
231 function initSetup(e) { 236 function initSetup(e) {