|
| 1 | +// Import Internal Dependencies |
| 2 | +import { CONNECTOR_GAP } from "./tree-layout.js"; |
| 3 | + |
| 4 | +export function drawConnectors(renderRoot) { |
| 5 | + const grid = renderRoot.querySelector(".tree-grid"); |
| 6 | + if (!grid) { |
| 7 | + return; |
| 8 | + } |
| 9 | + |
| 10 | + // Remove existing SVG |
| 11 | + grid.querySelector(".connectors-svg")?.remove(); |
| 12 | + |
| 13 | + const gridRect = grid.getBoundingClientRect(); |
| 14 | + if (gridRect.width === 0) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + const cells = grid.querySelectorAll(".tree-cell"); |
| 19 | + |
| 20 | + // Map each wrapper element to its rects: |
| 21 | + // - span bounds (top/bottom) from the stretched wrapper, for parent lookup |
| 22 | + // - midY from the inner card, for line anchoring at the visual card center |
| 23 | + const elementRects = new Map(); |
| 24 | + for (const cell of cells) { |
| 25 | + const wrapperRaw = cell.getBoundingClientRect(); |
| 26 | + const cardEl = cell.firstElementChild; |
| 27 | + const cardRaw = cardEl ? cardEl.getBoundingClientRect() : wrapperRaw; |
| 28 | + |
| 29 | + elementRects.set(cell, { |
| 30 | + left: wrapperRaw.left - gridRect.left, |
| 31 | + right: wrapperRaw.right - gridRect.left, |
| 32 | + top: wrapperRaw.top - gridRect.top, |
| 33 | + bottom: wrapperRaw.bottom - gridRect.top, |
| 34 | + midY: cardRaw.top - gridRect.top + (cardRaw.height / 2) |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + // Resolve parent element for each child using spatial overlap: |
| 39 | + // among all cells matching data-parent-id, pick the one whose vertical |
| 40 | + // span (stretched to fill its grid rows) contains the child's midY. |
| 41 | + const elementChildren = new Map(); |
| 42 | + for (const child of cells) { |
| 43 | + const rawParentId = child.dataset.parentId; |
| 44 | + if (!rawParentId) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + const parentId = Number(rawParentId); |
| 49 | + const childMidY = elementRects.get(child).midY; |
| 50 | + |
| 51 | + let bestParent = null; |
| 52 | + for (const candidate of cells) { |
| 53 | + if (Number(candidate.dataset.nodeId) !== parentId) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + const candidateRect = elementRects.get(candidate); |
| 58 | + if (childMidY >= candidateRect.top && childMidY <= candidateRect.bottom) { |
| 59 | + bestParent = candidate; |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (bestParent) { |
| 65 | + const children = elementChildren.get(bestParent) ?? []; |
| 66 | + children.push(child); |
| 67 | + elementChildren.set(bestParent, children); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + const isDark = document.body.classList.contains("dark"); |
| 72 | + const strokeColor = isDark |
| 73 | + ? "rgba(164, 148, 255, 0.3)" |
| 74 | + : "rgba(55, 34, 175, 0.18)"; |
| 75 | + |
| 76 | + const svgNS = "http://www.w3.org/2000/svg"; |
| 77 | + const svgEl = document.createElementNS(svgNS, "svg"); |
| 78 | + svgEl.classList.add("connectors-svg"); |
| 79 | + |
| 80 | + let hasPath = false; |
| 81 | + |
| 82 | + for (const [parent, children] of elementChildren) { |
| 83 | + const parentRect = elementRects.get(parent); |
| 84 | + const childRects = children.map((child) => elementRects.get(child)); |
| 85 | + |
| 86 | + const midX = parentRect.right + (CONNECTOR_GAP / 2); |
| 87 | + const childMidYs = childRects.map((rect) => rect.midY).sort((rectA, rectB) => rectA - rectB); |
| 88 | + const firstChildY = childMidYs[0]; |
| 89 | + const lastChildY = childMidYs.at(-1); |
| 90 | + |
| 91 | + let pathData = `M ${parentRect.right} ${parentRect.midY} H ${midX}`; |
| 92 | + |
| 93 | + // Vertical arm connecting to children's level |
| 94 | + if (Math.abs(parentRect.midY - firstChildY) > 1) { |
| 95 | + const targetY = childMidYs.length === 1 |
| 96 | + ? firstChildY |
| 97 | + : (firstChildY + lastChildY) / 2; |
| 98 | + pathData += ` V ${targetY}`; |
| 99 | + } |
| 100 | + |
| 101 | + // Vertical bracket if multiple children |
| 102 | + if (childMidYs.length > 1) { |
| 103 | + pathData += ` M ${midX} ${firstChildY} V ${lastChildY}`; |
| 104 | + } |
| 105 | + |
| 106 | + // Horizontal branch to each child |
| 107 | + for (const childRect of childRects) { |
| 108 | + pathData += ` M ${midX} ${childRect.midY} H ${childRect.left}`; |
| 109 | + } |
| 110 | + |
| 111 | + const pathEl = document.createElementNS(svgNS, "path"); |
| 112 | + pathEl.setAttribute("d", pathData); |
| 113 | + pathEl.setAttribute("fill", "none"); |
| 114 | + pathEl.setAttribute("stroke", strokeColor); |
| 115 | + pathEl.setAttribute("stroke-width", "1.5"); |
| 116 | + pathEl.setAttribute("stroke-linecap", "round"); |
| 117 | + pathEl.setAttribute("stroke-linejoin", "round"); |
| 118 | + svgEl.appendChild(pathEl); |
| 119 | + hasPath = true; |
| 120 | + } |
| 121 | + |
| 122 | + if (hasPath) { |
| 123 | + grid.insertBefore(svgEl, grid.firstChild); |
| 124 | + } |
| 125 | +} |
0 commit comments