add toggleMark command

This commit is contained in:
Philipp Kühn
2020-04-22 14:06:15 +02:00
parent 0e18e67c93
commit 4e6404a404
7 changed files with 57 additions and 14 deletions

View File

@@ -65,14 +65,16 @@ export class Editor extends EventEmitter {
this.createExtensionManager() this.createExtensionManager()
this.createSchema() this.createSchema()
this.createView() this.createView()
this.registerCommand('focus', require('./commands/focus').default)
this.registerCommand('insertText', require('./commands/insertText').default)
this.registerCommand('insertHTML', require('./commands/insertHTML').default)
this.registerCommand('setContent', require('./commands/setContent').default)
this.registerCommand('clearContent', require('./commands/clearContent').default) this.registerCommand('clearContent', require('./commands/clearContent').default)
this.registerCommand('focus', require('./commands/focus').default)
this.registerCommand('insertHTML', require('./commands/insertHTML').default)
this.registerCommand('insertText', require('./commands/insertText').default)
this.registerCommand('removeMarks', require('./commands/removeMarks').default) this.registerCommand('removeMarks', require('./commands/removeMarks').default)
this.registerCommand('toggleNode', require('./commands/toggleNode').default)
this.registerCommand('selectAll', require('./commands/selectAll').default) this.registerCommand('selectAll', require('./commands/selectAll').default)
this.registerCommand('selectParentNode', require('./commands/selectParentNode').default)
this.registerCommand('setContent', require('./commands/setContent').default)
this.registerCommand('toggleMark', require('./commands/toggleMark').default)
this.registerCommand('toggleNode', require('./commands/toggleNode').default)
if (this.options.injectCSS) { if (this.options.injectCSS) {
require('./style.css') require('./style.css')

View File

@@ -0,0 +1,15 @@
import { Editor } from '../Editor'
import { selectParentNode } from 'prosemirror-commands'
type SelectParentNode = () => any
declare module '../Editor' {
interface Editor {
selectParentNode: SelectParentNode,
}
}
export default (next: Function, { state, view }: Editor): SelectParentNode => () => {
selectParentNode(state, view.dispatch)
next()
}

View File

@@ -0,0 +1,20 @@
import { Editor } from '../Editor'
import { toggleMark } from 'prosemirror-commands'
import { MarkType } from 'prosemirror-model'
import getMarkType from '../utils/getMarkType'
type ToggleMark = (type: string | MarkType) => any
declare module '../Editor' {
interface Editor {
toggleMark: ToggleMark,
}
}
export default (next: Function, editor: Editor): ToggleMark => (typeOrName) => {
const { view, state, schema } = editor
const type = getMarkType(typeOrName, schema)
toggleMark(type)(state, view.dispatch)
next()
}

View File

@@ -0,0 +1,9 @@
import { MarkType, Schema } from 'prosemirror-model'
export default function getMarkType(nameOrType: string | MarkType, schema: Schema): MarkType {
if (typeof nameOrType === 'string') {
return schema.marks[nameOrType]
}
return nameOrType
}

View File

@@ -1,5 +1,4 @@
import { Mark, CommandSpec, markInputRule, markPasteRule } from '@tiptap/core' import { Mark, CommandSpec, markInputRule, markPasteRule } from '@tiptap/core'
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'
@@ -34,8 +33,8 @@ export default class Bold extends Mark {
commands(): CommandSpec { commands(): CommandSpec {
return { return {
bold: (next, { view }) => () => { bold: next => () => {
toggleMark(this.type)(view.state, view.dispatch) this.editor.toggleMark(this.name)
next() next()
}, },
} }

View File

@@ -1,5 +1,4 @@
import { Mark, markInputRule, markPasteRule, CommandSpec } from '@tiptap/core' import { Mark, markInputRule, markPasteRule, CommandSpec } from '@tiptap/core'
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'
@@ -25,8 +24,8 @@ export default class Code extends Mark {
commands(): CommandSpec { commands(): CommandSpec {
return { return {
code: (next, { view }) => () => { code: next => () => {
toggleMark(this.type)(view.state, view.dispatch) this.editor.toggleMark(this.name)
next() next()
}, },
} }

View File

@@ -1,5 +1,4 @@
import { Mark, markInputRule, markPasteRule, CommandSpec } from '@tiptap/core' import { Mark, markInputRule, markPasteRule, CommandSpec } from '@tiptap/core'
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'
@@ -26,8 +25,8 @@ export default class Italic extends Mark {
commands(): CommandSpec { commands(): CommandSpec {
return { return {
italic: (next, { view }) => () => { italic: next => () => {
toggleMark(this.type)(view.state, view.dispatch) this.editor.toggleMark(this.name)
next() next()
}, },
} }