@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.
- package/LICENSE +21 -0
- package/README.md +87 -2
- package/dist/auth.js +220 -0
- package/dist/cli.js +67 -0
- package/dist/keys.js +42 -0
- package/dist/manifest.js +16 -0
- package/dist/page/domain/connection.js +2 -0
- package/dist/page/domain/display.js +38 -0
- package/dist/page/domain/draft.js +80 -0
- package/dist/page/domain/failures-store.js +41 -0
- package/dist/page/domain/failures.js +75 -0
- package/dist/page/domain/layout-store.js +119 -0
- package/dist/page/domain/log-store.js +37 -0
- package/dist/page/domain/navigation-store.js +16 -0
- package/dist/page/domain/plugin-data.js +70 -0
- package/dist/page/domain/plugin-store.js +257 -0
- package/dist/page/domain/probe-store.js +100 -0
- package/dist/page/domain/profile-session.js +115 -0
- package/dist/page/domain/profile-store.js +21 -0
- package/dist/page/domain/samples.js +221 -0
- package/dist/page/domain/secrets-store.js +159 -0
- package/dist/page/domain/secrets.js +135 -0
- package/dist/page/domain/settings-data.js +64 -0
- package/dist/page/domain/settings-session.js +206 -0
- package/dist/page/domain/settings-store.js +62 -0
- package/dist/page/domain/slot-store.js +50 -0
- package/dist/page/domain/studio.js +121 -0
- package/dist/page/domain/surface-store.js +71 -0
- package/dist/page/domain/toasts-store.js +60 -0
- package/dist/page/domain/view-data.js +122 -0
- package/dist/page/domain/view-store.js +132 -0
- package/dist/page/host.css +907 -0
- package/dist/page/index.html +31 -0
- package/dist/page/main.js +31 -0
- package/dist/page/plugin-studio-logo.svg +4 -0
- package/dist/page/reload.svg +1 -0
- package/dist/page/transport/browser-storage.js +41 -0
- package/dist/page/transport/embed.js +142 -0
- package/dist/page/transport/plugin-keys.js +68 -0
- package/dist/page/transport/studio-api.js +74 -0
- package/dist/page/ui/app.js +61 -0
- package/dist/page/ui/boot.js +44 -0
- package/dist/page/ui/brand.js +3 -0
- package/dist/page/ui/context.js +11 -0
- package/dist/page/ui/json-field.js +19 -0
- package/dist/page/ui/json-tokens.js +40 -0
- package/dist/page/ui/json-view.js +11 -0
- package/dist/page/ui/memo.js +23 -0
- package/dist/page/ui/messages.js +34 -0
- package/dist/page/ui/page-switcher.js +11 -0
- package/dist/page/ui/plugin-frame.js +43 -0
- package/dist/page/ui/profile-page.js +13 -0
- package/dist/page/ui/resize-handle.js +63 -0
- package/dist/page/ui/root.js +9 -0
- package/dist/page/ui/section.js +19 -0
- package/dist/page/ui/settings/config-form.js +59 -0
- package/dist/page/ui/settings/secret-field.js +22 -0
- package/dist/page/ui/settings/settings-page.js +60 -0
- package/dist/page/ui/sidebar/custom-settings-section.js +10 -0
- package/dist/page/ui/sidebar/failures-section.js +27 -0
- package/dist/page/ui/sidebar/installation-section.js +30 -0
- package/dist/page/ui/sidebar/metadata-section.js +9 -0
- package/dist/page/ui/sidebar/plugin-section.js +18 -0
- package/dist/page/ui/sidebar/preferences-section.js +16 -0
- package/dist/page/ui/sidebar/record-section.js +17 -0
- package/dist/page/ui/sidebar/viewer-section.js +8 -0
- package/dist/page/ui/sidebar-group.js +4 -0
- package/dist/page/ui/splash.js +6 -0
- package/dist/page/ui/status.js +10 -0
- package/dist/page/ui/surface.js +23 -0
- package/dist/page/ui/theme.js +13 -0
- package/dist/page/ui/toasts.js +31 -0
- package/dist/page/ui/top-bar.js +37 -0
- package/dist/server.js +519 -0
- package/dist/start.js +108 -0
- package/package.json +55 -5
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The settings page's logic, as the plugins-2 settings screen has it: the values a form starts
|
|
3
|
+
* from, how a typed value becomes a config value, what a validation error says, and how the
|
|
4
|
+
* preview is scaled. No DOM here.
|
|
5
|
+
*/
|
|
6
|
+
import { configFormDefaults, } from "@livespace/plugin-sdk";
|
|
7
|
+
/** How long plugins-2 waits for the form before it says the plugin didn't answer. */
|
|
8
|
+
export const CONFIG_FORM_TIMEOUT_MS = 10000;
|
|
9
|
+
/**
|
|
10
|
+
* The values the form shows and the preview gets: the declared defaults with what's saved, then
|
|
11
|
+
* what's been typed, laid over them. Always an object, `{}` at least, as plugins-2 sends it.
|
|
12
|
+
*/
|
|
13
|
+
export function effectiveValues(form, saved, draft) {
|
|
14
|
+
return { ...configFormDefaults(form), ...(saved ?? {}), ...draft };
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A typed value as the config holds it. A number field's text becomes a number, except empty or
|
|
18
|
+
* not numeric, which stays text for validation to report, as plugins-2 does.
|
|
19
|
+
*/
|
|
20
|
+
export function valueFrom(field, raw) {
|
|
21
|
+
if (field.kind === "checkbox") {
|
|
22
|
+
return raw === true;
|
|
23
|
+
}
|
|
24
|
+
const text = String(raw);
|
|
25
|
+
if (field.kind === "number") {
|
|
26
|
+
const number = Number(text);
|
|
27
|
+
return text.trim() !== "" && Number.isFinite(number) ? number : text;
|
|
28
|
+
}
|
|
29
|
+
return text;
|
|
30
|
+
}
|
|
31
|
+
/** What a validation error says, in the words of a settings form. */
|
|
32
|
+
export function errorText(error) {
|
|
33
|
+
switch (error.code) {
|
|
34
|
+
case "required":
|
|
35
|
+
return "Required.";
|
|
36
|
+
case "minLength":
|
|
37
|
+
return `At least ${error.min} characters.`;
|
|
38
|
+
case "maxLength":
|
|
39
|
+
return `At most ${error.max} characters.`;
|
|
40
|
+
case "pattern":
|
|
41
|
+
return "Not in the expected format.";
|
|
42
|
+
case "min":
|
|
43
|
+
return `At least ${error.min}.`;
|
|
44
|
+
case "max":
|
|
45
|
+
return `At most ${error.max}.`;
|
|
46
|
+
case "notANumber":
|
|
47
|
+
return "Not a number.";
|
|
48
|
+
case "notAnOption":
|
|
49
|
+
return "Not one of the options.";
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The preview in the plugin's slot: rendered at the slot's size, or the plugin's minimum when
|
|
54
|
+
* that's bigger, and scaled down to fit the slot's width. A full-width slot (`width: null`) fills
|
|
55
|
+
* the pane, unscaled.
|
|
56
|
+
*/
|
|
57
|
+
export function previewGeometry(metadata, slot) {
|
|
58
|
+
const height = Math.max(slot.height, metadata?.minHeightPx ?? 0);
|
|
59
|
+
if (slot.width === null) {
|
|
60
|
+
return { width: null, height, scale: 1 };
|
|
61
|
+
}
|
|
62
|
+
const width = Math.max(slot.width, metadata?.minWidthPx ?? 0);
|
|
63
|
+
return { width, height, scale: slot.width / width };
|
|
64
|
+
}
|
|
@@ -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,121 @@
|
|
|
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 { timers } from "./draft.js";
|
|
7
|
+
import { FailuresStore } from "./failures-store.js";
|
|
8
|
+
import { LayoutStore } from "./layout-store.js";
|
|
9
|
+
import { LogStore } from "./log-store.js";
|
|
10
|
+
import { NavigationStore } from "./navigation-store.js";
|
|
11
|
+
import { PluginStore } from "./plugin-store.js";
|
|
12
|
+
import { ProbeStore } from "./probe-store.js";
|
|
13
|
+
import { ProfileStore } from "./profile-store.js";
|
|
14
|
+
import { SettingsStore } from "./settings-store.js";
|
|
15
|
+
import { SlotStore } from "./slot-store.js";
|
|
16
|
+
import { ToastsStore } from "./toasts-store.js";
|
|
17
|
+
import { ViewStore } from "./view-store.js";
|
|
18
|
+
/** The browser storage key prefix; each store's key adds its own name. */
|
|
19
|
+
export const STORAGE_PREFIX = "livespace-plugin-studio";
|
|
20
|
+
export class Studio {
|
|
21
|
+
api;
|
|
22
|
+
log;
|
|
23
|
+
toasts;
|
|
24
|
+
layout;
|
|
25
|
+
navigation;
|
|
26
|
+
plugin;
|
|
27
|
+
view;
|
|
28
|
+
failures;
|
|
29
|
+
slot;
|
|
30
|
+
probe;
|
|
31
|
+
settings;
|
|
32
|
+
profile;
|
|
33
|
+
constructor(deps) {
|
|
34
|
+
const storage = deps.storage ?? ((key) => new BrowserStorage(key));
|
|
35
|
+
const stored = (name) => storage(`${STORAGE_PREFIX}-${name}`);
|
|
36
|
+
this.api = deps.api;
|
|
37
|
+
this.log = new LogStore(deps.now);
|
|
38
|
+
this.toasts = new ToastsStore({ log: this.log.append });
|
|
39
|
+
this.layout = new LayoutStore(stored("layout"), deps.viewportWidth);
|
|
40
|
+
this.navigation = new NavigationStore(deps.hash);
|
|
41
|
+
this.plugin = new PluginStore({
|
|
42
|
+
api: deps.api,
|
|
43
|
+
storage: stored("address"),
|
|
44
|
+
studioOrigin: deps.origin,
|
|
45
|
+
log: this.log.append,
|
|
46
|
+
scheduler: deps.scheduler,
|
|
47
|
+
});
|
|
48
|
+
this.view = new ViewStore({
|
|
49
|
+
storage: stored("view"),
|
|
50
|
+
choices: this.plugin.recordChoices,
|
|
51
|
+
scheduler: deps.scheduler,
|
|
52
|
+
});
|
|
53
|
+
this.failures = new FailuresStore({
|
|
54
|
+
api: deps.api,
|
|
55
|
+
storage: stored("failures"),
|
|
56
|
+
log: this.log.append,
|
|
57
|
+
});
|
|
58
|
+
// The column key predates the slot: kept, so the size saved here stays.
|
|
59
|
+
this.slot = new SlotStore(stored("column"));
|
|
60
|
+
this.probe = new ProbeStore({
|
|
61
|
+
log: this.log,
|
|
62
|
+
view: this.view,
|
|
63
|
+
metadata: this.plugin,
|
|
64
|
+
scheduler: deps.scheduler ?? timers,
|
|
65
|
+
});
|
|
66
|
+
this.settings = new SettingsStore({
|
|
67
|
+
api: deps.api,
|
|
68
|
+
log: this.log,
|
|
69
|
+
toasts: this.toasts,
|
|
70
|
+
view: this.view,
|
|
71
|
+
studioOrigin: deps.origin,
|
|
72
|
+
metadata: this.plugin,
|
|
73
|
+
slot: this.slot,
|
|
74
|
+
probe: this.probe,
|
|
75
|
+
});
|
|
76
|
+
this.profile = new ProfileStore({
|
|
77
|
+
api: deps.api,
|
|
78
|
+
slot: this.slot,
|
|
79
|
+
log: this.log,
|
|
80
|
+
toasts: this.toasts,
|
|
81
|
+
view: this.view,
|
|
82
|
+
settings: this.settings,
|
|
83
|
+
failures: this.failures,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/** Reads what the server holds: the plugin, its saved settings, and the switched failures. */
|
|
87
|
+
async start() {
|
|
88
|
+
await Promise.all([
|
|
89
|
+
this.plugin.load(),
|
|
90
|
+
this.settings.load(),
|
|
91
|
+
this.failures.load(),
|
|
92
|
+
]);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The address field's action: the address embedded now is reloaded, with a clean message log;
|
|
96
|
+
* another one is embedded instead, or refused.
|
|
97
|
+
*/
|
|
98
|
+
embed = async (text) => {
|
|
99
|
+
if (this.plugin.isCurrent(text)) {
|
|
100
|
+
this.log.clear();
|
|
101
|
+
this.plugin.reload();
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
await this.plugin.setAddress(text);
|
|
105
|
+
};
|
|
106
|
+
/** Reads whatever is still being typed: the page is going away, and it should be kept. */
|
|
107
|
+
flush = () => {
|
|
108
|
+
this.plugin.clientIdDraft.flush();
|
|
109
|
+
this.plugin.manifestDraft.flush();
|
|
110
|
+
this.view.viewer.flush();
|
|
111
|
+
this.view.record.flush();
|
|
112
|
+
this.view.preferencesText.flush();
|
|
113
|
+
};
|
|
114
|
+
dispose() {
|
|
115
|
+
this.layout.dispose();
|
|
116
|
+
this.plugin.dispose();
|
|
117
|
+
this.view.dispose();
|
|
118
|
+
this.failures.dispose();
|
|
119
|
+
this.slot.dispose();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -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
|
+
}
|