@protolabsai/ui 0.19.0 → 0.19.1
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/package.json +1 -1
- package/src/plugin-kit.js +46 -22
package/package.json
CHANGED
package/src/plugin-kit.js
CHANGED
|
@@ -1,32 +1,62 @@
|
|
|
1
|
-
// @protolabsai/ui/plugin-kit — the
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// the
|
|
1
|
+
// @protolabsai/ui/plugin-kit — the protoAgent console handshake for plugin iframe
|
|
2
|
+
// pages, wired to the DS. The console (protoAgent ADR 0038 theming bridge) posts a
|
|
3
|
+
// CURATED theme — `{ bg, bgPanel, fg, fgMuted, brand, border }`, read from the
|
|
4
|
+
// console's own CSS vars — NOT the full --pl-* set. This bridges those six keys onto
|
|
5
|
+
// the --pl-* tokens that plugin-kit.css components consume, so the DS components
|
|
6
|
+
// re-skin to the operator's live theme. The rest of the --pl-* tokens keep the kit's
|
|
7
|
+
// (coherent dark) defaults. See protoAgent docs/guides/plugin-views.md +
|
|
8
|
+
// examples/plugins/chat_example.
|
|
6
9
|
//
|
|
7
|
-
// Two ways to use it (no build step
|
|
10
|
+
// Two ways to use it (no build step for either):
|
|
8
11
|
// 1. <script type="module">: import { initPluginView } from ".../plugin-kit.js"
|
|
9
12
|
// 2. classic <script src=".../plugin-kit.js">: use the window.protoPluginView global
|
|
10
|
-
// Or
|
|
13
|
+
// Or inline the snippet from docs/how-to/build-a-plugin-view.md.
|
|
14
|
+
|
|
15
|
+
// Curated console key → the --pl-* token(s) the DS components actually read.
|
|
16
|
+
const TOKEN_MAP = {
|
|
17
|
+
bg: ["--pl-color-bg"],
|
|
18
|
+
bgPanel: ["--pl-color-bg-raised", "--pl-color-bg-subtle"],
|
|
19
|
+
fg: ["--pl-color-fg"],
|
|
20
|
+
fgMuted: ["--pl-color-fg-muted"],
|
|
21
|
+
brand: ["--pl-color-accent"], // components key off --pl-color-accent (== brand lavender)
|
|
22
|
+
border: ["--pl-color-border"],
|
|
23
|
+
};
|
|
11
24
|
|
|
12
25
|
let token = null;
|
|
13
26
|
const listeners = new Set();
|
|
14
27
|
|
|
15
|
-
/**
|
|
16
|
-
*
|
|
28
|
+
/** Map the console's curated theme onto the DS --pl-* tokens on :root. Also passes
|
|
29
|
+
* through any key already in --pl-* form, so a future console that sends raw tokens
|
|
30
|
+
* Just Works. Unknown keys are ignored — a plugin page can never set an arbitrary
|
|
31
|
+
* CSS property this way. */
|
|
17
32
|
function applyTheme(theme) {
|
|
18
33
|
if (!theme || typeof theme !== "object") return;
|
|
19
34
|
const root = document.documentElement;
|
|
20
|
-
for (const [
|
|
21
|
-
if (
|
|
35
|
+
for (const [key, value] of Object.entries(theme)) {
|
|
36
|
+
if (value == null || value === "") continue;
|
|
37
|
+
const v = String(value);
|
|
38
|
+
if (key.startsWith("--pl-")) {
|
|
39
|
+
root.style.setProperty(key, v);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const targets = TOKEN_MAP[key];
|
|
43
|
+
if (targets) for (const cssVar of targets) root.style.setProperty(cssVar, v);
|
|
22
44
|
}
|
|
23
45
|
}
|
|
24
46
|
|
|
25
47
|
function onMessage(e) {
|
|
26
48
|
const d = e && e.data;
|
|
27
|
-
if (!d
|
|
28
|
-
|
|
29
|
-
|
|
49
|
+
if (!d) return;
|
|
50
|
+
// `protoagent:init` carries token + theme (after iframe load); `protoagent:theme`
|
|
51
|
+
// is the live re-theme on an operator theme switch.
|
|
52
|
+
if (d.type === "protoagent:init") {
|
|
53
|
+
if (d.token) token = d.token;
|
|
54
|
+
applyTheme(d.theme);
|
|
55
|
+
} else if (d.type === "protoagent:theme") {
|
|
56
|
+
applyTheme(d.theme);
|
|
57
|
+
} else {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
30
60
|
for (const fn of listeners) {
|
|
31
61
|
try {
|
|
32
62
|
fn({ token, theme: d.theme });
|
|
@@ -37,17 +67,11 @@ function onMessage(e) {
|
|
|
37
67
|
}
|
|
38
68
|
|
|
39
69
|
/** Start listening for the console handshake. Call once on load. `onInit` (optional)
|
|
40
|
-
* fires
|
|
41
|
-
*
|
|
70
|
+
* fires on the initial bearer+theme AND on every live re-theme. Returns a handle
|
|
71
|
+
* with the current token. */
|
|
42
72
|
export function initPluginView(onInit) {
|
|
43
73
|
if (typeof onInit === "function") listeners.add(onInit);
|
|
44
74
|
window.addEventListener("message", onMessage);
|
|
45
|
-
// Announce readiness so a console that waits for it sends the init eagerly.
|
|
46
|
-
try {
|
|
47
|
-
window.parent && window.parent.postMessage({ type: "protoagent:ready" }, "*");
|
|
48
|
-
} catch (_) {
|
|
49
|
-
/* sandboxed / no parent */
|
|
50
|
-
}
|
|
51
75
|
return { getToken };
|
|
52
76
|
}
|
|
53
77
|
|