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 { ChainedCommands, Editor } from "./Editor";
import { EditorState, Transaction } from "prosemirror-state"
import { ChainedCommands, Editor, CommandSpec } from "./Editor"
import getAllMethodNames from './utils/getAllMethodNames'
export default class CommandManager {
editor: Editor
commands: { [key: string]: any } = {}
constructor(editor: Editor, commands: any) {
constructor(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) {

View File

@@ -3,7 +3,6 @@ import { EditorView} from 'prosemirror-view'
import { Schema, DOMParser, DOMSerializer } from 'prosemirror-model'
import magicMethods from './utils/magicMethods'
import elementFromString from './utils/elementFromString'
import getAllMethodNames from './utils/getAllMethodNames'
import nodeIsActive from './utils/nodeIsActive'
import markIsActive from './utils/markIsActive'
import getNodeAttrs from './utils/getNodeAttrs'
@@ -81,7 +80,6 @@ export class Editor extends EventEmitter {
private proxy!: Editor
private commandManager!: CommandManager
private extensionManager!: ExtensionManager
private commands: CommandsSpec = {}
private css!: HTMLStyleElement
public schema!: Schema
public view!: EditorView
@@ -106,12 +104,12 @@ export class Editor extends EventEmitter {
* This method is called after the proxy is initialized.
*/
private init() {
this.createCommandManager()
this.createExtensionManager()
this.createSchema()
this.extensionManager.resolveConfigs()
this.createView()
this.registerCommands(commands)
this.createCommandManager()
if (this.options.injectCSS) {
require('./style.css')
@@ -181,29 +179,11 @@ export class Editor extends EventEmitter {
* @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).includes(name)) {
throw new Error(`tiptap: '${name}' is a protected name.`)
}
this.commands[name] = callback
this.commandManager.registerCommand(name, callback)
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.
*
@@ -245,7 +225,7 @@ export class Editor extends EventEmitter {
* Creates an command manager.
*/
private createCommandManager() {
this.commandManager = new CommandManager(this.proxy, this.commands)
this.commandManager = new CommandManager(this.proxy)
}
/**
@@ -401,5 +381,5 @@ export class Editor extends EventEmitter {
this.removeAllListeners()
removeElement(this.css)
}
}