/
extension.ts
132 lines (115 loc) · 3.23 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import {
convertCommand,
ExtensionTag,
InputRule,
KeyBindings,
NodeExtension,
NodeExtensionSpec,
ProsemirrorPlugin,
} from '@remirror/core'
import { NodeRange } from '@remirror/pm/model'
import {
createDedentListCommand,
createIndentListCommand,
createListPlugins,
createListSpec,
createMoveListCommand,
createSplitListCommand,
createToggleCollapsedCommand,
createToggleListCommand,
createUnwrapListCommand,
createWrapInListCommand,
DedentListOptions,
IndentListOptions,
ListAttributes,
listInputRules,
listKeymap,
protectCollapsed,
ToggleCollapsedOptions,
UnwrapListOptions,
} from 'prosemirror-flat-list'
/**
* A Remirror extension for creating lists. It's a simple wrapper around the API from `prosemirror-flat-list`.
*
* @public
*/
export class ListExtension extends NodeExtension {
static disableExtraAttributes = true
get name() {
return 'list' as const
}
createTags() {
return [ExtensionTag.Block]
}
createNodeSpec(): NodeExtensionSpec {
// @ts-expect-error: incompatible type
return createListSpec()
}
createKeymap(): KeyBindings {
const bindings: KeyBindings = {}
for (const [key, command] of Object.entries(listKeymap)) {
bindings[key] = convertCommand(command)
}
bindings['Tab'] = alwaysTrue(bindings['Mod-]'])
bindings['Shift-Tab'] = alwaysTrue(bindings['Mod-['])
return bindings
}
createExternalPlugins(): ProsemirrorPlugin[] {
return createListPlugins({ schema: this.store.schema })
}
createInputRules(): InputRule[] {
return listInputRules
}
createCommands() {
return {
indentList: (props?: IndentListOptions) => {
return convertCommand(createIndentListCommand(props))
},
dedentList: (props?: DedentListOptions) => {
return convertCommand(createDedentListCommand(props))
},
unwrapList: (options?: UnwrapListOptions) => {
return convertCommand(createUnwrapListCommand(options))
},
wrapInList: (
getAttrs:
| ListAttributes
| ((range: NodeRange) => ListAttributes | null),
) => {
return convertCommand(createWrapInListCommand<ListAttributes>(getAttrs))
},
moveList: (direction: 'up' | 'down') => {
return convertCommand(createMoveListCommand(direction))
},
splitList: () => convertCommand(createSplitListCommand()),
protectCollapsed: () => convertCommand(protectCollapsed),
toggleCollapsed: (props?: ToggleCollapsedOptions) => {
return convertCommand(createToggleCollapsedCommand(props))
},
toggleList: (attrs: ListAttributes) => {
return convertCommand(createToggleListCommand(attrs))
},
} as const
}
}
/**
* Wrap the giving command function so that it always returns `true`. This is
* useful when we want pressing `Tab` and `Shift-Tab` won't blur the editor even
* if the keybinding command returns `false`
*
* @public
*/
export function alwaysTrue<T extends (...args: any[]) => boolean>(func: T): T {
return ((...args) => {
func(...args)
return true
}) as T
}
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Remirror {
interface AllExtensions {
list: ListExtension
}
}
}