|
| 1 | +import { MessageData } from "../EdgeProps" |
| 2 | +import { IPoint } from "../Connection" |
| 3 | + |
| 4 | +/** |
| 5 | + * Geometry for communication-edge message labels, relative to the middle-segment |
| 6 | + * midpoint (position-independent, hence unit-testable on its own). |
| 7 | + * |
| 8 | + * Invariant: a group's SIDE is fixed by the segment orientation; flow direction |
| 9 | + * is encoded ONLY by the arrow rotation, never by the side. Gaps are constant by |
| 10 | + * construction — nothing measures text width. |
| 11 | + */ |
| 12 | + |
| 13 | +type ArrowDirection = "Up" | "Down" | "Left" | "Right" |
| 14 | +export type LabelTextAnchor = "start" | "middle" | "end" |
| 15 | + |
| 16 | +export const ARROW_SIZE = 16 |
| 17 | +export const ARROW_TEXT_GAP = 4 |
| 18 | +export const LINE_GAP = 8 |
| 19 | +export const STACK_SPACING = 22 |
| 20 | + |
| 21 | +export interface MessageGroupLayout { |
| 22 | + messages: MessageData[] |
| 23 | + arrowRotation: number |
| 24 | + textAnchor: LabelTextAnchor |
| 25 | + /** Top-left of the arrow icon for the first message, relative to the segment midpoint. */ |
| 26 | + arrowOrigin: IPoint |
| 27 | + /** Text anchor point for the first message, relative to the segment midpoint. */ |
| 28 | + textOrigin: IPoint |
| 29 | + /** Offset added per stacked message (multiplied by the message index). */ |
| 30 | + stackStep: IPoint |
| 31 | +} |
| 32 | + |
| 33 | +export interface MessageLayout { |
| 34 | + forward: MessageGroupLayout |
| 35 | + backward: MessageGroupLayout |
| 36 | +} |
| 37 | + |
| 38 | +const calculateRotation = (direction: ArrowDirection): number => { |
| 39 | + switch (direction) { |
| 40 | + case "Right": |
| 41 | + return 0 |
| 42 | + case "Left": |
| 43 | + return 180 |
| 44 | + case "Down": |
| 45 | + return 90 |
| 46 | + case "Up": |
| 47 | + return -90 |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export const computeMessageLayout = ( |
| 52 | + messages: MessageData[], |
| 53 | + sourcePosition: IPoint, |
| 54 | + targetPosition: IPoint, |
| 55 | + isHorizontalEdge: boolean |
| 56 | +): MessageLayout => { |
| 57 | + const forwardMessages = messages.filter((msg) => msg.direction === "target") |
| 58 | + const backwardMessages = messages.filter((msg) => msg.direction === "source") |
| 59 | + |
| 60 | + let sourceArrowDirection: ArrowDirection |
| 61 | + let targetArrowDirection: ArrowDirection |
| 62 | + if (isHorizontalEdge) { |
| 63 | + const sourceIsLeft = sourcePosition.x < targetPosition.x |
| 64 | + sourceArrowDirection = sourceIsLeft ? "Left" : "Right" |
| 65 | + targetArrowDirection = sourceIsLeft ? "Right" : "Left" |
| 66 | + } else { |
| 67 | + const sourceIsAbove = sourcePosition.y < targetPosition.y |
| 68 | + sourceArrowDirection = sourceIsAbove ? "Up" : "Down" |
| 69 | + targetArrowDirection = sourceIsAbove ? "Down" : "Up" |
| 70 | + } |
| 71 | + |
| 72 | + const forwardRotation = calculateRotation(targetArrowDirection) |
| 73 | + const backwardRotation = calculateRotation(sourceArrowDirection) |
| 74 | + |
| 75 | + const half = ARROW_SIZE / 2 |
| 76 | + const textInset = LINE_GAP + ARROW_SIZE + ARROW_TEXT_GAP |
| 77 | + |
| 78 | + if (isHorizontalEdge) { |
| 79 | + // Centred on the midpoint (text-anchor "middle") with the arrow stacked |
| 80 | + // between text and line, so a label never drifts sideways into a neighbour. |
| 81 | + const textGap = LINE_GAP + ARROW_SIZE + half + ARROW_TEXT_GAP |
| 82 | + return { |
| 83 | + forward: { |
| 84 | + messages: forwardMessages, |
| 85 | + arrowRotation: forwardRotation, |
| 86 | + textAnchor: "middle", |
| 87 | + arrowOrigin: { x: -half, y: -(LINE_GAP + ARROW_SIZE) }, |
| 88 | + textOrigin: { x: 0, y: -textGap }, |
| 89 | + stackStep: { x: 0, y: -STACK_SPACING }, |
| 90 | + }, |
| 91 | + backward: { |
| 92 | + messages: backwardMessages, |
| 93 | + arrowRotation: backwardRotation, |
| 94 | + textAnchor: "middle", |
| 95 | + arrowOrigin: { x: -half, y: LINE_GAP }, |
| 96 | + textOrigin: { x: 0, y: textGap }, |
| 97 | + stackStep: { x: 0, y: STACK_SPACING }, |
| 98 | + }, |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Vertical: the arrow sits innermost (nearest the line) so the text can anchor |
| 103 | + // outward and grow away from the line without ever measuring its width. |
| 104 | + return { |
| 105 | + forward: { |
| 106 | + messages: forwardMessages, |
| 107 | + arrowRotation: forwardRotation, |
| 108 | + textAnchor: "start", |
| 109 | + arrowOrigin: { x: LINE_GAP, y: -half }, |
| 110 | + textOrigin: { x: textInset, y: 0 }, |
| 111 | + stackStep: { x: 0, y: STACK_SPACING }, |
| 112 | + }, |
| 113 | + backward: { |
| 114 | + messages: backwardMessages, |
| 115 | + arrowRotation: backwardRotation, |
| 116 | + textAnchor: "end", |
| 117 | + arrowOrigin: { x: -(LINE_GAP + ARROW_SIZE), y: -half }, |
| 118 | + textOrigin: { x: -textInset, y: 0 }, |
| 119 | + stackStep: { x: 0, y: STACK_SPACING }, |
| 120 | + }, |
| 121 | + } |
| 122 | +} |
0 commit comments