diff --git a/components/ChatInputBox/ChatInputBox.tsx b/components/ChatInputBox/ChatInputBox.tsx index 13b4c88..65cc5ce 100644 --- a/components/ChatInputBox/ChatInputBox.tsx +++ b/components/ChatInputBox/ChatInputBox.tsx @@ -995,7 +995,11 @@ const PhotoStoryModal = ({ }; const router = useRouter(); const taskProgressRef = useRef(taskProgress); + const [cursorPosition, setCursorPosition] = useState(0); + const handleCursorPositionChange = (position: number) => { + setCursorPosition(position); + }; useEffect(() => { taskProgressRef.current = taskProgress; }, [taskProgress]); @@ -1279,6 +1283,8 @@ const PhotoStoryModal = ({ diff --git a/components/common/HighlightEditor.tsx b/components/common/HighlightEditor.tsx index 943bfb0..c69b9d7 100644 --- a/components/common/HighlightEditor.tsx +++ b/components/common/HighlightEditor.tsx @@ -14,6 +14,8 @@ export const HighlightEditor = ({ onContentChange, type, placeholder, + cursorPosition, + onCursorPositionChange, }: { /** 内容 */ content: string; @@ -23,7 +25,13 @@ export const HighlightEditor = ({ type: string; /**提示语 */ placeholder: string; + /** 光标位置 */ + cursorPosition?: number; + /** 光标位置变化回调 */ + onCursorPositionChange?: (position: number) => void; }) => { + console.log(44444); + const editor = useEditor({ extensions: [ StarterKit, @@ -40,8 +48,22 @@ export const HighlightEditor = ({ onContentChange(""); return; } - // 直接传递文本内容,不进行复杂的标签重建 - onContentChange(textContent); + + // 获取 HTML 内容并转换 highlight-text 标签 + const htmlContent = editor.getHTML(); + const convertedContent = htmlContent.replace( + /]*type="([^"]*)"[^>]*text="([^"]*)"[^>]*>([^<]*)<\/highlight-text>/g, + '<$1>$2' + ); + + console.log('convertedContent:::', convertedContent); + + // 保存当前光标位置 + const { from } = editor.state.selection; + onCursorPositionChange?.(from); + + // 传递转换后的内容 + onContentChange(convertedContent); }, editorProps: { handleKeyDown: (view, event) => { @@ -77,15 +99,25 @@ export const HighlightEditor = ({ editor.commands.clearContent(true); return; } - // 将带标签的内容转换为高亮显示(支持新的标签格式) const htmlContent = content.replace( new RegExp(`<${type}[^>]*>([^<]+)<\/${type}>`, "g"), '$1' ); + console.log('111111', 111111) editor.commands.setContent(htmlContent, { emitUpdate: false }); + + // 恢复光标位置 + if (cursorPosition && cursorPosition > 0) { + // 确保光标位置不超出文档范围 + const docSize = editor.state.doc.content.size; + const safePosition = Math.min(cursorPosition, docSize); + if (safePosition > 0) { + editor.commands.setTextSelection(safePosition); + } + } } - }, [content, editor]); + }, [content, editor, cursorPosition]); return (