Edges follow ELK's routes: layoutGraph returns each edge's start, bends, end and chip point, LiveEdge draws them square with rounded corners, moved onto the drawn handles, and keeps the curve for an edge whose node moved since; a feedback edge clears the nodes it passes
This commit is contained in:
@@ -12,6 +12,7 @@ import { describeArtifact, isRef } from "@/lib/media"
|
||||
import { duration } from "@/lib/motion"
|
||||
import { cn } from "@/lib/utils"
|
||||
import type { FlowEdgeData } from "./deriveEdges"
|
||||
import type { Point } from "./layout"
|
||||
import { useLiveValue } from "./liveStore"
|
||||
|
||||
/** Below this zoom the value chips would be unreadable, so they step aside. */
|
||||
@@ -20,22 +21,129 @@ const CHIP_MIN_ZOOM = 0.5
|
||||
/** How far a feedback edge stands off the ranks it has to get back past. */
|
||||
const BOW = 60
|
||||
|
||||
/** The radius a routed edge turns its corners with. */
|
||||
const CORNER = 10
|
||||
|
||||
/**
|
||||
* The path a feedback edge takes.
|
||||
* The route the layout found, met up with the handles React Flow drew.
|
||||
*
|
||||
* The layout reserves a card's widest and a label's widest, and either is
|
||||
* narrower when its text is, so a handle on screen can sit beside the port the
|
||||
* route was found for. Each end moves onto its handle and takes the bend next
|
||||
* to it along, which keeps every segment square. A straight run between two
|
||||
* handles that no longer line up gets a step halfway.
|
||||
*
|
||||
* Null for a route that does not alternate along and across the flow, which
|
||||
* is all this knows how to move.
|
||||
*/
|
||||
function stitch(
|
||||
route: Point[],
|
||||
from: Point,
|
||||
to: Point,
|
||||
vertical: boolean,
|
||||
): Point[] | null {
|
||||
// [along the flow, across it] and back.
|
||||
const split = (p: Point) => (vertical ? [p.y, p.x] : [p.x, p.y])
|
||||
const join = (along: number, across: number): Point =>
|
||||
vertical ? { x: across, y: along } : { x: along, y: across }
|
||||
|
||||
// ELK can hand back a corner that is not one: two runs in the same line.
|
||||
const points = route.filter((p, i) => {
|
||||
const [a, b] = [route[i - 1], route[i + 1]]
|
||||
return (
|
||||
!a || !b || ((a.x !== p.x || b.x !== p.x) && (a.y !== p.y || b.y !== p.y))
|
||||
)
|
||||
})
|
||||
for (let i = 1; i < points.length; i += 1) {
|
||||
const [a0, c0] = split(points[i - 1])
|
||||
const [a1, c1] = split(points[i])
|
||||
// Odd segments run along the flow, even ones across it.
|
||||
if (i % 2 ? c0 !== c1 : a0 !== a1) return null
|
||||
}
|
||||
if (points.length % 2) return null
|
||||
|
||||
const [fromAlong, fromAcross] = split(from)
|
||||
const [toAlong, toAcross] = split(to)
|
||||
if (points.length === 2) {
|
||||
if (fromAcross === toAcross) return [from, to]
|
||||
const halfway = (fromAlong + toAlong) / 2
|
||||
return [from, join(halfway, fromAcross), join(halfway, toAcross), to]
|
||||
}
|
||||
const bends = points.slice(1, -1)
|
||||
const first = bends[0]
|
||||
const last = bends[bends.length - 1]
|
||||
bends[0] = join(split(first)[0], fromAcross)
|
||||
bends[bends.length - 1] = join(split(last)[0], toAcross)
|
||||
return [from, ...bends, to]
|
||||
}
|
||||
|
||||
/** A square route with its corners rounded off. */
|
||||
function roundedPath(points: Point[]): string {
|
||||
const towards = (from: Point, to: Point, by: number): Point => {
|
||||
const length = Math.hypot(to.x - from.x, to.y - from.y) || 1
|
||||
return {
|
||||
x: from.x + ((to.x - from.x) * by) / length,
|
||||
y: from.y + ((to.y - from.y) * by) / length,
|
||||
}
|
||||
}
|
||||
let path = `M${points[0].x},${points[0].y}`
|
||||
for (let i = 1; i < points.length - 1; i += 1) {
|
||||
const [before, corner, after] = [points[i - 1], points[i], points[i + 1]]
|
||||
// Never more than half of either run, or two corners would overlap.
|
||||
const radius = Math.min(
|
||||
CORNER,
|
||||
Math.hypot(corner.x - before.x, corner.y - before.y) / 2,
|
||||
Math.hypot(after.x - corner.x, after.y - corner.y) / 2,
|
||||
)
|
||||
const enter = towards(corner, before, radius)
|
||||
const leave = towards(corner, after, radius)
|
||||
path += ` L${enter.x},${enter.y} Q${corner.x},${corner.y} ${leave.x},${leave.y}`
|
||||
}
|
||||
const end = points[points.length - 1]
|
||||
return `${path} L${end.x},${end.y}`
|
||||
}
|
||||
|
||||
/** The point of a polyline closest to `to`. */
|
||||
function nearest(points: Point[], to: Point): Point {
|
||||
let best = points[0]
|
||||
let gap = Number.POSITIVE_INFINITY
|
||||
for (let i = 1; i < points.length; i += 1) {
|
||||
const [a, b] = [points[i - 1], points[i]]
|
||||
const [dx, dy] = [b.x - a.x, b.y - a.y]
|
||||
const t = Math.max(
|
||||
0,
|
||||
Math.min(
|
||||
1,
|
||||
((to.x - a.x) * dx + (to.y - a.y) * dy) / (dx * dx + dy * dy || 1),
|
||||
),
|
||||
)
|
||||
const at = { x: a.x + t * dx, y: a.y + t * dy }
|
||||
const distance = Math.hypot(at.x - to.x, at.y - to.y)
|
||||
if (distance < gap) [best, gap] = [at, distance]
|
||||
}
|
||||
return best
|
||||
}
|
||||
|
||||
/**
|
||||
* React Flow reads a handle's place out of the DOM through the zoom, which
|
||||
* leaves noise in the last digits — enough for one flow to draw two different
|
||||
* paths on two loads. Half a pixel is as fine as a handle is ever placed.
|
||||
*/
|
||||
const half = (value: number) => Math.round(value * 2) / 2
|
||||
|
||||
/**
|
||||
* The path a feedback edge takes when the layout has no route for it.
|
||||
*
|
||||
* The layout ranks a flow in the direction it runs, so an edge closing a cycle
|
||||
* is the one that goes the other way — a widget setting the window a node
|
||||
* upstream of it reads. A curve drawn straight between those two handles is
|
||||
* drawn straight through everything between them. This one leaves its handle
|
||||
* the way every other edge does, swings out into a lane beside the graph, and
|
||||
* comes back into its target from the far side, so what it passes it passes
|
||||
* around.
|
||||
* comes back into its target from the far side.
|
||||
*
|
||||
* ponytail: how far out the lane sits is read off how far back the edge has to
|
||||
* travel, not off what is actually in the way — this stands a cycle apart from
|
||||
* the flow rather than guaranteeing it clears every node. The layout engine
|
||||
* routes these properly and React Flow draws its own curves; taking its bend
|
||||
* points is the real fix, and a bigger one.
|
||||
* How far out the lane sits is read off how far back the edge has to travel,
|
||||
* not off what is in the way, which is good enough for the moment between an
|
||||
* edit and the layout answering it.
|
||||
*/
|
||||
function cyclePath(
|
||||
sourceX: number,
|
||||
@@ -75,6 +183,8 @@ function formatValue(value: unknown): string {
|
||||
|
||||
function LiveEdgeComponent({
|
||||
id,
|
||||
source,
|
||||
target,
|
||||
sourceX,
|
||||
sourceY,
|
||||
targetX,
|
||||
@@ -84,9 +194,20 @@ function LiveEdgeComponent({
|
||||
data,
|
||||
selected,
|
||||
}: EdgeProps) {
|
||||
const { message, producerId, stream } = (data ?? {}) as FlowEdgeData
|
||||
const { message, producerId, stream, route } = (data ?? {}) as FlowEdgeData
|
||||
const live = useLiveValue(message)
|
||||
const zoom = useStore((state) => state.transform[2])
|
||||
// The route holds while both ends are where the layout put them. One that
|
||||
// has moved since — dragged, or placed by a layout this edge is not from yet
|
||||
// — takes the plain curve until the next layout routes it again.
|
||||
const placed = useStore((state) => {
|
||||
if (!route) return false
|
||||
const at = (node: string, where: Point) => {
|
||||
const now = state.nodeLookup.get(node)?.internals.positionAbsolute
|
||||
return now?.x === where.x && now?.y === where.y
|
||||
}
|
||||
return at(source, route.from) && at(target, route.to)
|
||||
})
|
||||
const [pulsing, setPulsing] = useState(false)
|
||||
const lastTs = useRef<number | null>(null)
|
||||
// Owned by the pulse rather than by the effect: a value from another
|
||||
@@ -95,21 +216,34 @@ function LiveEdgeComponent({
|
||||
const timer = useRef<ReturnType<typeof setTimeout>>(undefined)
|
||||
useEffect(() => () => clearTimeout(timer.current), [])
|
||||
|
||||
const vertical = sourcePosition === Position.Bottom
|
||||
const square =
|
||||
placed && route
|
||||
? stitch(
|
||||
route.points,
|
||||
{ x: half(sourceX), y: half(sourceY) },
|
||||
{ x: half(targetX), y: half(targetY) },
|
||||
vertical,
|
||||
)
|
||||
: null
|
||||
// Every rank sits further along the flow than the one before it, so an edge
|
||||
// whose target has ended up behind its source is one closing a cycle.
|
||||
const vertical = sourcePosition === Position.Bottom
|
||||
const backwards = vertical ? targetY < sourceY : targetX < sourceX
|
||||
const chip = square && route ? nearest(square, route.label) : null
|
||||
|
||||
const [path, labelX, labelY] = backwards
|
||||
? cyclePath(sourceX, sourceY, targetX, targetY, vertical)
|
||||
: getBezierPath({
|
||||
sourceX,
|
||||
sourceY,
|
||||
sourcePosition,
|
||||
targetX,
|
||||
targetY,
|
||||
targetPosition,
|
||||
})
|
||||
const [path, labelX, labelY] =
|
||||
square && chip
|
||||
? [roundedPath(square), chip.x, chip.y]
|
||||
: backwards
|
||||
? cyclePath(sourceX, sourceY, targetX, targetY, vertical)
|
||||
: getBezierPath({
|
||||
sourceX,
|
||||
sourceY,
|
||||
sourcePosition,
|
||||
targetX,
|
||||
targetY,
|
||||
targetPosition,
|
||||
})
|
||||
|
||||
// Restart the stroke animation whenever a newer message lands — but only
|
||||
// for the producer that actually published it. A message can have several
|
||||
|
||||
Reference in New Issue
Block a user