From a438174af4699fb1fae2fdd49ee1d305dded1be5 Mon Sep 17 00:00:00 2001 From: Phoenix <64720302+Concur-max@users.noreply.github.com> Date: Mon, 7 Jul 2025 13:41:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(editor):=20=E4=BF=AE=E5=A4=8D=E5=BB=BA?= =?UTF-8?q?=E8=AE=AE=E7=BB=84=E4=BB=B6=E7=A9=BA=E9=A1=B9=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=92=8C=E7=B2=98=E8=B4=B4=E5=8A=9F=E8=83=BD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复建议组件在空项时的处理逻辑,避免潜在错误。优化编辑器粘贴功能: 1. 处理空剪贴板数据时更安全 2. 添加纯文本粘贴支持 3. 简化图片节点更新逻辑 --- src/components/editor/TiptapEditor.vue | 21 +++++++++++++++------ src/components/editor/suggestion.js | 4 ++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/components/editor/TiptapEditor.vue b/src/components/editor/TiptapEditor.vue index 8194aca..69a68e8 100644 --- a/src/components/editor/TiptapEditor.vue +++ b/src/components/editor/TiptapEditor.vue @@ -236,7 +236,8 @@ const editor = useEditor({ }, editorProps: { handlePaste: (view, event) => { - const items = (event.clipboardData || event.originalEvent.clipboardData).items + const clipboardData = event.clipboardData || event.originalEvent.clipboardData + const items = clipboardData.items for (const item of items) { if (item.type.indexOf('image') === 0) { event.preventDefault() @@ -261,9 +262,7 @@ const editor = useEditor({ const pos = findImagePos(tempUrl) if (pos !== -1) { const { tr } = view.state - view.dispatch( - tr.setNodeMarkup(pos, null, { src: data.ori_url }) - ) + view.dispatch(tr.setNodeMarkup(pos, null, { src: data.ori_url })) } } else { window['$message'].error(message || '图片上传失败') @@ -279,10 +278,20 @@ const editor = useEditor({ URL.revokeObjectURL(tempUrl) }) - return true + return true // Handled } } - return false + + // If no image was handled, check for text and paste as plain text. + const text = clipboardData.getData('text/plain') + if (text) { + event.preventDefault() + const { state, dispatch } = view + dispatch(state.tr.insertText(text)) + return true // Handled + } + + return false // Fallback for other cases } } }) diff --git a/src/components/editor/suggestion.js b/src/components/editor/suggestion.js index 2c43b77..83412c2 100644 --- a/src/components/editor/suggestion.js +++ b/src/components/editor/suggestion.js @@ -99,10 +99,14 @@ export default { this.onExit() return true } + if(!component?.props.items?.length){ + return false + } return component.ref.onKeyDown(props) }, onExit() { + console.log('component.element',component.element) component.element.remove() component.destroy() },