@livespace/plugin-studio 0.0.0-stage → 0.2.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 (78) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +122 -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/address.js +79 -0
  8. package/dist/page/domain/connection.js +2 -0
  9. package/dist/page/domain/display.js +38 -0
  10. package/dist/page/domain/draft.js +80 -0
  11. package/dist/page/domain/failures-store.js +41 -0
  12. package/dist/page/domain/failures.js +75 -0
  13. package/dist/page/domain/layout-store.js +119 -0
  14. package/dist/page/domain/log-store.js +37 -0
  15. package/dist/page/domain/navigation-store.js +17 -0
  16. package/dist/page/domain/plugin-data.js +70 -0
  17. package/dist/page/domain/plugin-store.js +257 -0
  18. package/dist/page/domain/probe-store.js +100 -0
  19. package/dist/page/domain/profile-session.js +115 -0
  20. package/dist/page/domain/profile-store.js +51 -0
  21. package/dist/page/domain/samples.js +221 -0
  22. package/dist/page/domain/secrets-store.js +159 -0
  23. package/dist/page/domain/secrets.js +135 -0
  24. package/dist/page/domain/settings-data.js +64 -0
  25. package/dist/page/domain/settings-session.js +206 -0
  26. package/dist/page/domain/settings-store.js +62 -0
  27. package/dist/page/domain/slot-store.js +50 -0
  28. package/dist/page/domain/studio.js +162 -0
  29. package/dist/page/domain/surface-store.js +71 -0
  30. package/dist/page/domain/toasts-store.js +60 -0
  31. package/dist/page/domain/view-data.js +122 -0
  32. package/dist/page/domain/view-store.js +132 -0
  33. package/dist/page/host.css +907 -0
  34. package/dist/page/index.html +31 -0
  35. package/dist/page/main.js +43 -0
  36. package/dist/page/plugin-studio-logo.svg +4 -0
  37. package/dist/page/reload.svg +1 -0
  38. package/dist/page/transport/browser-storage.js +41 -0
  39. package/dist/page/transport/console-mirror.js +48 -0
  40. package/dist/page/transport/embed.js +142 -0
  41. package/dist/page/transport/plugin-keys.js +68 -0
  42. package/dist/page/transport/studio-api.js +74 -0
  43. package/dist/page/ui/app.js +61 -0
  44. package/dist/page/ui/boot.js +44 -0
  45. package/dist/page/ui/brand.js +3 -0
  46. package/dist/page/ui/context.js +11 -0
  47. package/dist/page/ui/json-field.js +19 -0
  48. package/dist/page/ui/json-tokens.js +40 -0
  49. package/dist/page/ui/json-view.js +11 -0
  50. package/dist/page/ui/memo.js +23 -0
  51. package/dist/page/ui/messages.js +34 -0
  52. package/dist/page/ui/page-switcher.js +11 -0
  53. package/dist/page/ui/plugin-frame.js +43 -0
  54. package/dist/page/ui/profile-page.js +13 -0
  55. package/dist/page/ui/resize-handle.js +63 -0
  56. package/dist/page/ui/root.js +9 -0
  57. package/dist/page/ui/section.js +19 -0
  58. package/dist/page/ui/settings/config-form.js +59 -0
  59. package/dist/page/ui/settings/secret-field.js +22 -0
  60. package/dist/page/ui/settings/settings-page.js +60 -0
  61. package/dist/page/ui/sidebar/custom-settings-section.js +10 -0
  62. package/dist/page/ui/sidebar/failures-section.js +27 -0
  63. package/dist/page/ui/sidebar/installation-section.js +30 -0
  64. package/dist/page/ui/sidebar/metadata-section.js +9 -0
  65. package/dist/page/ui/sidebar/plugin-section.js +18 -0
  66. package/dist/page/ui/sidebar/preferences-section.js +16 -0
  67. package/dist/page/ui/sidebar/record-section.js +17 -0
  68. package/dist/page/ui/sidebar/viewer-section.js +8 -0
  69. package/dist/page/ui/sidebar-group.js +4 -0
  70. package/dist/page/ui/splash.js +6 -0
  71. package/dist/page/ui/status.js +10 -0
  72. package/dist/page/ui/surface.js +23 -0
  73. package/dist/page/ui/theme.js +13 -0
  74. package/dist/page/ui/toasts.js +31 -0
  75. package/dist/page/ui/top-bar.js +37 -0
  76. package/dist/server.js +519 -0
  77. package/dist/start.js +108 -0
  78. package/package.json +55 -5
