add toggleMark command
This commit is contained in:
@@ -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')
|
||||
|
||||
15
packages/core/src/commands/selectParentNode.ts
Normal file
15
packages/core/src/commands/selectParentNode.ts
Normal 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()
|
||||
}
|
||||
20
packages/core/src/commands/toggleMark.ts
Normal file
20
packages/core/src/commands/toggleMark.ts
Normal 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()
|
||||
}
|
||||
9
packages/core/src/utils/getMarkType.ts
Normal file
9
packages/core/src/utils/getMarkType.ts
Normal 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
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Mark, CommandSpec, markInputRule, markPasteRule } from '@tiptap/core'
|
||||
import { toggleMark } from 'prosemirror-commands'
|
||||
import { MarkSpec } from 'prosemirror-model'
|
||||
import VerEx from 'verbal-expressions'
|
||||
|
||||
@@ -34,8 +33,8 @@ export default class Bold extends Mark {
|
||||
|
||||
commands(): CommandSpec {
|
||||
return {
|
||||
bold: (next, { view }) => () => {
|
||||
toggleMark(this.type)(view.state, view.dispatch)
|
||||
bold: next => () => {
|
||||
this.editor.toggleMark(this.name)
|
||||
next()
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Mark, markInputRule, markPasteRule, CommandSpec } from '@tiptap/core'
|
||||
import { toggleMark } from 'prosemirror-commands'
|
||||
import { MarkSpec } from 'prosemirror-model'
|
||||
import VerEx from 'verbal-expressions'
|
||||
|
||||
@@ -25,8 +24,8 @@ export default class Code extends Mark {
|
||||
|
||||
commands(): CommandSpec {
|
||||
return {
|
||||
code: (next, { view }) => () => {
|
||||
toggleMark(this.type)(view.state, view.dispatch)
|
||||
code: next => () => {
|
||||
this.editor.toggleMark(this.name)
|
||||
next()
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Mark, markInputRule, markPasteRule, CommandSpec } from '@tiptap/core'
|
||||
import { toggleMark } from 'prosemirror-commands'
|
||||
import { MarkSpec } from 'prosemirror-model'
|
||||
import VerEx from 'verbal-expressions'
|
||||
|
||||
@@ -26,8 +25,8 @@ export default class Italic extends Mark {
|
||||
|
||||
commands(): CommandSpec {
|
||||
return {
|
||||
italic: (next, { view }) => () => {
|
||||
toggleMark(this.type)(view.state, view.dispatch)
|
||||
italic: next => () => {
|
||||
this.editor.toggleMark(this.name)
|
||||
next()
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user