dont extend mark range for updateMarkAttributes

This commit is contained in:
Philipp Kühn
2020-11-06 13:38:48 +01:00
parent ab1d789161
commit d9df787fde
2 changed files with 17 additions and 21 deletions

View File

@@ -1,32 +1,24 @@
import { MarkType } from 'prosemirror-model'
import { Command } from '../Editor'
import getMarkType from '../utils/getMarkType'
import getMarkRange from '../utils/getMarkRange'
import getMarkAttrs from '../utils/getMarkAttrs'
export default (typeOrName: string | MarkType, attrs: {}): Command => ({ tr, state, dispatch }) => {
export default (typeOrName: string | MarkType, attributes: {}): Command => ({ tr, state, dispatch }) => {
const { selection } = tr
let { from, to } = selection
const { $from, empty } = selection
const { from, to, empty } = selection
const type = getMarkType(typeOrName, state.schema)
if (empty) {
const range = getMarkRange($from, type)
if (range) {
from = range.from
to = range.to
}
const oldAttributes = getMarkAttrs(state, type)
const newAttributes = {
...oldAttributes,
...attributes,
}
// TODO: toggleMark?
// const hasMark = doc.rangeHasMark(from, to, type)
// if (hasMark && dispatch) {
// tr.removeMark(from, to, type)
// }
if (dispatch) {
tr.addMark(from, to, type.create(attrs))
if (empty) {
tr.addStoredMark(type.create(newAttributes))
} else {
tr.addMark(from, to, type.create(newAttributes))
}
}
return true

View File

@@ -17,6 +17,7 @@ export default function getMarkRange($pos: ResolvedPos, type: MarkType): Range |
}
const link = start.node.marks.find(mark => mark.type === type)
if (!link) {
return
}
@@ -36,5 +37,8 @@ export default function getMarkRange($pos: ResolvedPos, type: MarkType): Range |
endIndex += 1
}
return { from: startPos, to: endPos }
return {
from: startPos,
to: endPos,
}
}