mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-08-06 13:38:49 +08:00
add commands to extension manager
This commit is contained in:
parent
748131637c
commit
c27ebe8473
@ -1,7 +1,7 @@
|
|||||||
import { Editor } from './src/Editor'
|
import { Editor, CommandSpec } from './src/Editor'
|
||||||
|
|
||||||
export default Editor
|
export default Editor
|
||||||
export { Editor }
|
export { Editor, CommandSpec }
|
||||||
export { default as Extension } from './src/Extension'
|
export { default as Extension } from './src/Extension'
|
||||||
export { default as Node } from './src/Node'
|
export { default as Node } from './src/Node'
|
||||||
export { default as Mark } from './src/Mark'
|
export { default as Mark } from './src/Mark'
|
||||||
|
@ -21,7 +21,10 @@ import Node from './Node'
|
|||||||
import EventEmitter from './EventEmitter'
|
import EventEmitter from './EventEmitter'
|
||||||
|
|
||||||
type EditorContent = string | JSON | null
|
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 {
|
interface Options {
|
||||||
content: EditorContent
|
content: EditorContent
|
||||||
|
@ -41,6 +41,10 @@ export default abstract class Extension {
|
|||||||
|
|
||||||
keys(): { [key: string]: any } {
|
keys(): { [key: string]: any } {
|
||||||
return {}
|
return {}
|
||||||
|
}
|
||||||
|
|
||||||
|
commands(): { [key: string]: any } {
|
||||||
|
return {}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import collect from 'collect.js'
|
import collect from 'collect.js'
|
||||||
import { keymap } from 'prosemirror-keymap'
|
import { keymap } from 'prosemirror-keymap'
|
||||||
import { inputRules } from 'prosemirror-inputrules'
|
import { inputRules } from 'prosemirror-inputrules'
|
||||||
import { Editor } from './Editor'
|
import { Editor, CommandSpec } from './Editor'
|
||||||
import Extension from './Extension'
|
import Extension from './Extension'
|
||||||
import Node from './Node'
|
import Node from './Node'
|
||||||
|
|
||||||
@ -16,11 +16,20 @@ export default class ExtensionManager {
|
|||||||
this.extensions.forEach(extension => {
|
this.extensions.forEach(extension => {
|
||||||
extension.bindEditor(editor)
|
extension.bindEditor(editor)
|
||||||
editor.on('schemaCreated', () => {
|
editor.on('schemaCreated', () => {
|
||||||
|
this.registerCommands(extension.commands())
|
||||||
extension.created()
|
extension.created()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registerCommands(commands: CommandSpec) {
|
||||||
|
Object
|
||||||
|
.entries(commands)
|
||||||
|
.forEach(([name, command]) => {
|
||||||
|
this.editor.registerCommand(name, command)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
get topNode() {
|
get topNode() {
|
||||||
const topNode = collect(this.extensions).firstWhere('topNode', true)
|
const topNode = collect(this.extensions).firstWhere('topNode', true)
|
||||||
|
|
||||||
|
@ -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 { toggleMark } from 'prosemirror-commands'
|
||||||
import { MarkSpec } from 'prosemirror-model'
|
import { MarkSpec } from 'prosemirror-model'
|
||||||
import VerEx from 'verbal-expressions'
|
import VerEx from 'verbal-expressions'
|
||||||
@ -13,13 +13,6 @@ export default class Bold extends Mark {
|
|||||||
|
|
||||||
name = 'bold'
|
name = 'bold'
|
||||||
|
|
||||||
created() {
|
|
||||||
this.editor.registerCommand('bold', (next, { view }) => {
|
|
||||||
toggleMark(this.schemaType)(view.state, view.dispatch)
|
|
||||||
next()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
schema(): MarkSpec {
|
schema(): MarkSpec {
|
||||||
return {
|
return {
|
||||||
parseDOM: [
|
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() {
|
keys() {
|
||||||
return {
|
return {
|
||||||
'Mod-b': () => this.editor.bold(),
|
'Mod-b': () => this.editor.bold(),
|
||||||
|
@ -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 { toggleMark } from 'prosemirror-commands'
|
||||||
import { MarkSpec } from 'prosemirror-model'
|
import { MarkSpec } from 'prosemirror-model'
|
||||||
import VerEx from 'verbal-expressions'
|
import VerEx from 'verbal-expressions'
|
||||||
@ -13,13 +13,6 @@ export default class Code extends Mark {
|
|||||||
|
|
||||||
name = 'code'
|
name = 'code'
|
||||||
|
|
||||||
created() {
|
|
||||||
this.editor.registerCommand('code', (next, { view }) => {
|
|
||||||
toggleMark(this.schemaType)(view.state, view.dispatch)
|
|
||||||
next()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
schema(): MarkSpec {
|
schema(): MarkSpec {
|
||||||
return {
|
return {
|
||||||
excludes: '_',
|
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() {
|
keys() {
|
||||||
return {
|
return {
|
||||||
'Mod-`': () => this.editor.code(),
|
'Mod-`': () => this.editor.code(),
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import Editor, { Extension } from '@tiptap/core'
|
import Editor, { Extension, CommandSpec } from '@tiptap/core'
|
||||||
import {
|
import {
|
||||||
history,
|
history,
|
||||||
undo,
|
undo,
|
||||||
@ -18,16 +18,17 @@ export default class History extends Extension {
|
|||||||
|
|
||||||
name = 'history'
|
name = 'history'
|
||||||
|
|
||||||
created() {
|
commands(): CommandSpec {
|
||||||
this.editor.registerCommand('undo', (next, { view }) => {
|
return {
|
||||||
undo(view.state, view.dispatch)
|
undo: (next, { view }) => {
|
||||||
next()
|
undo(view.state, view.dispatch)
|
||||||
})
|
next()
|
||||||
|
},
|
||||||
this.editor.registerCommand('redo', (next, { view }) => {
|
redo: (next, { view }) => {
|
||||||
redo(view.state, view.dispatch)
|
redo(view.state, view.dispatch)
|
||||||
next()
|
next()
|
||||||
})
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
keys() {
|
keys() {
|
||||||
|
@ -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 { toggleMark } from 'prosemirror-commands'
|
||||||
import { MarkSpec } from 'prosemirror-model'
|
import { MarkSpec } from 'prosemirror-model'
|
||||||
import VerEx from 'verbal-expressions'
|
import VerEx from 'verbal-expressions'
|
||||||
@ -13,13 +13,6 @@ export default class Italic extends Mark {
|
|||||||
|
|
||||||
name = 'italic'
|
name = 'italic'
|
||||||
|
|
||||||
created() {
|
|
||||||
this.editor.registerCommand('italic', (next, { view }) => {
|
|
||||||
toggleMark(this.schemaType)(view.state, view.dispatch)
|
|
||||||
next()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
schema(): MarkSpec {
|
schema(): MarkSpec {
|
||||||
return {
|
return {
|
||||||
parseDOM: [
|
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() {
|
keys() {
|
||||||
return {
|
return {
|
||||||
'Mod-i': () => this.editor.italic(),
|
'Mod-i': () => this.editor.italic(),
|
||||||
|
Loading…
Reference in New Issue
Block a user