-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(playground): migrate runner iframe to web component
- Loading branch information
Showing
6 changed files
with
154 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { html, LitElement } from "lit"; | ||
import { compressAndBase64Encode } from "./utils.ts"; | ||
import { PLAYGROUND_BASE_HOST } from "../env.ts"; | ||
import { createComponent } from "@lit/react"; | ||
import { Task } from "@lit/task"; | ||
import React from "react"; | ||
|
||
import styles from "./runner.scss?css" with { type: "css" }; | ||
|
||
/** @import { EditorContent } from "./utils.ts" */ | ||
/** @import { VConsole } from "./console.tsx" */ | ||
/** @import { EventName } from "@lit/react" */ | ||
|
||
export class PlayRunner extends LitElement { | ||
static properties = { | ||
code: { type: Object }, | ||
srcPrefix: { type: String, attribute: "src-prefix" }, | ||
_src: { state: true }, | ||
}; | ||
|
||
static styles = styles; | ||
|
||
constructor() { | ||
super(); | ||
/** @type {EditorContent | undefined} */ | ||
this.code = undefined; | ||
/** @type {string | undefined} */ | ||
this.srcPrefix = undefined; | ||
this._src = "about:blank"; | ||
|
||
this._subdomain = crypto.randomUUID(); | ||
this._flipFlop = 0; | ||
} | ||
|
||
/** @param {MessageEvent} e */ | ||
_onMessage({ data: { typ, prop, message } }) { | ||
if (typ === "console") { | ||
if ( | ||
(prop === "log" || prop === "error" || prop === "warn") && | ||
typeof message === "string" | ||
) { | ||
/** @type {VConsole} */ | ||
const detail = { prop, message }; | ||
this.dispatchEvent( | ||
new CustomEvent("console", { bubbles: true, composed: true, detail }) | ||
); | ||
} else { | ||
const warning = "[Playground] Unsupported console message"; | ||
/** @type {VConsole} */ | ||
const detail = { | ||
prop: "warn", | ||
message: `${warning} (see browser console)`, | ||
}; | ||
this.dispatchEvent( | ||
new CustomEvent("console", { bubbles: true, composed: true, detail }) | ||
); | ||
console.warn(warning, { prop, message }); | ||
} | ||
} | ||
} | ||
|
||
_updateSrc = new Task(this, { | ||
args: () => /** @type {const} */ ([this.code, this.srcPrefix]), | ||
task: async ([code, srcPrefix], { signal }) => { | ||
if (code) { | ||
const { state } = await compressAndBase64Encode(JSON.stringify(code)); | ||
signal.throwIfAborted(); | ||
// We're using a random subdomain for origin isolation. | ||
const url = new URL( | ||
window.location.hostname.endsWith("localhost") | ||
? window.location.origin | ||
: `${window.location.protocol}//${ | ||
PLAYGROUND_BASE_HOST.startsWith("localhost") | ||
? "" | ||
: `${this._subdomain}.` | ||
}${PLAYGROUND_BASE_HOST}` | ||
); | ||
url.searchParams.set("state", state); | ||
// ensure iframe reloads even if code doesn't change | ||
url.searchParams.set("f", this._flipFlop.toString()); | ||
url.pathname = `${srcPrefix || code.src || ""}/runner.html`; | ||
this._src = url.href; | ||
this._flipFlop = (this._flipFlop + 1) % 2; | ||
} else { | ||
this._src = "about:blank"; | ||
} | ||
}, | ||
}); | ||
|
||
connectedCallback() { | ||
super.connectedCallback(); | ||
this._onMessage = this._onMessage.bind(this); | ||
window.addEventListener("message", this._onMessage); | ||
} | ||
|
||
render() { | ||
return html` | ||
<iframe | ||
title="runner" | ||
src=${this._src} | ||
sandbox="allow-scripts allow-same-origin allow-forms" | ||
></iframe> | ||
`; | ||
} | ||
|
||
disconnectedCallback() { | ||
super.disconnectedCallback(); | ||
window.removeEventListener("message", this._onMessage); | ||
} | ||
} | ||
|
||
customElements.define("play-runner", PlayRunner); | ||
|
||
export const ReactPlayRunner = createComponent({ | ||
tagName: "play-runner", | ||
elementClass: PlayRunner, | ||
react: React, | ||
events: { | ||
onConsole: /** @type {EventName<CustomEvent<VConsole>>} */ ("console"), | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
iframe { | ||
border: none; | ||
height: 100%; | ||
width: 100%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters