-
Notifications
You must be signed in to change notification settings - Fork 10
feat(Plugin): add layered plugin #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/components/canvas/connections/BezierMultipointConnection.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { MultipointConnection } from "./MultipointConnection"; | ||
| import { bezierCurveLine, generateBezierParams } from "./bezierHelpers"; | ||
|
|
||
| /** | ||
| * Multipoint connection that draws segments as Bezier curves and keeps straight | ||
| * segments aligned with the configured bezier direction as lines. | ||
| * From commit a90f9c65 (ytsaurus-ui) — alternative line representation for layout graphs. | ||
| */ | ||
| export class BezierMultipointConnection extends MultipointConnection { | ||
| public override createPath(): Path2D { | ||
| const points = this.getPoints(); | ||
| const direction = this.props.bezierDirection; | ||
| if (!points.length) { | ||
| return super.createPath(); | ||
| } | ||
|
|
||
| const path = new Path2D(); | ||
|
|
||
| if (points.length === 1) { | ||
| return path; | ||
| } | ||
|
|
||
| if (points.length === 2) { | ||
| return bezierCurveLine(points[0], points[1], direction); | ||
| } | ||
|
|
||
| for (let i = 1; i < points.length; i++) { | ||
| const startPoint = points[i - 1]; | ||
| const endPoint = points[i]; | ||
| const isStraightSegment = direction === "vertical" ? startPoint.x === endPoint.x : startPoint.y === endPoint.y; | ||
|
|
||
| if (isStraightSegment) { | ||
| path.moveTo(startPoint.x, startPoint.y); | ||
| path.lineTo(endPoint.x, endPoint.y); | ||
| } else { | ||
| const [start, firstPoint, secondPoint, end] = generateBezierParams(startPoint, endPoint, direction); | ||
| path.moveTo(start.x, start.y); | ||
| path.bezierCurveTo(firstPoint.x, firstPoint.y, secondPoint.x, secondPoint.y, end.x, end.y); | ||
| } | ||
| } | ||
|
|
||
| return path; | ||
| } | ||
| } | ||
9 changes: 5 additions & 4 deletions
9
...ts/elk/components/MultipointConnection.ts → ...anvas/connections/MultipointConnection.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| export * from "./BaseConnection"; | ||
| export * from "./BlockConnection"; | ||
| export * from "./MultipointConnection"; | ||
|
SimbiozizV marked this conversation as resolved.
|
||
| export * from "./BezierMultipointConnection"; | ||
| export * from "./types"; | ||
| export * from "./Arrow"; | ||
| export * from "./BatchPath2D"; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import type { TConnection } from "../../../store/connection/ConnectionState"; | ||
| import type { TPoint } from "../../../utils/types/shapes"; | ||
|
|
||
| export type TLabel = { | ||
| height?: number; | ||
| width?: number; | ||
| x?: number; | ||
| y?: number; | ||
| text?: string; | ||
| }; | ||
|
|
||
| export type TMultipointConnection = TConnection & { | ||
| points?: TPoint[]; | ||
| labels?: TLabel[]; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export * from "./minimap/layer"; | ||
| export * from "./cssVariables"; | ||
| export * from "./layered"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| import { TPoint } from "../../../utils/types/shapes"; | ||
| import { DEFAULT_NODE_WIDTH, Edge } from "../layout"; | ||
| import { ConverterResult } from "../types"; | ||
|
|
||
| function buildAdjacency(edges: Edge<string>[]) { | ||
| const adjacency = new Map<string, Array<{ to: string; arrows?: Edge<string>["arrows"] }>>(); | ||
| for (const edge of edges) { | ||
| const from = String(edge.from); | ||
| const to = String(edge.to); | ||
| const neighbors = adjacency.get(from); | ||
| if (neighbors) { | ||
| neighbors.push({ to, arrows: edge.arrows }); | ||
| } else { | ||
| adjacency.set(from, [{ to, arrows: edge.arrows }]); | ||
| } | ||
| } | ||
| return adjacency; | ||
| } | ||
|
|
||
| function getVirtualNodeCenter( | ||
| nodePositions: Map<string, TPoint>, | ||
| virtualNodeSize?: number | ||
| ): (id: string) => TPoint | undefined { | ||
| return (id: string) => { | ||
| const pos = nodePositions.get(id); | ||
| if (!pos) return undefined; | ||
| const size = virtualNodeSize ?? DEFAULT_NODE_WIDTH; | ||
| return { | ||
| x: pos.x + size / 2, | ||
| y: pos.y + size / 2, | ||
| }; | ||
| }; | ||
| } | ||
|
|
||
| function getBlockRightEdge( | ||
| id: string, | ||
| nodePositions: Map<string, TPoint>, | ||
| blockSizes: Map<string, { width: number; height: number }> | ||
| ): TPoint | undefined { | ||
| const pos = nodePositions.get(id); | ||
| if (!pos) return undefined; | ||
| const size = blockSizes.get(id); | ||
| if (size) { | ||
| return { | ||
| x: pos.x + size.width, | ||
| y: pos.y + size.height / 2, | ||
| }; | ||
| } | ||
| return pos; | ||
| } | ||
|
|
||
| function getBlockLeftEdge( | ||
| id: string, | ||
| nodePositions: Map<string, TPoint>, | ||
| blockSizes: Map<string, { width: number; height: number }> | ||
| ): TPoint | undefined { | ||
| const pos = nodePositions.get(id); | ||
| if (!pos) return undefined; | ||
| const size = blockSizes.get(id); | ||
| if (size) { | ||
| return { | ||
| x: pos.x, | ||
| y: pos.y + size.height / 2, | ||
| }; | ||
| } | ||
| return pos; | ||
| } | ||
|
|
||
| export type LayeredLayoutResult = { | ||
| nodes: Array<{ id: string; x?: number; y?: number; shape?: string }>; | ||
| edges: Edge<string>[]; | ||
| }; | ||
|
|
||
| export type LayeredConverterParams = { | ||
| layoutResult: LayeredLayoutResult; | ||
| /** Map of "sourceId/targetId" -> queue of connection ids (for multiple edges between same pair) */ | ||
| connectionIdBySourceTarget: Map<string, (string | number | symbol)[]>; | ||
| blockSizes: Map<string, { width: number; height: number }>; | ||
| virtualNodeSize?: number; | ||
| }; | ||
|
|
||
| /** | ||
| * Converts the result of layoutGraph() into the same format as ELK plugin (ConverterResult) | ||
| * so it can be used with setEntities(blocks, connections) the same way. | ||
| */ | ||
| export function layeredConverter({ | ||
| layoutResult, | ||
| connectionIdBySourceTarget, | ||
| blockSizes, | ||
| virtualNodeSize, | ||
| }: LayeredConverterParams): ConverterResult { | ||
| const { nodes, edges } = layoutResult; | ||
| const nodePositions = new Map<string, TPoint>(); | ||
| const dotNodeIds = new Set<string>(); | ||
|
|
||
| for (const node of nodes) { | ||
| const id = String(node.id); | ||
| nodePositions.set(id, { x: node.x ?? 0, y: node.y ?? 0 }); | ||
| if (node.shape === "dot") { | ||
| dotNodeIds.add(id); | ||
| } | ||
| } | ||
|
|
||
| const blocks: ConverterResult["blocks"] = {}; | ||
| for (const node of nodes) { | ||
| if (node.shape === "dot") continue; | ||
| const id = node.id; | ||
| const pos = nodePositions.get(String(id)); | ||
| if (pos) { | ||
| blocks[id] = pos; | ||
| } | ||
| } | ||
|
|
||
| const edgesResult: ConverterResult["edges"] = {}; | ||
| const adjacency = buildAdjacency(edges); | ||
| const visitedEdges = new Set<string>(); | ||
| const getVirtualCenter = getVirtualNodeCenter(nodePositions, virtualNodeSize); | ||
|
|
||
| for (const edge of edges) { | ||
| const from = String(edge.from); | ||
| const to = String(edge.to); | ||
| const edgeKey = `${from}->${to}`; | ||
| if (visitedEdges.has(edgeKey)) continue; | ||
| if (dotNodeIds.has(from)) continue; | ||
|
|
||
| const chain: string[] = [from]; | ||
| let current = to; | ||
| visitedEdges.add(edgeKey); | ||
|
|
||
| while (dotNodeIds.has(current)) { | ||
| chain.push(current); | ||
| const nextEdges = adjacency.get(current); | ||
| if (!nextEdges || nextEdges.length === 0) break; | ||
| const nextEdge = nextEdges[0]; | ||
| const nextEdgeKey = `${current}->${nextEdge.to}`; | ||
| visitedEdges.add(nextEdgeKey); | ||
| current = nextEdge.to; | ||
| } | ||
| chain.push(current); | ||
|
|
||
| const sourceId = chain[0]; | ||
| const targetId = chain[chain.length - 1]; | ||
| const key = `${sourceId}/${targetId}`; | ||
| const idQueue = connectionIdBySourceTarget.get(key); | ||
| const connectionId = (idQueue?.length ? idQueue.shift() : null) ?? key; | ||
|
|
||
| const points: TPoint[] = []; | ||
| const sourceEdge = getBlockRightEdge(sourceId, nodePositions, blockSizes); | ||
| if (sourceEdge) points.push(sourceEdge); | ||
|
|
||
| if (chain.length > 2) { | ||
| for (let i = 1; i < chain.length - 1; i++) { | ||
| const center = getVirtualCenter(chain[i]); | ||
| if (center) points.push(center); | ||
| } | ||
| } | ||
|
|
||
| const targetEdge = getBlockLeftEdge(targetId, nodePositions, blockSizes); | ||
| if (targetEdge) points.push(targetEdge); | ||
|
|
||
| if (points.length >= 2) { | ||
| (edgesResult as Record<string | number | symbol, { points: TPoint[] }>)[connectionId] = { | ||
| points, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return { blocks, edges: edgesResult }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.