Skip to content

Commit 7a4b57d

Browse files
author
robin
committed
refactor(editor): improve editor component functionality and code clarity
- Updated ToolItem component to ensure consistent return type for command functions. - Modified heading toolbar to allow optional label parameter in handleClick function. - Enhanced image component comments for clarity on editor state updates and event listener management. - Cleaned up utility functions in htmlRender to remove unnecessary comments and improve readability.
1 parent d87726b commit 7a4b57d

5 files changed

Lines changed: 6 additions & 19 deletions

File tree

ui/src/components/Editor/ToolBars/heading.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const Heading = () => {
6262
const [isLocked, setLockState] = useState(false);
6363
const [currentEditor, setCurrentEditor] = useState<Editor | null>(null);
6464

65-
const handleClick = (level: Level = 2, label = '大标题') => {
65+
const handleClick = (level: Level = 2, label?: string) => {
6666
if (!currentEditor) {
6767
return;
6868
}

ui/src/components/Editor/ToolBars/image.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ const Image = () => {
3232
const editor = useContext(EditorContext);
3333
const [editorState, setEditorState] = useState<Editor | null>(editor);
3434

35-
// editor 改变时,更新 editor state
36-
// 这样切换编辑器模式时,事件监听器会重新绑定
35+
// Update editor state when editor context changes
36+
// This ensures event listeners are re-bound when switching editor modes
3737
useEffect(() => {
3838
if (editor) {
3939
setEditorState(editor);
@@ -329,20 +329,18 @@ const Image = () => {
329329
if (!editorState) {
330330
return undefined;
331331
}
332-
// 绑定事件监听器
332+
333333
editorState.on('dragenter', dragenter);
334334
editorState.on('dragover', dragover);
335335
editorState.on('drop', drop);
336336
editorState.on('paste', paste);
337+
337338
return () => {
338-
// 清理事件监听器
339339
editorState.off('dragenter', dragenter);
340340
editorState.off('dragover', dragover);
341341
editorState.off('drop', drop);
342342
editorState.off('paste', paste);
343343
};
344-
// 注意:dragenter, dragover, drop, paste 函数在组件生命周期内是稳定的
345-
// 它们不依赖任何会变化的值,所以不需要加入依赖项
346344
}, [editorState]);
347345

348346
useEffect(() => {

ui/src/components/Editor/toolItem.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const ToolItem: FC<IProps> = (props) => {
6363
editor?.addKeyMap({
6464
[key]: () => {
6565
onClick?.(editor);
66-
return true; // Command 类型要求返回 boolean
66+
return true;
6767
},
6868
});
6969
});

ui/src/components/Editor/types.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,45 +61,36 @@ export interface ExtendEditor {
6161
setSelection: (anchor: Position, head?: Position) => void;
6262
setReadOnly: (readOnly: boolean) => void;
6363

64-
// 底层方法(供编辑器内部使用,不推荐工具栏直接使用)
6564
wrapText: (before: string, after?: string, defaultText?: string) => void;
6665
replaceLines: (
6766
replace: Parameters<Array<string>['map']>[0],
6867
symbolLen?: number,
6968
) => void;
7069
appendBlock: (content: string) => void;
7170

72-
// 语义化高级方法(工具栏推荐使用)
73-
// 文本格式
7471
insertBold: (text?: string) => void;
7572
insertItalic: (text?: string) => void;
7673
insertCode: (text?: string) => void;
7774
insertStrikethrough: (text?: string) => void;
7875

79-
// 块级元素
8076
insertHeading: (level: Level, text?: string) => void;
8177
insertBlockquote: (text?: string) => void;
8278
insertCodeBlock: (language?: string, code?: string) => void;
8379
insertHorizontalRule: () => void;
8480

85-
// 列表
8681
insertOrderedList: () => void;
8782
insertUnorderedList: () => void;
8883
toggleOrderedList: () => void;
8984
toggleUnorderedList: () => void;
9085

91-
// 链接和媒体
9286
insertLink: (url: string, text?: string) => void;
9387
insertImage: (url: string, alt?: string) => void;
9488

95-
// 表格
9689
insertTable: (rows?: number, cols?: number) => void;
9790

98-
// 缩进
9991
indent: () => void;
10092
outdent: () => void;
10193

102-
// 状态查询
10394
isBold: () => boolean;
10495
isItalic: () => boolean;
10596
isHeading: (level?: number) => boolean;

ui/src/components/Editor/utils/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,8 @@ export function htmlRender(el: HTMLElement | null, config?: htmlRenderConfig) {
9595
`;
9696
codeTool.innerHTML = str;
9797

98-
// Add copy button to pre tag
9998
pre.style.position = 'relative';
10099

101-
// 将 codeTool 和 pre 插入到 codeWrap 中, 并且使用 codeWrap 替换 pre
102100
codeWrap.appendChild(codeTool);
103101
pre.parentNode?.replaceChild(codeWrap, pre);
104102
codeWrap.appendChild(pre);

0 commit comments

Comments
 (0)