@takazudo/zudo-sg 0.5.0 → 0.5.2
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/CHANGELOG.md +60 -0
- package/README.md +266 -0
- package/dist/code-panel/code-panel.js +15 -5
- package/dist/code-panel/css-injection.d.ts +11 -5
- package/dist/code-panel/css-injection.js +179 -25
- package/dist/config/index.d.ts +1 -0
- package/dist/plugins/routes.d.ts +2 -0
- package/dist/plugins/routes.js +7 -3
- package/dist/preview/detail-workbench.d.ts +2 -1
- package/dist/preview/detail-workbench.js +3 -0
- package/dist/preview/external-runtime.d.ts +117 -0
- package/dist/preview/external-runtime.js +121 -0
- package/dist/preview/runtime-controller.d.ts +34 -0
- package/dist/preview/runtime-controller.js +302 -0
- package/dist/preview/variant-frame.d.ts +3 -0
- package/dist/preview/variant-frame.js +161 -15
- package/dist/registry/catalog.d.ts +2 -0
- package/dist/registry/catalog.js +2 -1
- package/dist/registry/descriptors.d.ts +3 -0
- package/dist/registry/descriptors.js +6 -1
- package/dist/registry/external-catalog-node.d.ts +65 -0
- package/dist/registry/external-catalog-node.js +220 -0
- package/dist/registry/external-catalog.d.ts +44 -0
- package/dist/registry/external-catalog.js +72 -0
- package/dist/registry/index.d.ts +2 -0
- package/dist/registry/index.js +4 -0
- package/dist/sg-context.d.ts +2 -0
- package/dist/token-tweak/iframe-css-vars-bridge.d.ts +14 -6
- package/dist/token-tweak/iframe-css-vars-bridge.js +46 -17
- package/dist/token-tweak/index.d.ts +1 -1
- package/dist/token-tweak/index.js +2 -0
- package/dist/token-tweak/preview-iframe-registry.d.ts +10 -1
- package/dist/token-tweak/preview-iframe-registry.js +48 -7
- package/package.json +27 -10
- package/routes-src/components-slug.tsx +12 -9
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { isJsonValue, isSerializableProps } from "../preview/external-runtime.js";
|
|
2
|
+
function externalCatalogKey(slug, variant) {
|
|
3
|
+
return JSON.stringify([slug, variant]);
|
|
4
|
+
}
|
|
5
|
+
function nonEmpty(value) {
|
|
6
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
7
|
+
}
|
|
8
|
+
function isDescriptorSource(value) {
|
|
9
|
+
return isSerializableProps(value) && nonEmpty(value.key) && typeof value.code === "string";
|
|
10
|
+
}
|
|
11
|
+
function fixtureReference(value) {
|
|
12
|
+
if (!isSerializableProps(value) || !nonEmpty(value.module) || !nonEmpty(value.exportName)) return false;
|
|
13
|
+
const segments = value.module.slice(2).split("/");
|
|
14
|
+
return value.module.startsWith("./") && !/[\\?#%\s]/u.test(value.module) && segments.every((segment) => segment !== "" && segment !== "." && segment !== "..") && /^[A-Za-z_$][\w$]*$/u.test(value.exportName);
|
|
15
|
+
}
|
|
16
|
+
function assetPath(value) {
|
|
17
|
+
return typeof value === "string" && value.startsWith("/") && !/[\\?#%\s]/u.test(value) && value.slice(1).split("/").every((segment) => segment !== "" && segment !== "." && segment !== "..");
|
|
18
|
+
}
|
|
19
|
+
function positiveDimension(value) {
|
|
20
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value > 0;
|
|
21
|
+
}
|
|
22
|
+
function validateExternalCatalogManifest(input) {
|
|
23
|
+
if (!Array.isArray(input)) throw new Error("[zudo-sg] external catalog manifest must be an array");
|
|
24
|
+
if (!isJsonValue(input)) throw new Error("[zudo-sg] external catalog manifest must contain only dense, serializable data");
|
|
25
|
+
const keys = /* @__PURE__ */ new Set();
|
|
26
|
+
const assets = /* @__PURE__ */ new Set();
|
|
27
|
+
return input.map((raw, index) => {
|
|
28
|
+
const fail = (message) => {
|
|
29
|
+
throw new Error(`[zudo-sg] external catalog[${index}]: ${message}`);
|
|
30
|
+
};
|
|
31
|
+
if (!isSerializableProps(raw)) return fail("entry must contain only serializable data");
|
|
32
|
+
if (!nonEmpty(raw.slug) || /[\s/\\?#%]/u.test(raw.slug) || raw.slug === "." || raw.slug === "..") return fail("invalid slug");
|
|
33
|
+
if (!nonEmpty(raw.variant)) return fail("variant must be a non-empty string");
|
|
34
|
+
const key = externalCatalogKey(raw.slug, raw.variant);
|
|
35
|
+
if (keys.has(key)) return fail(`duplicate identity ${key}`);
|
|
36
|
+
keys.add(key);
|
|
37
|
+
if (!isSerializableProps(raw.loader) || !fixtureReference(raw.loader) || raw.loader.key !== key) return fail(`loader must name ${key} and a valid module/export`);
|
|
38
|
+
if (!isDescriptorSource(raw.source) || raw.source.key !== key) return fail(`source must name ${key} and provide code text`);
|
|
39
|
+
const capture = raw.capture;
|
|
40
|
+
if (!isSerializableProps(capture) || capture.key !== key) return fail(`capture must name ${key}`);
|
|
41
|
+
let normalizedCapture;
|
|
42
|
+
if (capture.kind === "image") {
|
|
43
|
+
if (!fixtureReference(capture.fixture) || capture.fixture.module !== raw.loader.module || capture.fixture.exportName !== raw.loader.exportName) return fail("capture fixture differs from loader");
|
|
44
|
+
if (!assetPath(capture.src)) return fail("capture image needs a root-absolute emitted asset path without traversal/query/hash");
|
|
45
|
+
if (!positiveDimension(capture.width) || !positiveDimension(capture.height)) return fail("capture image needs positive integer dimensions");
|
|
46
|
+
if (assets.has(capture.src)) return fail(`capture asset is owned by multiple variants: ${capture.src}`);
|
|
47
|
+
assets.add(capture.src);
|
|
48
|
+
normalizedCapture = {
|
|
49
|
+
kind: "image",
|
|
50
|
+
key,
|
|
51
|
+
src: capture.src,
|
|
52
|
+
width: capture.width,
|
|
53
|
+
height: capture.height,
|
|
54
|
+
fixture: { module: capture.fixture.module, exportName: capture.fixture.exportName }
|
|
55
|
+
};
|
|
56
|
+
} else if ((capture.kind === "placeholder" || capture.kind === "failed") && nonEmpty(capture.note)) {
|
|
57
|
+
normalizedCapture = { kind: capture.kind, key, note: capture.note };
|
|
58
|
+
} else return fail("capture must be an image promise or a labelled placeholder/failed disposition");
|
|
59
|
+
return {
|
|
60
|
+
slug: raw.slug,
|
|
61
|
+
variant: raw.variant,
|
|
62
|
+
loader: { key, module: raw.loader.module, exportName: raw.loader.exportName },
|
|
63
|
+
source: { key, code: raw.source.code },
|
|
64
|
+
capture: normalizedCapture
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
export {
|
|
69
|
+
externalCatalogKey,
|
|
70
|
+
isDescriptorSource,
|
|
71
|
+
validateExternalCatalogManifest
|
|
72
|
+
};
|
package/dist/registry/index.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ export type { CategoryGroup, CreateRegistryOptions, Registry, StoryEntry, Varian
|
|
|
3
3
|
export { buildNavNodes } from "./nav-nodes.js";
|
|
4
4
|
export type { BuildNavNodesOptions, NavNode, NavNodesSource } from "./nav-nodes.js";
|
|
5
5
|
export { validateStoryDescriptors } from "./descriptors.js";
|
|
6
|
+
export { externalCatalogKey, isDescriptorSource, validateExternalCatalogManifest } from "./external-catalog.js";
|
|
7
|
+
export type { DescriptorSource, ExternalFixtureReference, ExternalCatalogLoader, ExternalCaptureDisposition, ExternalCatalogEntry } from "./external-catalog.js";
|
|
6
8
|
export type { StoryDescriptor, ThumbnailDescriptor, ValidateStoryDescriptorsOptions, VariantDescriptor, } from "./descriptors.js";
|
|
7
9
|
export { catalogEntriesFromDescriptors, catalogEntriesFromRegistry, createCatalog } from "./catalog.js";
|
|
8
10
|
export type { Catalog, CatalogCategoryGroup, CatalogEntry, CatalogVariant, CreateCatalogOptions, DescriptorCatalogEntry, ModuleCatalogEntry, } from "./catalog.js";
|
package/dist/registry/index.js
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
} from "./registry.js";
|
|
8
8
|
import { buildNavNodes } from "./nav-nodes.js";
|
|
9
9
|
import { validateStoryDescriptors } from "./descriptors.js";
|
|
10
|
+
import { externalCatalogKey, isDescriptorSource, validateExternalCatalogManifest } from "./external-catalog.js";
|
|
10
11
|
import { catalogEntriesFromDescriptors, catalogEntriesFromRegistry, createCatalog } from "./catalog.js";
|
|
11
12
|
import {
|
|
12
13
|
COMPONENT_DOCS_COLLECTION,
|
|
@@ -28,7 +29,10 @@ export {
|
|
|
28
29
|
createCatalog,
|
|
29
30
|
createRegistry,
|
|
30
31
|
deriveMapKeyPrefix,
|
|
32
|
+
externalCatalogKey,
|
|
33
|
+
isDescriptorSource,
|
|
31
34
|
resolveComponentDoc,
|
|
32
35
|
slugify,
|
|
36
|
+
validateExternalCatalogManifest,
|
|
33
37
|
validateStoryDescriptors
|
|
34
38
|
};
|
package/dist/sg-context.d.ts
CHANGED
|
@@ -42,6 +42,8 @@ export interface SgContext {
|
|
|
42
42
|
* as `previewUrl` to `DetailWorkbench` and the code panel.
|
|
43
43
|
*/
|
|
44
44
|
externalPreviewUrl: string | null;
|
|
45
|
+
/** Explicit strict protocol selection, defaulting to legacy for old serialized contexts. */
|
|
46
|
+
externalPreviewRuntime?: "legacy" | "external-v1";
|
|
45
47
|
/**
|
|
46
48
|
* Whether `zdtpApplyProxy.tabsModule` is configured (issue #872's
|
|
47
49
|
* `isPreviewTokenPanelWired()` verdict, `sg-routes.ts`). Gates the `/tokens`
|
|
@@ -1,17 +1,22 @@
|
|
|
1
|
+
import { type ExternalRuntimeIdentity } from "../preview/external-runtime.js";
|
|
2
|
+
interface StrictBridgeFields {
|
|
3
|
+
identity?: ExternalRuntimeIdentity;
|
|
4
|
+
sequence?: number;
|
|
5
|
+
}
|
|
1
6
|
/** Envelope `source` tag. Part of the wire contract (e2e/preview-token-panel.spec.ts mirrors it). */
|
|
2
7
|
export declare const BRIDGE_SOURCE: "zudo-sg-token-tweak-bridge";
|
|
3
8
|
export type CssVarPair = readonly [name: string, value: string];
|
|
4
|
-
export interface ApplyCssVarsMessage {
|
|
9
|
+
export interface ApplyCssVarsMessage extends StrictBridgeFields {
|
|
5
10
|
source: typeof BRIDGE_SOURCE;
|
|
6
11
|
type: "apply-css-vars";
|
|
7
12
|
vars: ReadonlyArray<CssVarPair>;
|
|
8
13
|
}
|
|
9
|
-
export interface ClearCssVarsMessage {
|
|
14
|
+
export interface ClearCssVarsMessage extends StrictBridgeFields {
|
|
10
15
|
source: typeof BRIDGE_SOURCE;
|
|
11
16
|
type: "clear-css-vars";
|
|
12
17
|
names: ReadonlyArray<string>;
|
|
13
18
|
}
|
|
14
|
-
export interface ReadyMessage {
|
|
19
|
+
export interface ReadyMessage extends StrictBridgeFields {
|
|
15
20
|
source: typeof BRIDGE_SOURCE;
|
|
16
21
|
type: "ready";
|
|
17
22
|
}
|
|
@@ -19,17 +24,20 @@ export type BridgeMessage = ApplyCssVarsMessage | ClearCssVarsMessage | ReadyMes
|
|
|
19
24
|
/** Type guard. Accepts any postMessage payload and confirms it is a bridge envelope. */
|
|
20
25
|
export declare function isBridgeMessage(value: unknown): value is BridgeMessage;
|
|
21
26
|
/** Send `apply-css-vars` to the iframe. No-op when the iframe has not loaded. */
|
|
22
|
-
export declare function sendApplyCssVars(iframe: HTMLIFrameElement | null, vars: ReadonlyArray<CssVarPair>): void;
|
|
27
|
+
export declare function sendApplyCssVars(iframe: HTMLIFrameElement | null, vars: ReadonlyArray<CssVarPair>, strict?: Required<StrictBridgeFields>): void;
|
|
23
28
|
/** Send `clear-css-vars` to the iframe so it removes inline overrides. */
|
|
24
|
-
export declare function sendClearCssVars(iframe: HTMLIFrameElement | null, names: ReadonlyArray<string>): void;
|
|
29
|
+
export declare function sendClearCssVars(iframe: HTMLIFrameElement | null, names: ReadonlyArray<string>, strict?: Required<StrictBridgeFields>): void;
|
|
25
30
|
/**
|
|
26
31
|
* Install a message listener inside the preview iframe document that applies
|
|
27
32
|
* any incoming CSS-var batches onto `documentElement.style`. Returns a
|
|
28
33
|
* teardown function. Intended to be called once at iframe boot.
|
|
29
34
|
*/
|
|
30
|
-
export declare function installIframeReceiver(target?: Window
|
|
35
|
+
export declare function installIframeReceiver(target?: Window, options?: {
|
|
36
|
+
identity?: () => ExternalRuntimeIdentity | null;
|
|
37
|
+
}): () => void;
|
|
31
38
|
/**
|
|
32
39
|
* Install a `ready` listener on the panel side so callers can defer the
|
|
33
40
|
* initial CSS-var sync until the iframe document has booted.
|
|
34
41
|
*/
|
|
35
42
|
export declare function onIframeReady(expectedSource: Window | null, callback: () => void): () => void;
|
|
43
|
+
export {};
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
+
import { isExternalRuntimeIdentity, isNewExternalRuntimeSequence, isSerializableProps, matchesExternalRuntimeIdentity } from "../preview/external-runtime.js";
|
|
1
2
|
const BRIDGE_SOURCE = "zudo-sg-token-tweak-bridge";
|
|
2
3
|
function isBridgeMessage(value) {
|
|
3
|
-
if (!value
|
|
4
|
+
if (!isSerializableProps(value)) return false;
|
|
4
5
|
const v = value;
|
|
5
6
|
if (v.source !== BRIDGE_SOURCE) return false;
|
|
7
|
+
const strict = value;
|
|
8
|
+
if ((strict.identity !== void 0 || strict.sequence !== void 0) && (!isExternalRuntimeIdentity(strict.identity) || !isNewExternalRuntimeSequence(strict.sequence, -1))) return false;
|
|
6
9
|
switch (v.type) {
|
|
7
10
|
case "apply-css-vars":
|
|
8
11
|
return Array.isArray(v.vars) && v.vars.every(isCssVarPair);
|
|
9
12
|
case "clear-css-vars":
|
|
10
|
-
return Array.isArray(v.names) && v.names.every(
|
|
13
|
+
return Array.isArray(v.names) && v.names.every(isBridgeCssProperty);
|
|
11
14
|
case "ready":
|
|
12
15
|
return true;
|
|
13
16
|
default:
|
|
@@ -15,48 +18,74 @@ function isBridgeMessage(value) {
|
|
|
15
18
|
}
|
|
16
19
|
}
|
|
17
20
|
function isCssVarPair(value) {
|
|
18
|
-
return Array.isArray(value) && value.length === 2 &&
|
|
21
|
+
return Array.isArray(value) && value.length === 2 && isBridgeCssProperty(value[0]) && typeof value[1] === "string";
|
|
19
22
|
}
|
|
20
|
-
function
|
|
23
|
+
function isBridgeCssProperty(value) {
|
|
24
|
+
return value === "color-scheme" || typeof value === "string" && /^--(?:[A-Za-z0-9_-]|[^\u0000-\u007f])+$/u.test(value);
|
|
25
|
+
}
|
|
26
|
+
function sendApplyCssVars(iframe, vars, strict) {
|
|
21
27
|
const win = iframe?.contentWindow;
|
|
22
|
-
if (!win) return;
|
|
23
|
-
const message = { source: BRIDGE_SOURCE, type: "apply-css-vars", vars };
|
|
28
|
+
if (!win || window.location.origin === "null") return;
|
|
29
|
+
const message = { source: BRIDGE_SOURCE, type: "apply-css-vars", vars, ...strict };
|
|
24
30
|
win.postMessage(message, window.location.origin);
|
|
25
31
|
}
|
|
26
|
-
function sendClearCssVars(iframe, names) {
|
|
32
|
+
function sendClearCssVars(iframe, names, strict) {
|
|
27
33
|
const win = iframe?.contentWindow;
|
|
28
|
-
if (!win) return;
|
|
29
|
-
const message = { source: BRIDGE_SOURCE, type: "clear-css-vars", names };
|
|
34
|
+
if (!win || window.location.origin === "null") return;
|
|
35
|
+
const message = { source: BRIDGE_SOURCE, type: "clear-css-vars", names, ...strict };
|
|
30
36
|
win.postMessage(message, window.location.origin);
|
|
31
37
|
}
|
|
32
|
-
function installIframeReceiver(target = window) {
|
|
38
|
+
function installIframeReceiver(target = window, options = {}) {
|
|
39
|
+
let lastSequence = -1;
|
|
40
|
+
let acceptedIdentity = "";
|
|
41
|
+
const applied = /* @__PURE__ */ new Set();
|
|
33
42
|
function handler(event) {
|
|
34
|
-
if (event.origin !== target.location.origin) return;
|
|
43
|
+
if (target.location.origin === "null" || event.origin !== target.location.origin || event.source !== target.parent) return;
|
|
35
44
|
const data = event.data;
|
|
36
45
|
if (!isBridgeMessage(data)) return;
|
|
46
|
+
if (data.type === "ready") return;
|
|
47
|
+
if (options.identity) {
|
|
48
|
+
const identity = options.identity();
|
|
49
|
+
if (!identity || !data.identity || !matchesExternalRuntimeIdentity(data.identity, identity)) return;
|
|
50
|
+
const key = JSON.stringify(identity);
|
|
51
|
+
const previous = key === acceptedIdentity ? lastSequence : -1;
|
|
52
|
+
if (!isNewExternalRuntimeSequence(data.sequence, previous)) return;
|
|
53
|
+
acceptedIdentity = key;
|
|
54
|
+
lastSequence = data.sequence;
|
|
55
|
+
}
|
|
37
56
|
if (data.type === "apply-css-vars") {
|
|
38
57
|
const root = target.document.documentElement;
|
|
39
|
-
for (const [name, value] of data.vars)
|
|
58
|
+
for (const [name, value] of data.vars) {
|
|
59
|
+
root.style.setProperty(name, value);
|
|
60
|
+
applied.add(name);
|
|
61
|
+
}
|
|
40
62
|
return;
|
|
41
63
|
}
|
|
42
64
|
if (data.type === "clear-css-vars") {
|
|
43
65
|
const root = target.document.documentElement;
|
|
44
|
-
for (const name of data.names)
|
|
66
|
+
for (const name of data.names) {
|
|
67
|
+
root.style.removeProperty(name);
|
|
68
|
+
applied.delete(name);
|
|
69
|
+
}
|
|
45
70
|
return;
|
|
46
71
|
}
|
|
47
72
|
}
|
|
48
73
|
target.addEventListener("message", handler);
|
|
49
74
|
const parent = target.parent;
|
|
50
|
-
if (parent && parent !== target) {
|
|
75
|
+
if (!options.identity && parent && parent !== target && target.location.origin !== "null") {
|
|
51
76
|
const ready = { source: BRIDGE_SOURCE, type: "ready" };
|
|
52
77
|
parent.postMessage(ready, target.location.origin);
|
|
53
78
|
}
|
|
54
|
-
return () =>
|
|
79
|
+
return () => {
|
|
80
|
+
target.removeEventListener("message", handler);
|
|
81
|
+
for (const name of applied) target.document.documentElement.style.removeProperty(name);
|
|
82
|
+
applied.clear();
|
|
83
|
+
};
|
|
55
84
|
}
|
|
56
85
|
function onIframeReady(expectedSource, callback) {
|
|
57
86
|
function handler(event) {
|
|
58
|
-
if (event.origin !== window.location.origin) return;
|
|
59
|
-
if (expectedSource
|
|
87
|
+
if (window.location.origin === "null" || event.origin !== window.location.origin) return;
|
|
88
|
+
if (!expectedSource || event.source !== expectedSource) return;
|
|
60
89
|
if (!isBridgeMessage(event.data)) return;
|
|
61
90
|
if (event.data.type === "ready") callback();
|
|
62
91
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { BRIDGE_SOURCE, installIframeReceiver, isBridgeMessage, onIframeReady, sendApplyCssVars, sendClearCssVars, } from "./iframe-css-vars-bridge.js";
|
|
2
2
|
export type { ApplyCssVarsMessage, BridgeMessage, ClearCssVarsMessage, CssVarPair, ReadyMessage, } from "./iframe-css-vars-bridge.js";
|
|
3
|
-
export { applyPreviewVars, clearPreviewVars, registerPreviewIframe, unregisterPreviewIframe, } from "./preview-iframe-registry.js";
|
|
3
|
+
export { applyPreviewVars, clearPreviewVars, registerPreviewIframe, replayPreviewVars, unregisterPreviewIframe, } from "./preview-iframe-registry.js";
|
|
4
4
|
export { PREVIEW_TOKEN_PANEL_TOGGLE_EVENT, createPreviewTokenPanelConfig, } from "./preview-token-panel-config.js";
|
|
5
5
|
export type { PreviewTokenPanelConfigOptions, PreviewTokenPanelManifest, } from "./preview-token-panel-config.js";
|
|
6
6
|
export { drainPrehydrationToggle, installOwnerConsoleHelpers, loadZdtp } from "./token-panel-native-bootstrap.js";
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
applyPreviewVars,
|
|
11
11
|
clearPreviewVars,
|
|
12
12
|
registerPreviewIframe,
|
|
13
|
+
replayPreviewVars,
|
|
13
14
|
unregisterPreviewIframe
|
|
14
15
|
} from "./preview-iframe-registry.js";
|
|
15
16
|
import {
|
|
@@ -33,6 +34,7 @@ export {
|
|
|
33
34
|
onIframeReady,
|
|
34
35
|
previewTokenPanelCaptureScript,
|
|
35
36
|
registerPreviewIframe,
|
|
37
|
+
replayPreviewVars,
|
|
36
38
|
sendApplyCssVars,
|
|
37
39
|
sendClearCssVars,
|
|
38
40
|
unregisterPreviewIframe
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { type CssVarPair } from "./iframe-css-vars-bridge.js";
|
|
2
|
+
import type { ExternalRuntimeIdentity } from "../preview/external-runtime.js";
|
|
3
|
+
/**
|
|
4
|
+
* Replay the current sink overrides onto one iframe. Called on `onIframeReady`
|
|
5
|
+
* so a late-mounted or re-mounted iframe receives the active tweaks once its
|
|
6
|
+
* receiver is installed.
|
|
7
|
+
*/
|
|
8
|
+
export declare function replayPreviewVars(iframe: HTMLIFrameElement): void;
|
|
2
9
|
/**
|
|
3
10
|
* Push overrides to every registered preview iframe (sink/relay API). Upserts
|
|
4
11
|
* each pair into the current-override map so it is replayed to iframes that
|
|
@@ -11,6 +18,8 @@ export declare function applyPreviewVars(pairs: ReadonlyArray<CssVarPair>): void
|
|
|
11
18
|
*/
|
|
12
19
|
export declare function clearPreviewVars(names: ReadonlyArray<string>): void;
|
|
13
20
|
/** Register a preview iframe so future token tweaks are pushed to it. Idempotent. */
|
|
14
|
-
export declare function registerPreviewIframe(iframe: HTMLIFrameElement
|
|
21
|
+
export declare function registerPreviewIframe(iframe: HTMLIFrameElement, options?: {
|
|
22
|
+
identity?: () => ExternalRuntimeIdentity | null;
|
|
23
|
+
}): void;
|
|
15
24
|
/** Stop pushing token tweaks to an iframe (on unmount). Idempotent. */
|
|
16
25
|
export declare function unregisterPreviewIframe(iframe: HTMLIFrameElement): void;
|
|
@@ -5,35 +5,75 @@ import {
|
|
|
5
5
|
} from "./iframe-css-vars-bridge.js";
|
|
6
6
|
const registered = /* @__PURE__ */ new Set();
|
|
7
7
|
const readyTeardowns = /* @__PURE__ */ new Map();
|
|
8
|
+
const strictFrames = /* @__PURE__ */ new Map();
|
|
9
|
+
const sentNames = /* @__PURE__ */ new Map();
|
|
10
|
+
function bridgeFields(iframe) {
|
|
11
|
+
const state = strictFrames.get(iframe);
|
|
12
|
+
if (!state) return void 0;
|
|
13
|
+
const identity = state.identity();
|
|
14
|
+
if (!identity) return null;
|
|
15
|
+
const key = JSON.stringify(identity);
|
|
16
|
+
if (state.key !== key) {
|
|
17
|
+
state.key = key;
|
|
18
|
+
state.sequence = -1;
|
|
19
|
+
}
|
|
20
|
+
return { identity, sequence: ++state.sequence };
|
|
21
|
+
}
|
|
8
22
|
const previewOverrides = /* @__PURE__ */ new Map();
|
|
9
23
|
function overridePairs() {
|
|
10
24
|
return [...previewOverrides].map(([name, value]) => [name, value]);
|
|
11
25
|
}
|
|
12
|
-
function
|
|
26
|
+
function replayPreviewVars(iframe) {
|
|
13
27
|
const pairs = overridePairs();
|
|
14
|
-
|
|
28
|
+
const fields = bridgeFields(iframe);
|
|
29
|
+
if (fields === null) return;
|
|
30
|
+
const previous = sentNames.get(iframe) ?? /* @__PURE__ */ new Set();
|
|
31
|
+
const stale = [...previous].filter((name) => !previewOverrides.has(name));
|
|
32
|
+
if (stale.length) sendClearCssVars(iframe, stale, fields);
|
|
33
|
+
if (pairs.length > 0) sendApplyCssVars(iframe, pairs, stale.length ? bridgeFields(iframe) ?? void 0 : fields);
|
|
34
|
+
sentNames.set(iframe, new Set(previewOverrides.keys()));
|
|
15
35
|
}
|
|
16
36
|
function applyPreviewVars(pairs) {
|
|
17
37
|
if (pairs.length === 0) return;
|
|
18
38
|
for (const [name, value] of pairs) previewOverrides.set(name, value);
|
|
19
|
-
for (const iframe of registered)
|
|
39
|
+
for (const iframe of registered) {
|
|
40
|
+
const fields = bridgeFields(iframe);
|
|
41
|
+
if (fields !== null) {
|
|
42
|
+
sendApplyCssVars(iframe, pairs, fields);
|
|
43
|
+
const names = sentNames.get(iframe) ?? /* @__PURE__ */ new Set();
|
|
44
|
+
for (const [name] of pairs) names.add(name);
|
|
45
|
+
sentNames.set(iframe, names);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
20
48
|
}
|
|
21
49
|
function clearPreviewVars(names) {
|
|
22
50
|
if (names.length === 0) return;
|
|
23
51
|
for (const name of names) previewOverrides.delete(name);
|
|
24
|
-
for (const iframe of registered)
|
|
52
|
+
for (const iframe of registered) {
|
|
53
|
+
const fields = bridgeFields(iframe);
|
|
54
|
+
if (fields !== null) {
|
|
55
|
+
sendClearCssVars(iframe, names, fields);
|
|
56
|
+
for (const name of names) sentNames.get(iframe)?.delete(name);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
25
59
|
}
|
|
26
|
-
function registerPreviewIframe(iframe) {
|
|
60
|
+
function registerPreviewIframe(iframe, options = {}) {
|
|
27
61
|
if (registered.has(iframe)) return;
|
|
28
62
|
registered.add(iframe);
|
|
63
|
+
if (options.identity) {
|
|
64
|
+
strictFrames.set(iframe, { identity: options.identity, key: "", sequence: -1 });
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
29
67
|
const teardown = onIframeReady(iframe.contentWindow, () => {
|
|
30
|
-
|
|
68
|
+
replayPreviewVars(iframe);
|
|
31
69
|
});
|
|
32
70
|
readyTeardowns.set(iframe, teardown);
|
|
33
|
-
|
|
71
|
+
replayPreviewVars(iframe);
|
|
34
72
|
}
|
|
35
73
|
function unregisterPreviewIframe(iframe) {
|
|
36
74
|
registered.delete(iframe);
|
|
75
|
+
strictFrames.delete(iframe);
|
|
76
|
+
sentNames.delete(iframe);
|
|
37
77
|
const teardown = readyTeardowns.get(iframe);
|
|
38
78
|
if (teardown) {
|
|
39
79
|
teardown();
|
|
@@ -44,5 +84,6 @@ export {
|
|
|
44
84
|
applyPreviewVars,
|
|
45
85
|
clearPreviewVars,
|
|
46
86
|
registerPreviewIframe,
|
|
87
|
+
replayPreviewVars,
|
|
47
88
|
unregisterPreviewIframe
|
|
48
89
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@takazudo/zudo-sg",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "zudo-sg styleguide engine — story registry, catalog routes and preview machinery for zfb + zudo-doc hosts.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -63,6 +63,22 @@
|
|
|
63
63
|
"types": "./dist/preview/messages.d.ts",
|
|
64
64
|
"default": "./dist/preview/messages.js"
|
|
65
65
|
},
|
|
66
|
+
"./preview/external-runtime": {
|
|
67
|
+
"types": "./dist/preview/external-runtime.d.ts",
|
|
68
|
+
"default": "./dist/preview/external-runtime.js"
|
|
69
|
+
},
|
|
70
|
+
"./preview/runtime": {
|
|
71
|
+
"types": "./dist/preview/runtime-controller.d.ts",
|
|
72
|
+
"default": "./dist/preview/runtime-controller.js"
|
|
73
|
+
},
|
|
74
|
+
"./registry/external-catalog": {
|
|
75
|
+
"types": "./dist/registry/external-catalog.d.ts",
|
|
76
|
+
"default": "./dist/registry/external-catalog.js"
|
|
77
|
+
},
|
|
78
|
+
"./registry/external-catalog-node": {
|
|
79
|
+
"types": "./dist/registry/external-catalog-node.d.ts",
|
|
80
|
+
"default": "./dist/registry/external-catalog-node.js"
|
|
81
|
+
},
|
|
66
82
|
"./code-panel": {
|
|
67
83
|
"types": "./dist/code-panel/index.d.ts",
|
|
68
84
|
"default": "./dist/code-panel/index.js"
|
|
@@ -131,13 +147,13 @@
|
|
|
131
147
|
"README.md"
|
|
132
148
|
],
|
|
133
149
|
"dependencies": {
|
|
134
|
-
"@codemirror/commands": "^6.
|
|
150
|
+
"@codemirror/commands": "^6.11.1",
|
|
135
151
|
"@codemirror/lang-css": "^6.0.0",
|
|
136
152
|
"@codemirror/lang-javascript": "^6.0.0",
|
|
137
|
-
"@codemirror/language": "^6.
|
|
138
|
-
"@codemirror/state": "^6.
|
|
139
|
-
"@codemirror/view": "^6.
|
|
140
|
-
"@lezer/highlight": "^1.
|
|
153
|
+
"@codemirror/language": "^6.13.1",
|
|
154
|
+
"@codemirror/state": "^6.7.6",
|
|
155
|
+
"@codemirror/view": "^6.43.14",
|
|
156
|
+
"@lezer/highlight": "^1.2.5",
|
|
141
157
|
"postcss": "^8.5.28",
|
|
142
158
|
"preact": "^10.29.1"
|
|
143
159
|
},
|
|
@@ -150,13 +166,14 @@
|
|
|
150
166
|
"@takazudo/zdtp": "0.8.6",
|
|
151
167
|
"@takazudo/zfb": "4.3.1",
|
|
152
168
|
"@takazudo/zudo-doc": "6.0.0",
|
|
153
|
-
"@testing-library/jest-dom": "^
|
|
169
|
+
"@testing-library/jest-dom": "^7.0.1",
|
|
154
170
|
"@types/culori": "^4.0.1",
|
|
155
|
-
"@types/node": "^22.
|
|
171
|
+
"@types/node": "^22.20.5",
|
|
156
172
|
"culori": "^4.0.2",
|
|
157
|
-
"happy-dom": "
|
|
173
|
+
"happy-dom": "20.14.6",
|
|
158
174
|
"tsup": "^8.5.0",
|
|
159
|
-
"typescript": "
|
|
175
|
+
"@typescript/native": "npm:typescript@^7.0.2",
|
|
176
|
+
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
160
177
|
"vite": "7.3.5",
|
|
161
178
|
"vitest": "4.1.11"
|
|
162
179
|
},
|
|
@@ -8,9 +8,8 @@
|
|
|
8
8
|
// code panel's CSS injection selects iframes by it). The optional co-located
|
|
9
9
|
// component MDX doc renders as a trailing section.
|
|
10
10
|
//
|
|
11
|
-
// Descriptor
|
|
12
|
-
//
|
|
13
|
-
// workbench, built from the mode-neutral `CatalogEntry` fields.
|
|
11
|
+
// Descriptor entries may opt into source text without supplying render functions.
|
|
12
|
+
// Source-less entries retain their workbench-only behavior.
|
|
14
13
|
|
|
15
14
|
import type { Description } from "@takazudo/zfb/zudo-react";
|
|
16
15
|
import type { JSX } from "@takazudo/zfb/zudo-react/jsx-runtime";
|
|
@@ -71,12 +70,16 @@ export default function ComponentsSlugRoute(props: SlugProps & { params: { slug:
|
|
|
71
70
|
const storyEntry = entry.source === "module" ? entry.storyEntry : undefined;
|
|
72
71
|
|
|
73
72
|
let codePanel: Description | null = null;
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
const panelVariants: CodePanelVariant[] = storyEntry
|
|
74
|
+
? storyEntry.variants.map((v) => ({
|
|
76
75
|
exportName: v.exportName,
|
|
77
76
|
name: v.name,
|
|
78
77
|
source: v.story.source ?? storyEntry.meta.usage,
|
|
79
|
-
}))
|
|
78
|
+
}))
|
|
79
|
+
: entry.variants.flatMap((v) => v.source === undefined ? [] : [{
|
|
80
|
+
exportName: v.exportName, name: v.name, source: v.source.code,
|
|
81
|
+
}]);
|
|
82
|
+
if (panelVariants.length > 0) {
|
|
80
83
|
const codePanelIsland = (
|
|
81
84
|
<Island when="load">
|
|
82
85
|
<CodePanel
|
|
@@ -113,8 +116,7 @@ export default function ComponentsSlugRoute(props: SlugProps & { params: { slug:
|
|
|
113
116
|
//
|
|
114
117
|
// The engine's own detail route opts the two toolbar groups that default OFF
|
|
115
118
|
// (#883, issue #872) back in explicitly, matching what this route actually
|
|
116
|
-
// mounts: `codePanel` only when
|
|
117
|
-
// CodePanel island above is mounted (descriptor-mode entries get none, S2b) —
|
|
119
|
+
// mounts: `codePanel` only when real source variants mount a CodePanel —
|
|
118
120
|
// and `tokenPanel` following `ctx.previewTokenPanel`, so the button is absent
|
|
119
121
|
// rather than dead when no `zdtpApplyProxy.tabsModule` is wired.
|
|
120
122
|
// `selection`/`theme` are passed at their pre-#883 defaults, unconditionally,
|
|
@@ -124,6 +126,7 @@ export default function ComponentsSlugRoute(props: SlugProps & { params: { slug:
|
|
|
124
126
|
<DetailWorkbench
|
|
125
127
|
slug={slug}
|
|
126
128
|
previewUrl={previewUrl}
|
|
129
|
+
runtime={ctx.externalPreviewRuntime ?? "legacy"}
|
|
127
130
|
variants={entry.variants.map((v) => ({
|
|
128
131
|
exportName: v.exportName,
|
|
129
132
|
name: v.name,
|
|
@@ -131,7 +134,7 @@ export default function ComponentsSlugRoute(props: SlugProps & { params: { slug:
|
|
|
131
134
|
}))}
|
|
132
135
|
selection={{ mode: "all" }}
|
|
133
136
|
theme={{ mode: "toolbar" }}
|
|
134
|
-
toolbar={{ codePanel:
|
|
137
|
+
toolbar={{ codePanel: codePanel !== null, tokenPanel: ctx.previewTokenPanel }}
|
|
135
138
|
/>
|
|
136
139
|
</Island>
|
|
137
140
|
);
|