From 6a70d80af2dc77c5fd32fcc65c5aac5ed8ea89a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Fri, 21 Aug 2020 21:44:34 +0200 Subject: [PATCH] refactoring --- packages/core/src/Editor.ts | 38 ++++++--------------------- packages/core/src/ExtensionManager.ts | 10 +------ packages/core/src/commands/index.ts | 14 ++++++++++ 3 files changed, 23 insertions(+), 39 deletions(-) create mode 100644 packages/core/src/commands/index.ts diff --git a/packages/core/src/Editor.ts b/packages/core/src/Editor.ts index f17d3907..dc5cd16b 100644 --- a/packages/core/src/Editor.ts +++ b/packages/core/src/Editor.ts @@ -18,22 +18,7 @@ import Mark from './Mark' import EventEmitter from './EventEmitter' import ComponentRenderer from './ComponentRenderer' import defaultPlugins from './plugins' - -// commands -import clearContent from './commands/clearContent' -import deleteSelection from './commands/deleteSelection' -import focus from './commands/focus' -import insertHTML from './commands/insertHTML' -import insertText from './commands/insertText' -import removeMark from './commands/removeMark' -import removeMarks from './commands/removeMarks' -import replaceWithNode from './commands/replaceWithNode' -import selectAll from './commands/selectAll' -import selectParentNode from './commands/selectParentNode' -import setContent from './commands/setContent' -import toggleMark from './commands/toggleMark' -import toggleNode from './commands/toggleNode' -import updateMark from './commands/updateMark' +import * as commands from './commands' export type Command = (next: Function, editor: Editor) => (...args: any) => any @@ -82,20 +67,7 @@ export class Editor extends EventEmitter { this.createExtensionManager() this.createSchema() this.createView() - this.registerCommand('clearContent', clearContent) - this.registerCommand('deleteSelection', deleteSelection) - this.registerCommand('focus', focus) - this.registerCommand('insertHTML', insertHTML) - this.registerCommand('insertText', insertText) - this.registerCommand('removeMark', removeMark) - this.registerCommand('removeMarks', removeMarks) - this.registerCommand('replaceWithNode', replaceWithNode) - this.registerCommand('selectAll', selectAll) - this.registerCommand('selectParentNode', selectParentNode) - this.registerCommand('setContent', setContent) - this.registerCommand('toggleMark', toggleMark) - this.registerCommand('toggleNode', toggleNode) - this.registerCommand('updateMark', updateMark) + this.registerCommands(commands) if (this.options.injectCSS) { require('./style.css') @@ -120,6 +92,12 @@ export class Editor extends EventEmitter { return this.view.state } + public registerCommands(commands: CommandSpec): void { + Object + .entries(commands) + .forEach(([name, command]) => this.registerCommand(name, command)) + } + public registerCommand(name: string, callback: Command): Editor { if (this.commands[name]) { throw new Error(`tiptap: command '${name}' is already defined.`) diff --git a/packages/core/src/ExtensionManager.ts b/packages/core/src/ExtensionManager.ts index d4aa49ad..36f3ceb0 100644 --- a/packages/core/src/ExtensionManager.ts +++ b/packages/core/src/ExtensionManager.ts @@ -19,20 +19,12 @@ export default class ExtensionManager { this.extensions.forEach(extension => { extension.bindEditor(editor) editor.on('schemaCreated', () => { - this.registerCommands(extension.commands()) + this.editor.registerCommands(extension.commands()) extension.created() }) }) } - registerCommands(commands: CommandSpec) { - Object - .entries(commands) - .forEach(([name, command]) => { - this.editor.registerCommand(name, command) - }) - } - get topNode() { const topNode = collect(this.extensions).firstWhere('topNode', true) diff --git a/packages/core/src/commands/index.ts b/packages/core/src/commands/index.ts new file mode 100644 index 00000000..6ef7d1b3 --- /dev/null +++ b/packages/core/src/commands/index.ts @@ -0,0 +1,14 @@ +export { default as clearContent } from './clearContent' +export { default as deleteSelection } from './deleteSelection' +export { default as focus } from './focus' +export { default as insertHTML } from './insertHTML' +export { default as insertText } from './insertText' +export { default as removeMark } from './removeMark' +export { default as removeMarks } from './removeMarks' +export { default as replaceWithNode } from './replaceWithNode' +export { default as selectAll } from './selectAll' +export { default as selectParentNode } from './selectParentNode' +export { default as setContent } from './setContent' +export { default as toggleMark } from './toggleMark' +export { default as toggleNode } from './toggleNode' +export { default as updateMark } from './updateMark'