remove registerCommands

This commit is contained in:
Philipp Kühn
2021-02-10 14:52:08 +01:00
parent 290ff76e37
commit 2340840621
4 changed files with 25 additions and 61 deletions

View File

@@ -4,6 +4,7 @@ import {
SingleCommands,
ChainedCommands,
CanCommands,
Commands,
CommandSpec,
CommandProps,
} from './types'
@@ -13,35 +14,16 @@ export default class CommandManager {
editor: Editor
commands: { [key: string]: any } = {}
commands: Commands
methodNames: string[] = []
constructor(editor: Editor) {
constructor(editor: Editor, commands: Commands) {
this.editor = editor
this.commands = commands
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
@@ -73,7 +55,7 @@ export default class CommandManager {
const tr = startTr || state.tr
return new Proxy({}, {
get: (_, name: string, proxy) => {
get: (_, name: keyof ChainedCommands, proxy) => {
if (name === 'run') {
if (!hasStartTransaction && shouldDispatch && !tr.getMeta('preventDispatch')) {
view.dispatch(tr)
@@ -82,13 +64,13 @@ export default class CommandManager {
return () => callbacks.every(callback => callback === true)
}
const command = commands[name]
const command = commands[name] as CommandSpec
if (!command) {
throw new Error(`tiptap: command '${name}' not found.`)
}
return (...args: any) => {
return (...args: any[]) => {
const props = this.buildProps(tr, shouldDispatch)
const callback = command(...args)(props)