refactoring
This commit is contained in:
@@ -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.`)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
14
packages/core/src/commands/index.ts
Normal file
14
packages/core/src/commands/index.ts
Normal file
@@ -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'
|
||||
Reference in New Issue
Block a user