-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26c3724
commit 9b929c3
Showing
9 changed files
with
268 additions
and
150 deletions.
There are no files selected for viewing
This file contains 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,94 @@ | ||
import { Path2DRenderStyleResult } from "../../../components/canvas/connections/BatchPath2D"; | ||
import { BlockConnection } from "../../../components/canvas/connections/BlockConnection"; | ||
import { TMultipointConnection } from "../../../store/connection/ConnectionState"; | ||
import { curvePolyline } from "../../../utils/shapes/curvePolyline"; | ||
import { trangleArrowForVector } from "../../../utils/shapes/triangle"; | ||
|
||
const DEFAULT_FONT_SIZE = 14; | ||
|
||
export class MultipointConnection extends BlockConnection<TMultipointConnection> { | ||
public createPath() { | ||
const { points } = this.connectedState.$state.value; | ||
if (!points.length) { | ||
return super.createPath(); | ||
} | ||
return curvePolyline(points, 10); | ||
} | ||
|
||
public createArrowPath(): Path2D { | ||
const { points } = this.connectedState.$state.value; | ||
if (!points.length) { | ||
return undefined; | ||
} | ||
|
||
const [start, end] = points.slice(points.length - 2); | ||
return trangleArrowForVector(start, end, 16, 10); | ||
} | ||
|
||
public styleArrow(ctx: CanvasRenderingContext2D): Path2DRenderStyleResult { | ||
ctx.fillStyle = this.state.selected | ||
? this.context.colors.connection.selectedBackground | ||
: this.context.colors.connection.background; | ||
ctx.strokeStyle = ctx.fillStyle; | ||
ctx.lineWidth = this.state.selected || this.state.hovered ? -1 : 1; | ||
return { type: "both" }; | ||
} | ||
|
||
public getPoints() { | ||
return this.connectedState.$state.value.points || []; | ||
} | ||
|
||
public afterRender?(ctx: CanvasRenderingContext2D): void { | ||
this.renderLabelsText(ctx); | ||
} | ||
|
||
public updatePoints(): void { | ||
super.updatePoints(); | ||
return; | ||
} | ||
|
||
public getBBox() { | ||
const { points } = this.connectedState.$state.value; | ||
if (!points.length) { | ||
return super.getBBox(); | ||
} | ||
|
||
const x = []; | ||
const y = []; | ||
points.forEach((point) => { | ||
x.push(point.x); | ||
y.push(point.y); | ||
}); | ||
|
||
return [Math.min(...x), Math.min(...y), Math.max(...x), Math.max(...y)] as const; | ||
} | ||
|
||
private renderLabelsText(ctx: CanvasRenderingContext2D) { | ||
const { labels } = this.connectedState.$state.value; | ||
if (!labels || !labels.length) { | ||
return; | ||
} | ||
|
||
labels.forEach(({ x, y, text, height }) => { | ||
if ([x, y, text].some((i) => i === undefined)) { | ||
return; | ||
} | ||
ctx.fillStyle = this.context.colors.connectionLabel.text; | ||
|
||
if (this.state.hovered) ctx.fillStyle = this.context.colors.connectionLabel.hoverText; | ||
if (this.state.selected) ctx.fillStyle = this.context.colors.connectionLabel.selectedText; | ||
|
||
ctx.textBaseline = "top"; | ||
ctx.textAlign = "center"; | ||
ctx.font = `${height || DEFAULT_FONT_SIZE}px sans-serif`; | ||
ctx.fillText(text, x, y); | ||
|
||
ctx.fillStyle = this.context.colors.connectionLabel.background; | ||
|
||
if (this.state.hovered) ctx.fillStyle = this.context.colors.connectionLabel.hoverBackground; | ||
if (this.state.selected) ctx.fillStyle = this.context.colors.connectionLabel.selectedBackground; | ||
}); | ||
|
||
return; | ||
} | ||
} |
This file contains 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,40 @@ | ||
import { useCallback, useEffect, useMemo, useState } from "react"; | ||
|
||
import ELK, { ElkLayoutArguments, ElkNode } from "elkjs"; | ||
|
||
import { elkConverter } from "../../utils/converters/eklConverter"; | ||
import { ConverterResult } from "../../utils/types/types"; | ||
|
||
export const useElk = (config: ElkNode, args?: ElkLayoutArguments) => { | ||
const [result, setResult] = useState<ConverterResult | null>(null); | ||
const [isLoading, setIsLoading] = useState<boolean>(true); | ||
|
||
const elk = useMemo(() => new ELK(), []); | ||
|
||
const layout = useCallback(() => { | ||
return elk.layout(config, args); | ||
}, [elk, config, args]); | ||
|
||
useEffect(() => { | ||
let isCancelled = false; | ||
|
||
layout() | ||
.then((data) => { | ||
if (isCancelled) return; | ||
setResult(elkConverter(data)); | ||
setIsLoading(false); | ||
}) | ||
.catch((error) => { | ||
if (!isCancelled) { | ||
console.error("Error during ELK layout:", error); | ||
setIsLoading(false); | ||
} | ||
}); | ||
|
||
return () => { | ||
isCancelled = true; | ||
}; | ||
}, [layout]); | ||
|
||
return { result, isLoading, elk }; | ||
}; |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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,10 @@ | ||
export const calculateTextDimensions = (text: string, fontSize: number, fontFamily: string) => { | ||
const canvas = document.createElement("canvas"); | ||
const context = canvas.getContext("2d")!; | ||
context.font = `${fontSize}px ${fontFamily}`; | ||
const metrics = context.measureText(text); | ||
return { | ||
width: metrics.width, | ||
height: fontSize, | ||
}; | ||
}; |
This file contains 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
Oops, something went wrong.