storybook-addon-sdc 0.24.6 → 0.24.8
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/dist/preset.js +72 -19
- package/package.json +1 -1
package/dist/preset.js
CHANGED
|
@@ -154,9 +154,24 @@ var renderComponent = (item) => {
|
|
|
154
154
|
return `${kebabCaseName}.default.component({...${kebabCaseName}.Basic.baseArgs, ${storyArgs}, ${componentProps}})`;
|
|
155
155
|
};
|
|
156
156
|
var renderImage = (item) => {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
)
|
|
157
|
+
const attrs = { ...item.attributes ?? {} };
|
|
158
|
+
if (item.uri != null) attrs.src = item.uri;
|
|
159
|
+
if (Array.isArray(item.srcset) && item.srcset.length > 0) {
|
|
160
|
+
attrs.srcset = item.srcset.map(
|
|
161
|
+
(s) => s && typeof s === "object" ? [s.uri, s.width].filter(Boolean).join(" ") : s
|
|
162
|
+
).join(", ");
|
|
163
|
+
}
|
|
164
|
+
const mapped = {
|
|
165
|
+
width: item.width,
|
|
166
|
+
height: item.height,
|
|
167
|
+
alt: item.alt ?? "",
|
|
168
|
+
sizes: item.sizes,
|
|
169
|
+
title: item.title
|
|
170
|
+
};
|
|
171
|
+
for (const key of ["width", "height", "alt", "sizes", "title"]) {
|
|
172
|
+
if (mapped[key] != null) attrs[key] = mapped[key];
|
|
173
|
+
}
|
|
174
|
+
return JSON.stringify(`<img${toAttributes(attrs)} />`);
|
|
160
175
|
};
|
|
161
176
|
var renderElement = (item) => {
|
|
162
177
|
return JSON.stringify(
|
|
@@ -239,9 +254,12 @@ ${globalsBlock}
|
|
|
239
254
|
const wrapper = ${JSON.stringify(library_wrapper)};
|
|
240
255
|
if (!wrapper) return Story();
|
|
241
256
|
|
|
242
|
-
|
|
243
|
-
const
|
|
244
|
-
|
|
257
|
+
const placeholder = '___SDC_STORY___';
|
|
258
|
+
const rendered = _sdcRenderInline(
|
|
259
|
+
wrapper.replace(/\\{\\{\\s*_story\\s*\\}\\}/g, placeholder),
|
|
260
|
+
{}
|
|
261
|
+
);
|
|
262
|
+
return rendered.split(placeholder).join(Story());
|
|
245
263
|
}
|
|
246
264
|
],` : ""}
|
|
247
265
|
play: async ({ canvasElement }) => {
|
|
@@ -472,7 +490,7 @@ var toNamespaces = (namespaceDefinition) => {
|
|
|
472
490
|
return new Namespaces(namespaceDefinition ?? {});
|
|
473
491
|
};
|
|
474
492
|
|
|
475
|
-
// src/vite-plugin-sdc-
|
|
493
|
+
// src/vite-plugin-sdc-twig-runtime.ts
|
|
476
494
|
import { existsSync as existsSync3, readdirSync as readdirSync3, readFileSync as readFileSync2 } from "fs";
|
|
477
495
|
import { join as join3 } from "path";
|
|
478
496
|
import { parse as parseYaml2 } from "yaml";
|
|
@@ -675,7 +693,7 @@ async function fetchRemoteSprite(pack, fetchSvg) {
|
|
|
675
693
|
return content;
|
|
676
694
|
}
|
|
677
695
|
|
|
678
|
-
// src/vite-plugin-sdc-
|
|
696
|
+
// src/vite-plugin-sdc-twig-runtime.ts
|
|
679
697
|
var remoteSvgCache = /* @__PURE__ */ new Map();
|
|
680
698
|
async function fetchSvgFromUrl(url) {
|
|
681
699
|
if (remoteSvgCache.has(url)) return remoteSvgCache.get(url) ?? null;
|
|
@@ -719,9 +737,9 @@ function readNamespaceYamlData(namespaces) {
|
|
|
719
737
|
}
|
|
720
738
|
return out;
|
|
721
739
|
}
|
|
722
|
-
var VIRTUAL_TWIG = "virtual:sdc-
|
|
740
|
+
var VIRTUAL_TWIG = "virtual:sdc-twig-runtime:twig";
|
|
723
741
|
var RESOLVED_TWIG = "\0" + VIRTUAL_TWIG;
|
|
724
|
-
var VIRTUAL_TWING = "virtual:sdc-
|
|
742
|
+
var VIRTUAL_TWING = "virtual:sdc-twig-runtime:twing";
|
|
725
743
|
var RESOLVED_TWING = "\0" + VIRTUAL_TWING;
|
|
726
744
|
var PACK_PREFIX = "\0icons-pack:";
|
|
727
745
|
var SHARED_HELPERS = (
|
|
@@ -809,7 +827,19 @@ export function renderIcon(packId, iconId, settings) {
|
|
|
809
827
|
}
|
|
810
828
|
}
|
|
811
829
|
|
|
812
|
-
|
|
830
|
+
// Render an inline Twig template string (e.g. a story library_wrapper) so
|
|
831
|
+
// {{ include(...) }} and other Twig works; {{ _story }} comes from context.
|
|
832
|
+
export function renderInline(template, context) {
|
|
833
|
+
if (!_sdcTwig || !template) return template || '';
|
|
834
|
+
try {
|
|
835
|
+
return _sdcTwig.twig({ data: template }).render(context || {});
|
|
836
|
+
} catch (e) {
|
|
837
|
+
console.error('[SDC] renderInline error:', e);
|
|
838
|
+
return template;
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
export function registerSdcRuntime(Twig) {
|
|
813
843
|
_sdcTwig = Twig;
|
|
814
844
|
if (Twig.__sdcIconRegistered) return;
|
|
815
845
|
Twig.__sdcIconRegistered = true;
|
|
@@ -853,7 +883,30 @@ export function renderIcon(packId, iconId, settings) {
|
|
|
853
883
|
}
|
|
854
884
|
}
|
|
855
885
|
|
|
856
|
-
|
|
886
|
+
// Render an inline Twig template string (e.g. a story library_wrapper) through
|
|
887
|
+
// the captured SDC environment, so {{ include(...) }} resolves against the same
|
|
888
|
+
// loader and Twig works; {{ _story }} comes from context.
|
|
889
|
+
var _sdcInlineNames = {};
|
|
890
|
+
var _sdcInlineSeq = 0;
|
|
891
|
+
export function renderInline(template, context) {
|
|
892
|
+
if (!_sdcIconEnv || !template) return template || '';
|
|
893
|
+
var loader = _sdcIconEnv.loader;
|
|
894
|
+
if (!loader || typeof loader.setTemplate !== 'function') return template;
|
|
895
|
+
var name = _sdcInlineNames[template];
|
|
896
|
+
if (!name) {
|
|
897
|
+
name = '_sdc_inline_' + (_sdcInlineSeq++);
|
|
898
|
+
_sdcInlineNames[template] = name;
|
|
899
|
+
loader.setTemplate(name, template);
|
|
900
|
+
}
|
|
901
|
+
try {
|
|
902
|
+
return _sdcIconEnv.render(name, context || {});
|
|
903
|
+
} catch (e) {
|
|
904
|
+
console.error('[SDC] renderInline error:', e);
|
|
905
|
+
return template;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
export function registerSdcRuntime(env) {
|
|
857
910
|
_sdcIconEnv = env;
|
|
858
911
|
// Register icon templates into the existing loader so env.render() finds them.
|
|
859
912
|
// createSynchronousArrayLoader (used by vite-plugin-twing-drupal's SDC loader)
|
|
@@ -895,10 +948,10 @@ var TWIG_JS_MARKER = "from 'drupal-twig-extensions/twig'";
|
|
|
895
948
|
var TWIG_JS_INJECT_AFTER = "addDrupalExtensions(Twig);";
|
|
896
949
|
var TWING_MARKER = "createSynchronousEnvironment";
|
|
897
950
|
var TWING_INJECT_AFTER = "addDrupalExtensions(env);";
|
|
898
|
-
var INJECTED_GUARD = "
|
|
899
|
-
function
|
|
951
|
+
var INJECTED_GUARD = "_sdcRegisterRuntime";
|
|
952
|
+
function sdcTwigRuntimePlugin(namespaces, resolveIconSource) {
|
|
900
953
|
return {
|
|
901
|
-
name: "vite-plugin-sdc-
|
|
954
|
+
name: "vite-plugin-sdc-twig-runtime",
|
|
902
955
|
resolveId(id) {
|
|
903
956
|
if (id === VIRTUAL_TWIG) return RESOLVED_TWIG;
|
|
904
957
|
if (id === VIRTUAL_TWING) return RESOLVED_TWING;
|
|
@@ -969,7 +1022,7 @@ if (typeof document !== 'undefined') {
|
|
|
969
1022
|
if (code.includes(INJECTED_GUARD)) return null;
|
|
970
1023
|
if (code.includes(TWING_MARKER) && code.includes(TWING_INJECT_AFTER)) {
|
|
971
1024
|
return {
|
|
972
|
-
code: `import {
|
|
1025
|
+
code: `import { registerSdcRuntime as ${INJECTED_GUARD} } from '${VIRTUAL_TWING}';
|
|
973
1026
|
` + code.replace(
|
|
974
1027
|
TWING_INJECT_AFTER,
|
|
975
1028
|
`${TWING_INJECT_AFTER}
|
|
@@ -980,7 +1033,7 @@ ${INJECTED_GUARD}(env);`
|
|
|
980
1033
|
}
|
|
981
1034
|
if (code.includes(TWIG_JS_MARKER) && code.includes(TWIG_JS_INJECT_AFTER)) {
|
|
982
1035
|
return {
|
|
983
|
-
code: `import {
|
|
1036
|
+
code: `import { registerSdcRuntime as ${INJECTED_GUARD} } from '${VIRTUAL_TWIG}';
|
|
984
1037
|
` + code.replace(
|
|
985
1038
|
TWIG_JS_INJECT_AFTER,
|
|
986
1039
|
`${TWIG_JS_INJECT_AFTER}
|
|
@@ -1212,7 +1265,7 @@ var vite_plugin_storybook_yaml_stories_default = ({
|
|
|
1212
1265
|
import COMPONENT from '${namespace}/${componentBaseName}.twig';
|
|
1213
1266
|
void import.meta.glob('./*.css', { eager: true });
|
|
1214
1267
|
import { injectAssets } from '${VIRTUAL_ASSET_INJECTOR}';
|
|
1215
|
-
import { renderIcon as _sdcRenderIcon } from '${iconModule}';
|
|
1268
|
+
import { renderIcon as _sdcRenderIcon, renderInline as _sdcRenderInline } from '${iconModule}';
|
|
1216
1269
|
${storiesImports}
|
|
1217
1270
|
${twigImports}
|
|
1218
1271
|
${assetInjection}
|
|
@@ -1455,7 +1508,7 @@ async function viteFinal(config, options) {
|
|
|
1455
1508
|
}),
|
|
1456
1509
|
...twigPlugin ? [twigPlugin] : [],
|
|
1457
1510
|
vite_plugin_storybook_yaml_stories_default({ ...options, globalDefs, namespaces }),
|
|
1458
|
-
|
|
1511
|
+
sdcTwigRuntimePlugin(namespaces, sdcStorybookOptions.resolveIconSource),
|
|
1459
1512
|
...headTags.length > 0 ? [{ name: "vite-plugin-sdc-head", transformIndexHtml: () => headTags }] : []
|
|
1460
1513
|
],
|
|
1461
1514
|
optimizeDeps: {
|