add support for nested chained commands

This commit is contained in:
Philipp Kühn
2020-09-22 23:06:37 +02:00
parent a865ec4b4e
commit 389756d258
2 changed files with 16 additions and 9 deletions

View File

@@ -33,16 +33,23 @@ export default class CommandManager {
} }
} }
public createChain() { public createChain(startTr?: Transaction) {
const { commands, editor } = this const { commands, editor } = this
const { state, view } = editor const { state, view } = editor
const { tr } = state
const callbacks: boolean[] = [] const callbacks: boolean[] = []
const hasStartTransaction = !!startTr
const tr = hasStartTransaction ? startTr : state.tr
if (!tr) {
return
}
return new Proxy({}, { return new Proxy({}, {
get: (_, name: string, proxy) => { get: (_, name: string, proxy) => {
if (name === 'run') { if (name === 'run') {
view.dispatch(tr) if (!hasStartTransaction) {
view.dispatch(tr)
}
return () => callbacks.every(callback => callback === true) return () => callbacks.every(callback => callback === true)
} }
@@ -69,12 +76,12 @@ export default class CommandManager {
const { state, view } = editor const { state, view } = editor
const props = { const props = {
editor,
state: this.chainableState(tr, state),
view,
dispatch: () => false,
// chain: this.chain.bind(this),
tr, tr,
editor,
view,
state: this.chainableState(tr, state),
dispatch: () => false,
chain: () => this.createChain(tr),
get commands() { get commands() {
return Object.fromEntries(Object return Object.fromEntries(Object
.entries(commands) .entries(commands)

View File

@@ -25,7 +25,7 @@ export type Command = (props: {
editor: Editor, editor: Editor,
tr: Transaction, tr: Transaction,
commands: SingleCommands, commands: SingleCommands,
// chain: () => ChainedCommands, chain: () => ChainedCommands,
state: EditorState, state: EditorState,
view: EditorView, view: EditorView,
dispatch: (args?: any) => any, dispatch: (args?: any) => any,