move code to commandmanager

This commit is contained in:
Philipp Kühn
2020-09-23 08:59:21 +02:00
parent 9a8ce8c5fb
commit b0378e593d
2 changed files with 28 additions and 28 deletions

View File

@@ -1,14 +1,34 @@
import { EditorState, Transaction } from "prosemirror-state"; import { EditorState, Transaction } from "prosemirror-state"
import { ChainedCommands, Editor } from "./Editor"; import { ChainedCommands, Editor, CommandSpec } from "./Editor"
import getAllMethodNames from './utils/getAllMethodNames'
export default class CommandManager { export default class CommandManager {
editor: Editor editor: Editor
commands: { [key: string]: any } = {} commands: { [key: string]: any } = {}
constructor(editor: Editor, commands: any) { constructor(editor: Editor) {
this.editor = editor this.editor = editor
this.commands = commands }
/**
* 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 (getAllMethodNames(this.editor).includes(name)) {
throw new Error(`tiptap: '${name}' is a protected name.`)
}
this.commands[name] = callback
return this.editor
} }
public runSingleCommand(name: string) { public runSingleCommand(name: string) {

View File

@@ -3,7 +3,6 @@ import { EditorView} from 'prosemirror-view'
import { Schema, DOMParser, DOMSerializer } from 'prosemirror-model' import { Schema, DOMParser, DOMSerializer } from 'prosemirror-model'
import magicMethods from './utils/magicMethods' import magicMethods from './utils/magicMethods'
import elementFromString from './utils/elementFromString' import elementFromString from './utils/elementFromString'
import getAllMethodNames from './utils/getAllMethodNames'
import nodeIsActive from './utils/nodeIsActive' import nodeIsActive from './utils/nodeIsActive'
import markIsActive from './utils/markIsActive' import markIsActive from './utils/markIsActive'
import getNodeAttrs from './utils/getNodeAttrs' import getNodeAttrs from './utils/getNodeAttrs'
@@ -81,7 +80,6 @@ export class Editor extends EventEmitter {
private proxy!: Editor private proxy!: Editor
private commandManager!: CommandManager private commandManager!: CommandManager
private extensionManager!: ExtensionManager private extensionManager!: ExtensionManager
private commands: CommandsSpec = {}
private css!: HTMLStyleElement private css!: HTMLStyleElement
public schema!: Schema public schema!: Schema
public view!: EditorView public view!: EditorView
@@ -106,12 +104,12 @@ export class Editor extends EventEmitter {
* This method is called after the proxy is initialized. * This method is called after the proxy is initialized.
*/ */
private init() { private init() {
this.createCommandManager()
this.createExtensionManager() this.createExtensionManager()
this.createSchema() this.createSchema()
this.extensionManager.resolveConfigs() this.extensionManager.resolveConfigs()
this.createView() this.createView()
this.registerCommands(commands) this.registerCommands(commands)
this.createCommandManager()
if (this.options.injectCSS) { if (this.options.injectCSS) {
require('./style.css') require('./style.css')
@@ -181,29 +179,11 @@ export class Editor extends EventEmitter {
* @param callback The method of your command * @param callback The method of your command
*/ */
public registerCommand(name: string, callback: CommandSpec): Editor { public registerCommand(name: string, callback: CommandSpec): Editor {
if (this.commands[name]) { this.commandManager.registerCommand(name, callback)
throw new Error(`tiptap: command '${name}' is already defined.`)
}
if (getAllMethodNames(this).includes(name)) {
throw new Error(`tiptap: '${name}' is a protected name.`)
}
this.commands[name] = callback
return this.proxy return this.proxy
} }
/**
* Call a command.
*
* @param name The name of the command you want to call.
* @param options The options of the command.
*/
public command(name: string, ...options: any) {
return this.commands[name](...options)
}
/** /**
* Register a ProseMirror plugin. * Register a ProseMirror plugin.
* *
@@ -245,7 +225,7 @@ export class Editor extends EventEmitter {
* Creates an command manager. * Creates an command manager.
*/ */
private createCommandManager() { private createCommandManager() {
this.commandManager = new CommandManager(this.proxy, this.commands) this.commandManager = new CommandManager(this.proxy)
} }
/** /**