Skip to content

Commit 1b0840f

Browse files
authored
Merge pull request #43 from M-DEV-1/M-DEV-1/fix-loading
Fix SVG.js loading issue
2 parents 28e3812 + 0da32ef commit 1b0840f

File tree

7 files changed

+61
-91
lines changed

7 files changed

+61
-91
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: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import React, { useEffect, useRef, useState } from "react";
33
import { Wrapper, CanvasContainer, OutputBox, StyledSVG } from "./shapeBuilder.styles";
44
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';
57

6-
// import { useTheme } from "@layer5/sistent/components/ThemeProvider";
7-
// import { useMediaQuery } from "@layer5/sistent/components/MediaQuery";
8-
8+
SVGextend(SVG.Polygon, draw);
99

1010
const ShapeBuilder = () => {
1111
const boardRef = useRef(null);
@@ -14,30 +14,46 @@ const ShapeBuilder = () => {
1414
const [result, setResult] = useState("");
1515
const [error, setError] = useState(null);
1616

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

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

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

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

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

40-
poly.size(width > height ? 520 : null, height >= width ? 520 : null);
56+
poly.size(width > height ? 520 : undefined, height >= width ? 520 : undefined);
4157
poly.move(0, 0);
4258
showCytoArray();
4359
};
@@ -57,9 +73,9 @@ const ShapeBuilder = () => {
5773
}
5874

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

@@ -88,18 +104,9 @@ const ShapeBuilder = () => {
88104
return;
89105
}
90106

91-
if (!window.SVG) {
92-
setError("SVG.js not loaded");
93-
return;
94-
}
95-
96-
if (!window.SVG.Element.prototype.draw) {
97-
setError("svg.draw.js plugin not loaded");
98-
return;
99-
}
100-
101107
try {
102-
const draw = window.SVG(boardRef.current)
108+
const draw = SVG()
109+
.addTo(boardRef.current)
103110
.size("100%", "100%")
104111
.polygon()
105112
.draw()
@@ -138,28 +145,7 @@ const ShapeBuilder = () => {
138145
};
139146

140147
useEffect(() => {
141-
const checkSVG = () => {
142-
if (!window.SVG || !window.SVG.Element.prototype.draw) {
143-
setError("SVG.js or svg.draw.js plugin not loaded");
144-
return false;
145-
}
146-
return true;
147-
};
148-
149-
// Initial check
150-
if (checkSVG()) {
151-
initializeDrawing();
152-
} else {
153-
// If not loaded, try again after a short delay
154-
const timer = setTimeout(() => {
155-
if (checkSVG()) {
156-
initializeDrawing();
157-
}
158-
}, 1000);
159-
160-
return () => clearTimeout(timer);
161-
}
162-
148+
initializeDrawing();
163149
return () => {
164150
detachKeyListeners();
165151
if (polyRef.current) {

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)