add extension keymaps

This commit is contained in:
Philipp Kühn 2020-03-31 22:17:54 +02:00
parent 94349015ec
commit 208ba890ef
7 changed files with 45 additions and 5 deletions

View File

@ -54,6 +54,9 @@ export class Editor extends EventEmitter {
constructor(options: Options) {
super()
this.options = { ...this.options, ...options }
}
private init() {
this.createExtensionManager()
this.createSchema()
this.createView()
@ -86,7 +89,7 @@ export class Editor extends EventEmitter {
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.`)
}
@ -103,7 +106,7 @@ export class Editor extends EventEmitter {
}
private createExtensionManager() {
this.extensionManager = new ExtensionManager(this.options.extensions, this)
this.extensionManager = new ExtensionManager(this.options.extensions, this.proxy)
}
private createSchema() {
@ -116,8 +119,10 @@ export class Editor extends EventEmitter {
}
private get plugins() {
console.log(this.extensionManager.plugins)
return [
...this.extensionManager.plugins,
...this.extensionManager.keymaps,
keymap({ Backspace: undoInputRule }),
keymap(baseKeymap),
dropCursor(),

View File

@ -39,8 +39,8 @@ export default abstract class Extension {
return []
}
keys(): any {
keys(): { [key: string]: any } {
return {}
}
}
}

View File

@ -6,9 +6,11 @@ import Node from './Node'
export default class ExtensionManager {
editor: Editor
extensions: (Extension | Node)[]
constructor(extensions: (Extension | Node)[], editor: Editor) {
this.editor = editor
this.extensions = extensions
this.extensions.forEach(extension => {
extension.bindEditor(editor)
@ -46,4 +48,12 @@ export default class ExtensionManager {
.toArray()
}
get keymaps() {
return collect(this.extensions)
.map(extension => extension.keys())
.filter(keys => !!Object.keys(keys).length)
.map(keys => keymap(keys))
.toArray()
}
}

View File

@ -5,7 +5,7 @@ import minMax from '../utils/minMax'
declare module '../Editor' {
interface Editor {
focus(position: Position): Editor
focus(position?: Position): Editor
}
}

View File

@ -25,6 +25,7 @@ export default function magicMethods(clazz: any) {
}
instance.proxy = new Proxy(instance, instanceHandler)
instance.init()
return instance.proxy
}

View File

@ -1,6 +1,12 @@
import { Mark } from '@tiptap/core'
import { toggleMark } from 'prosemirror-commands'
declare module '@tiptap/core/src/Editor' {
interface Editor {
bold(): Editor,
}
}
export default class Bold extends Mark {
name = 'bold'
@ -31,4 +37,10 @@ export default class Bold extends Mark {
}
}
keys() {
return {
'Mod-b': () => this.editor.bold(),
}
}
}

View File

@ -1,6 +1,12 @@
import { Mark } from '@tiptap/core'
import { toggleMark } from 'prosemirror-commands'
declare module '@tiptap/core/src/Editor' {
interface Editor {
italic(): Editor,
}
}
export default class Italic extends Mark {
name = 'italic'
@ -23,4 +29,10 @@ export default class Italic extends Mark {
}
}
keys() {
return {
'Mod-i': () => this.editor.italic(),
}
}
}