add way to nest commands
This commit is contained in:
@@ -26,6 +26,8 @@ import * as commands from './commands'
|
|||||||
export type Command = (props: {
|
export type Command = (props: {
|
||||||
editor: Editor
|
editor: Editor
|
||||||
tr: Transaction
|
tr: Transaction
|
||||||
|
// TODO: find correct type
|
||||||
|
commands: any
|
||||||
}) => boolean
|
}) => boolean
|
||||||
|
|
||||||
export interface CommandSpec {
|
export interface CommandSpec {
|
||||||
@@ -143,7 +145,22 @@ export class Editor extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (...args: any) => {
|
return (...args: any) => {
|
||||||
const callback = command(...args)({ editor: this.proxy, tr })
|
const props = {
|
||||||
|
editor: this.proxy,
|
||||||
|
tr,
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.defineProperty(props, 'commands', {
|
||||||
|
get: function() {
|
||||||
|
return Object.fromEntries(Object
|
||||||
|
.entries(this.commands)
|
||||||
|
.map(([name, command]) => {
|
||||||
|
return [name, (...args) => command(...args)(props)]
|
||||||
|
}))
|
||||||
|
}.bind(this)
|
||||||
|
});
|
||||||
|
|
||||||
|
const callback = command(...args)(props)
|
||||||
callbacks.push(callback)
|
callbacks.push(callback)
|
||||||
|
|
||||||
return proxy
|
return proxy
|
||||||
|
|||||||
@@ -8,9 +8,6 @@ declare module '../Editor' {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const clearContent: ClearContentCommand = (emitUpdate = false) => ({ editor }) => {
|
export const clearContent: ClearContentCommand = (emitUpdate = false) => ({ commands }) => {
|
||||||
// TODO: doesn’t work, we have to re-use `tr`
|
return commands.setContent('', emitUpdate)
|
||||||
editor.setContent('', emitUpdate)
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user