@livespace/plugin-studio 0.0.0-stage → 0.1.0

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.
Files changed (76) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +87 -2
  3. package/dist/auth.js +220 -0
  4. package/dist/cli.js +67 -0
  5. package/dist/keys.js +42 -0
  6. package/dist/manifest.js +16 -0
  7. package/dist/page/domain/connection.js +2 -0
  8. package/dist/page/domain/display.js +38 -0
  9. package/dist/page/domain/draft.js +80 -0
  10. package/dist/page/domain/failures-store.js +41 -0
  11. package/dist/page/domain/failures.js +75 -0
  12. package/dist/page/domain/layout-store.js +119 -0
  13. package/dist/page/domain/log-store.js +37 -0
  14. package/dist/page/domain/navigation-store.js +16 -0
  15. package/dist/page/domain/plugin-data.js +70 -0
  16. package/dist/page/domain/plugin-store.js +257 -0
  17. package/dist/page/domain/probe-store.js +100 -0
  18. package/dist/page/domain/profile-session.js +115 -0
  19. package/dist/page/domain/profile-store.js +21 -0
  20. package/dist/page/domain/samples.js +221 -0
  21. package/dist/page/domain/secrets-store.js +159 -0
  22. package/dist/page/domain/secrets.js +135 -0
  23. package/dist/page/domain/settings-data.js +64 -0
  24. package/dist/page/domain/settings-session.js +206 -0
  25. package/dist/page/domain/settings-store.js +62 -0
  26. package/dist/page/domain/slot-store.js +50 -0
  27. package/dist/page/domain/studio.js +121 -0
  28. package/dist/page/domain/surface-store.js +71 -0
  29. package/dist/page/domain/toasts-store.js +60 -0
  30. package/dist/page/domain/view-data.js +122 -0
  31. package/dist/page/domain/view-store.js +132 -0
  32. package/dist/page/host.css +907 -0
  33. package/dist/page/index.html +31 -0
  34. package/dist/page/main.js +31 -0
  35. package/dist/page/plugin-studio-logo.svg +4 -0
  36. package/dist/page/reload.svg +1 -0
  37. package/dist/page/transport/browser-storage.js +41 -0
  38. package/dist/page/transport/embed.js +142 -0
  39. package/dist/page/transport/plugin-keys.js +68 -0
  40. package/dist/page/transport/studio-api.js +74 -0
  41. package/dist/page/ui/app.js +61 -0
  42. package/dist/page/ui/boot.js +44 -0
  43. package/dist/page/ui/brand.js +3 -0
  44. package/dist/page/ui/context.js +11 -0
  45. package/dist/page/ui/json-field.js +19 -0
  46. package/dist/page/ui/json-tokens.js +40 -0
  47. package/dist/page/ui/json-view.js +11 -0
  48. package/dist/page/ui/memo.js +23 -0
  49. package/dist/page/ui/messages.js +34 -0
  50. package/dist/page/ui/page-switcher.js +11 -0
  51. package/dist/page/ui/plugin-frame.js +43 -0
  52. package/dist/page/ui/profile-page.js +13 -0
  53. package/dist/page/ui/resize-handle.js +63 -0
  54. package/dist/page/ui/root.js +9 -0
  55. package/dist/page/ui/section.js +19 -0
  56. package/dist/page/ui/settings/config-form.js +59 -0
  57. package/dist/page/ui/settings/secret-field.js +22 -0
  58. package/dist/page/ui/settings/settings-page.js +60 -0
  59. package/dist/page/ui/sidebar/custom-settings-section.js +10 -0
  60. package/dist/page/ui/sidebar/failures-section.js +27 -0
  61. package/dist/page/ui/sidebar/installation-section.js +30 -0
  62. package/dist/page/ui/sidebar/metadata-section.js +9 -0
  63. package/dist/page/ui/sidebar/plugin-section.js +18 -0
  64. package/dist/page/ui/sidebar/preferences-section.js +16 -0
  65. package/dist/page/ui/sidebar/record-section.js +17 -0
  66. package/dist/page/ui/sidebar/viewer-section.js +8 -0
  67. package/dist/page/ui/sidebar-group.js +4 -0
  68. package/dist/page/ui/splash.js +6 -0
  69. package/dist/page/ui/status.js +10 -0
  70. package/dist/page/ui/surface.js +23 -0
  71. package/dist/page/ui/theme.js +13 -0
  72. package/dist/page/ui/toasts.js +31 -0
  73. package/dist/page/ui/top-bar.js +37 -0
  74. package/dist/server.js +519 -0
  75. package/dist/start.js +108 -0
  76. package/package.json +55 -5
