forked from McJeffr/react-schematicwebviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
77 lines (71 loc) · 1.8 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import * as React from "react";
import { renderSchematic } from "@enginehub/schematicwebviewer";
import "./styles.scss";
const { useState, useEffect } = React;
export interface ISchematicViewerProps {
jarUrl: string | string[];
schematic: string;
id?: string;
className?: string;
width?: number;
height?: number;
size?: number;
renderArrow?: boolean;
renderBars?: boolean;
orbit?: boolean;
antialias?: boolean;
loader?: React.ReactElement;
backgroundColor?: number | "transparent";
onLoaded?: () => void;
}
const SchematicViewer: React.FC<ISchematicViewerProps> = ({
id,
jarUrl,
schematic,
className,
size = 500,
width = size ? size : 500,
height = size ? size : 500,
renderArrow = false,
renderBars = false,
orbit = true,
antialias = false,
loader,
backgroundColor = 0xffffff,
onLoaded,
}) => {
const [canvasRef] = useState<React.RefObject<HTMLCanvasElement>>(
React.createRef<HTMLCanvasElement>()
);
const [loading, setLoading] = useState<boolean>(true);
useEffect(() => {
if (!canvasRef?.current) {
throw new Error("SchematicView's canvas ref is null");
}
renderSchematic(canvasRef.current, schematic, {
size,
jarUrl,
renderArrow,
renderBars,
orbit,
antialias,
backgroundColor,
loadingSpinner: false,
}).then(() => {
setLoading(false);
if (onLoaded) onLoaded();
});
}, [canvasRef, size, width, height]);
return (
<div
className={"schematic-container" + (className ? " " + className : "")}
style={{ width, height }}
>
<canvas ref={canvasRef} id={id} width={width} height={height}></canvas>
{loading && loader ? (
<div className="schematic-loading-container">{loader}</div>
) : null}
</div>
);
};
export default SchematicViewer;