Skip to content

Commit 31c9012

Browse files
committed
Make tab and shift-tab keybindings configurable
1 parent 537ce65 commit 31c9012

File tree

9 files changed

+61
-1946
lines changed

9 files changed

+61
-1946
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ The `ink-mde/svelte` subpath exports a Svelte component.
205205
These are the default options, and any of them can be overridden when initializing (or reconfiguring) an instance of `ink-mde`.
206206

207207
```ts
208-
// ./src/store.ts#L12-L58
208+
// ./src/store.ts#L12-L65
209209
const options = {
210210
doc: '',
211211
files: {
@@ -230,6 +230,11 @@ const options = {
230230
toolbar: false,
231231
},
232232
katex: false,
233+
keybindings: {
234+
// Todo: Set these to false by default. https://codemirror.net/examples/tab
235+
tab: true,
236+
shiftTab: true,
237+
},
233238
placeholder: '',
234239
plugins: [
235240
katex(),
@@ -251,6 +256,8 @@ const options = {
251256
taskList: true,
252257
upload: false,
253258
},
259+
// This value overrides both `tab` and `shiftTab` keybindings.
260+
trapTab: undefined,
254261
vim: false,
255262
}
256263
```

src/extensions.ts

+13
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ export const lazyResolvers: InkInternal.LazyExtensionResolvers = [
101101

102102
return compartment.reconfigure([])
103103
},
104+
async ([state]: InkInternal.Store, compartment: InkInternal.Vendor.Compartment) => {
105+
const { keybindings, trapTab } = state().options
106+
const tab = trapTab ?? keybindings.tab
107+
const shiftTab = trapTab ?? keybindings.shiftTab
108+
109+
if (tab || shiftTab) {
110+
const { indentWithTab } = await import('/src/vendor/extensions/indentWithTab')
111+
112+
return compartment.reconfigure(indentWithTab({ tab, shiftTab }))
113+
}
114+
115+
return compartment.reconfigure([])
116+
},
104117
async ([state]: InkInternal.Store, compartment: InkInternal.Vendor.Compartment) => {
105118
if (state().options.interface.lists) {
106119
const { lists } = await import('/src/vendor/extensions/lists')

src/store.ts

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export const blankState = (): InkInternal.StateResolved => {
3333
toolbar: false,
3434
},
3535
katex: false,
36+
keybindings: {
37+
// Todo: Set these to false by default. https://codemirror.net/examples/tab
38+
tab: true,
39+
shiftTab: true,
40+
},
3641
placeholder: '',
3742
plugins: [
3843
katex(),
@@ -54,6 +59,8 @@ export const blankState = (): InkInternal.StateResolved => {
5459
taskList: true,
5560
upload: false,
5661
},
62+
// This value overrides both `tab` and `shiftTab` keybindings.
63+
trapTab: undefined,
5764
vim: false,
5865
}
5966

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { indentLess, indentMore } from '@codemirror/commands'
2+
import { type Extension } from '@codemirror/state'
3+
import { keymap } from '@codemirror/view'
4+
5+
export const indentWithTab = ({ tab = true, shiftTab = true } = {}): Extension => {
6+
return keymap.of([
7+
{
8+
key: 'Tab',
9+
run: tab ? indentMore : undefined,
10+
},
11+
{
12+
key: 'Shift-Tab',
13+
run: shiftTab ? indentLess : undefined,
14+
},
15+
])
16+
}

src/vendor/extensions/keymaps.ts

-33
This file was deleted.

src/vendor/state.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { history } from '@codemirror/commands'
1+
import { defaultKeymap, history, historyKeymap } from '@codemirror/commands'
22
import { type EditorSelection, EditorState } from '@codemirror/state'
3+
import { keymap } from '@codemirror/view'
34
import { toCodeMirror } from './adapters/selections'
45
import { buildVendors } from '/src/extensions'
56
import { blockquote } from '/src/vendor/extensions/blockquote'
67
import { code } from '/src/vendor/extensions/code'
78
import { ink } from '/src/vendor/extensions/ink'
8-
import { keymaps } from '/src/vendor/extensions/keymaps'
99
import { lineWrapping } from '/src/vendor/extensions/line_wrapping'
1010
import { theme } from '/src/vendor/extensions/theme'
1111
import type * as Ink from '/types/ink'
@@ -21,11 +21,14 @@ export const makeState = ([state, setState]: InkInternal.Store): InkInternal.Ven
2121
doc: state().options.doc,
2222
selection: toVendorSelection(state().options.selections),
2323
extensions: [
24+
keymap.of([
25+
...defaultKeymap,
26+
...historyKeymap,
27+
]),
2428
blockquote(),
2529
code(),
2630
history(),
2731
ink(),
28-
keymaps(),
2932
lineWrapping(),
3033
theme(),
3134
...buildVendors([state, setState]),

0 commit comments

Comments
 (0)