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)

View File

@ -1,4 +1,4 @@
import { Mark, markInputRule, markPasteRule } from '@tiptap/core'
import { Mark, CommandSpec, markInputRule, markPasteRule } from '@tiptap/core'
import { toggleMark } from 'prosemirror-commands'
import { MarkSpec } from 'prosemirror-model'
import VerEx from 'verbal-expressions'
@ -13,13 +13,6 @@ export default class Bold extends Mark {
name = 'bold'
created() {
this.editor.registerCommand('bold', (next, { view }) => {
toggleMark(this.schemaType)(view.state, view.dispatch)
next()
})
}
schema(): MarkSpec {
return {
parseDOM: [
@ -39,6 +32,15 @@ export default class Bold extends Mark {
}
}
commands(): CommandSpec {
return {
bold: (next, { view }) => {
toggleMark(this.schemaType)(view.state, view.dispatch)
next()
},
}
}
keys() {
return {
'Mod-b': () => this.editor.bold(),

View File

@ -1,4 +1,4 @@
import { Mark, markInputRule, markPasteRule } from '@tiptap/core'
import { Mark, markInputRule, markPasteRule, CommandSpec } from '@tiptap/core'
import { toggleMark } from 'prosemirror-commands'
import { MarkSpec } from 'prosemirror-model'
import VerEx from 'verbal-expressions'
@ -13,13 +13,6 @@ export default class Code extends Mark {
name = 'code'
created() {
this.editor.registerCommand('code', (next, { view }) => {
toggleMark(this.schemaType)(view.state, view.dispatch)
next()
})
}
schema(): MarkSpec {
return {
excludes: '_',
@ -30,6 +23,15 @@ export default class Code extends Mark {
}
}
commands(): CommandSpec {
return {
code: (next, { view }) => {
toggleMark(this.schemaType)(view.state, view.dispatch)
next()
},
}
}
keys() {
return {
'Mod-`': () => this.editor.code(),

View File

@ -1,4 +1,4 @@
import Editor, { Extension } from '@tiptap/core'
import Editor, { Extension, CommandSpec } from '@tiptap/core'
import {
history,
undo,
@ -18,16 +18,17 @@ export default class History extends Extension {
name = 'history'
created() {
this.editor.registerCommand('undo', (next, { view }) => {
undo(view.state, view.dispatch)
next()
})
this.editor.registerCommand('redo', (next, { view }) => {
redo(view.state, view.dispatch)
next()
})
commands(): CommandSpec {
return {
undo: (next, { view }) => {
undo(view.state, view.dispatch)
next()
},
redo: (next, { view }) => {
redo(view.state, view.dispatch)
next()
},
}
}
keys() {

View File

@ -1,4 +1,4 @@
import { Mark, markInputRule, markPasteRule } from '@tiptap/core'
import { Mark, markInputRule, markPasteRule, CommandSpec } from '@tiptap/core'
import { toggleMark } from 'prosemirror-commands'
import { MarkSpec } from 'prosemirror-model'
import VerEx from 'verbal-expressions'
@ -13,13 +13,6 @@ export default class Italic extends Mark {
name = 'italic'
created() {
this.editor.registerCommand('italic', (next, { view }) => {
toggleMark(this.schemaType)(view.state, view.dispatch)
next()
})
}
schema(): MarkSpec {
return {
parseDOM: [
@ -31,6 +24,15 @@ export default class Italic extends Mark {
}
}
commands(): CommandSpec {
return {
italic: (next, { view }) => {
toggleMark(this.schemaType)(view.state, view.dispatch)
next()
},
}
}
keys() {
return {
'Mod-i': () => this.editor.italic(),