Skip to content

Commit

Permalink
fix(draw-canvas): support label placement and offset
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Jan 6, 2025
1 parent d722d20 commit 123992b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
6 changes: 5 additions & 1 deletion bricks/diagram/docs/eo-draw-canvas.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,9 @@
type: "edge",
source: "X",
target: "Y",
description: "X->Y"
description: "X->Y",
placement: "end",
offset: 10
},
{
type: "edge",
Expand Down Expand Up @@ -573,6 +575,8 @@
defaultEdgeLines:
- callLabelOnDoubleClick: enableEditing
label:
placement: <% DATA.edge.placement %>
offset: <% DATA.edge.offset %>
useBrick:
brick: diagram.editable-label
properties:
Expand Down
2 changes: 2 additions & 0 deletions bricks/diagram/src/diagram/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,14 @@ export interface LineLabelConf {
if?: string | boolean | null;
useBrick: UseSingleBrickConf;
placement?: LineLabelPlacement;
offset?: number;
}

export interface TextOptions {
content: string;
style?: CSSProperties;
placement?: LineLabelPlacement;
offset?: number;
}

export type LineLabelPlacement = "center" | "start" | "end";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function getRenderedLineLabels(

return list.map<RenderedLineLabel>((item) => {
const placement = item.placement ?? "center";
const offset = 0;
const offset = item.offset ?? 0;
// istanbul ignore next
const point =
process.env.NODE_ENV === "test"
Expand Down
14 changes: 12 additions & 2 deletions bricks/diagram/src/draw-canvas/EdgeComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,25 @@ export function EdgeComponent({

const updateLabelPosition = useCallback(() => {
const path = pathRef.current;
const { label, text } = lineConf;
// istanbul ignore next
if (path && (lineConf.label || lineConf.text)) {
if (path && (label || text)) {
if (process.env.NODE_ENV === "test") {
setLabelPosition([30, 40]);
setLineRect({ x: 10, y: 20, width: 300, height: 400 });
return;
}
const placement = (label ? label.placement : text.placement) ?? "center";
const offset = (label ? label.offset : text.offset) ?? 0;
const pathLength = path.getTotalLength();
const pathPoint = path.getPointAtLength(pathLength / 2);
const halfPathLength = pathLength / 2;
const pathPoint = path.getPointAtLength(
placement === "start"
? Math.min(offset, halfPathLength)
: placement === "end"
? Math.max(pathLength - offset, halfPathLength)
: halfPathLength
);
setLabelPosition([pathPoint.x, pathPoint.y]);
const rect = path.getBBox();
setLineRect({
Expand Down

0 comments on commit 123992b

Please sign in to comment.