add removeMark command

This commit is contained in:
Philipp Kühn
2020-04-22 14:22:31 +02:00
parent fd4453f7c5
commit fd8f5de375
4 changed files with 77 additions and 0 deletions

View File

@@ -69,6 +69,7 @@ export class Editor extends EventEmitter {
this.registerCommand('focus', require('./commands/focus').default)
this.registerCommand('insertHTML', require('./commands/insertHTML').default)
this.registerCommand('insertText', require('./commands/insertText').default)
this.registerCommand('removeMark', require('./commands/removeMark').default)
this.registerCommand('removeMarks', require('./commands/removeMarks').default)
this.registerCommand('selectAll', require('./commands/selectAll').default)
this.registerCommand('selectParentNode', require('./commands/selectParentNode').default)

View File

@@ -0,0 +1,32 @@
import { Editor } from '../Editor'
import { MarkType } from 'prosemirror-model'
import getMarkType from '../utils/getMarkType'
import getMarkRange from '../utils/getMarkRange'
type RemoveMark = (type: string | MarkType) => any
declare module '../Editor' {
interface Editor {
toggleMark: RemoveMark,
}
}
export default (next: Function, editor: Editor): RemoveMark => typeOrName => {
const { view, state, schema } = editor
const { tr, selection } = state
const type = getMarkType(typeOrName, schema)
let { from, to, $from, empty } = selection
if (empty) {
const range = getMarkRange($from, type)
if (range) {
from = range.from
to = range.to
}
}
tr.removeMark(from, to, type)
view.dispatch(tr)
next()
}

View File

@@ -0,0 +1,40 @@
import { MarkType, ResolvedPos } from 'prosemirror-model'
interface Range {
from: number,
to: number,
}
export default function getMarkRange($pos: ResolvedPos, type: MarkType): Range | void {
if (!$pos || !type) {
return
}
const start = $pos.parent.childAfter($pos.parentOffset)
if (!start.node) {
return
}
const link = start.node.marks.find(mark => mark.type === type)
if (!link) {
return
}
let startIndex = $pos.index()
let startPos = $pos.start() + start.offset
let endIndex = startIndex + 1
let endPos = startPos + start.node.nodeSize
while (startIndex > 0 && link.isInSet($pos.parent.child(startIndex - 1).marks)) {
startIndex -= 1
startPos -= $pos.parent.child(startIndex).nodeSize
}
while (endIndex < $pos.parent.childCount && link.isInSet($pos.parent.child(endIndex).marks)) {
endPos += $pos.parent.child(endIndex).nodeSize
endIndex += 1
}
return { from: startPos, to: endPos }
}