Skip to content
D
Documentation

LinkModel

reference
3 min readUpdated

Import it from @grafloria/engine.

Also has every member of DiagramEntity, listed on its own entry.

ts
class LinkModel extends DiagramEntity

Properties

NameTypeDefaultDescription
diagram?DiagramModelBack-reference to the owning diagram, set by DiagramModel.installLink, as on NodeModel. It is how a LinkModel mutator (waypoints, control points, labels, reconnection) sees the document's read-only lock.
sourcePortIdstring
targetPortIdstring
sourceNodeId?string
targetNodeId?string
pathType'direct' | 'orthogonal' | 'smooth' | 'bezier''smooth'
router?LinkRouterNameWHERE the line goes. When unset, derived from pathType — see {@link effectiveRouter}. Setting it does NOT touch pathType, so legacy consumers keep working; the explicit field simply wins.
connector?LinkConnectorNameHOW the polyline is drawn. Unset = derived from pathType.
pointsPoint[][]
segmentsPathSegment[][]
labelsLinkLabel[][]
state'default' | 'selected' | 'hovered' | 'highlighted''default'
stylePartial<LinkStyle>{}
dataRecord<string, any>{}
isSourceEndpointSelectedbooleanfalseWhether source endpoint handle is selected Used for dragging endpoint to reconnect
isTargetEndpointSelectedbooleanfalseWhether target endpoint handle is selected Used for dragging endpoint to reconnect
DEFAULT_CURVATURE (static) (not released yet)0.5The curve tightness of a smooth/bezier link.

Methods

  • constructor( sourcePortId: string, targetPortId: string, pathType?: 'direct' | 'orthogonal' | 'smooth' | 'bezier' )
  • isSelfLoop(): boolean — Is this link a SELF-LOOP, i.e. do both ends live on the same node?
  • getNodePairKey(): string | null — The unordered node pair this link connects, as a stable key. UNORDERED on purpose: A→B and B→A are the same visual bundle and must fan out together, or a bidirectional pair would draw both links on the same centre line. Returns null when the owning nodes are unknown.
  • setSourcePort(portId: string, nodeId?: string): void — Set source port
  • setTargetPort(portId: string, nodeId?: string): void — Set target port
  • setPathType(pathType: 'direct' | 'orthogonal' | 'smooth' | 'bezier'): void — Set path type
  • setRouter(router: LinkRouterName | undefined): void — Set the routing geometry explicitly. Clears the cached route the same way setPathType does — the old polyline belongs to the old router.
  • setConnector(connector: LinkConnectorName | undefined): void — Set the polyline rendering explicitly. Pure re-render; the routed points are still valid, so the cache is NOT cleared.
  • effectiveRouter(): LinkRouterName — The router actually in force: the explicit field, else derived from pathType exactly as the renderer always derived it (direct → straight, orthogonal → orthogonal, smooth/bezier → straight-with-curved-rendering).
  • effectiveConnector(): LinkConnectorName — The connector actually in force: the explicit field, else implied by an explicitly axis-aligned ROUTER, else derived from pathType.
  • setPoints(points: Point[]): void — Set points for custom path
  • addPoint(point: Point, index?: number): void — Add point to path
  • removePoint(index: number): Point | undefined — Remove point from path
  • generatePath( sourcePoint: Point, targetPoint: Point, sourceDirection?: 'left' | 'right' | 'top' | 'bottom', targetDirection?: 'left' | 'right' | 'top' | 'bottom' ): void — Generate path based on type
  • getCurvature(): number
  • addLabel( label: Partial<LinkLabel> & { text: string } & ( | { position: number } | { slot: NonNullable<LinkLabel['slot']> } ) ): void — Add label
  • removeLabel(labelId: string): LinkLabel | undefined — Remove label by ID
  • removeLabelAt(index: number): LinkLabel | undefined — Remove label by index
  • updateLabel(index: number, updates: Partial<LinkLabel>): void — Update label by index
  • setLabels(labels: LinkLabel[]): void — REPLACE the whole label collection — the write addLabel/updateLabel cannot express.
  • override getLabel(): string | undefined — Overloaded label read.
  • override getLabel(labelId: string): LinkLabel | undefined — Overloaded label read.
  • override getLabel(labelId?: string): string | LinkLabel | undefined — Overloaded label read.
  • setState(state: 'default' | 'selected' | 'hovered' | 'highlighted'): void — Set state
  • updateStyle(style: Partial<LinkStyle>): void — Update style
  • replaceStyle(style: Partial<LinkStyle>): void — REPLACE the whole style object — the write updateStyle cannot express.
  • setData(key: string, value: any): void — Set data property
  • getData(key: string): any — Get data property
  • reconnectSource(newPortId: string, newNodeId?: string): void — Reconnect source endpoint to new port Used for link reconnection workflow
  • reconnectTarget(newPortId: string, newNodeId?: string): void — Reconnect target endpoint to new port Used for link reconnection workflow
  • getSourceEndpoint(): Point — Get source endpoint position Returns the first point in the path (source end)
  • getTargetEndpoint(): Point — Get target endpoint position Returns the last point in the path (target end)
  • selectSourceEndpoint(): void — Select source endpoint handle
  • selectTargetEndpoint(): void — Select target endpoint handle
  • deselectEndpoints(): void — Deselect all endpoint handles
  • hasSelectedEndpoint(): boolean — Check if any endpoint is selected
  • getPointAtPosition(t: number): Point | null — Get point at position along link (0-1)
  • getTotalLength(): number — Get total path length
  • getLength(): number — Get path length (alias for getTotalLength for API consistency)
  • getTangentAt(t: number): Point | null — Get tangent (direction vector) at position along path (0-1) Returns normalized direction vector
  • getNormalAt(t: number): Point | null — Get normal (perpendicular vector) at position along path (0-1) Returns normalized perpendicular vector (90° counter-clockwise from tangent)
  • getClosestPoint(point: Point): { point: Point; distance: number; t: number } | null — Get closest point on path to a given point Returns the closest point, distance, and normalized position (t)
  • getAngleAt(t: number): number | null — Get angle at position along path (0-1) in degrees Useful for label rotation
  • override serialize(): SerializedLink — Serialize to JSON
  • static fromJSON(data: SerializedLink): LinkModel (static) — Deserialize from JSON

Was this page helpful?

LinkModel — Grafloria