(layoutState: LayoutTreeState, computeInsertAction: LayoutTreeComputeMoveNodeAction)
| 41 | * @param computeInsertAction The operation to compute. |
| 42 | */ |
| 43 | export function computeMoveNode(layoutState: LayoutTreeState, computeInsertAction: LayoutTreeComputeMoveNodeAction) { |
| 44 | const rootNode = layoutState.rootNode; |
| 45 | const { nodeId, nodeToMoveId, direction } = computeInsertAction; |
| 46 | if (!nodeId || !nodeToMoveId) { |
| 47 | console.warn("either nodeId or nodeToMoveId not set", nodeId, nodeToMoveId); |
| 48 | return; |
| 49 | } |
| 50 | if (direction === undefined) { |
| 51 | console.warn("No direction provided for insertItemInDirection"); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | if (nodeId === nodeToMoveId) { |
| 56 | console.warn("Cannot compute move node action since both nodes are equal"); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | let newMoveOperation: MoveOperation; |
| 61 | const parent = lazy(() => findParent(rootNode, nodeId)); |
| 62 | const grandparent = lazy(() => findParent(rootNode, parent().id)); |
| 63 | const indexInParent = lazy(() => parent()?.children.findIndex((child) => nodeId === child.id)); |
| 64 | const indexInGrandparent = lazy(() => grandparent()?.children.findIndex((child) => parent().id === child.id)); |
| 65 | const nodeToMoveParent = lazy(() => findParent(rootNode, nodeToMoveId)); |
| 66 | const nodeToMoveIndexInParent = lazy(() => |
| 67 | nodeToMoveParent()?.children.findIndex((child) => nodeToMoveId === child.id) |
| 68 | ); |
| 69 | const isRoot = rootNode.id === nodeId; |
| 70 | |
| 71 | // TODO: this should not be necessary. The drag layer is having trouble tracking changes to the LayoutNode fields, so I need to grab the node again here to get the latest data. |
| 72 | const node = findNode(rootNode, nodeId); |
| 73 | const nodeToMove = findNode(rootNode, nodeToMoveId); |
| 74 | |
| 75 | if (!node || !nodeToMove) { |
| 76 | console.warn("node or nodeToMove not set", nodeId, nodeToMoveId); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | switch (direction) { |
| 81 | case DropDirection.OuterTop: |
| 82 | if (node.flexDirection === FlexDirection.Column) { |
| 83 | const grandparentNode = grandparent(); |
| 84 | if (grandparentNode) { |
| 85 | const index = indexInGrandparent(); |
| 86 | newMoveOperation = { |
| 87 | parentId: grandparentNode.id, |
| 88 | node: nodeToMove, |
| 89 | index, |
| 90 | }; |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | // falls through |
| 95 | case DropDirection.Top: |
| 96 | if (node.flexDirection === FlexDirection.Column) { |
| 97 | newMoveOperation = { parentId: nodeId, index: 0, node: nodeToMove }; |
| 98 | } else { |
| 99 | if (isRoot) |
| 100 | newMoveOperation = { |
no test coverage detected