fix: condition node supports drag and drop#2833
Conversation
|
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. DetailsInstructions 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. |
|
[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. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
| set(props.nodeModel.properties.node_data, 'branch', list) | ||
| return | ||
| } | ||
| list[newIndex].type = list[oldIndex].type |
There was a problem hiding this comment.
The provided function has a few potential issues and can be optimized:
Potential Issues:
-
Cloning Logic: While
cloneDeepis used, it doesn't seem to affect the original arrayprops.nodeModel.properties.node_data.branch. Ensure you understand that cloning isn't necessary here if you're only modifying existing elements. -
Edge Case Handling: The condition when both
oldIndexandnewIndexarelist.length - 1checks 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. -
Simplified Assignment: Instead of creating new variables (
clonedData,list) just to swap values, you could directly manipulate the existing array using array methods likesplice. -
Set Operation: Using
set()from Redux Toolkit for updating state might not be necessary unless your implementation requires managing multiple properties separately. Direct manipulation withObject.assign(),spread operator, or even mutation might be more efficient depending on your use case.
Optimizations:
-
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; }
-
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.
What this PR does / why we need it?
Summary of your change
Please indicate you've done the following: