-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordpress.tsx
More file actions
51 lines (41 loc) · 1.47 KB
/
wordpress.tsx
File metadata and controls
51 lines (41 loc) · 1.47 KB
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
import { handleLoadWidget } from "@/entrypoints/loader";
import { Root } from "react-dom/client";
type ContainerId = string;
const mountedContainers = new Map<ContainerId, Root>();
// WordPress allows multiple instances of the widget
window.loadDostanesiePlWidget = (container) => {
const containerId = container.getAttribute("id");
const root = handleLoadWidget(container);
// unmount to avoid multiple instances of the widget coming from the same container
if (containerId) {
const mountedContainer = mountedContainers.get(containerId);
mountedContainer?.unmount();
mountedContainers.set(containerId, root);
}
};
window.unloadDostanesiePlWidget = (container) => {
const containerId = container.getAttribute("id");
if (!containerId) {
return;
}
const mountedContainer = mountedContainers.get(containerId);
mountedContainer?.unmount();
mountedContainers.delete(containerId);
};
window.addEventListener("load", () => {
const regex = /^dstpl-widget-.*$/;
const widgetContainers = Array.from(document.querySelectorAll("div")).filter(
(div) => regex.test(div.id),
);
widgetContainers.forEach((container) => {
const containerId = container.getAttribute("id");
const root = handleLoadWidget(container);
if (!containerId) {
console.error("Container ID not found");
return;
}
const mountedContainer = mountedContainers.get(containerId);
mountedContainer?.unmount();
mountedContainers.set(containerId, root);
});
});