修复 在受控组件中直接使用contentEditable 报错问题

This commit is contained in:
北枳 2025-08-13 18:20:43 +08:00
parent 1b4cb95caa
commit 33e3ef3a1e

View File

@ -17,13 +17,25 @@ interface HighlightTextOptions {
}
export function HighlightText(props: ReactNodeViewProps) {
const { text, color } = props.node.attrs as HighlightTextAttributes
const { text: initialText, color } = props.node.attrs as HighlightTextAttributes
const [text, setText] = useState(initialText)
const handleInput = (e: React.FormEvent<HTMLSpanElement>) => {
const newText = e.currentTarget.textContent || ''
setText(newText)
// 通知Tiptap更新内容
props.updateAttributes({
text: newText
})
}
return (
<NodeViewWrapper
as="span"
data-alt="highlight-text"
contentEditable={true}
suppressContentEditableWarning={true}
onInput={handleInput}
className={`relative inline text-${color}-400 hover:text-${color}-300 transition-colors duration-200`}
>
{text}