mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-11 20:23:36 +08:00
fix(core): updateAttributes command update only the current node(#5154)
This commit is contained in:
parent
b45fb70fa8
commit
a52118c34b
@ -1,4 +1,7 @@
|
|||||||
import { MarkType, NodeType } from '@tiptap/pm/model'
|
import {
|
||||||
|
Mark, MarkType, Node, NodeType,
|
||||||
|
} from '@tiptap/pm/model'
|
||||||
|
import { SelectionRange } from '@tiptap/pm/state'
|
||||||
|
|
||||||
import { getMarkType } from '../helpers/getMarkType.js'
|
import { getMarkType } from '../helpers/getMarkType.js'
|
||||||
import { getNodeType } from '../helpers/getNodeType.js'
|
import { getNodeType } from '../helpers/getNodeType.js'
|
||||||
@ -51,37 +54,49 @@ export const updateAttributes: RawCommands['updateAttributes'] = (typeOrName, at
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (dispatch) {
|
if (dispatch) {
|
||||||
tr.selection.ranges.forEach(range => {
|
let lastPos: number | undefined
|
||||||
|
let lastNode: Node | undefined
|
||||||
|
let trimmedFrom: number
|
||||||
|
let trimmedTo: number
|
||||||
|
|
||||||
|
tr.selection.ranges.forEach((range: SelectionRange) => {
|
||||||
const from = range.$from.pos
|
const from = range.$from.pos
|
||||||
const to = range.$to.pos
|
const to = range.$to.pos
|
||||||
|
|
||||||
state.doc.nodesBetween(from, to, (node, pos) => {
|
state.doc.nodesBetween(from, to, (node: Node, pos: number) => {
|
||||||
if (nodeType && nodeType === node.type) {
|
if (nodeType && nodeType === node.type) {
|
||||||
tr.setNodeMarkup(pos, undefined, {
|
trimmedFrom = Math.max(pos, from)
|
||||||
...node.attrs,
|
trimmedTo = Math.min(pos + node.nodeSize, to)
|
||||||
...attributes,
|
lastPos = pos
|
||||||
})
|
lastNode = node
|
||||||
}
|
|
||||||
|
|
||||||
if (markType && node.marks.length) {
|
|
||||||
node.marks.forEach(mark => {
|
|
||||||
if (markType === mark.type) {
|
|
||||||
const trimmedFrom = Math.max(pos, from)
|
|
||||||
const trimmedTo = Math.min(pos + node.nodeSize, to)
|
|
||||||
|
|
||||||
tr.addMark(
|
|
||||||
trimmedFrom,
|
|
||||||
trimmedTo,
|
|
||||||
markType.create({
|
|
||||||
...mark.attrs,
|
|
||||||
...attributes,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (lastNode) {
|
||||||
|
|
||||||
|
if (lastPos !== undefined) {
|
||||||
|
tr.setNodeMarkup(lastPos, undefined, {
|
||||||
|
...lastNode.attrs,
|
||||||
|
...attributes,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (markType && lastNode.marks.length) {
|
||||||
|
lastNode.marks.forEach((mark: Mark) => {
|
||||||
|
if (markType === mark.type) {
|
||||||
|
tr.addMark(
|
||||||
|
trimmedFrom,
|
||||||
|
trimmedTo,
|
||||||
|
markType.create({
|
||||||
|
...mark.attrs,
|
||||||
|
...attributes,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
Loading…
Reference in New Issue
Block a user