(layoutState: LayoutTreeState, action: LayoutTreeSplitHorizontalAction)
| 452 | // ─── SPLIT HORIZONTAL ───────────────────────────────────────────────────────────── |
| 453 | |
| 454 | export function splitHorizontal(layoutState: LayoutTreeState, action: LayoutTreeSplitHorizontalAction) { |
| 455 | const { targetNodeId, newNode, position } = action; |
| 456 | const targetNode = findNode(layoutState.rootNode, targetNodeId); |
| 457 | if (!targetNode) { |
| 458 | console.error("splitHorizontal: Target node not found", targetNodeId); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | const parent = findParent(layoutState.rootNode, targetNodeId); |
| 463 | if (parent && parent.flexDirection === FlexDirection.Row) { |
| 464 | const index = parent.children.findIndex((child) => child.id === targetNodeId); |
| 465 | if (index === -1) { |
| 466 | console.error("splitHorizontal: Target node not found in parent's children", targetNodeId); |
| 467 | return; |
| 468 | } |
| 469 | const insertIndex = position === "before" ? index : index + 1; |
| 470 | // Directly splice in the new node instead of calling addChildAt (which may flatten nodes) |
| 471 | parent.children.splice(insertIndex, 0, newNode); |
| 472 | } else { |
| 473 | // Otherwise, if no parent or parent's flexDirection is not Row, we need to wrap |
| 474 | // Create a new group node with horizontal layout. |
| 475 | // IMPORTANT: pass an initial children array so the new node is valid. |
| 476 | const groupNode = newLayoutNode(FlexDirection.Row, targetNode.size, [targetNode], undefined); |
| 477 | // Now decide the ordering based on the "position" |
| 478 | groupNode.children = position === "before" ? [newNode, targetNode] : [targetNode, newNode]; |
| 479 | if (parent) { |
| 480 | const index = parent.children.findIndex((child) => child.id === targetNodeId); |
| 481 | if (index === -1) { |
| 482 | console.error("splitHorizontal (wrap): Target node not found in parent's children", targetNodeId); |
| 483 | return; |
| 484 | } |
| 485 | parent.children[index] = groupNode; |
| 486 | } else { |
| 487 | layoutState.rootNode = groupNode; |
| 488 | } |
| 489 | } |
| 490 | if (action.focused) { |
| 491 | layoutState.focusedNodeId = newNode.id; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | // ─── SPLIT VERTICAL ───────────────────────────────────────────────────────────── |
| 496 |
no test coverage detected