|
1 | 1 | import cloneDeep from 'lodash/cloneDeep';
|
2 |
| -import { computed, nextTick, onMounted, reactive, Ref, ref, SetupContext, toRefs, watch } from 'vue'; |
| 2 | +import { computed, nextTick, onMounted, reactive, Ref, ref, SetupContext, toRefs, watch, onBeforeUnmount } from 'vue'; |
3 | 3 | import { debounce } from '../../../shared/utils';
|
4 | 4 | import { EditorMdProps, Mode } from '../editor-md-types';
|
5 | 5 | import { DEFAULT_TOOLBARS } from '../toolbar-config';
|
@@ -27,6 +27,7 @@ export function useEditorMd(props: EditorMdProps, ctx: SetupContext) {
|
27 | 27 | const renderRef = ref();
|
28 | 28 | const overlayRef = ref();
|
29 | 29 | const cursorRef = ref();
|
| 30 | + const containerRef = ref(); |
30 | 31 | const isHintShow = ref();
|
31 | 32 | const previewHtmlList: Ref<any[]> = ref([]);
|
32 | 33 | let editorIns: any;
|
@@ -214,12 +215,7 @@ export function useEditorMd(props: EditorMdProps, ctx: SetupContext) {
|
214 | 215 | const startPos = value.lastIndexOf(nowPrefix, cursor.ch);
|
215 | 216 | const endPos = value.indexOf(' ', cursor.ch) > -1 ? value.indexOf(' ', cursor.ch) : value.length;
|
216 | 217 | hint = value.slice(startPos, cursor.ch);
|
217 |
| - if ( |
218 |
| - startPos < 0 || |
219 |
| - !hint.includes(nowPrefix) || |
220 |
| - hint.endsWith(' ') || |
221 |
| - isImgRegx.test(hint) |
222 |
| - ) { |
| 218 | + if (startPos < 0 || !hint.includes(nowPrefix) || hint.endsWith(' ') || isImgRegx.test(hint)) { |
223 | 219 | cursorHint = '';
|
224 | 220 | cursorHintStart = -1;
|
225 | 221 | cursorHintEnd = -1;
|
@@ -342,12 +338,23 @@ export function useEditorMd(props: EditorMdProps, ctx: SetupContext) {
|
342 | 338 | }
|
343 | 339 | };
|
344 | 340 |
|
| 341 | + const onDocumentClick = (e: Event) => { |
| 342 | + if (isHintShow.value && e.target !== containerRef.value && !containerRef.value?.contains(e.target)) { |
| 343 | + hideHint(); |
| 344 | + } |
| 345 | + }; |
| 346 | + |
345 | 347 | onMounted(async () => {
|
346 | 348 | await import('codemirror/addon/display/placeholder.js');
|
347 | 349 | await import('codemirror/mode/markdown/markdown.js');
|
348 | 350 | const module = await import('codemirror');
|
349 | 351 | CodeMirror = module.default;
|
350 | 352 | initEditor();
|
| 353 | + document.addEventListener('click', onDocumentClick); |
| 354 | + }); |
| 355 | + |
| 356 | + onBeforeUnmount(() => { |
| 357 | + document.removeEventListener('click', onDocumentClick); |
351 | 358 | });
|
352 | 359 |
|
353 | 360 | watch(modelValue, (val: string) => {
|
@@ -398,6 +405,7 @@ export function useEditorMd(props: EditorMdProps, ctx: SetupContext) {
|
398 | 405 | overlayRef,
|
399 | 406 | cursorRef,
|
400 | 407 | renderRef,
|
| 408 | + containerRef, |
401 | 409 | toolbars,
|
402 | 410 | previewHtmlList,
|
403 | 411 | isHintShow,
|
|
0 commit comments