Skip to content

Commit

Permalink
main.mixin.DeltaUpdates: du_moveNode() => improve the support for dir…
Browse files Browse the repository at this point in the history
…ect swap OPs #6346
  • Loading branch information
tobiu committed Jan 30, 2025
1 parent 316f290 commit 81b3416
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main/mixin/DeltaUpdates.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,24 @@ class DeltaUpdates extends Base {
* @param {String} delta.index
* @param {String} delta.parentId
*/
du_moveNode(delta) {
let {index} = delta,
node = this.getElement(delta.id),
parentNode = this.getElement(delta.parentId);
du_moveNode({id, index, parentId}) {
let node = this.getElement(id),
parentNode = this.getElement(parentId),
currentNode;

if (node && parentNode) {
if (index >= parentNode.children.length) {
parentNode.appendChild(node)
} else {
//index++; // todo?: increase the index in case same parent, oldIndex < newIndex, direct swap
if (node && parentNode.children[index].id !== delta.id) {
parentNode.insertBefore(node, parentNode.children[index])
currentNode = parentNode.children[index];

if (node && currentNode.id !== id) {
// Check for a direct swap OP
if (node === currentNode.nextElementSibling) {
node.replaceWith(currentNode)
}

parentNode.insertBefore(node, currentNode)
}
}
}
Expand Down

0 comments on commit 81b3416

Please sign in to comment.