Skip to content

Commit fb5b685

Browse files
Hemomoo邓亮
andauthored
feat(编辑器): 实现自定义Tab键处理逻辑并移除冗余表格扩展 (#321)
将Tab键处理逻辑从TextAlign扩展迁移到页面组件中,实现更精细的控制 移除未使用的表格相关扩展(TableCell, TableHeader, TableRow) Co-authored-by: 邓亮 <787615673@qq.com>
1 parent f817280 commit fb5b685

2 files changed

Lines changed: 29 additions & 32 deletions

File tree

apps/DocFlow/src/app/docs/[room]/page.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,34 @@ export default function DocumentPage() {
328328
class: 'min-h-full',
329329
spellcheck: 'false',
330330
},
331+
handleKeyDown: (view, event) => {
332+
if (event.key === 'Tab') {
333+
if (event.shiftKey) {
334+
const { state } = view;
335+
const { from } = state.selection;
336+
const $from = state.doc.resolve(from);
337+
const startOfLine = $from.start($from.depth);
338+
const textBeforeCursor = state.doc.textBetween(startOfLine, from);
339+
340+
if (textBeforeCursor.endsWith(' ')) {
341+
const deleteFrom = Math.max(startOfLine, from - 2);
342+
view.dispatch(state.tr.deleteRange(deleteFrom, from));
343+
344+
return true;
345+
} else if (textBeforeCursor.endsWith(' ')) {
346+
view.dispatch(state.tr.deleteRange(from - 1, from));
347+
348+
return true;
349+
}
350+
} else {
351+
view.dispatch(view.state.tr.insertText(' '));
352+
353+
return true;
354+
}
355+
}
356+
357+
return false;
358+
},
331359
},
332360
immediatelyRender: false,
333361
shouldRerenderOnTransaction: false,

apps/DocFlow/src/extensions/extension-kit.ts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ import {
3939
Superscript,
4040
TableKit,
4141
TableOfContents,
42-
TableCell,
43-
TableHeader,
44-
TableRow,
4542
TextAlign,
4643
TextStyle,
4744
TrailingNode,
@@ -318,32 +315,7 @@ export const ExtensionKit = ({ provider }: ExtensionKitProps) => [
318315
enableEmoticons: true,
319316
suggestion: emojiSuggestion,
320317
}),
321-
TextAlign.extend({
322-
addKeyboardShortcuts() {
323-
return {
324-
Tab: () => {
325-
return this.editor.commands.insertContent(' ');
326-
},
327-
'Shift-Tab': () => {
328-
const { state } = this.editor;
329-
const { from } = state.selection;
330-
const $from = state.doc.resolve(from);
331-
const startOfLine = $from.start($from.depth);
332-
const textBeforeCursor = state.doc.textBetween(startOfLine, from);
333-
334-
if (textBeforeCursor.endsWith(' ')) {
335-
const deleteFrom = Math.max(startOfLine, from - 2);
336-
337-
return this.editor.commands.deleteRange({ from: deleteFrom, to: from });
338-
} else if (textBeforeCursor.endsWith(' ')) {
339-
return this.editor.commands.deleteRange({ from: from - 1, to: from });
340-
}
341-
342-
return false;
343-
},
344-
};
345-
},
346-
}).configure({
318+
TextAlign.configure({
347319
types: ['heading', 'paragraph'],
348320
}),
349321
Subscript,
@@ -353,9 +325,6 @@ export const ExtensionKit = ({ provider }: ExtensionKitProps) => [
353325
resizable: true,
354326
},
355327
}),
356-
TableCell,
357-
TableHeader,
358-
TableRow,
359328
Typography,
360329
Placeholder.configure({
361330
includeChildren: true,

0 commit comments

Comments
 (0)