@@ -0,0 +1,31 @@
1
+ <!doctype html>
2
+ <html lang="en" class="wa-dark">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
6
+ <title>Plugin Studio</title>
7
+ <link rel="stylesheet" href="/vendor/webawesome/styles/webawesome.css" />
8
+ <!-- Livespace's own tokens, for the chrome around the plugin only. -->
9
+ <link rel="stylesheet" href="/sdk/ls-tokens.css" />
10
+ <link rel="stylesheet" href="/host.css" />
11
+ <!-- The splash shows it before anything else has loaded. -->
12
+ <link rel="preload" as="image" href="/plugin-studio-logo.svg" />
13
+ <script type="importmap">
14
+ {
15
+ "imports": {
16
+ "@livespace/plugin-sdk": "/sdk/index.js",
17
+ "preact": "/vendor/preact.js",
18
+ "preact/hooks": "/vendor/preact-hooks.js",
19
+ "preact/jsx-runtime": "/vendor/preact-jsx-runtime.js",
20
+ "@preact/signals": "/vendor/signals.js",
21
+ "@preact/signals-core": "/vendor/signals-core.js"
22
+ }
23
+ }
24
+ </script>
25
+ <script type="module" src="/vendor/webawesome/webawesome.loader.js" data-webawesome="/vendor/webawesome"></script>
26
+ <script type="module" src="/main.js"></script>
27
+ </head>
28
+ <body>
29
+ <div id="app"></div>
30
+ </body>
31
+ </html>
@@ -0,0 +1,31 @@
1
+ import { jsx as _jsx } from "preact/jsx-runtime";
2
+ /**
3
+ * The studio's page: the root store with its transport, and the Preact app, which shows a splash
4
+ * until the store has started and the app is drawn (ui/boot.ts).
5
+ */
6
+ import { signal } from "@preact/signals";
7
+ import { render } from "preact";
8
+ import { Studio } from "./domain/studio.js";
9
+ import { StudioApi } from "./transport/studio-api.js";
10
+ import { boot } from "./ui/boot.js";
11
+ import { StudioContext } from "./ui/context.js";
12
+ import { Root } from "./ui/root.js";
13
+ import { followColorScheme } from "./ui/theme.js";
14
+ followColorScheme();
15
+ const root = document.getElementById("app");
16
+ if (root === null) {
17
+ throw new Error("index.html has no #app");
18
+ }
19
+ const viewportWidth = signal(window.innerWidth);
20
+ window.addEventListener("resize", () => {
21
+ viewportWidth.value = window.innerWidth;
22
+ });
23
+ const studio = new Studio({
24
+ api: new StudioApi(),
25
+ origin: window.location.origin,
26
+ hash: window.location.hash,
27
+ viewportWidth,
28
+ });
29
+ window.addEventListener("hashchange", () => studio.navigation.follow(window.location.hash));
30
+ window.addEventListener("pagehide", studio.flush);
31
+ render(_jsx(StudioContext.Provider, { value: studio, children: _jsx(Root, { phase: boot(studio, root) }) }), root);
@@ -0,0 +1,4 @@
1
+ <svg width="475" height="520" viewBox="0 0 475 520" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <path d="M474.772 4.3612C380.141 -10.6667 314.253 13.1429 275.956 75.7899C246.112 124.608 227.045 206.89 218.76 322.634L218.463 519.46L403.563 147.45L474.772 4.3612Z" fill-rule="evenodd" clip-rule="evenodd" fill="#4FD465"/>
3
+ <path d="M218.438 322.638L273.413 205.163C293.709 161.79 340.339 137.157 387.617 144.834L403.574 147.425L218.445 519.45L179.396 440.882L195.509 432.98L193.308 428.491L177.171 436.404L161.593 405.062L177.896 397.067L175.694 392.578L159.367 400.584L143.79 369.242L160.283 361.153L158.081 356.665L141.564 364.764L125.987 333.421L142.67 325.24L140.468 320.751L123.762 328.943L108.185 297.601L125.057 289.327L122.855 284.838L105.959 293.123L90.3818 261.78L107.443 253.413L105.242 248.924L88.1562 257.303L72.5791 225.96L89.8311 217.5L87.6289 213.011L70.3535 221.482L54.7764 190.14L72.2178 181.586L70.0156 177.097L52.5508 185.662L36.9736 154.318L54.6045 145.673L52.4033 141.184L34.748 149.841L19.1709 118.498L36.9912 109.759L34.79 105.271L16.9463 114.021L0 79.9238L89.2344 42.4297L218.438 322.638Z" fill="#d1d1d2"/>
4
+ </svg>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 12a8 8 0 1 1-2.34-5.66L20 8.5"/><path d="M20 3.5v5h-5"/></svg>
@@ -0,0 +1,41 @@
1
+ /**
2
+ * What the page remembers in this browser. Storage can be missing or refuse (a private window,
3
+ * blocked site data), so every read falls back and every write may silently do nothing.
4
+ */
5
+ /** One versioned key's JSON value. */
6
+ export class BrowserStorage {
7
+ key;
8
+ area;
9
+ constructor(key, area = () => globalThis.localStorage) {
10
+ this.key = key;
11
+ this.area = area;
12
+ }
13
+ /**
14
+ * The saved value, merged over `fallback` field by field, so a value saved by an older page
15
+ * gains the fields it lacked. Anything unreadable is the fallback.
16
+ */
17
+ read(fallback) {
18
+ try {
19
+ const saved = JSON.parse(this.area()?.getItem(this.key) ?? "null");
20
+ if (saved === null ||
21
+ typeof saved !== "object" ||
22
+ Array.isArray(saved) ||
23
+ fallback === null ||
24
+ typeof fallback !== "object") {
25
+ return fallback;
26
+ }
27
+ return { ...fallback, ...saved };
28
+ }
29
+ catch {
30
+ return fallback;
31
+ }
32
+ }
33
+ write(value) {
34
+ try {
35
+ this.area()?.setItem(this.key, JSON.stringify(value));
36
+ }
37
+ catch {
38
+ // Not remembered, then: the page works the same without it.
39
+ }
40
+ }
41
+ }
@@ -0,0 +1,142 @@
1
+ /**
2
+ * One embedded plugin, as plugin-slot speaks to it: every `ready` opens a new handshake and replays
3
+ * the mode, preferences, context and config, so a page its dev server reloads is set up again
4
+ * (ls-fe micro-apps/plugin-slot, IframeRenderer.tsx). No DOM here: the page wires it to an iframe.
5
+ */
6
+ import { MSG_CONFIG, MSG_CONTEXT, MSG_FULLSCREEN_CHANGED, MSG_MODE, MSG_PREFERENCES, MSG_READY, MSG_REQUEST_CONFIG_FORM, READY_GRACE_MS, resolveProtocolVersion, } from "@livespace/plugin-sdk";
7
+ const OBJECT_KEYS = ["deal", "company", "person", "space"];
8
+ /** Protocol 1 knows each object as `{ id, name }` only. */
9
+ export function contextFor(context, protocol) {
10
+ if (protocol >= 2) {
11
+ return context;
12
+ }
13
+ const shaped = { user: context.user };
14
+ for (const key of OBJECT_KEYS) {
15
+ const object = context[key];
16
+ if (object) {
17
+ Object.assign(shaped, { [key]: { id: object.id, name: object.name } });
18
+ }
19
+ }
20
+ return shaped;
21
+ }
22
+ export class Embed {
23
+ deps;
24
+ /** Counts handshakes; 0 until the first. */
25
+ handshake = 0;
26
+ protocol = 1;
27
+ readySinceLoad = false;
28
+ graceTimer;
29
+ /** What this handshake last sent, so an unchanged value isn't sent twice. */
30
+ sent = new Map();
31
+ setTimer;
32
+ clearTimer;
33
+ constructor(deps) {
34
+ this.deps = deps;
35
+ this.setTimer = deps.setTimer ?? ((run, ms) => setTimeout(run, ms));
36
+ this.clearTimer =
37
+ deps.clearTimer ??
38
+ ((timer) => clearTimeout(timer));
39
+ }
40
+ /** A message from the plugin, already checked to come from this embed's iframe. */
41
+ receive(data) {
42
+ const message = data;
43
+ if (!message || typeof message.type !== "string") {
44
+ return;
45
+ }
46
+ this.deps.log("in", message.type, message.payload);
47
+ if (message.type === MSG_READY) {
48
+ this.readySinceLoad = true;
49
+ this.clearGrace();
50
+ this.open(resolveProtocolVersion(message.payload));
51
+ }
52
+ }
53
+ /**
54
+ * The iframe's `load`. The SDK sends `ready` before it, so usually nothing to do; a page that
55
+ * loaded without one gets `READY_GRACE_MS`, then is spoken to as protocol 1.
56
+ */
57
+ loaded() {
58
+ if (this.readySinceLoad) {
59
+ this.readySinceLoad = false;
60
+ return;
61
+ }
62
+ this.handshake = 0;
63
+ this.clearGrace();
64
+ this.graceTimer = this.setTimer(() => {
65
+ this.graceTimer = undefined;
66
+ this.deps.log("note", `No ready ${READY_GRACE_MS} ms after load: speaking protocol 1, as Livespace does`);
67
+ this.open(1);
68
+ }, READY_GRACE_MS);
69
+ }
70
+ /** The host's state changed: send what changed, within the current handshake. */
71
+ update() {
72
+ if (this.handshake === 0) {
73
+ return;
74
+ }
75
+ const { mode, preferences, context, config } = this.deps.state();
76
+ this.send(MSG_PREFERENCES, { preferences }, true);
77
+ if (mode === "view") {
78
+ this.send(MSG_CONTEXT, contextFor(context, this.protocol), true);
79
+ }
80
+ if (mode !== "config") {
81
+ this.send(MSG_CONFIG, { config }, true);
82
+ }
83
+ }
84
+ /**
85
+ * The embed entered or left fullscreen. Sent at once, outside the per-handshake dedupe, as
86
+ * plugin-slot sends each edge. Before the first handshake there's no one to tell; the handshake
87
+ * says so if it's still fullscreen.
88
+ */
89
+ fullscreenChanged(active) {
90
+ if (this.handshake === 0) {
91
+ return;
92
+ }
93
+ this.deps.post({ type: MSG_FULLSCREEN_CHANGED, payload: { active } });
94
+ this.deps.log("out", MSG_FULLSCREEN_CHANGED, { active });
95
+ }
96
+ /** An answer to one of the plugin's requests, sent whatever the handshake, as plugin-slot does. */
97
+ reply(type, payload) {
98
+ this.deps.post({ type, payload });
99
+ this.deps.log("out", type, payload);
100
+ }
101
+ dispose() {
102
+ this.clearGrace();
103
+ }
104
+ open(protocol) {
105
+ this.handshake += 1;
106
+ this.protocol = protocol;
107
+ this.sent.clear();
108
+ this.deps.log("note", `Handshake ${this.handshake}, protocol ${protocol}`);
109
+ const { mode, preferences, context, config, fullscreen } = this.deps.state();
110
+ this.send(MSG_MODE, { mode }, false);
111
+ this.send(MSG_PREFERENCES, { preferences }, false);
112
+ // The settings screen's probe asks for the form, and is sent nothing else (plugins-2).
113
+ if (mode === "config") {
114
+ this.deps.post({ type: MSG_REQUEST_CONFIG_FORM });
115
+ this.deps.log("out", MSG_REQUEST_CONFIG_FORM);
116
+ return;
117
+ }
118
+ // A page reloaded while fullscreen stays fullscreen: tell the new document.
119
+ if (fullscreen) {
120
+ this.send(MSG_FULLSCREEN_CHANGED, { active: true }, false);
121
+ }
122
+ if (mode === "view") {
123
+ this.send(MSG_CONTEXT, contextFor(context, protocol), false);
124
+ }
125
+ this.send(MSG_CONFIG, { config }, false);
126
+ }
127
+ send(type, payload, onlyIfChanged) {
128
+ const json = JSON.stringify(payload);
129
+ if (onlyIfChanged && this.sent.get(type) === json) {
130
+ return;
131
+ }
132
+ this.sent.set(type, json);
133
+ this.deps.post({ type, payload });
134
+ this.deps.log("out", type, payload);
135
+ }
136
+ clearGrace() {
137
+ if (this.graceTimer !== undefined) {
138
+ this.clearTimer(this.graceTimer);
139
+ this.graceTimer = undefined;
140
+ }
141
+ }
142
+ }
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Where a plugin's key set is, as Livespace finds it: a catalog plugin's in its manifest (which the
3
+ * studio's server holds), else published on the plugin's own origin, fetched as plugins-2 does.
4
+ */
5
+ import { parseKeySet, readKeySet, } from "../domain/secrets.js";
6
+ /** How plugins-2 fetches a published key set: no cookies, no redirects, small, quick. */
7
+ const KEYS_TIMEOUT_MS = 5000;
8
+ const KEYS_MAX_BYTES = 16 * 1024;
9
+ export const KEYS_PATH = "/.well-known/livespace/jwks.json";
10
+ /** The key set: the manifest's for a catalog plugin, else the one published on its origin. */
11
+ export async function lookUpKeys(api, pluginOrigin, fetch = globalThis.fetch.bind(globalThis)) {
12
+ const fromServer = await api.keys();
13
+ if (fromServer.source === "manifest") {
14
+ const from = "plugin-manifest.json's publicKeys";
15
+ if (fromServer.keySet === null) {
16
+ return { status: "missing", from, why: "the manifest has no publicKeys" };
17
+ }
18
+ const result = readKeySet(fromServer.keySet);
19
+ return result.ok
20
+ ? { status: "found", from, keySet: result.keySet }
21
+ : { status: "unusable", from, issue: result.issue };
22
+ }
23
+ const from = pluginOrigin + KEYS_PATH;
24
+ const controller = new AbortController();
25
+ const timer = setTimeout(() => controller.abort(), KEYS_TIMEOUT_MS);
26
+ try {
27
+ const response = await fetch(from, {
28
+ credentials: "omit",
29
+ redirect: "error",
30
+ // "Check again" must read it again, not the browser's copy.
31
+ cache: "no-store",
32
+ headers: { Accept: "application/json" },
33
+ signal: controller.signal,
34
+ });
35
+ if (response.status === 404) {
36
+ return {
37
+ status: "missing",
38
+ from,
39
+ why: "the plugin's origin answered 404",
40
+ };
41
+ }
42
+ if (!response.ok) {
43
+ return {
44
+ status: "unreachable",
45
+ from,
46
+ why: `it answered ${response.status}`,
47
+ };
48
+ }
49
+ const text = await response.text();
50
+ if (text.length > KEYS_MAX_BYTES) {
51
+ return { status: "unusable", from, issue: "notKeySet" };
52
+ }
53
+ const result = parseKeySet(text);
54
+ return result.ok
55
+ ? { status: "found", from, keySet: result.keySet }
56
+ : { status: "unusable", from, issue: result.issue };
57
+ }
58
+ catch {
59
+ return {
60
+ status: "unreachable",
61
+ from,
62
+ why: "it couldn't be read: a network error, a redirect, a timeout, or no Access-Control-Allow-Origin: * (Livespace reads it from another origin)",
63
+ };
64
+ }
65
+ finally {
66
+ clearTimeout(timer);
67
+ }
68
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * The studio server's HTTP API, typed: everything the page keeps or asks for on the server. No
3
+ * signals and no DOM; the stores call it, and a test calls it with a fake `fetch`.
4
+ */
5
+ /** The server said no; `message` is its reason, fit to show. */
6
+ export class Refused extends Error {
7
+ status;
8
+ constructor(message, status) {
9
+ super(message);
10
+ this.status = status;
11
+ this.name = "Refused";
12
+ }
13
+ }
14
+ export class StudioApi {
15
+ fetch;
16
+ constructor(fetch = globalThis.fetch.bind(globalThis)) {
17
+ this.fetch = fetch;
18
+ }
19
+ plugin() {
20
+ return this.get("/api/plugin");
21
+ }
22
+ /** Changes some of the plugin's state; the server answers with the whole of it. */
23
+ changePlugin(changes) {
24
+ return this.send("PUT", "/api/plugin", changes);
25
+ }
26
+ /** The project's `plugin-manifest.json`, or `null` when it has none. */
27
+ async projectManifest() {
28
+ return (await this.get("/api/manifest")).manifest;
29
+ }
30
+ keys() {
31
+ return this.get("/api/keys");
32
+ }
33
+ /** The saved settings, `null` until saved. */
34
+ async settings() {
35
+ return (await this.get("/api/settings")).config;
36
+ }
37
+ async saveSettings(config) {
38
+ return (await this.send("PUT", "/api/settings", { config })).config;
39
+ }
40
+ /** Which secrets are stored, and for which key: never their values. */
41
+ async secrets() {
42
+ return (await this.get("/api/secrets")).stored;
43
+ }
44
+ /** Sealed secrets by field key; `null` clears one. */
45
+ async storeSecrets(changes) {
46
+ return (await this.send("PUT", "/api/secrets", {
47
+ changes,
48
+ })).stored;
49
+ }
50
+ /** A plugin token for the viewer, from the dev account; already expired, when asked. */
51
+ sessionToken(sub, expired) {
52
+ return this.send("POST", "/api/session-token", { sub, expired });
53
+ }
54
+ /** How the dev account's token exchange refuses, or `null` to accept. */
55
+ async refuseExchanges(code) {
56
+ await this.send("PUT", "/api/failures", { exchange: code });
57
+ }
58
+ async get(path) {
59
+ return this.read(await this.fetch(path));
60
+ }
61
+ async send(method, path, body) {
62
+ return this.read(await this.fetch(path, { method, body: JSON.stringify(body) }));
63
+ }
64
+ async read(response) {
65
+ const body = (await response.json().catch(() => null));
66
+ if (!response.ok) {
67
+ const error = body?.error;
68
+ throw new Refused(typeof error === "string"
69
+ ? error
70
+ : `The studio refused it (${response.status})`, response.status);
71
+ }
72
+ return body;
73
+ }
74
+ }
@@ -0,0 +1,61 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "preact/jsx-runtime";
2
+ /**
3
+ * The studio's frame: the top bar, the resizable sidebar of sections, the main area with the page
4
+ * shown, the resizable messages pane, and the toasts.
5
+ */
6
+ import { useRef } from "preact/hooks";
7
+ import { HANDLE_PX, MIN_MESSAGES_PX, MIN_SIDEBAR_PX, } from "../domain/layout-store.js";
8
+ import { useStudio } from "./context.js";
9
+ import { Messages } from "./messages.js";
10
+ import { PageSwitcher } from "./page-switcher.js";
11
+ import { PluginFrame } from "./plugin-frame.js";
12
+ import { ProfilePage } from "./profile-page.js";
13
+ import { ResizeHandle } from "./resize-handle.js";
14
+ import { Section } from "./section.js";
15
+ import { SettingsPage } from "./settings/settings-page.js";
16
+ import { SidebarGroup } from "./sidebar-group.js";
17
+ import { CustomSettingsSection } from "./sidebar/custom-settings-section.js";
18
+ import { FailuresSection } from "./sidebar/failures-section.js";
19
+ import { InstallationSection } from "./sidebar/installation-section.js";
20
+ import { MetadataSection } from "./sidebar/metadata-section.js";
21
+ import { PluginSection } from "./sidebar/plugin-section.js";
22
+ import { PreferencesSection } from "./sidebar/preferences-section.js";
23
+ import { RecordSection } from "./sidebar/record-section.js";
24
+ import { ViewerSection } from "./sidebar/viewer-section.js";
25
+ import { Toasts } from "./toasts.js";
26
+ import { TopBar } from "./top-bar.js";
27
+ /** Why the settings page can't use a section: its preview gets less than the profile page does. */
28
+ const UNUSED_IN_SETTINGS = {
29
+ record: "Not used on the settings page: Livespace previews the plugin with no record.",
30
+ viewer: "Not used on the settings page: the preview has no viewer (context.user is null).",
31
+ failures: "Not used on the settings page: the preview is given no credentials or tokens, so none can fail.",
32
+ };
33
+ /**
34
+ * The plugin and what Livespace gives it, grouped: what it declares about itself, what's stored for
35
+ * it, the context it receives, and the failures to answer with. The same on both pages; those the
36
+ * settings page can't use are marked.
37
+ */
38
+ function Sidebar() {
39
+ const { navigation } = useStudio();
40
+ const unused = navigation.page.value === "settings" ? UNUSED_IN_SETTINGS : undefined;
41
+ return (_jsxs("aside", { class: "sidebar", "aria-label": "The plugin and what Livespace sends", children: [_jsxs(SidebarGroup, { title: "Plugin information", children: [_jsx(Section, { name: "plugin", title: "Identity", initiallyOpen: true, children: _jsx(PluginSection, {}) }), _jsx(Section, { name: "metadata", title: "Metadata", children: _jsx(MetadataSection, {}) })] }), _jsxs(SidebarGroup, { title: "Stored settings", children: [_jsx(Section, { name: "installation", title: "Installation", initiallyOpen: true, children: _jsx(InstallationSection, {}) }), _jsx(Section, { name: "custom-settings", title: "Custom settings", children: _jsx(CustomSettingsSection, {}) })] }), _jsxs(SidebarGroup, { title: "Context", children: [_jsx(Section, { name: "record", title: "Profile", initiallyOpen: true, unused: unused?.record, children: _jsx(RecordSection, {}) }), _jsx(Section, { name: "viewer", title: "Viewer", initiallyOpen: true, unused: unused?.viewer, children: _jsx(ViewerSection, {}) }), _jsx(Section, { name: "preferences", title: "Preferences", children: _jsx(PreferencesSection, {}) })] }), _jsx(SidebarGroup, { title: "Error handling", children: _jsx(Section, { name: "failures", title: "Switches", unused: unused?.failures, children: _jsx(FailuresSection, {}) }) })] }));
42
+ }
43
+ /** The page shown, with only its own embeds, once there's an address to embed. */
44
+ function Main() {
45
+ const { navigation, plugin, probe } = useStudio();
46
+ const url = plugin.pluginUrl.value;
47
+ // A reload makes the page anew: its iframes, its session and its handshakes.
48
+ const reloads = plugin.reloads.value;
49
+ return (_jsxs("main", { class: "main", children: [_jsx(PageSwitcher, {}), _jsx("div", { class: "main-scroll", children: url === null ? (_jsx("p", { class: "empty", children: "Enter your plugin's address above: any http or https page, a deployed one included." })) : navigation.page.value === "profile" ? (_jsx(ProfilePage, { url: url }, `profile ${url} ${reloads}`)) : (_jsx(SettingsPage, { url: url }, `settings ${url} ${reloads}`)) }), url === null ? null : (_jsx("div", { class: "probe-host", "aria-hidden": "true", children: _jsx(PluginFrame, { url: url, title: "Plugin config probe", connect: probe.connect }, `probe ${url} ${reloads}`) }))] }));
50
+ }
51
+ export function App() {
52
+ const { layout } = useStudio();
53
+ const workspace = useRef(null);
54
+ const shown = layout.messagesShown.value;
55
+ const bounds = () => workspace.current?.getBoundingClientRect();
56
+ return (_jsxs(_Fragment, { children: [_jsx(TopBar, {}), _jsxs("div", { ref: workspace, class: "workspace", "data-messages": shown ? "shown" : "hidden", style: {
57
+ "--sidebar-width": `${layout.sidebarWidth.value}px`,
58
+ "--messages-width": `${layout.messagesWidth.value}px`,
59
+ "--handle-width": `${HANDLE_PX}px`,
60
+ }, children: [_jsx(Sidebar, {}), _jsx(ResizeHandle, { label: "Resize the sidebar", width: layout.sidebarWidth.value, min: MIN_SIDEBAR_PX, widthAt: (x) => x - (bounds()?.left ?? 0), resize: layout.resizeSidebar, grow: "ArrowRight", step: layout.stepSidebar }), _jsx(Main, {}), shown ? (_jsxs(_Fragment, { children: [_jsx(ResizeHandle, { label: "Resize the messages", controls: "messages", width: layout.messagesWidth.value, min: MIN_MESSAGES_PX, widthAt: (x) => (bounds()?.right ?? 0) - x, resize: layout.resizeMessages, grow: "ArrowLeft", step: layout.stepMessages }), _jsx(Messages, {})] })) : null] }), _jsx(Toasts, {})] }));
61
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Starting the page: the splash shows until the studio has what it needs and the app is drawn as
3
+ * it will stay, its Web Awesome elements defined and rendered. A step that must finish before the
4
+ * studio shows goes in `boot`.
5
+ */
6
+ import { signal } from "@preact/signals";
7
+ /** How long the splash waits for the elements before it gives way all the same. */
8
+ export const DRAW_TIMEOUT_MS = 5000;
9
+ const nextFrame = () => new Promise((resolve) => requestAnimationFrame(() => resolve()));
10
+ /**
11
+ * Waits until every custom element under `root` is defined and has rendered once. An element is
12
+ * undefined until Web Awesome's loader fetches it, and shows its light DOM unstyled until then: a
13
+ * collapsed section, open.
14
+ */
15
+ async function drawn(root) {
16
+ // Preact has drawn by now; the loader sees the new elements on the next frame.
17
+ await nextFrame();
18
+ const undefinedTags = new Set([...root.querySelectorAll(":not(:defined)")].map((el) => el.localName));
19
+ await Promise.all([...undefinedTags].map((tag) => customElements.whenDefined(tag)));
20
+ // Defined, Web Awesome's elements render in a microtask: each says when.
21
+ await Promise.all([...root.querySelectorAll("*")]
22
+ .map((el) => el.updateComplete)
23
+ .filter((done) => done !== undefined));
24
+ await nextFrame();
25
+ }
26
+ export function boot(studio, root) {
27
+ const phase = signal({ kind: "starting" });
28
+ void (async () => {
29
+ try {
30
+ await studio.start();
31
+ }
32
+ catch (error) {
33
+ phase.value = { kind: "failed", reason: String(error) };
34
+ return;
35
+ }
36
+ phase.value = { kind: "drawing" };
37
+ await Promise.race([
38
+ drawn(root),
39
+ new Promise((resolve) => setTimeout(resolve, DRAW_TIMEOUT_MS)),
40
+ ]);
41
+ phase.value = { kind: "ready" };
42
+ })();
43
+ return phase;
44
+ }
@@ -0,0 +1,3 @@
1
+ /** The studio's name and logo, as the top bar and the splash show them. */
2
+ export const STUDIO_NAME = "LS Plugin Studio";
3
+ export const STUDIO_LOGO = "/plugin-studio-logo.svg";
@@ -0,0 +1,11 @@
1
+ /** How components reach the root store. */
2
+ import { createContext } from "preact";
3
+ import { useContext } from "preact/hooks";
4
+ export const StudioContext = createContext(null);
5
+ export function useStudio() {
6
+ const studio = useContext(StudioContext);
7
+ if (studio === null) {
8
+ throw new Error("useStudio() outside <StudioContext.Provider>");
9
+ }
10
+ return studio;
11
+ }
@@ -0,0 +1,19 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "preact/jsx-runtime";
2
+ /**
3
+ * Editable JSON in the sidebar, coloured as it's typed: a native text area with transparent text,
4
+ * over a coloured copy of the same text that sets its height. No editor library. It grows to fit up
5
+ * to ten lines, then clamps with a fade and "Show more"; focusing it opens it, so editing never
6
+ * happens behind the fade.
7
+ */
8
+ import { useId, useState } from "preact/hooks";
9
+ import { JsonText } from "./json-view.js";
10
+ export const CLAMP_LINES = 10;
11
+ export function JsonField({ label, draft, edit, describedBy }) {
12
+ const id = useId();
13
+ const [open, setOpen] = useState(false);
14
+ const text = draft.text.value;
15
+ const tall = text.split("\n").length > CLAMP_LINES;
16
+ const clamped = tall && !open;
17
+ const invalid = !draft.parsed.value.ok;
18
+ return (_jsxs("div", { class: "json-field wa-stack wa-gap-3xs", children: [_jsx("label", { for: id, class: "field-label", children: label }), _jsxs("div", { class: "json-editor", "data-clamped": clamped, "data-invalid": invalid, children: [_jsxs("pre", { class: "json-backdrop", "aria-hidden": "true", children: [_jsx(JsonText, { text: text }), "\n "] }), _jsx("textarea", { id: id, class: "json-input", value: text, spellcheck: false, autocomplete: "off", "aria-invalid": invalid, "aria-describedby": describedBy, onInput: (event) => (edit ?? draft.edit)(event.target.value), onFocus: () => setOpen(true) })] }), tall ? (_jsx("button", { type: "button", class: "link", "aria-expanded": open, onClick: () => setOpen(!open), children: open ? "Show less" : "Show more" })) : null] }));
19
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * JSON split into coloured pieces, for display. It reads text as typed too, unfinished or wrong:
3
+ * what it can't place is plain text. No DOM here.
4
+ */
5
+ /** A string (closed or not), whatever follows it up to a colon, a number, a literal, punctuation. */
6
+ const TOKEN = /("(?:\\.|[^"\\\n])*"?)(\s*:)?|(-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)|\b(true|false|null)\b|([{}[\],:])/g;
7
+ export function jsonTokens(text) {
8
+ const tokens = [];
9
+ const plain = (from, to) => {
10
+ if (to > from) {
11
+ tokens.push({ kind: "plain", text: text.slice(from, to) });
12
+ }
13
+ };
14
+ let last = 0;
15
+ for (const match of text.matchAll(TOKEN)) {
16
+ const at = match.index ?? 0;
17
+ plain(last, at);
18
+ const [whole, string, colon, number, literal] = match;
19
+ if (string !== undefined) {
20
+ // A string followed by a colon is a key; the colon is punctuation.
21
+ tokens.push({ kind: colon ? "key" : "string", text: string });
22
+ if (colon) {
23
+ plain(at + string.length, at + whole.length - 1);
24
+ tokens.push({ kind: "punctuation", text: ":" });
25
+ }
26
+ }
27
+ else if (number !== undefined) {
28
+ tokens.push({ kind: "number", text: number });
29
+ }
30
+ else if (literal !== undefined) {
31
+ tokens.push({ kind: "literal", text: literal });
32
+ }
33
+ else {
34
+ tokens.push({ kind: "punctuation", text: whole });
35
+ }
36
+ last = at + whole.length;
37
+ }
38
+ plain(last, text.length);
39
+ return tokens;
40
+ }
@@ -0,0 +1,11 @@
1
+ import { jsx as _jsx, Fragment as _Fragment } from "preact/jsx-runtime";
2
+ /** JSON, coloured: for what the studio shows but doesn't let you edit. */
3
+ import { jsonTokens } from "./json-tokens.js";
4
+ /** The coloured spans for some JSON text. */
5
+ export function JsonText({ text }) {
6
+ return (_jsx(_Fragment, { children: jsonTokens(text).map((token, index) => token.kind === "plain" ? (token.text) : (_jsx("span", { class: `json-${token.kind}`, children: token.text }, index))) }));
7
+ }
8
+ /** A value as pretty-printed, coloured JSON. */
9
+ export function JsonView({ value, class: className, }) {
10
+ return (_jsx("pre", { class: className ? `json-view ${className}` : "json-view", children: _jsx(JsonText, { text: JSON.stringify(value, null, 2) ?? "undefined" }) }));
11
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Components that re-render only when their props change. Preact's own `memo` lives in
3
+ * `preact/compat`, which the page doesn't load.
4
+ */
5
+ import { Component, } from "preact";
6
+ const sameProps = (a, b) => {
7
+ const keys = Object.keys(a);
8
+ return (keys.length === Object.keys(b).length &&
9
+ keys.every((key) => a[key] ===
10
+ b[key]));
11
+ };
12
+ /** `render`, re-run only when a prop changes identity. */
13
+ export function memo(render) {
14
+ class Memo extends Component {
15
+ shouldComponentUpdate(next) {
16
+ return !sameProps(this.props, next);
17
+ }
18
+ render(props) {
19
+ return render(props);
20
+ }
21
+ }
22
+ return Memo;
23
+ }