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.createSchema()
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('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('toggleNode', require('./commands/toggleNode').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) {
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
}