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 19 </template>
20 20  
21 21 <script lang="ts">
22   - import type { RawEditorSettings } from 'tinymce';
  22 + import type { Editor, RawEditorSettings } from 'tinymce';
23 23 import tinymce from 'tinymce/tinymce';
24 24 import 'tinymce/themes/silver';
25 25 import 'tinymce/icons/default/icons';
... ... @@ -60,8 +60,8 @@
60 60 ref,
61 61 unref,
62 62 watch,
63   - onUnmounted,
64 63 onDeactivated,
  64 + onBeforeUnmount,
65 65 } from 'vue';
66 66 import ImgUpload from './ImgUpload.vue';
67 67 import { toolbar, plugins } from './tinymce';
... ... @@ -114,9 +114,9 @@
114 114 components: { ImgUpload },
115 115 inheritAttrs: false,
116 116 props: tinymceProps,
117   - emits: ['change', 'update:modelValue'],
  117 + emits: ['change', 'update:modelValue', 'inited', 'init-error'],
118 118 setup(props, { emit, attrs }) {
119   - const editorRef = ref();
  119 + const editorRef = ref<Nullable<Editor>>(null);
120 120 const fullscreen = ref(false);
121 121 const tinymceId = ref<string>(buildShortUUID('tiny-vue'));
122 122 const elRef = ref<Nullable<HTMLElement>>(null);
... ... @@ -165,7 +165,7 @@
165 165 content_css:
166 166 publicPath + 'resource/tinymce/skins/ui/' + skinName.value + '/content.min.css',
167 167 ...options,
168   - setup: (editor) => {
  168 + setup: (editor: Editor) => {
169 169 editorRef.value = editor;
170 170 editor.on('init', (e) => initSetup(e));
171 171 },
... ... @@ -194,9 +194,7 @@
194 194 );
195 195  
196 196 onMountedOrActivated(() => {
197   - if (initOptions.value.inline) {
198   - tinymceId.value = unref(initOptions).selector!;
199   - } else {
  197 + if (!initOptions.value.inline) {
200 198 tinymceId.value = buildShortUUID('tiny-vue');
201 199 }
202 200 nextTick(() => {
... ... @@ -206,7 +204,7 @@
206 204 });
207 205 });
208 206  
209   - onUnmounted(() => {
  207 + onBeforeUnmount(() => {
210 208 destory();
211 209 });
212 210  
... ... @@ -216,7 +214,7 @@
216 214  
217 215 function destory() {
218 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 223 if (el) {
226 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 236 function initSetup(e) {
... ...