|
| 1 | +import { LitElement, html } from "lit-element"; |
| 2 | +import { customElement, property } from "lit/decorators.js"; |
| 3 | +import { |
| 4 | + Cell, |
| 5 | + addAction, |
| 6 | + removeAction, |
| 7 | + type Action, |
| 8 | + type ReactivityLog, |
| 9 | +} from "@commontools/common-runner"; |
| 10 | +import { Ref, createRef, ref } from "lit/directives/ref.js"; |
| 11 | + |
| 12 | +@customElement("common-iframe") |
| 13 | +export class CommonIframe extends LitElement { |
| 14 | + @property({ type: String }) src = ""; |
| 15 | + // HACK: The UI framework already translates the top level cell into updated |
| 16 | + // properties, but we want to only have to deal with one type of listening, so |
| 17 | + // we'll add a an extra level of indirection with the "context" property. |
| 18 | + @property({ type: Object }) context?: Cell<any> | any; |
| 19 | + |
| 20 | + private iframeRef: Ref<HTMLIFrameElement> = createRef(); |
| 21 | + |
| 22 | + private subscriptions: Map<string, Action[]> = new Map(); |
| 23 | + |
| 24 | + private handleMessage = (event: MessageEvent) => { |
| 25 | + console.log("Received message", event); |
| 26 | + if (event.source === this.iframeRef.value?.contentWindow) { |
| 27 | + const { type, key, value } = event.data; |
| 28 | + if (typeof key !== "string") { |
| 29 | + console.error("Invalid key type. Expected string."); |
| 30 | + return; |
| 31 | + } |
| 32 | + if (type === "read" && this.context) { |
| 33 | + const value = this.context?.getAsProxy |
| 34 | + ? this.context?.getAsProxy([key]) |
| 35 | + : this.context?.[key]; |
| 36 | + // TODO: This might cause infinite loops, since the data can be a graph. |
| 37 | + const copy = |
| 38 | + value !== undefined ? JSON.parse(JSON.stringify(value)) : undefined; |
| 39 | + console.log("readResponse", key, value); |
| 40 | + this.iframeRef.value?.contentWindow?.postMessage( |
| 41 | + { type: "readResponse", key, value: copy }, |
| 42 | + "*" |
| 43 | + ); |
| 44 | + } else if (type === "write" && this.context) { |
| 45 | + this.context.getAsProxy()[key] = value; |
| 46 | + } else if (type === "subscribe" && this.context) { |
| 47 | + console.log("subscribing", key, this.context); |
| 48 | + |
| 49 | + const action: Action = (log: ReactivityLog) => |
| 50 | + this.notifySubscribers(key, this.context.getAsProxy([key], log)); |
| 51 | + |
| 52 | + addAction(action); |
| 53 | + if (!this.subscriptions.has(key)) this.subscriptions.set(key, [action]); |
| 54 | + else this.subscriptions.get(key)!.push(action); |
| 55 | + } else if (type === "unsubscribe" && this.context) { |
| 56 | + if (this.subscriptions && this.subscriptions.has(key)) { |
| 57 | + const actions = this.subscriptions.get(key); |
| 58 | + if (actions && actions.length) removeAction(actions.pop()!); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + private notifySubscribers(key: string, value: any) { |
| 65 | + console.log("notifySubscribers", key, value); |
| 66 | + // TODO: This might cause infinite loops, since the data can be a graph. |
| 67 | + const copy = |
| 68 | + value !== undefined ? JSON.parse(JSON.stringify(value)) : undefined; |
| 69 | + this.iframeRef.value?.contentWindow?.postMessage( |
| 70 | + { type: "update", key, value: copy }, |
| 71 | + "*" |
| 72 | + ); |
| 73 | + } |
| 74 | + private boundHandleMessage = this.handleMessage.bind(this); |
| 75 | + |
| 76 | + override connectedCallback() { |
| 77 | + super.connectedCallback(); |
| 78 | + window.addEventListener("message", this.boundHandleMessage); |
| 79 | + } |
| 80 | + |
| 81 | + override disconnectedCallback() { |
| 82 | + super.disconnectedCallback(); |
| 83 | + window.removeEventListener("message", this.boundHandleMessage); |
| 84 | + } |
| 85 | + |
| 86 | + override render() { |
| 87 | + return html` |
| 88 | + <iframe |
| 89 | + ${ref(this.iframeRef)} |
| 90 | + sandbox="allow-scripts" |
| 91 | + .srcdoc=${this.src} |
| 92 | + height="512px" |
| 93 | + width="100%" |
| 94 | + @load=${this.iframeRef.value?.contentWindow?.postMessage( |
| 95 | + { type: "init" }, |
| 96 | + "*" |
| 97 | + )} |
| 98 | + ></iframe> |
| 99 | + `; |
| 100 | + } |
| 101 | +} |
0 commit comments