Skip to content

fix: condition node supports drag and drop#2833

Merged
wangdan-fit2cloud merged 1 commit intomainfrom
pr@main/condition-node
Apr 9, 2025
Merged

fix: condition node supports drag and drop#2833
wangdan-fit2cloud merged 1 commit intomainfrom
pr@main/condition-node

Conversation

@wangdan-fit2cloud
Copy link
Copy Markdown
Contributor

What this PR does / why we need it?

Summary of your change

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follow the rule of Conventional Commits specification.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed.

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Apr 9, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Apr 9, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@wangdan-fit2cloud wangdan-fit2cloud merged commit 675d236 into main Apr 9, 2025
4 checks passed
@wangdan-fit2cloud wangdan-fit2cloud deleted the pr@main/condition-node branch April 9, 2025 03:52
set(props.nodeModel.properties.node_data, 'branch', list)
return
}
list[newIndex].type = list[oldIndex].type
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided function has a few potential issues and can be optimized:

Potential Issues:

  1. Cloning Logic: While cloneDeep is used, it doesn't seem to affect the original array props.nodeModel.properties.node_data.branch. Ensure you understand that cloning isn't necessary here if you're only modifying existing elements.

  2. Edge Case Handling: The condition when both oldIndex and newIndex are list.length - 1 checks whether swapping these positions would cause an index out of bounds error. This should always handle safely without further action needed since indices wrap around arrays in JavaScript.

  3. Simplified Assignment: Instead of creating new variables (clonedData, list) just to swap values, you could directly manipulate the existing array using array methods like splice.

  4. Set Operation: Using set() from Redux Toolkit for updating state might not be necessary unless your implementation requires managing multiple properties separately. Direct manipulation with Object.assign(), spread operator, or even mutation might be more efficient depending on your use case.

Optimizations:

  1. Direct Swapping:

    if (newIndex === list.length - 1 && oldIndex === list.length - 1) {
      [list[newIndex], list[oldIndex]] = [list[oldIndex], list[newIndex]];
      set(props.nodeModel.properties.node_data, 'branch', list);
      return;
    }
  2. Efficient Manipulation:

    if (newIndex === oldIndex) return;
    
    // Create a copy of the data at the new and old indexes
    let newDataAtNew = clone(new ListAt(newNode));
    let origDataAtOld = list[oldIndex];
    
    // Update the current node's branch at new index
    if (origDataAtOld.type !== List.atType) throw new Error('wrong type');
    newDataAtNew.value = [...origDataAtOld.value];
    newDataAtNew.keypath = origDataAtOld.keypath.concat([List.dataKey]);
    newDataAtNew.indexInMap = map.indexOf(oldIndex);
    
    // Remove the copied value at new index before adding back as newList
    delete map[newListIndex];
    
    // Put newList into its place in map, making sure to update keypaths
    const parentKeys = getKeyPathForValue(map, origDataAtOld.value)[0].split('.');
    let prevItemKey = '';
    try {
      map.update(keypath.join(dotSep), function(item) {
        // Update item keys based on their relative position to the old path
        if (!prevItemKey || prevItemKey.split('.').some((k) => !item[keypath][k])) newItemKey += '.' + Object.keys(item)[0];
        item[keypath]['$key'] = '$$' + (++nextId++); // Generate unique ID
        prevItemKey = newItemKey;
        // Reorder items in nested lists
        if (item.$array && item.$.size > 1) reorderArray(childLists[item.$key], item.$.sortIndex ? item.$.sortIndex : childLists[item.$key].indexOf(origDataAtOld.value),
                                                            {from: prevItemKey});
        updateChildMaps(mapToUpdateOnRemove, map, '', null);
    
        return item;
      });
    } catch(err) {
      console.error(`${err}`);
    }
    
    // Place newList after last inserted element by key so they appear correctly later
    map.set(list[list.length - 1].indexInMap + listSize + 1, newItemKey);
    
    map.delete(prevItemKey); // Delete the previously unused key
    
    this._onListInsertion({ ...dataBeforeChange, updatedNodes: [{ node: newNode }] }, context);
    nextSibling.push(appendWithKey(newDataAtNew.clone(), nextSibling[nextSibling.length - 1]));

This optimized version leverages direct swapping and ensures efficient updates while handling cases where the new index matches the old one gracefully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants