A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
refactor(editor): improve editor component functionality and code clarity
robin7a4b57d1ffdb
diff --git a/ui/src/components/Editor/ToolBars/heading.tsx b/ui/src/components/Editor/ToolBars/heading.tsxindex 4b49212c..bcce69ae 100644--- a/ui/src/components/Editor/ToolBars/heading.tsx+++ b/ui/src/components/Editor/ToolBars/heading.tsx@@ -62,7 +62,7 @@ const Heading = () => {const [isLocked, setLockState] = useState(false);const [currentEditor, setCurrentEditor] = useState<Editor | null>(null);- const handleClick = (level: Level = 2, label = '大标题') => {+ const handleClick = (level: Level = 2, label?: string) => {if (!currentEditor) {return;}diff --git a/ui/src/components/Editor/ToolBars/image.tsx b/ui/src/components/Editor/ToolBars/image.tsxindex 67a79b3c..52b2e546 100644--- a/ui/src/components/Editor/ToolBars/image.tsx+++ b/ui/src/components/Editor/ToolBars/image.tsx@@ -32,8 +32,8 @@ const Image = () => {const editor = useContext(EditorContext);const [editorState, setEditorState] = useState<Editor | null>(editor);- // 当 editor 改变时,更新 editor state- // 这样切换编辑器模式时,事件监听器会重新绑定+ // Update editor state when editor context changes+ // This ensures event listeners are re-bound when switching editor modesuseEffect(() => {if (editor) {setEditorState(editor);@@ -329,20 +329,18 @@ const Image = () => {if (!editorState) {return undefined;}- // 绑定事件监听器+editorState.on('dragenter', dragenter);editorState.on('dragover', dragover);editorState.on('drop', drop);editorState.on('paste', paste);+return () => {- // 清理事件监听器editorState.off('dragenter', dragenter);editorState.off('dragover', dragover);editorState.off('drop', drop);editorState.off('paste', paste);};- // 注意:dragenter, dragover, drop, paste 函数在组件生命周期内是稳定的- // 它们不依赖任何会变化的值,所以不需要加入依赖项}, [editorState]);useEffect(() => {diff --git a/ui/src/components/Editor/toolItem.tsx b/ui/src/components/Editor/toolItem.tsxindex c0fdf175..39f82d70 100644--- a/ui/src/components/Editor/toolItem.tsx+++ b/ui/src/components/Editor/toolItem.tsx@@ -63,7 +63,7 @@ const ToolItem: FC<IProps> = (props) => {editor?.addKeyMap({[key]: () => {onClick?.(editor);- return true; // Command 类型要求返回 boolean+ return true;},});});diff --git a/ui/src/components/Editor/types.ts b/ui/src/components/Editor/types.tsindex e7d23081..48f69322 100644--- a/ui/src/components/Editor/types.ts+++ b/ui/src/components/Editor/types.ts@@ -61,7 +61,6 @@ export interface ExtendEditor {setSelection: (anchor: Position, head?: Position) => void;setReadOnly: (readOnly: boolean) => void;- // 底层方法(供编辑器内部使用,不推荐工具栏直接使用)wrapText: (before: string, after?: string, defaultText?: string) => void;replaceLines: (replace: Parameters<Array<string>['map']>[0],@@ -69,37 +68,29 @@ export interface ExtendEditor {) => void;appendBlock: (content: string) => void;- // 语义化高级方法(工具栏推荐使用)- // 文本格式insertBold: (text?: string) => void;insertItalic: (text?: string) => void;insertCode: (text?: string) => void;insertStrikethrough: (text?: string) => void;- // 块级元素insertHeading: (level: Level, text?: string) => void;insertBlockquote: (text?: string) => void;insertCodeBlock: (language?: string, code?: string) => void;insertHorizontalRule: () => void;- // 列表insertOrderedList: () => void;insertUnorderedList: () => void;toggleOrderedList: () => void;toggleUnorderedList: () => void;- // 链接和媒体insertLink: (url: string, text?: string) => void;insertImage: (url: string, alt?: string) => void;- // 表格insertTable: (rows?: number, cols?: number) => void;- // 缩进indent: () => void;outdent: () => void;- // 状态查询isBold: () => boolean;isItalic: () => boolean;isHeading: (level?: number) => boolean;diff --git a/ui/src/components/Editor/utils/index.ts b/ui/src/components/Editor/utils/index.tsindex 5ba7192c..eb4ad16e 100644--- a/ui/src/components/Editor/utils/index.ts+++ b/ui/src/components/Editor/utils/index.ts@@ -95,10 +95,8 @@ export function htmlRender(el: HTMLElement | null, config?: htmlRenderConfig) {`;codeTool.innerHTML = str;- // Add copy button to pre tagpre.style.position = 'relative';- // 将 codeTool 和 pre 插入到 codeWrap 中, 并且使用 codeWrap 替换 precodeWrap.appendChild(codeTool);pre.parentNode?.replaceChild(codeWrap, pre);codeWrap.appendChild(pre);