mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-27 14:59:27 +08:00
fix(core): handle selections better for updateAttributes
(#5738)
This commit is contained in:
parent
e33885847e
commit
c50eb4bc2b
5
.changeset/swift-keys-collect.md
Normal file
5
.changeset/swift-keys-collect.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@tiptap/core": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Improve handling of selections with `updateAttributes`. Should no longer modify parent nodes of the same type.
|
@ -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'
|
||||||
@ -30,6 +33,7 @@ declare module '@tiptap/core' {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const updateAttributes: RawCommands['updateAttributes'] = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
|
export const updateAttributes: RawCommands['updateAttributes'] = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
|
||||||
|
|
||||||
let nodeType: NodeType | null = null
|
let nodeType: NodeType | null = null
|
||||||
let markType: MarkType | null = null
|
let markType: MarkType | null = null
|
||||||
|
|
||||||
@ -51,24 +55,80 @@ export const updateAttributes: RawCommands['updateAttributes'] = (typeOrName, at
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (dispatch) {
|
if (dispatch) {
|
||||||
tr.selection.ranges.forEach(range => {
|
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) => {
|
let lastPos: number | undefined
|
||||||
if (nodeType && nodeType === node.type) {
|
let lastNode: Node | undefined
|
||||||
tr.setNodeMarkup(pos, undefined, {
|
let trimmedFrom: number
|
||||||
...node.attrs,
|
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,
|
...attributes,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (markType && node.marks.length) {
|
if (markType && lastNode.marks.length) {
|
||||||
node.marks.forEach(mark => {
|
lastNode.marks.forEach((mark: Mark) => {
|
||||||
if (markType === mark.type) {
|
|
||||||
const trimmedFrom = Math.max(pos, from)
|
|
||||||
const trimmedTo = Math.min(pos + node.nodeSize, to)
|
|
||||||
|
|
||||||
|
if (markType === mark.type) {
|
||||||
tr.addMark(
|
tr.addMark(
|
||||||
trimmedFrom,
|
trimmedFrom,
|
||||||
trimmedTo,
|
trimmedTo,
|
||||||
@ -80,7 +140,7 @@ export const updateAttributes: RawCommands['updateAttributes'] = (typeOrName, at
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user