Newer
Older
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
78
79
80
81
82
83
84
85
86
87
88
89
import { useEffect, useRef } from "react";
import { Template as TemplateCl } from "../lib/template";
import { useAppContext } from "../contexts/AppContext";
import { useTemplateContext } from "../contexts/TemplateContext";
export const Template = () => {
const { config } = useAppContext();
const { enable, url, width, setWidth, x, y, opacity } = useTemplateContext();
const templateHolder = useRef<HTMLDivElement>(null);
const instance = useRef<TemplateCl>();
useEffect(() => {
if (!templateHolder?.current) {
console.warn("No templateHolder, cannot initialize");
return;
}
instance.current = new TemplateCl(config, templateHolder.current);
instance.current.on("autoDetectWidth", (width) => {
console.log("autodetectwidth", width);
setWidth(width);
});
return () => {
instance.current?.destroy();
};
}, []);
useEffect(() => {
if (!instance.current) {
console.warn("Received template enable but no instance exists");
return;
}
instance.current.setOption("enable", enable);
if (enable && url) {
instance.current.loadImage(url).then(() => {
console.log("enable: load image finished");
});
}
}, [enable]);
useEffect(() => {
if (!instance.current) {
console.warn(
"recieved template url update but no template instance exists"
);
return;
}
if (!url) {
console.warn("received template url blank");
return;
}
if (!enable) {
console.info("Got template URL but not enabled, ignoring");
return;
}
instance.current.loadImage(url).then(() => {
console.log("template loader finished");
});
}, [url]);
useEffect(() => {
if (!instance.current) {
console.warn("received template width with no instance");
return;
}
instance.current.setOption("width", width);
instance.current.rasterizeTemplate();
}, [width]);
return (
<div
id="template"
ref={templateHolder}
style={{
top: y,
left: x,
opacity: opacity / 100,
}}
></div>
);
};