diff --git a/src/views/home/components/Input.vue b/src/views/home/components/Input.vue index f0529eb..4dc8e35 100644 --- a/src/views/home/components/Input.vue +++ b/src/views/home/components/Input.vue @@ -378,7 +378,12 @@ // Insert tag at the current cursor position const selection = window.getSelection() - if (selection && selection.rangeCount > 0) { + + // 检查selection是否在editorRef内部,不在则强制使用editorRef + const isInEditor = + editorRef.value && selection && editorRef.value.contains(selection.anchorNode) + + if (isInEditor && selection && selection.rangeCount > 0) { const range = selection.getRangeAt(0) range.insertNode(tag) @@ -397,7 +402,7 @@ // ensure editor has focus editorRef.value && (editorRef.value as HTMLElement).focus() } else if (editorRef.value) { - // If no selection, append directly to editor and place caret after + // If no selection in editor, append directly to editor and place caret after editorRef.value.appendChild(tag) const zwsp = document.createTextNode('\u200B') editorRef.value.appendChild(zwsp)