From 1b10b20ec39aa9e160a9a49f332ac48f570a7f54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Tue, 10 Mar 2020 23:19:41 +0100 Subject: [PATCH] check for already defined commands --- packages/tiptap-core/src/Editor.ts | 35 ++++++++++++++++++------------ 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/packages/tiptap-core/src/Editor.ts b/packages/tiptap-core/src/Editor.ts index 311521ec..8e487ada 100644 --- a/packages/tiptap-core/src/Editor.ts +++ b/packages/tiptap-core/src/Editor.ts @@ -34,6 +34,7 @@ export class Editor extends EventEmitter { injectCSS: true, extensions: [], } + commands: { [key: string]: any } = {} private lastCommand = Promise.resolve() @@ -54,6 +55,26 @@ export class Editor extends EventEmitter { } } + public registerCommand(name: string, callback: Command): Editor { + if (this.commands[name]) { + throw new Error(`tiptap: command '${name}' is already defined.`) + } + + this.commands[name] = callback + + // @ts-ignore + this[name] = this.chainCommand((...args: any) => { + return new Promise(resolve => callback(resolve, this, ...args)) + }) + + return this + } + + public command(name: string, ...args: any) { + // @ts-ignore + return this[name](...args) + } + private createExtensionManager() { this.extensionManager = new ExtensionManager(this.options.extensions, this) } @@ -158,20 +179,6 @@ export class Editor extends EventEmitter { return this } - public registerCommand(name: string, callback: Command): Editor { - // @ts-ignore - this[name] = this.chainCommand((...args: any) => { - return new Promise(resolve => callback(resolve, this, ...args)) - }) - - return this - } - - public command(name: string, ...args: any) { - // @ts-ignore - return this[name](...args) - } - // public setParentComponent(component = null) { // if (!component) { // return