All files / src/graph-runtime render.ts

100% Statements 220/220
71.42% Branches 5/7
100% Functions 5/5
100% Lines 220/220

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 2213x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/** Canvas rendering โ€” grid, edges, nodes. */
export const GRAPH_RUNTIME_RENDER = `
  function statusColor(status) {
    if (status === "โœ…") return getComputedStyle(document.documentElement).getPropertyValue("--fg-done").trim() || "#22c55e";
    if (status === "๐Ÿ”ถ") return getComputedStyle(document.documentElement).getPropertyValue("--fg-partial").trim() || "#f59e0b";
    if (status === "๐ŸงŠ") return getComputedStyle(document.documentElement).getPropertyValue("--fg-frozen").trim() || "#60a5fa";
    if (status === "๐Ÿšซ") return getComputedStyle(document.documentElement).getPropertyValue("--fg-deprecated").trim() || "#f87171";
    if (status === "โŒ") return getComputedStyle(document.documentElement).getPropertyValue("--fg-open").trim() || "#fb923c";
    return getComputedStyle(document.documentElement).getPropertyValue("--fg-muted").trim() || "#94a3b8";
  }
 
  function neighborsOf(node) {
    if (!node) return new Set();
    const set = new Set([node]);
    for (const e of edges) {
      if (e.from === node) set.add(e.to);
      if (e.to === node) set.add(e.from);
    }
    return set;
  }
 
  function drawGrid() {
    const step = 20 * transform.scale;
    if (step < 6) return;
    const ox = transform.x % step;
    const oy = transform.y % step;
    ctx.save();
    ctx.fillStyle = getComputedStyle(document.documentElement).getPropertyValue("--fg-grid").trim() || "#334155";
    ctx.globalAlpha = 0.45;
    const dot = Math.max(0.8, 1.2 / transform.scale);
    for (let x = ox; x < width; x += step) {
      for (let y = oy; y < height; y += step) {
        ctx.beginPath();
        ctx.arc(x, y, dot, 0, Math.PI * 2);
        ctx.fill();
      }
    }
    ctx.restore();
  }
 
  function drawEdge(e, dimmed) {
    const a = e.from;
    const b = e.to;
    const dx = b.x - a.x;
    const dy = b.y - a.y;
    const len = Math.hypot(dx, dy) || 1;
    const ux = dx / len;
    const uy = dy / len;
    const x1 = a.x + ux * a.r;
    const y1 = a.y + uy * a.r;
    const x2 = b.x - ux * b.r;
    const y2 = b.y - uy * b.r;
    const edgeColor = getComputedStyle(document.documentElement).getPropertyValue("--fg-edge").trim() || "#475569";
    ctx.strokeStyle = edgeColor;
    ctx.fillStyle = edgeColor;
    ctx.lineWidth = 1.2 / transform.scale;
    ctx.globalAlpha = dimmed ? 0.12 : 0.55;
    ctx.beginPath();
    ctx.moveTo(x1, y1);
    ctx.lineTo(x2, y2);
    ctx.stroke();
    const ah = 7 / transform.scale;
    const ax = x2 - ux * ah;
    const ay = y2 - uy * ah;
    ctx.beginPath();
    ctx.moveTo(x2, y2);
    ctx.lineTo(ax - uy * ah * 0.45, ay + ux * ah * 0.45);
    ctx.lineTo(ax + uy * ah * 0.45, ay - ux * ah * 0.45);
    ctx.closePath();
    ctx.fill();
    ctx.globalAlpha = 1;
  }
 
  function nodeFill(n) {
    if (payload.kind === "import" && n.color) return n.color;
    if (payload.kind === "delivery") return statusColor(n.status);
    return getComputedStyle(document.documentElement).getPropertyValue("--fg-node").trim() || "#38bdf8";
  }
 
  function drawImportLegend() {
    const legend = payload.fileTypeLegend || [];
    if (!legend.length || payload.kind !== "import") return;
    const pad = 10;
    const rowH = 14;
    const boxW = 118;
    const boxH = pad * 2 + legend.length * rowH;
    const x = width - boxW - 12;
    const y = 12;
    ctx.save();
    ctx.fillStyle = "rgba(15, 23, 42, 0.82)";
    ctx.strokeStyle = "rgba(148, 163, 184, 0.35)";
    ctx.lineWidth = 1;
    ctx.beginPath();
    ctx.rect(x, y, boxW, boxH);
    ctx.fill();
    ctx.stroke();
    ctx.font = "10px var(--vscode-font-family)";
    ctx.textAlign = "left";
    ctx.textBaseline = "middle";
    for (let i = 0; i < legend.length; i++) {
      const item = legend[i];
      const rowY = y + pad + i * rowH + rowH / 2;
      ctx.fillStyle = item.color;
      ctx.beginPath();
      ctx.arc(x + 12, rowY, 4, 0, Math.PI * 2);
      ctx.fill();
      ctx.fillStyle = "var(--fg-label)";
      const label = item.type + " (" + item.count + ")";
      ctx.fillText(label, x + 22, rowY);
    }
    ctx.restore();
  }
 
  function drawDeliveryLegend() {
    if (payload.kind !== "delivery") return;
    const items = [
      { label: "Open", color: statusColor("โŒ") },
      { label: "Partial", color: statusColor("๐Ÿ”ถ") },
      { label: "Done", color: statusColor("โœ…") },
      { label: "Frozen", color: statusColor("๐ŸงŠ") },
      { label: "Deprecated", color: statusColor("๐Ÿšซ") },
    ];
    const pad = 10;
    const rowH = 14;
    const boxW = 128;
    const boxH = pad * 2 + items.length * rowH;
    const x = width - boxW - 12;
    const y = 12;
    ctx.save();
    ctx.fillStyle = "rgba(15, 23, 42, 0.82)";
    ctx.strokeStyle = "rgba(148, 163, 184, 0.35)";
    ctx.lineWidth = 1;
    ctx.beginPath();
    ctx.rect(x, y, boxW, boxH);
    ctx.fill();
    ctx.stroke();
    ctx.font = "10px var(--vscode-font-family)";
    ctx.textAlign = "left";
    ctx.textBaseline = "middle";
    for (let i = 0; i < items.length; i++) {
      const item = items[i];
      const rowY = y + pad + i * rowH + rowH / 2;
      ctx.fillStyle = item.color;
      ctx.beginPath();
      ctx.arc(x + 12, rowY, 4, 0, Math.PI * 2);
      ctx.fill();
      ctx.fillStyle = "var(--fg-label)";
      ctx.fillText(item.label, x + 22, rowY);
    }
    ctx.restore();
  }
 
  function worldBounds() {
    const pad = 40 / transform.scale;
    return {
      minX: (-transform.x) / transform.scale - pad,
      minY: (-transform.y) / transform.scale - pad,
      maxX: (width - transform.x) / transform.scale + pad,
      maxY: (height - transform.y) / transform.scale + pad,
    };
  }
 
  function nodeVisible(n, bounds) {
    return n.x + n.r >= bounds.minX && n.x - n.r <= bounds.maxX &&
      n.y + n.r >= bounds.minY && n.y - n.r <= bounds.maxY;
  }
 
  function draw() {
    ctx.save();
    ctx.clearRect(0, 0, width, height);
    drawGrid();
    ctx.translate(transform.x, transform.y);
    ctx.scale(transform.scale, transform.scale);
 
    const focus = neighborsOf(hovered);
    const hasFocus = hovered && focus.size > 1;
    const cull = nodes.length > 180;
    const bounds = cull ? worldBounds() : null;
 
    for (const e of edges) {
      if (cull && bounds && !nodeVisible(e.from, bounds) && !nodeVisible(e.to, bounds)) continue;
      const dimmed = hasFocus && !focus.has(e.from) && !focus.has(e.to);
      drawEdge(e, dimmed);
    }
 
    for (const n of nodes) {
      if (cull && bounds && !nodeVisible(n, bounds) && n !== hovered && n !== draggingNode) continue;
      const active = n === hovered || n === draggingNode;
      const dimmed = hasFocus && !focus.has(n);
      const fill = nodeFill(n);
      ctx.beginPath();
      ctx.arc(n.x, n.y, n.r, 0, Math.PI * 2);
      ctx.fillStyle = fill;
      ctx.globalAlpha = dimmed ? 0.18 : active ? 1 : 0.92;
      ctx.fill();
      ctx.strokeStyle = active ? "var(--fg-highlight)" : "rgba(255,255,255,0.15)";
      ctx.lineWidth = (active ? 2.2 : 1) / transform.scale;
      ctx.stroke();
      ctx.globalAlpha = 1;
 
      if (!dimmed && (active || transform.scale > 0.55)) {
        ctx.fillStyle = "var(--fg-label)";
        ctx.font = Math.max(9, 11 / transform.scale) + "px var(--vscode-font-family)";
        ctx.textAlign = "center";
        ctx.textBaseline = "top";
        const shortId = n.id.split("/").pop() || n.id;
        const label = active
          ? (n.label || shortId)
          : (shortId.length > 14 ? shortId.slice(0, 12) + "โ€ฆ" : shortId);
        ctx.fillText(label, n.x, n.y + n.r + 3);
      }
    }
    ctx.restore();
    drawImportLegend();
    drawDeliveryLegend();
 
    const stats = document.getElementById("graphStats");
    if (stats) stats.textContent = nodes.length + " nodes ยท " + edges.length + " links";
  }
`.trim()