add commands to extension manager

This commit is contained in:
Philipp Kühn
2020-04-02 14:34:07 +02:00
parent 748131637c
commit c27ebe8473
8 changed files with 62 additions and 39 deletions

View File

@@ -1,7 +1,7 @@
import { Editor } from './src/Editor'
import { Editor, CommandSpec } from './src/Editor'
export default Editor
export { Editor }
export { Editor, CommandSpec }
export { default as Extension } from './src/Extension'
export { default as Node } from './src/Node'
export { default as Mark } from './src/Mark'

View File

@@ -21,7 +21,10 @@ import Node from './Node'
import EventEmitter from './EventEmitter'
type EditorContent = string | JSON | null
type Command = (next: Function, editor: Editor, ...args: any) => any
export type Command = (next: Function, editor: Editor, ...args: any) => any
export interface CommandSpec {
[key: string]: Command
}
interface Options {
content: EditorContent

View File

@@ -41,6 +41,10 @@ export default abstract class Extension {
keys(): { [key: string]: any } {
return {}
}
commands(): { [key: string]: any } {
return {}
}
}

View File

@@ -1,7 +1,7 @@
import collect from 'collect.js'
import { keymap } from 'prosemirror-keymap'
import { inputRules } from 'prosemirror-inputrules'
import { Editor } from './Editor'
import { Editor, CommandSpec } from './Editor'
import Extension from './Extension'
import Node from './Node'
@@ -16,11 +16,20 @@ export default class ExtensionManager {
this.extensions.forEach(extension => {
extension.bindEditor(editor)
editor.on('schemaCreated', () => {
this.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)