|
1 | 1 | import { h, render } from "preact";
|
2 |
| -import App from "./App"; |
| 2 | +/* import App from "./App"; |
3 | 3 |
|
4 |
| -render(<App />, document.getElementById("root")!); |
| 4 | +render(<App />, document.getElementById("root")!); */ |
| 5 | + |
| 6 | +import GluonWeb2 from "./GluonWeb2"; |
| 7 | + |
| 8 | +const INSTANCES: Record<string, GluonWeb2Instance> = {} |
| 9 | + |
| 10 | +class GluonWeb2Instance { |
| 11 | + readonly el: HTMLElement; |
| 12 | + readonly rendered: any; |
| 13 | + |
| 14 | + constructor(el: HTMLElement) { |
| 15 | + this.el = el; |
| 16 | + const dataset = this.el.dataset |
| 17 | + |
| 18 | + try { |
| 19 | + let parsedData = JSON.parse(atob(dataset.componentData)) |
| 20 | + |
| 21 | + this.rendered = render(<GluonWeb2 |
| 22 | + id={dataset.gluonWeb2} |
| 23 | + component={dataset.component} |
| 24 | + data={parsedData} |
| 25 | + />, this.el) |
| 26 | + } catch (error) { |
| 27 | + const errEl = document.createElement('p') |
| 28 | + errEl.classList.add('web2-error') |
| 29 | + el.appendChild(errEl) |
| 30 | + errEl.appendChild(document.createTextNode( |
| 31 | + JSON.stringify(dataset, null, 2) + ' ' + String(error) |
| 32 | + )) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + teardown() { |
| 37 | + |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function findGluonWeb2() { |
| 42 | + const nodes = document.querySelectorAll('gluon-web2') |
| 43 | + |
| 44 | + let seen: Record<string, boolean> = {} |
| 45 | + |
| 46 | + for (let i = 0; i < nodes.length; i++) { |
| 47 | + let node = nodes[i] as HTMLElement |
| 48 | + |
| 49 | + if (!node.dataset.gluonWeb2) { |
| 50 | + node.dataset.gluonWeb2 = String(Math.random()) |
| 51 | + INSTANCES[node.dataset.gluonWeb2] = new GluonWeb2Instance(node) |
| 52 | + } |
| 53 | + |
| 54 | + seen[node.dataset.gluonWeb2] = true |
| 55 | + } |
| 56 | + |
| 57 | + for (const key of Object.keys(INSTANCES).filter(k => !seen[k])) { |
| 58 | + INSTANCES[key].teardown() |
| 59 | + delete INSTANCES[key] |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +window.GluonWeb2Refresh = findGluonWeb2 |
| 64 | + |
| 65 | +findGluonWeb2() |
0 commit comments