Skip to content

Commit

Permalink
Fix NodePos logic for child position calculation and attribute changes (
Browse files Browse the repository at this point in the history
#5716)

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

* fix nodepos attribute update when node is not text

* added changesets
  • Loading branch information
bdbch authored Oct 10, 2024
1 parent 0c2b2d1 commit d96f679
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-bats-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/core": patch
---

Fixed an issue while updating attributes on a NodePos that was not a text
5 changes: 5 additions & 0 deletions .changeset/sweet-feet-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/core": patch
---

Fixed issues with NodePos child positions causing wrong positions when used on non-text atoms
13 changes: 9 additions & 4 deletions packages/core/src/NodePos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit d96f679

Please sign in to comment.