Skip to content

Commit d4b1e5c

Browse files
authored
Merge pull request #29 from meshery-extensions/leecalcote/proper-load-and-init
Fix: proper load and initialization of plugin
2 parents c536214 + b370115 commit d4b1e5c

File tree

3 files changed

+91
-25
lines changed

3 files changed

+91
-25
lines changed

site/gatsby-browser.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
// gatsby-browser.js
22
export const onInitialClientRender = () => {
33
const loadScript = (src) => {
4-
const script = document.createElement('script');
5-
script.src = src;
6-
script.async = false;
7-
document.body.appendChild(script);
4+
return new Promise((resolve, reject) => {
5+
const script = document.createElement('script');
6+
script.src = src;
7+
script.async = false;
8+
script.onload = () => {
9+
// Small delay to ensure SVG.js is fully initialized
10+
setTimeout(resolve, 100);
11+
};
12+
script.onerror = reject;
13+
document.body.appendChild(script);
14+
});
815
};
916

10-
loadScript('/js/svg.min.js');
11-
loadScript('/js/svg.draw.min.js');
17+
// Load SVG.js first, then load svg.draw.js after it's ready
18+
loadScript('/js/svg.min.js')
19+
.then(() => {
20+
// Ensure SVG.js is properly initialized
21+
if (window.SVG) {
22+
return loadScript('/js/svg.draw.min.js');
23+
} else {
24+
throw new Error('SVG.js failed to initialize');
25+
}
26+
})
27+
.catch((error) => {
28+
console.error('Error loading SVG libraries:', error);
29+
});
1230
};
1331

site/src/components/ShapeBuilder/index.js

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const ShapeBuilder = () => {
1212
const polyRef = useRef(null);
1313
const keyHandlersRef = useRef({});
1414
const [result, setResult] = useState("");
15+
const [error, setError] = useState(null);
1516

1617
const showCytoArray = () => {
1718
const poly = polyRef.current;
@@ -82,19 +83,37 @@ const ShapeBuilder = () => {
8283
};
8384

8485
const initializeDrawing = () => {
85-
if (!boardRef.current || !window.SVG) return;
86+
if (!boardRef.current) {
87+
setError("Canvas reference not found");
88+
return;
89+
}
8690

87-
const draw = window.SVG(boardRef.current)
88-
.size("100%", "100%")
89-
.polygon()
90-
.draw()
91-
.attr({ stroke: "#00B39F", "stroke-width": 1, fill: "none" });
91+
if (!window.SVG) {
92+
setError("SVG.js not loaded");
93+
return;
94+
}
9295

93-
draw.draw("param", "snapToGrid", 0.001);
94-
draw.on("drawstart", attachKeyListeners);
95-
draw.on("drawdone", detachKeyListeners);
96+
if (!window.SVG.Element.prototype.draw) {
97+
setError("svg.draw.js plugin not loaded");
98+
return;
99+
}
96100

97-
polyRef.current = draw;
101+
try {
102+
const draw = window.SVG(boardRef.current)
103+
.size("100%", "100%")
104+
.polygon()
105+
.draw()
106+
.attr({ stroke: "#00B39F", "stroke-width": 1, fill: "none" });
107+
108+
draw.draw("param", "snapToGrid", 0.001);
109+
draw.on("drawstart", attachKeyListeners);
110+
draw.on("drawdone", detachKeyListeners);
111+
112+
polyRef.current = draw;
113+
setError(null);
114+
} catch (err) {
115+
setError(`Failed to initialize drawing: ${err.message}`);
116+
}
98117
};
99118

100119
const clearShape = () => {
@@ -119,12 +138,27 @@ const ShapeBuilder = () => {
119138
};
120139

121140
useEffect(() => {
122-
if (!window.SVG || !window.SVG.Element.prototype.draw) {
123-
console.error("SVG.js or svg.draw.js plugin not loaded.");
124-
return;
125-
}
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+
};
126148

127-
initializeDrawing();
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+
}
128162

129163
return () => {
130164
detachKeyListeners();
@@ -147,6 +181,20 @@ const ShapeBuilder = () => {
147181
</defs>
148182
<rect className="grid" width="100%" height="100%" fill="url(#grid)" />
149183
</StyledSVG>
184+
{error && (
185+
<div style={{
186+
position: 'absolute',
187+
top: '50%',
188+
left: '50%',
189+
transform: 'translate(-50%, -50%)',
190+
color: 'red',
191+
backgroundColor: 'white',
192+
padding: '10px',
193+
borderRadius: '5px'
194+
}}>
195+
{error}
196+
</div>
197+
)}
150198
</CanvasContainer>
151199

152200
<Controls>

site/src/components/Toggle.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const toggleStyle = {
1111
{theme === "dark" ? (
1212
<svg
1313
xmlns="http://www.w3.org/2000/svg"
14-
width="auto"
15-
height="auto"
14+
width="24px"
15+
height="24px"
1616
viewBox="0 0 24 24"
1717
fill="rgb(0,179,159)"
1818
stroke="none"
@@ -26,8 +26,8 @@ const toggleStyle = {
2626
) : (
2727
<svg
2828
xmlns="http://www.w3.org/2000/svg"
29-
width="auto"
30-
height="auto"
29+
width="24px"
30+
height="24px"
3131
viewBox="0 0 24 24"
3232
fill="rgb(60, 73, 79)"
3333
stroke="rgb(60, 73, 79)"

0 commit comments

Comments
 (0)