@@ -0,0 +1,206 @@
1
+ /**
2
+ * One visit to the settings page, as the plugins-2 settings screen does it: the form the config
3
+ * probe heard, a visible preview that shows the plugin with what's typed, and Save, which stores the
4
+ * config the profile page then sends, sealing any secrets first.
5
+ */
6
+ import { MSG_MODAL_SIZE, isSecretField, isValueField, validateConfigValues, } from "@livespace/plugin-sdk";
7
+ import { batch, computed, effect, signal, } from "@preact/signals";
8
+ import { Embed } from "../transport/embed.js";
9
+ import { effectiveValues, previewGeometry, valueFrom } from "./settings-data.js";
10
+ import { SurfaceStore } from "./surface-store.js";
11
+ export class SettingsSession {
12
+ deps;
13
+ #draft = signal({});
14
+ #errors = signal(null);
15
+ #saveStatus;
16
+ #stops = [];
17
+ #states = new Map();
18
+ /** The preview's surface: inline in its pane, or in the dialog or fullscreen. */
19
+ surface;
20
+ /** What the form shows and the preview gets: defaults, then saved, then typed. */
21
+ values;
22
+ valueFields;
23
+ secretFields;
24
+ /** The preview in its slot: at the plugin's minimum size when bigger, scaled to fit. */
25
+ geometry;
26
+ constructor(deps) {
27
+ this.deps = deps;
28
+ this.#saveStatus = signal(deps.settings.saved.peek() === null
29
+ ? {
30
+ text: "Never saved: the profile page sends config null.",
31
+ isError: false,
32
+ }
33
+ : null);
34
+ this.values = computed(() => {
35
+ const form = deps.probe.form.value;
36
+ return form === null
37
+ ? {}
38
+ : effectiveValues(form, deps.settings.saved.value, this.#draft.value);
39
+ });
40
+ this.valueFields = computed(() => deps.probe.form.value?.fields.filter(isValueField) ?? []);
41
+ this.secretFields = computed(() => deps.probe.form.value?.fields.filter(isSecretField) ?? []);
42
+ this.geometry = computed(() => previewGeometry(deps.metadata.metadata.value, deps.slot.size.value));
43
+ this.surface = new SurfaceStore({
44
+ log: deps.log.append,
45
+ source: "preview",
46
+ }, computed(() => {
47
+ const { height, scale } = this.geometry.value;
48
+ return { width: deps.slot.size.value.width, height: height * scale };
49
+ }));
50
+ // The keys matter only once there's a secret to seal.
51
+ this.#stops.push(effect(() => {
52
+ if (this.secretFields.value.length > 0 && !deps.secrets.checked) {
53
+ void deps.secrets.checkKeys();
54
+ }
55
+ }));
56
+ // Forgotten, here or in the sidebar: what was typed goes with it.
57
+ let saved = deps.settings.saved.peek();
58
+ this.#stops.push(effect(() => {
59
+ const now = deps.settings.saved.value;
60
+ const forgotten = saved !== null && now === null;
61
+ saved = now;
62
+ if (forgotten) {
63
+ batch(() => {
64
+ this.#draft.value = {};
65
+ this.#errors.value = null;
66
+ this.#saveStatus.value = {
67
+ text: "Forgotten: the plugin is never configured again (config null).",
68
+ isError: false,
69
+ };
70
+ });
71
+ }
72
+ }));
73
+ void deps.secrets.loadStored();
74
+ }
75
+ /** The secret fields' keys, stored secrets and pending changes. */
76
+ get secrets() {
77
+ return this.deps.secrets;
78
+ }
79
+ get form() {
80
+ return this.deps.probe.form;
81
+ }
82
+ get formStatus() {
83
+ return this.deps.probe.formStatus;
84
+ }
85
+ get metadata() {
86
+ return this.deps.metadata.metadata;
87
+ }
88
+ get errors() {
89
+ return this.#errors;
90
+ }
91
+ get saveStatus() {
92
+ return this.#saveStatus;
93
+ }
94
+ /** The visible preview: preview mode, no record, and the settings as typed. */
95
+ connectPreview = (post) => {
96
+ const embed = this.#embed(post, "preview", () => ({
97
+ mode: "preview",
98
+ preferences: this.deps.view.preferences.value,
99
+ context: { user: null },
100
+ config: this.values.value,
101
+ fullscreen: this.surface.display.value === "fullscreen",
102
+ }));
103
+ // Fullscreen's edges, as the settings screen sends them.
104
+ let fullscreen = this.surface.display.peek() === "fullscreen";
105
+ const stop = effect(() => {
106
+ const now = this.surface.display.value === "fullscreen";
107
+ if (now !== fullscreen) {
108
+ fullscreen = now;
109
+ embed.fullscreenChanged(now);
110
+ }
111
+ });
112
+ const connection = this.#connection(embed, (type, payload) => {
113
+ if (type === MSG_MODAL_SIZE) {
114
+ this.surface.modalSize(payload);
115
+ }
116
+ });
117
+ return {
118
+ ...connection,
119
+ dispose: () => {
120
+ stop();
121
+ connection.dispose();
122
+ },
123
+ };
124
+ };
125
+ #embed(post, source, state) {
126
+ const embed = new Embed({
127
+ post,
128
+ state,
129
+ log: (direction, type, payload) => this.deps.log.append(direction, type, payload, source),
130
+ });
131
+ this.#states.set(embed, state);
132
+ return embed;
133
+ }
134
+ /** Wires an embed: messages in, and updates out as what it's sent changes. */
135
+ #connection(embed, handle) {
136
+ const state = this.#states.get(embed);
137
+ // Every change reaches it, every keystroke included, as on the settings screen. The state is
138
+ // read first, so the effect follows it even before the first handshake.
139
+ const stop = effect(() => {
140
+ state?.();
141
+ embed.update();
142
+ });
143
+ return {
144
+ receive: (data) => {
145
+ embed.receive(data);
146
+ const { type, payload } = (data ?? {});
147
+ handle(type, payload);
148
+ },
149
+ loaded: () => embed.loaded(),
150
+ dispose: () => {
151
+ stop();
152
+ embed.dispose();
153
+ },
154
+ };
155
+ }
156
+ /** A field's typed value: text, or a checkbox's state. */
157
+ setValue = (field, raw) => {
158
+ this.#draft.value = {
159
+ ...this.#draft.value,
160
+ [field.key]: valueFrom(field, raw),
161
+ };
162
+ };
163
+ /** Checks the form as Livespace does, seals the secrets, and saves the settings. */
164
+ save = async () => {
165
+ const form = this.deps.probe.form.value;
166
+ if (form === null) {
167
+ return;
168
+ }
169
+ const config = this.values.value;
170
+ const errors = validateConfigValues(form.fields, config);
171
+ const secretsValid = this.deps.secrets.validate(this.secretFields.value);
172
+ this.#errors.value = errors;
173
+ if (errors || !secretsValid) {
174
+ this.#saveStatus.value = {
175
+ text: "Not saved: fix the fields marked above.",
176
+ isError: true,
177
+ };
178
+ return;
179
+ }
180
+ try {
181
+ // Sealed here, in the browser, before anything leaves the page.
182
+ const sealed = await this.deps.secrets.save(this.secretFields.value, this.deps.iss);
183
+ const hasValues = this.valueFields.value.length > 0;
184
+ if (hasValues) {
185
+ await this.deps.settings.save(config);
186
+ this.#draft.value = {};
187
+ }
188
+ this.#saveStatus.value = {
189
+ text: `Saved.${hasValues ? " The profile page sends the settings as config." : ""}${sealed > 0 ? ` ${sealed} secret${sealed === 1 ? "" : "s"} sealed for the plugin's backend.` : ""}`,
190
+ isError: false,
191
+ };
192
+ }
193
+ catch (error) {
194
+ this.#saveStatus.value = {
195
+ text: `Not saved: ${error.message}`,
196
+ isError: true,
197
+ };
198
+ }
199
+ };
200
+ dispose() {
201
+ for (const stop of this.#stops) {
202
+ stop();
203
+ }
204
+ this.surface.reset();
205
+ }
206
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * The plugin's saved settings: what the settings page saves, and the profile page sends, and which
3
+ * of its secrets are stored. Each visit to the settings page is a `SettingsSession`.
4
+ */
5
+ import { signal } from "@preact/signals";
6
+ import { lookUpKeys } from "../transport/plugin-keys.js";
7
+ import { SecretsStore } from "./secrets-store.js";
8
+ import { SettingsSession } from "./settings-session.js";
9
+ export class SettingsStore {
10
+ deps;
11
+ /** `null` until saved, as in Livespace. */
12
+ #saved = signal(null);
13
+ /** Shared with each visit's `SecretsStore`, which stores them. */
14
+ #secrets = signal({});
15
+ constructor(deps) {
16
+ this.deps = deps;
17
+ }
18
+ get saved() {
19
+ return this.#saved;
20
+ }
21
+ /** Which secrets are stored, and for which key: never their values. */
22
+ get secrets() {
23
+ return this.#secrets;
24
+ }
25
+ async load() {
26
+ const [saved, secrets] = await Promise.all([
27
+ this.deps.api.settings(),
28
+ this.deps.api.secrets(),
29
+ ]);
30
+ this.#saved.value = saved;
31
+ this.#secrets.value = secrets;
32
+ }
33
+ /** Saves a config, or `null` to forget it; refused by the server as a `Refused`. */
34
+ save = async (config) => {
35
+ this.#saved.value = await this.deps.api.saveSettings(config);
36
+ };
37
+ /** Forgets the saved settings: the plugin is never configured again (config null). */
38
+ forget = async () => {
39
+ await this.save(null);
40
+ this.deps.log.append("note", "Settings forgotten: config null");
41
+ };
42
+ /** A visit to the settings page for the plugin on `pluginOrigin`. */
43
+ open = (pluginOrigin) => {
44
+ const { api } = this.deps;
45
+ const lookUp = this.deps.lookUp ?? lookUpKeys;
46
+ return new SettingsSession({
47
+ log: this.deps.log,
48
+ toasts: this.deps.toasts,
49
+ view: this.deps.view,
50
+ settings: this,
51
+ secrets: new SecretsStore({
52
+ api,
53
+ lookUp: () => lookUp(api, pluginOrigin),
54
+ stored: this.#secrets,
55
+ }),
56
+ metadata: this.deps.metadata,
57
+ probe: this.deps.probe,
58
+ slot: this.deps.slot,
59
+ iss: this.deps.studioOrigin,
60
+ });
61
+ };
62
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * The slot the plugin is installed in: its column's width, which the slot decides, and the
3
+ * height it's shown at. The profile page and the settings preview both show it there. Kept in this
4
+ * browser.
5
+ */
6
+ import { effect, signal, } from "@preact/signals";
7
+ import { DEFAULT_HEIGHT_PX } from "./display.js";
8
+ /** The shortest a plugin may be inline. */
9
+ export const MIN_HEIGHT_PX = 40;
10
+ export const MAX_HEIGHT_PX = 2000;
11
+ export const DEFAULT_SLOT = {
12
+ width: 420,
13
+ height: DEFAULT_HEIGHT_PX,
14
+ };
15
+ export class SlotStore {
16
+ #size;
17
+ #stopSaving;
18
+ constructor(storage) {
19
+ const saved = storage.read(DEFAULT_SLOT);
20
+ this.#size = signal({
21
+ width: saved.width === null || typeof saved.width === "number"
22
+ ? saved.width
23
+ : DEFAULT_SLOT.width,
24
+ height: SlotStore.#height(saved.height),
25
+ });
26
+ this.#stopSaving = effect(() => storage.write(this.#size.value));
27
+ }
28
+ static #height(value) {
29
+ const height = Number(value);
30
+ return Number.isFinite(height) && height >= MIN_HEIGHT_PX
31
+ ? Math.min(height, MAX_HEIGHT_PX)
32
+ : DEFAULT_HEIGHT_PX;
33
+ }
34
+ get size() {
35
+ return this.#size;
36
+ }
37
+ setWidth = (width) => {
38
+ this.#size.value = { ...this.#size.value, width };
39
+ };
40
+ /** A height that isn't one, or is too short, is the default. */
41
+ setHeight = (height) => {
42
+ this.#size.value = {
43
+ ...this.#size.value,
44
+ height: SlotStore.#height(height),
45
+ };
46
+ };
47
+ dispose() {
48
+ this.#stopSaving();
49
+ }
50
+ }
@@ -0,0 +1,162 @@
1
+ /**
2
+ * The root store: the page's state, made of the stores each area reads, built once with the
3
+ * transport it needs. Components reach it through `StudioContext` (ui/context.ts).
4
+ */
5
+ import { BrowserStorage } from "../transport/browser-storage.js";
6
+ import { readAddress } from "./address.js";
7
+ import { timers } from "./draft.js";
8
+ import { FailuresStore } from "./failures-store.js";
9
+ import { LayoutStore } from "./layout-store.js";
10
+ import { LogStore } from "./log-store.js";
11
+ import { NavigationStore } from "./navigation-store.js";
12
+ import { PluginStore } from "./plugin-store.js";
13
+ import { ProbeStore } from "./probe-store.js";
14
+ import { ProfileStore } from "./profile-store.js";
15
+ import { SettingsStore } from "./settings-store.js";
16
+ import { SlotStore } from "./slot-store.js";
17
+ import { ToastsStore } from "./toasts-store.js";
18
+ import { ViewStore } from "./view-store.js";
19
+ /** The browser storage key prefix; each store's key adds its own name. */
20
+ export const STORAGE_PREFIX = "livespace-plugin-studio";
21
+ export class Studio {
22
+ api;
23
+ log;
24
+ toasts;
25
+ layout;
26
+ navigation;
27
+ plugin;
28
+ view;
29
+ failures;
30
+ slot;
31
+ probe;
32
+ settings;
33
+ profile;
34
+ constructor(deps) {
35
+ const storage = deps.storage ?? ((key) => new BrowserStorage(key));
36
+ const stored = (name) => storage(`${STORAGE_PREFIX}-${name}`);
37
+ this.api = deps.api;
38
+ this.log = new LogStore(deps.now);
39
+ this.toasts = new ToastsStore({ log: this.log.append });
40
+ this.layout = new LayoutStore(stored("layout"), deps.viewportWidth);
41
+ this.navigation = new NavigationStore(deps.hash);
42
+ this.plugin = new PluginStore({
43
+ api: deps.api,
44
+ storage: stored("address"),
45
+ studioOrigin: deps.origin,
46
+ log: this.log.append,
47
+ scheduler: deps.scheduler,
48
+ });
49
+ this.view = new ViewStore({
50
+ storage: stored("view"),
51
+ choices: this.plugin.recordChoices,
52
+ scheduler: deps.scheduler,
53
+ });
54
+ this.failures = new FailuresStore({
55
+ api: deps.api,
56
+ storage: stored("failures"),
57
+ log: this.log.append,
58
+ });
59
+ // The column key predates the slot: kept, so the size saved here stays.
60
+ this.slot = new SlotStore(stored("column"));
61
+ this.probe = new ProbeStore({
62
+ log: this.log,
63
+ view: this.view,
64
+ metadata: this.plugin,
65
+ scheduler: deps.scheduler ?? timers,
66
+ });
67
+ this.settings = new SettingsStore({
68
+ api: deps.api,
69
+ log: this.log,
70
+ toasts: this.toasts,
71
+ view: this.view,
72
+ studioOrigin: deps.origin,
73
+ metadata: this.plugin,
74
+ slot: this.slot,
75
+ probe: this.probe,
76
+ });
77
+ this.profile = new ProfileStore({
78
+ api: deps.api,
79
+ slot: this.slot,
80
+ log: this.log,
81
+ toasts: this.toasts,
82
+ view: this.view,
83
+ settings: this.settings,
84
+ failures: this.failures,
85
+ });
86
+ }
87
+ /**
88
+ * The address changed, or the page opened on it: the page follows its hash, and what its params
89
+ * ask for is set as if chosen in the sidebar, with a note in the log. Returns the hash to leave
90
+ * in the address, without the params, so a reload never sets them again.
91
+ */
92
+ followAddress = (hash) => {
93
+ const { page, changes, applied, ignored, clean } = readAddress(hash);
94
+ this.navigation.follow(page === "settings" ? "#settings" : "");
95
+ if (changes.type !== undefined) {
96
+ this.view.choose(changes.type);
97
+ }
98
+ if (changes.viewer !== undefined) {
99
+ this.view.viewer.set(changes.viewer);
100
+ }
101
+ if (changes.preferences !== undefined) {
102
+ this.view.choosePreset(changes.preferences);
103
+ }
104
+ if (changes.width !== undefined) {
105
+ this.slot.setWidth(changes.width);
106
+ }
107
+ if (changes.height !== undefined) {
108
+ this.slot.setHeight(changes.height);
109
+ }
110
+ if (changes.display !== undefined) {
111
+ this.profile.show(changes.display);
112
+ }
113
+ if (changes.credentials !== undefined) {
114
+ void this.failures.set("credentials", changes.credentials);
115
+ }
116
+ if (changes.token !== undefined) {
117
+ void this.failures.set("token", changes.token);
118
+ }
119
+ if (applied.length > 0) {
120
+ this.log.append("note", `From the address: ${applied.join(", ")}`);
121
+ }
122
+ if (ignored.length > 0) {
123
+ this.log.append("note", `From the address, ignored: ${ignored.join("; ")}`);
124
+ }
125
+ return clean;
126
+ };
127
+ /** Reads what the server holds: the plugin, its saved settings, and the switched failures. */
128
+ async start() {
129
+ await Promise.all([
130
+ this.plugin.load(),
131
+ this.settings.load(),
132
+ this.failures.load(),
133
+ ]);
134
+ }
135
+ /**
136
+ * The address field's action: the address embedded now is reloaded, with a clean message log;
137
+ * another one is embedded instead, or refused.
138
+ */
139
+ embed = async (text) => {
140
+ if (this.plugin.isCurrent(text)) {
141
+ this.log.clear();
142
+ this.plugin.reload();
143
+ return;
144
+ }
145
+ await this.plugin.setAddress(text);
146
+ };
147
+ /** Reads whatever is still being typed: the page is going away, and it should be kept. */
148
+ flush = () => {
149
+ this.plugin.clientIdDraft.flush();
150
+ this.plugin.manifestDraft.flush();
151
+ this.view.viewer.flush();
152
+ this.view.record.flush();
153
+ this.view.preferencesText.flush();
154
+ };
155
+ dispose() {
156
+ this.layout.dispose();
157
+ this.plugin.dispose();
158
+ this.view.dispose();
159
+ this.failures.dispose();
160
+ this.slot.dispose();
161
+ }
162
+ }
@@ -0,0 +1,71 @@
1
+ /**
2
+ * A plugin's surface, as plugin-slot lays it out: inline in a profile column, in a dialog, or
3
+ * fullscreen. The same iframe is restyled, never moved, so the plugin never reloads. Fullscreen
4
+ * returns to where it was entered from: the dialog, or inline. In the studio, the dialog and
5
+ * fullscreen cover the main area, which stands for Livespace's page.
6
+ */
7
+ import { sanitizeModalSize } from "@livespace/plugin-sdk";
8
+ import { computed, signal, } from "@preact/signals";
9
+ import { dialogSize } from "./display.js";
10
+ /** The chrome bar's height: inline, the frame is the plugin's height plus this. */
11
+ export const CHROME_PX = 32;
12
+ export class SurfaceStore {
13
+ deps;
14
+ inline;
15
+ #display = signal("inline");
16
+ #bounds = signal({});
17
+ #beforeFullscreen = "inline";
18
+ /** The frame's CSS size: the inline size, the dialog's, or none (fullscreen fills the window). */
19
+ frameSize;
20
+ constructor(deps,
21
+ /** The inline size, which the page decides. */
22
+ inline) {
23
+ this.deps = deps;
24
+ this.inline = inline;
25
+ this.frameSize = computed(() => {
26
+ const display = this.#display.value;
27
+ if (display === "dialog") {
28
+ return dialogSize(this.#bounds.value);
29
+ }
30
+ if (display === "fullscreen") {
31
+ return { width: "", height: "" };
32
+ }
33
+ const { width, height } = this.inline.value;
34
+ return {
35
+ width: width === null ? "100%" : `${width}px`,
36
+ height: `${height + CHROME_PX}px`,
37
+ };
38
+ });
39
+ }
40
+ get display() {
41
+ return this.#display;
42
+ }
43
+ show = (display) => {
44
+ const from = this.#display.value;
45
+ if (display === from) {
46
+ return;
47
+ }
48
+ if (display === "fullscreen") {
49
+ this.#beforeFullscreen = from;
50
+ }
51
+ this.#display.value = display;
52
+ this.deps.log("note", `Display: ${display}`, undefined, this.deps.source);
53
+ };
54
+ /** Closes the dialog, or leaves fullscreen for where it came from. */
55
+ leave = () => {
56
+ this.show(this.#display.value === "fullscreen" ? this.#beforeFullscreen : "inline");
57
+ };
58
+ /** `modal-size`, sanitised as Livespace does, applied at once, even with the dialog open. */
59
+ modalSize = (raw) => {
60
+ const { bounds, dropped } = sanitizeModalSize(raw);
61
+ this.#bounds.value = bounds;
62
+ if (dropped.length > 0) {
63
+ this.deps.log("note", `modal-size: dropped ${dropped.join(", ")}, as Livespace would`, undefined, this.deps.source);
64
+ }
65
+ };
66
+ /** Back inline, as a new embed starts. */
67
+ reset = () => {
68
+ this.show("inline");
69
+ this.#bounds.value = {};
70
+ };
71
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * The plugin's toasts, as Livespace shows them: checked by `sanitizeNotifyPayload`, at most one a
3
+ * second, and gone after `NOTIFY_DURATION_MS`. They're drawn above the dialog and fullscreen, so
4
+ * whatever opens on the top layer asks them to be raised again.
5
+ */
6
+ import { NOTIFY_DURATION_MS, NotifyRateLimiter, sanitizeNotifyPayload, } from "@livespace/plugin-sdk";
7
+ import { signal } from "@preact/signals";
8
+ export class ToastsStore {
9
+ deps;
10
+ #toasts = signal([]);
11
+ /** Bumped whenever the toasts must go back on top of the top layer. */
12
+ #raised = signal(0);
13
+ #limiter = new NotifyRateLimiter();
14
+ #nextId = 1;
15
+ constructor(deps) {
16
+ this.deps = deps;
17
+ }
18
+ get toasts() {
19
+ return this.#toasts;
20
+ }
21
+ get raised() {
22
+ return this.#raised;
23
+ }
24
+ /** A fresh limit for a fresh embed, as each of plugin-slot's renderers has its own. */
25
+ reset = () => {
26
+ this.#limiter = new NotifyRateLimiter();
27
+ };
28
+ /** A `notify` message's payload, shown if Livespace would show it. */
29
+ notify = (raw) => {
30
+ const toast = sanitizeNotifyPayload(raw);
31
+ if (toast === null) {
32
+ this.deps.log("note", "notify: dropped, it has no title");
33
+ return;
34
+ }
35
+ if (!this.#limiter.tryAcquire()) {
36
+ this.deps.log("note", "notify: dropped, less than a second after the last one");
37
+ return;
38
+ }
39
+ const id = this.#nextId++;
40
+ this.#toasts.value = [
41
+ ...this.#toasts.value,
42
+ {
43
+ id,
44
+ variant: toast.variant,
45
+ title: toast.title,
46
+ ...(toast.body ? { body: toast.body } : {}),
47
+ },
48
+ ];
49
+ const later = this.deps.later ?? ((run, ms) => setTimeout(run, ms));
50
+ later(() => this.dismiss(id), NOTIFY_DURATION_MS);
51
+ this.raise();
52
+ };
53
+ dismiss = (id) => {
54
+ this.#toasts.value = this.#toasts.value.filter((toast) => toast.id !== id);
55
+ };
56
+ /** Puts the toasts back above whatever just opened on the top layer. */
57
+ raise = () => {
58
+ this.#raised.value++;
59
+ };
60
+ }