Skip to content

Commit 8d580da

Browse files
authored
feat: insert tab at cursor (#1121)
1 parent 130c2f3 commit 8d580da

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

packages/shared/src/editor/markdown.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { defaultKeymap, history, historyKeymap } from '@codemirror/commands'
33
import { markdown, markdownLanguage } from '@codemirror/lang-markdown'
44
import { languages } from '@codemirror/language-data'
55
import { highlightSelectionMatches } from '@codemirror/search'
6-
import { EditorState, Prec } from '@codemirror/state'
6+
import { EditorSelection, EditorState, Prec } from '@codemirror/state'
77
import { EditorView, keymap, placeholder } from '@codemirror/view'
88
import { indentationMarkers } from '@replit/codemirror-indentation-markers'
99
import { formatDoc } from '../utils/fileHelpers'
@@ -20,6 +20,19 @@ async function formatMarkdown(view: EditorView) {
2020
})
2121
}
2222

23+
/**
24+
* 在光标位置插入缩进(空格)
25+
*/
26+
function insertTabAtCursor(view: EditorView): boolean {
27+
const spaces = ` ` // 2 个空格作为缩进
28+
const changes = view.state.changeByRange(range => ({
29+
changes: { from: range.from, to: range.to, insert: spaces },
30+
range: EditorSelection.range(range.from + spaces.length, range.from + spaces.length),
31+
}))
32+
view.dispatch(changes)
33+
return true
34+
}
35+
2336
interface MarkdownKeymapOptions {
2437
onSearch?: (view: EditorView) => void
2538
}
@@ -34,6 +47,9 @@ export function markdownKeymap(options?: MarkdownKeymapOptions) {
3447
const { onSearch } = options || {}
3548

3649
return keymap.of([
50+
// Tab 键在光标位置插入缩进
51+
{ key: `Tab`, run: insertTabAtCursor },
52+
3753
// 撤销/重做
3854
{ key: `Mod-z`, run: undoAction },
3955
{ key: `Mod-y`, run: redoAction },

0 commit comments

Comments
 (0)