Skip to content

Commit 8cd9705

Browse files
committed
chore(story): Fix plyground. Add ConnectionLayer usage
1 parent 8393772 commit 8cd9705

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/components/canvas/layers/connectionLayer/ConnectionLayer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ export class ConnectionLayer extends Layer<
150150

151151
this.eventAborter = new AbortController();
152152
this.performRender = this.performRender.bind(this);
153+
154+
this.onSignal(this.props.graph.rootStore.settings.$settings, (value) => {
155+
this.enabled = Boolean(value.canCreateNewConnections);
156+
});
153157
}
154158

155159
/**

src/stories/Playground/GraphPlayground.tsx

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { StoryFn } from "storybook/internal/types";
55

66
import { TBlock } from "../../components/canvas/blocks/Block";
77
import { random } from "../../components/canvas/blocks/generate";
8+
import { ConnectionLayer } from "../../components/canvas/layers/connectionLayer/ConnectionLayer";
89
import { Graph, GraphState, TGraphConfig } from "../../graph";
9-
import { GraphBlock, GraphCanvas, HookGraphParams, useGraph, useGraphEvent } from "../../react-component";
10+
import { GraphBlock, GraphCanvas, HookGraphParams, useGraph, useGraphEvent, useLayer } from "../../react-component";
1011
import { ECanChangeBlockGeometry } from "../../store/settings";
1112
import { useFn } from "../../utils/hooks/useFn";
1213
import { EAnchorType } from "../configurations/definitions";
@@ -93,10 +94,50 @@ const graphSizeOptions: RadioButtonOption[] = [
9394
{ value: "10000", content: "10 000" },
9495
];
9596

97+
const createIcon = {
98+
path: "M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z", // Plus icon
99+
fill: "rgba(255, 190, 92, 1)",
100+
width: 24,
101+
height: 24,
102+
viewWidth: 24,
103+
viewHeight: 24,
104+
};
105+
106+
// Icon for connection point
107+
const pointIcon = {
108+
path: "M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z", // Circle icon
109+
fill: "rgba(234, 201, 74, 1)",
110+
stroke: "rgba(255, 190, 92, 1)",
111+
width: 24,
112+
height: 24,
113+
viewWidth: 24,
114+
viewHeight: 24,
115+
};
116+
117+
// Function for drawing connection line
118+
const drawLine = (start, end) => {
119+
const path = new Path2D();
120+
path.moveTo(start.x, start.y);
121+
path.lineTo(end.x, end.y);
122+
return {
123+
path,
124+
style: {
125+
color: "rgba(255, 190, 92, 1)",
126+
dash: [5, 5], // Dashed line
127+
},
128+
};
129+
};
130+
96131
export function GraphPLayground() {
97132
const { graph, setEntities, updateEntities, start } = useGraph(config);
98133
const editorRef = useRef<ConfigEditorController>(null);
99134

135+
useLayer(graph, ConnectionLayer, {
136+
createIcon,
137+
point: pointIcon,
138+
drawLine,
139+
});
140+
100141
const updateVisibleConfig = useFn(() => {
101142
const config = graph.rootStore.getAsConfig();
102143
editorRef?.current.setContent({
@@ -154,7 +195,7 @@ export function GraphPLayground() {
154195
}
155196
let block: TBlock;
156197
const pullSourceAnchor = graph.rootStore.blocksList.getBlockState(sourceBlockId).getAnchorById(sourceAnchorId);
157-
if (pullSourceAnchor.state.type === EAnchorType.IN) {
198+
if (pullSourceAnchor?.state.type === EAnchorType.IN) {
158199
block = createActionBlock(point.x - 126, point.y - 63, graph.rootStore.blocksList.$blocksMap.value.size + 1);
159200
graph.api.addBlock(block);
160201
graph.api.addConnection({

0 commit comments

Comments
 (0)