add support for nested chained commands
This commit is contained in:
@@ -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)
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
Reference in New Issue
Block a user