/* ui-canvas-edges.js —— 边线 canvas 视觉层(2026-11-30) * * 为什么不再用 SVG 画线: * - SVG 每次平移/缩放都要重栅格化一个屏幕大小的矢量层;WKWebView 上这是明确的热点。 * - canvas 只做一次 2D stroke,100 条贝塞尔很便宜,且不产生 SVG 的 DOM/合成层。 * * 交互仍由 .iwb-edges(透明 hit path)负责:canvas 只画,不接收指针事件。 * 平移/拖节点走命令式 draw(applyViewportDom / dragApplyVisual),不触发 React。 */ ;(function (global) { 'use strict' const React = global.React || {} const el = (type, prop, ...children) => React.createElement(type, prop, ...children) const useRef = React.useRef const useEffect = React.useEffect function cssVar(name, fallback) { try { const v = getComputedStyle(document.documentElement).getPropertyValue(name).trim() return v || fallback } catch (e) { return fallback } } function draw(canvas, o) { if (!canvas || !o || typeof canvas.getContext !== 'function') return const rect = canvas.getBoundingClientRect ? canvas.getBoundingClientRect() : null const w = Math.max(1, Math.round((rect && rect.width) || canvas.clientWidth || 1)) const h = Math.max(1, Math.round((rect && rect.height) || canvas.clientHeight || 1)) const dpr = Math.min(2, (typeof devicePixelRatio === 'number' && devicePixelRatio > 0) ? devicePixelRatio : 1) const bw = Math.round(w * dpr), bh = Math.round(h * dpr) if (canvas.width !== bw || canvas.height !== bh) { canvas.width = bw; canvas.height = bh } const ctx = canvas.getContext('2d') if (!ctx) return ctx.setTransform(dpr, 0, 0, dpr, 0, 0) ctx.clearRect(0, 0, w, h) const vp = o.viewport || { x: 0, y: 0, scale: 1 } const scale = Number(vp.scale) > 0 ? Number(vp.scale) : 1 const baseW = parseFloat(cssVar('--iwb-edge-w', '1.4')) || 1.4 const edgeColor = cssVar('--iwb-edge', '#8b95a5') const hotColor = cssVar('--iwb-edge-hot', '#6b7280') const warnColor = cssVar('--iwb-warn', '#e0a030') const errColor = cssVar('--iwb-err', '#e05252') const hoverId = o.hoverEdgeId || '' const selected = o.selEdge || '' const focus = o.focusNodeId || '' const edgeHover = o.edgeHover || null const edges = Array.isArray(o.edges) ? o.edges : [] const pathOf = typeof o.computeEdgePath === 'function' ? o.computeEdgePath : null if (!pathOf) return ctx.setTransform(dpr * scale, 0, 0, dpr * scale, dpr * (Number(vp.x) || 0), dpr * (Number(vp.y) || 0)) ctx.lineCap = 'round' ctx.lineJoin = 'round' for (let i = 0; i < edges.length; i++) { const ed = edges[i] if (!ed) continue let d = '' try { d = pathOf(ed, o.graph, o.nodeMap, o.measuredH, o.pathCache) || '' } catch (e) { d = '' } if (!d || typeof Path2D === 'undefined') continue const isSel = selected === ed.id const isHot = focus && (ed.to === focus || ed.from === focus) const isHover = hoverId === ed.id const isDetach = !!(edgeHover && edgeHover.mode === 'detach' && Array.isArray(edgeHover.edgeIds) && edgeHover.edgeIds.indexOf(ed.id) >= 0) const isInsert = !!(edgeHover && edgeHover.mode === 'insert' && edgeHover.edgeId === ed.id) ctx.strokeStyle = isSel ? warnColor : (isDetach ? errColor : (isHot || isHover || isInsert ? hotColor : edgeColor)) ctx.lineWidth = (isSel || isHot || isHover || isInsert || isDetach) ? Math.max(2.2, baseW * 1.6) : baseW ctx.setLineDash(isDetach ? [6 / scale, 4 / scale] : (isInsert ? [6 / scale, 4 / scale] : [])) ctx.stroke(new Path2D(d)) } if (o.tempD && typeof Path2D !== 'undefined') { ctx.strokeStyle = o.connTarget && o.connTarget.valid === false ? errColor : hotColor ctx.lineWidth = Math.max(2, baseW) ctx.setLineDash([6 / scale, 4 / scale]) try { ctx.stroke(new Path2D(o.tempD)) } catch (e) { /* ignore */ } } ctx.setLineDash([]) } function EdgeCanvas(props) { const ref = useRef(null) const latest = useRef(props) latest.current = props useEffect(() => { const canvas = ref.current if (!canvas) return draw(canvas, latest.current) if (typeof props.onReady === 'function') { props.onReady(function (x, y, scale) { const p = latest.current || {} draw(canvas, Object.assign({}, p, { viewport: { x: x, y: y, scale: scale } })) }) } }) useEffect(() => () => { if (typeof props.onReady === 'function') props.onReady(null) }, []) return el('canvas', { className: 'iwb-edge-canvas', 'aria-hidden': 'true', ref: ref }) } global.IWBCanvasEdges = { EdgeCanvas, draw } })(typeof window !== 'undefined' ? window : globalThis)