Fix NodePos logic for child position calculation and attribute changes (#5716)

* fix( nodepos child offsets when child is a non-text atom

* fix nodepos attribute update when node is not text

* added changesets
This commit is contained in:
bdbch 2024-10-10 12:43:58 +02:00 committed by GitHub
parent 0c2b2d100d
commit d96f679585
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 4 deletions

View File

@ -0,0 +1,5 @@
---
"@tiptap/core": patch
---
Fixed an issue while updating attributes on a NodePos that was not a text

View File

@ -0,0 +1,5 @@
---
"@tiptap/core": patch
---
Fixed issues with NodePos child positions causing wrong positions when used on non-text atoms

View File

@ -135,8 +135,9 @@ export class NodePos {
this.node.content.forEach((node, offset) => {
const isBlock = node.isBlock && !node.isTextblock
const isNonTextAtom = node.isAtom && !node.isText
const targetPos = this.pos + offset + 1
const targetPos = this.pos + offset + (isNonTextAtom ? 0 : 1)
const $pos = this.resolvedPos.doc.resolve(targetPos)
if (!isBlock && $pos.depth <= this.depth) {
@ -235,9 +236,13 @@ export class NodePos {
}
setAttribute(attributes: { [key: string]: any }) {
const oldSelection = this.editor.state.selection
const { tr } = this.editor.state
this.editor.chain().setTextSelection(this.from).updateAttributes(this.node.type.name, attributes).setTextSelection(oldSelection.from)
.run()
tr.setNodeMarkup(this.from, undefined, {
...this.node.attrs,
...attributes,
})
this.editor.view.dispatch(tr)
}
}