@@ -3,7 +3,7 @@ import { defaultKeymap, history, historyKeymap } from '@codemirror/commands'
33import { markdown , markdownLanguage } from '@codemirror/lang-markdown'
44import { languages } from '@codemirror/language-data'
55import { highlightSelectionMatches } from '@codemirror/search'
6- import { EditorState , Prec } from '@codemirror/state'
6+ import { EditorSelection , EditorState , Prec } from '@codemirror/state'
77import { EditorView , keymap , placeholder } from '@codemirror/view'
88import { indentationMarkers } from '@replit/codemirror-indentation-markers'
99import { 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+
2336interface 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