mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-29 08:19:34 +08:00
add extension keymaps
This commit is contained in:
parent
94349015ec
commit
208ba890ef
@ -54,6 +54,9 @@ export class Editor extends EventEmitter {
|
|||||||
constructor(options: Options) {
|
constructor(options: Options) {
|
||||||
super()
|
super()
|
||||||
this.options = { ...this.options, ...options }
|
this.options = { ...this.options, ...options }
|
||||||
|
}
|
||||||
|
|
||||||
|
private init() {
|
||||||
this.createExtensionManager()
|
this.createExtensionManager()
|
||||||
this.createSchema()
|
this.createSchema()
|
||||||
this.createView()
|
this.createView()
|
||||||
@ -86,7 +89,7 @@ export class Editor extends EventEmitter {
|
|||||||
if (this.commands[name]) {
|
if (this.commands[name]) {
|
||||||
throw new Error(`tiptap: command '${name}' is already defined.`)
|
throw new Error(`tiptap: command '${name}' is already defined.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getAllMethodNames(this).includes(name)) {
|
if (getAllMethodNames(this).includes(name)) {
|
||||||
throw new Error(`tiptap: '${name}' is a protected name.`)
|
throw new Error(`tiptap: '${name}' is a protected name.`)
|
||||||
}
|
}
|
||||||
@ -103,7 +106,7 @@ export class Editor extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private createExtensionManager() {
|
private createExtensionManager() {
|
||||||
this.extensionManager = new ExtensionManager(this.options.extensions, this)
|
this.extensionManager = new ExtensionManager(this.options.extensions, this.proxy)
|
||||||
}
|
}
|
||||||
|
|
||||||
private createSchema() {
|
private createSchema() {
|
||||||
@ -116,8 +119,10 @@ export class Editor extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private get plugins() {
|
private get plugins() {
|
||||||
|
console.log(this.extensionManager.plugins)
|
||||||
return [
|
return [
|
||||||
...this.extensionManager.plugins,
|
...this.extensionManager.plugins,
|
||||||
|
...this.extensionManager.keymaps,
|
||||||
keymap({ Backspace: undoInputRule }),
|
keymap({ Backspace: undoInputRule }),
|
||||||
keymap(baseKeymap),
|
keymap(baseKeymap),
|
||||||
dropCursor(),
|
dropCursor(),
|
||||||
|
@ -39,8 +39,8 @@ export default abstract class Extension {
|
|||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
keys(): any {
|
keys(): { [key: string]: any } {
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,11 @@ import Node from './Node'
|
|||||||
|
|
||||||
export default class ExtensionManager {
|
export default class ExtensionManager {
|
||||||
|
|
||||||
|
editor: Editor
|
||||||
extensions: (Extension | Node)[]
|
extensions: (Extension | Node)[]
|
||||||
|
|
||||||
constructor(extensions: (Extension | Node)[], editor: Editor) {
|
constructor(extensions: (Extension | Node)[], editor: Editor) {
|
||||||
|
this.editor = editor
|
||||||
this.extensions = extensions
|
this.extensions = extensions
|
||||||
this.extensions.forEach(extension => {
|
this.extensions.forEach(extension => {
|
||||||
extension.bindEditor(editor)
|
extension.bindEditor(editor)
|
||||||
@ -46,4 +48,12 @@ export default class ExtensionManager {
|
|||||||
.toArray()
|
.toArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get keymaps() {
|
||||||
|
return collect(this.extensions)
|
||||||
|
.map(extension => extension.keys())
|
||||||
|
.filter(keys => !!Object.keys(keys).length)
|
||||||
|
.map(keys => keymap(keys))
|
||||||
|
.toArray()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import minMax from '../utils/minMax'
|
|||||||
|
|
||||||
declare module '../Editor' {
|
declare module '../Editor' {
|
||||||
interface Editor {
|
interface Editor {
|
||||||
focus(position: Position): Editor
|
focus(position?: Position): Editor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ export default function magicMethods(clazz: any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
instance.proxy = new Proxy(instance, instanceHandler)
|
instance.proxy = new Proxy(instance, instanceHandler)
|
||||||
|
instance.init()
|
||||||
|
|
||||||
return instance.proxy
|
return instance.proxy
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
import { Mark } from '@tiptap/core'
|
import { Mark } from '@tiptap/core'
|
||||||
import { toggleMark } from 'prosemirror-commands'
|
import { toggleMark } from 'prosemirror-commands'
|
||||||
|
|
||||||
|
declare module '@tiptap/core/src/Editor' {
|
||||||
|
interface Editor {
|
||||||
|
bold(): Editor,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default class Bold extends Mark {
|
export default class Bold extends Mark {
|
||||||
|
|
||||||
name = 'bold'
|
name = 'bold'
|
||||||
@ -31,4 +37,10 @@ export default class Bold extends Mark {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
keys() {
|
||||||
|
return {
|
||||||
|
'Mod-b': () => this.editor.bold(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,6 +1,12 @@
|
|||||||
import { Mark } from '@tiptap/core'
|
import { Mark } from '@tiptap/core'
|
||||||
import { toggleMark } from 'prosemirror-commands'
|
import { toggleMark } from 'prosemirror-commands'
|
||||||
|
|
||||||
|
declare module '@tiptap/core/src/Editor' {
|
||||||
|
interface Editor {
|
||||||
|
italic(): Editor,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default class Italic extends Mark {
|
export default class Italic extends Mark {
|
||||||
|
|
||||||
name = 'italic'
|
name = 'italic'
|
||||||
@ -23,4 +29,10 @@ export default class Italic extends Mark {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
keys() {
|
||||||
|
return {
|
||||||
|
'Mod-i': () => this.editor.italic(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user