fix(core): handle selections better for updateAttributes (#5738)

This commit is contained in:
Julien Cigar 2024-11-04 11:04:58 +01:00 committed by GitHub
parent e33885847e
commit c50eb4bc2b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 77 additions and 12 deletions

View File

@ -0,0 +1,5 @@
---
"@tiptap/core": patch
---
Improve handling of selections with `updateAttributes`. Should no longer modify parent nodes of the same type.

View File

@ -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 { getNodeType } from '../helpers/getNodeType.js'
@ -30,6 +33,7 @@ declare module '@tiptap/core' {
}
export const updateAttributes: RawCommands['updateAttributes'] = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
let nodeType: NodeType | null = null
let markType: MarkType | null = null
@ -51,24 +55,80 @@ export const updateAttributes: RawCommands['updateAttributes'] = (typeOrName, at
}
if (dispatch) {
tr.selection.ranges.forEach(range => {
tr.selection.ranges.forEach((range: SelectionRange) => {
const from = range.$from.pos
const to = range.$to.pos
state.doc.nodesBetween(from, to, (node, pos) => {
if (nodeType && nodeType === node.type) {
tr.setNodeMarkup(pos, undefined, {
...node.attrs,
let lastPos: number | undefined
let lastNode: Node | undefined
let trimmedFrom: number
let trimmedTo: number
if (tr.selection.empty) {
state.doc.nodesBetween(from, to, (node: Node, pos: number) => {
if (nodeType && nodeType === node.type) {
trimmedFrom = Math.max(pos, from)
trimmedTo = Math.min(pos + node.nodeSize, to)
lastPos = pos
lastNode = node
}
})
} else {
state.doc.nodesBetween(from, to, (node: Node, pos: number) => {
if (pos < from && nodeType && nodeType === node.type) {
trimmedFrom = Math.max(pos, from)
trimmedTo = Math.min(pos + node.nodeSize, to)
lastPos = pos
lastNode = node
}
if (pos >= from && pos <= to) {
if (nodeType && nodeType === node.type) {
tr.setNodeMarkup(pos, undefined, {
...node.attrs,
...attributes,
})
}
if (markType && node.marks.length) {
node.marks.forEach((mark: Mark) => {
if (markType === mark.type) {
const trimmedFrom2 = Math.max(pos, from)
const trimmedTo2 = Math.min(pos + node.nodeSize, to)
tr.addMark(
trimmedFrom2,
trimmedTo2,
markType.create({
...mark.attrs,
...attributes,
}),
)
}
})
}
}
})
}
if (lastNode) {
if (lastPos !== undefined) {
tr.setNodeMarkup(lastPos, undefined, {
...lastNode.attrs,
...attributes,
})
}
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)
if (markType && lastNode.marks.length) {
lastNode.marks.forEach((mark: Mark) => {
if (markType === mark.type) {
tr.addMark(
trimmedFrom,
trimmedTo,
@ -80,7 +140,7 @@ export const updateAttributes: RawCommands['updateAttributes'] = (typeOrName, at
}
})
}
})
}
})
}