@oceanum/eidos 0.9.0 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/lib/render.ts DELETED
@@ -1,86 +0,0 @@
1
- import { proxy, subscribe, snapshot } from "valtio/vanilla";
2
- import { validateSchema } from "./eidosmodel";
3
- import { EidosSpec } from "../schema/interfaces";
4
-
5
- const DEFAULT_RENDERER = "https://render.eidos.oceanum.io";
6
-
7
- /**
8
- * Embed the EIDOS iframe and set up message passing
9
- * @param element HTML element to render the EIDOS spec into
10
- * @param spec The EIDOS specification object
11
- * @param id The unique identifier for the EIDOS spec (optional, defaults to spec.id)
12
- * @param eventListener Optional callback for handling events from the renderer
13
- * @param renderer URL of the EIDOS renderer
14
- * @returns A proxy object that can be used with useEidosSpec
15
- */
16
- const render = async (
17
- element: HTMLElement,
18
- spec: EidosSpec,
19
- id?: string,
20
- eventListener?: (payload: unknown) => void,
21
- renderer = DEFAULT_RENDERER
22
- ): Promise<Proxy<EidosSpec>> => {
23
- // Validate the spec before creating proxy
24
- try {
25
- await validateSchema(spec);
26
- } catch (e: any) {
27
- throw new Error(`Invalid Eidos Spec: ${e.message}`);
28
- }
29
-
30
- // Create Valtio proxy - naturally mutable, no unprotect needed
31
- const eidos = proxy(structuredClone(spec));
32
- const _id = id || spec.id;
33
-
34
- return new Promise((resolve, reject) => {
35
- const iframe = document.createElement("iframe");
36
- iframe.src = `${renderer}?id=${_id}`;
37
- iframe.width = "100%";
38
- iframe.height = "100%";
39
- iframe.frameBorder = "0";
40
- element.appendChild(iframe);
41
-
42
- iframe.onload = () => {
43
- const win = iframe.contentWindow;
44
-
45
- window.addEventListener("message", (event) => {
46
- if (event.source !== win) return;
47
- if (event.data.id !== id) return;
48
-
49
- if (event.data.type === "init") {
50
- // Send initial spec
51
- win?.postMessage(
52
- {
53
- id: _id,
54
- type: "spec",
55
- payload: structuredClone(eidos),
56
- },
57
- "*"
58
- );
59
- } else {
60
- eventListener?.(event.data.payload);
61
- }
62
- });
63
-
64
- // Subscribe to changes and send patches to renderer
65
- subscribe(eidos, () => {
66
- win?.postMessage(
67
- {
68
- id: _id,
69
- type: "spec",
70
- payload: snapshot(eidos),
71
- },
72
- "*"
73
- );
74
- });
75
-
76
- resolve(eidos);
77
- };
78
-
79
- iframe.onerror = () => {
80
- reject(new Error("Failed to load EIDOS renderer"));
81
- };
82
- });
83
- return eidos;
84
- };
85
-
86
- export { render };