fix(editor): 修复建议组件空项处理和粘贴功能优化

修复建议组件在空项时的处理逻辑,避免潜在错误。优化编辑器粘贴功能:
1. 处理空剪贴板数据时更安全
2. 添加纯文本粘贴支持
3. 简化图片节点更新逻辑
This commit is contained in:
Phoenix 2025-07-07 13:41:26 +08:00
parent 4863bc2220
commit a438174af4
2 changed files with 19 additions and 6 deletions

View File

@ -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
}
}
})

View File

@ -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()
},