Skip to content

Commit 7ed8905

Browse files
authored
Merge branch 'master' into M-DEV-1/copy-to-clipboard
2 parents 3a1ee0d + 7847be2 commit 7ed8905

File tree

8 files changed

+65
-95
lines changed

8 files changed

+65
-95
lines changed

site/gatsby-browser.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

site/gatsby-srr.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
import React from "react";
1+
// gatsby-ssr.js
22

3-
export const onRenderBody = ({ setHeadComponents }) => {
4-
setHeadComponents([
5-
<script key="svg-js" src="/js/svg.min.js" async={false} />,
6-
<script key="svg-draw-js" src="/js/svg.draw.min.js" async={false} />,
7-
]);
8-
};
3+
// TODO: apply SistentThemeProvider to root, once everything is migrated to Sistent

site/package-lock.json

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
"dependencies": {
2121
"@layer5/sistent": "^0.14.173",
2222
"@mui/icons-material": "^5.15.14",
23+
"@svgdotjs/svg.draw.js": "^3.0.2",
24+
"@svgdotjs/svg.js": "^3.2.4",
2325
"babel-plugin-styled-components": "^2.1.4",
2426
"eslint": "^7.32.0",
2527
"gatsby": "^5.14.0",

site/src/components/ShapeBuilder/index.js

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// /* global window */
22
import React, { useEffect, useRef, useState } from "react";
3-
import { Wrapper, CanvasContainer, StyledSVG } from "./shapeBuilder.styles";
4-
import { Button, Box } from "@layer5/sistent";
5-
import { useNotificationHandlers } from "../utils/notification-handlers"
6-
// import { useTheme } from "@layer5/sistent/components/ThemeProvider";
7-
// import { useMediaQuery } from "@layer5/sistent/components/MediaQuery";
3+
import { Wrapper, CanvasContainer, OutputBox, StyledSVG } from "./shapeBuilder.styles";
4+
import { Button, Typography, Box } from "@layer5/sistent";
5+
import { SVG, extend as SVGextend } from '@svgdotjs/svg.js';
6+
import draw from '@svgdotjs/svg.draw.js';
87

8+
SVGextend(SVG.Polygon, draw);
99

1010
const ShapeBuilder = () => {
1111
const { handleError, handleSuccess } = useNotificationHandlers();
@@ -15,30 +15,46 @@ const ShapeBuilder = () => {
1515
const [result, setResult] = useState("");
1616
const [error, setError] = useState(null);
1717

18+
const getPlottedPoints = (poly) => {
19+
if (!poly) return null;
20+
const plotted = poly.plot();
21+
const points = Array.isArray(plotted) ? plotted : plotted?.value;
22+
return Array.isArray(points) ? points : null;
23+
};
24+
1825
const showCytoArray = () => {
1926
const poly = polyRef.current;
2027
if (!poly) return;
2128

22-
const points = poly.array().value;
23-
const normalized = points
24-
.map(([x, y]) => [(x - 260) / 260, (y - 260) / 260])
25-
.flat()
26-
.join(" ");
27-
setResult(normalized);
29+
try {
30+
const points = getPlottedPoints(poly);
31+
if (!points) throw new Error("Invalid or empty polygon points");
32+
33+
const normalized = points
34+
.map(([x, y]) => [(x - 260) / 260, (y - 260) / 260])
35+
.flat()
36+
.join(" ");
37+
setResult(normalized);
38+
setError(null);
39+
} catch (err) {
40+
setError("Failed to extract and normalize polygon points.");
41+
console.error("showCytoArray error:", err);
42+
}
2843
};
2944

3045
const handleMaximize = () => {
3146
const poly = polyRef.current;
3247
if (!poly) return;
3348

34-
const points = poly.array().value;
49+
const points = getPlottedPoints(poly);
50+
if (!points) return;
3551
const xs = points.map(p => p[0]);
3652
const ys = points.map(p => p[1]);
3753

3854
const width = Math.abs(Math.max(...xs) - Math.min(...xs));
3955
const height = Math.abs(Math.max(...ys) - Math.min(...ys));
4056

41-
poly.size(width > height ? 520 : null, height >= width ? 520 : null);
57+
poly.size(width > height ? 520 : undefined, height >= width ? 520 : undefined);
4258
poly.move(0, 0);
4359
showCytoArray();
4460
};
@@ -51,16 +67,16 @@ const ShapeBuilder = () => {
5167
poly.draw("param", "snapToGrid", 0.001);
5268
}
5369

54-
if (e.ctrlKey && e.key === "Enter") {
70+
if (e.key === "Enter") {
5571
poly.draw("done");
5672
poly.fill("#00B39F");
5773
showCytoArray();
5874
}
5975

6076
if (e.ctrlKey && e.key.toLowerCase() === "z") {
61-
const points = poly.array().value;
62-
points.pop();
63-
poly.plot(points);
77+
const points = getPlottedPoints(poly);
78+
if (!points) return;
79+
poly.plot(points.slice(0, -1));
6480
}
6581
};
6682

@@ -89,18 +105,9 @@ const ShapeBuilder = () => {
89105
return;
90106
}
91107

92-
if (!window.SVG) {
93-
setError("SVG.js not loaded");
94-
return;
95-
}
96-
97-
if (!window.SVG.Element.prototype.draw) {
98-
setError("svg.draw.js plugin not loaded");
99-
return;
100-
}
101-
102108
try {
103-
const draw = window.SVG(boardRef.current)
109+
const draw = SVG()
110+
.addTo(boardRef.current)
104111
.size("100%", "100%")
105112
.polygon()
106113
.draw()
@@ -149,28 +156,7 @@ const ShapeBuilder = () => {
149156
};
150157

151158
useEffect(() => {
152-
const checkSVG = () => {
153-
if (!window.SVG || !window.SVG.Element.prototype.draw) {
154-
setError("SVG.js or svg.draw.js plugin not loaded");
155-
return false;
156-
}
157-
return true;
158-
};
159-
160-
// Initial check
161-
if (checkSVG()) {
162-
initializeDrawing();
163-
} else {
164-
// If not loaded, try again after a short delay
165-
const timer = setTimeout(() => {
166-
if (checkSVG()) {
167-
initializeDrawing();
168-
}
169-
}, 1000);
170-
171-
return () => clearTimeout(timer);
172-
}
173-
159+
initializeDrawing();
174160
return () => {
175161
detachKeyListeners();
176162
if (polyRef.current) {

site/src/components/utils/instructionsModal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Modal, ModalBody, Typography, List, ListItem, ListItemText, ModalFooter
33

44
const instructionPoints = [
55
{
6-
primary: "Press CTRL + Enter to close the shape",
6+
primary: "Press ENTER to close the shape",
77
secondary: "It won’t draw the last position of your cursor, don’t stress!"
88
},
99
{

site/static/js/svg.draw.min.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

site/static/js/svg.min.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)