This repository has been archived by the owner on Apr 21, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandManager.ts
183 lines (151 loc) · 4.52 KB
/
CommandManager.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { EditorState, Transaction } from 'prosemirror-state'
import { Editor } from './Editor'
import {
SingleCommands,
ChainedCommands,
CanCommands,
CommandSpec,
CommandProps,
} from './types'
import getAllMethodNames from './utilities/getAllMethodNames'
export default class CommandManager {
editor: Editor
commands: { [key: string]: any } = {}
methodNames: string[] = []
constructor(editor: Editor) {
this.editor = editor
this.methodNames = getAllMethodNames(this.editor)
}
/**
* Register a command.
*
* @param name The name of your command
* @param callback The method of your command
*/
public registerCommand(name: string, callback: CommandSpec): Editor {
if (this.commands[name]) {
throw new Error(`tiptap: command '${name}' is already defined.`)
}
if (this.methodNames.includes(name)) {
throw new Error(`tiptap: '${name}' is a protected name.`)
}
this.commands[name] = callback
return this.editor
}
public createCommands(): SingleCommands {
const { commands, editor } = this
const { state, view } = editor
const { tr } = state
const props = this.buildProps(tr)
return Object.fromEntries(Object
.entries(commands)
.map(([name, command]) => {
const method = (...args: any) => {
const callback = command(...args)(props)
if (!tr.getMeta('preventDispatch')) {
view.dispatch(tr)
}
return callback
}
return [name, method]
})) as SingleCommands
}
public createChain(startTr?: Transaction, shouldDispatch = true): ChainedCommands {
const { commands, editor } = this
const { state, view } = editor
const callbacks: boolean[] = []
const hasStartTransaction = !!startTr
const tr = startTr || state.tr
return new Proxy({}, {
get: (_, name: string, proxy) => {
if (name === 'run') {
if (!hasStartTransaction && shouldDispatch && !tr.getMeta('preventDispatch')) {
view.dispatch(tr)
}
return () => callbacks.every(callback => callback === true)
}
const command = commands[name]
if (!command) {
throw new Error(`tiptap: command '${name}' not found.`)
}
return (...args: any) => {
const props = this.buildProps(tr, shouldDispatch)
const callback = command(...args)(props)
callbacks.push(callback)
return proxy
}
},
}) as ChainedCommands
}
public createCan(startTr?: Transaction): CanCommands {
const { commands, editor } = this
const { state } = editor
const dispatch = false
const tr = startTr || state.tr
const props = this.buildProps(tr, dispatch)
const formattedCommands = Object.fromEntries(Object
.entries(commands)
.map(([name, command]) => {
return [name, (...args: any[]) => command(...args)({ ...props, dispatch })]
})) as SingleCommands
return {
...formattedCommands,
chain: () => this.createChain(tr, dispatch),
} as CanCommands
}
public buildProps(tr: Transaction, shouldDispatch = true): CommandProps {
const { editor, commands } = this
const { state, view } = editor
if (state.storedMarks) {
tr.setStoredMarks(state.storedMarks)
}
const props = {
tr,
editor,
view,
state: this.chainableState(tr, state),
dispatch: shouldDispatch
? () => undefined
: undefined,
chain: () => this.createChain(tr),
can: () => this.createCan(tr),
get commands() {
return Object.fromEntries(Object
.entries(commands)
.map(([name, command]) => {
return [name, (...args: any[]) => command(...args)(props)]
})) as SingleCommands
},
}
return props
}
public chainableState(tr: Transaction, state: EditorState): EditorState {
let { selection } = tr
let { doc } = tr
let { storedMarks } = tr
return {
...state,
schema: state.schema,
plugins: state.plugins,
apply: state.apply.bind(state),
applyTransaction: state.applyTransaction.bind(state),
reconfigure: state.reconfigure.bind(state),
toJSON: state.toJSON.bind(state),
get storedMarks() {
return storedMarks
},
get selection() {
return selection
},
get doc() {
return doc
},
get tr() {
selection = tr.selection
doc = tr.doc
storedMarks = tr.storedMarks
return tr
},
}
}
}