storybook-addon-sdc 0.24.13 → 0.24.14

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 CHANGED
@@ -759,6 +759,8 @@ function generateRuntimeGlue(packFilePaths, lib) {
759
759
  ${imports}
760
760
  import { ${factory} } from 'storybook-addon-sdc/runtime/${lib}';
761
761
 
762
+ export { PrintableArray } from 'storybook-addon-sdc/runtime/${lib}';
763
+
762
764
  export const { renderIcon, renderInline, makeStory, registerSdcRuntime } =
763
765
  ${factory}(${mergeExpr});
764
766
  `;
@@ -1150,17 +1152,12 @@ void import.meta.glob('./*.css', { eager: true });
1150
1152
  ${localCssImports}
1151
1153
  ${localCssUrlImports}
1152
1154
  import { injectAssets } from '${VIRTUAL_ASSET_INJECTOR}';
1153
- import { renderIcon as _sdcRenderIcon, renderInline as _sdcRenderInline, makeStory as _sdcMakeStory } from '${iconModule}';
1155
+ import { renderIcon as _sdcRenderIcon, renderInline as _sdcRenderInline, makeStory as _sdcMakeStory, PrintableArray as TwigSafeArray } from '${iconModule}';
1154
1156
  ${storiesImports}
1155
1157
  ${twigImports}
1156
1158
  ${assetInjection}
1157
1159
  ${localJsImports}
1158
1160
  await Promise.all(Object.values(import.meta.glob('./*.{js,mjs}')).map(fn => fn()));
1159
- class TwigSafeArray extends Array {
1160
- toString() {
1161
- return this.join('');
1162
- }
1163
- }
1164
1161
 
1165
1162
  export default {
1166
1163
  component: COMPONENT,
@@ -1,6 +1,19 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+
1
5
  // src/runtime/twig.ts
2
6
  import DrupalAttribute3 from "drupal-attribute";
3
7
 
8
+ // src/runtime/collections.ts
9
+ function entries(value) {
10
+ if (value == null) return [];
11
+ if (value instanceof Map) return [...value.entries()];
12
+ if (Array.isArray(value)) return value.map((x, i) => [i, x]);
13
+ if (typeof value === "object") return Object.entries(value);
14
+ return [];
15
+ }
16
+
4
17
  // src/runtime/iconContext.ts
5
18
  import DrupalAttribute from "drupal-attribute";
6
19
  function buildIconContext(pack, iconId, settings) {
@@ -44,27 +57,13 @@ function buildIconContext(pack, iconId, settings) {
44
57
 
45
58
  // src/runtime/attributes.ts
46
59
  import DrupalAttribute2 from "drupal-attribute";
47
-
48
- // src/runtime/collections.ts
49
- function entries(value) {
50
- if (value == null) return [];
51
- if (value instanceof Map) return [...value.entries()];
52
- if (Array.isArray(value)) return value.map((x, i) => [i, x]);
53
- if (typeof value === "object") return Object.entries(value);
54
- return [];
55
- }
56
- function toPlain(value) {
57
- if (value instanceof Map && typeof value.addClass !== "function") {
58
- const out = {};
59
- value.forEach((v, k) => {
60
- out[k] = toPlain(v);
61
- });
62
- return out;
63
- }
64
- return value;
60
+ function toAttribute(value) {
61
+ const v = value;
62
+ if (v && typeof v.addClass === "function") return value;
63
+ return new DrupalAttribute2(
64
+ entries(value)
65
+ );
65
66
  }
66
-
67
- // src/runtime/attributes.ts
68
67
  function patchDrupalAttribute(AttrClass) {
69
68
  const proto = AttrClass.prototype;
70
69
  if (!proto || proto.__sdcClassPatched) return;
@@ -106,10 +105,12 @@ function isStory(value) {
106
105
  }
107
106
  return false;
108
107
  }
109
- function storyContext(story, base) {
110
- const props = toPlain(story["#props"]) || {};
111
- const slots = toPlain(story["#slots"]) || {};
112
- return Object.assign({}, base && base.context, props, slots);
108
+ function storyContext(story, base, resolveValue = (v) => v) {
109
+ const ctx = Object.assign({}, base && base.context);
110
+ for (const src of [story["#props"], story["#slots"]]) {
111
+ for (const [k, v] of entries(src)) ctx[k] = resolveValue(v);
112
+ }
113
+ return ctx;
113
114
  }
114
115
  function createStoryFactory(renderStory) {
115
116
  const storyBase = /* @__PURE__ */ new WeakMap();
@@ -137,12 +138,109 @@ function createStoryFactory(renderStory) {
137
138
  return { makeStory, mergeStory };
138
139
  }
139
140
 
141
+ // src/runtime/nodes.ts
142
+ var PrintableArray = class {
143
+ constructor(...items) {
144
+ __publicField(this, "length");
145
+ for (let i = 0; i < items.length; i++) this[i] = items[i];
146
+ this.length = items.length;
147
+ }
148
+ [Symbol.iterator]() {
149
+ let i = 0;
150
+ return {
151
+ next: () => i < this.length ? { value: this[i++], done: false } : { value: void 0, done: true }
152
+ };
153
+ }
154
+ toString() {
155
+ return Array.prototype.join.call(this, "");
156
+ }
157
+ };
158
+ function nodeGet(node, key) {
159
+ return node instanceof Map ? node.get(key) : node[key];
160
+ }
161
+ var NODE_TYPES = /* @__PURE__ */ new Set([
162
+ "component",
163
+ "html_tag",
164
+ "element",
165
+ "image",
166
+ "icon"
167
+ ]);
168
+ function isNode(value) {
169
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
170
+ if (typeof value.addClass === "function")
171
+ return false;
172
+ const type = nodeGet(value, "type");
173
+ if (NODE_TYPES.has(type)) return true;
174
+ if (type === "markup" || typeof nodeGet(value, "markup") === "string")
175
+ return true;
176
+ const theme = nodeGet(value, "theme");
177
+ return theme === "image" || theme === "element";
178
+ }
179
+ function attributesString(node) {
180
+ return toAttribute(nodeGet(node, "attributes")).toString();
181
+ }
182
+ function createNodeResolver(renderers) {
183
+ function resolve(value) {
184
+ if (value == null) return value;
185
+ if (Array.isArray(value)) return new PrintableArray(...value.map(resolve));
186
+ if (isNode(value)) return renderNode(value);
187
+ return value;
188
+ }
189
+ function toHtml(value) {
190
+ const r = resolve(value);
191
+ return r == null ? "" : String(r);
192
+ }
193
+ function renderNode(node) {
194
+ const type = nodeGet(node, "type") ?? nodeGet(node, "theme");
195
+ if (type === "markup" || typeof nodeGet(node, "markup") === "string") {
196
+ return String(nodeGet(node, "markup") ?? "");
197
+ }
198
+ if (type === "component") return renderers.renderComponent(node);
199
+ if (type === "icon") {
200
+ return renderers.renderIcon(
201
+ String(nodeGet(node, "pack_id") ?? ""),
202
+ String(nodeGet(node, "icon_id") ?? ""),
203
+ nodeGet(node, "settings") ?? {}
204
+ );
205
+ }
206
+ if (type === "image") {
207
+ const attrs = toAttribute(nodeGet(node, "attributes"));
208
+ if (nodeGet(node, "uri") != null) {
209
+ attrs.setAttribute("src", nodeGet(node, "uri"));
210
+ }
211
+ for (const k of ["width", "height", "alt", "sizes", "title"]) {
212
+ if (nodeGet(node, k) != null)
213
+ attrs.setAttribute(k, nodeGet(node, k));
214
+ }
215
+ return `<img${attrs.toString()} />`;
216
+ }
217
+ const tag = String(nodeGet(node, "tag") ?? "div");
218
+ const children = toHtml(nodeGet(node, "value") ?? nodeGet(node, "children"));
219
+ return `<${tag}${attributesString(node)}>${children}</${tag}>`;
220
+ }
221
+ return { resolve };
222
+ }
223
+
140
224
  // src/runtime/twig.ts
141
225
  function createTwigRuntime(iconPacks) {
142
226
  let twig = null;
143
227
  patchDrupalAttribute(DrupalAttribute3);
228
+ function renderComponentNode(node) {
229
+ const id = String(nodeGet(node, "component") ?? "");
230
+ if (!twig || !id) return "";
231
+ const vars = {};
232
+ for (const src of [nodeGet(node, "props"), nodeGet(node, "slots")]) {
233
+ for (const [k, v] of entries(src)) vars[k] = resolve(v);
234
+ }
235
+ vars["attributes"] = toAttribute(vars["attributes"]);
236
+ return renderInline("{% include '" + id + "' with vars only %}", { vars });
237
+ }
238
+ const { resolve } = createNodeResolver({
239
+ renderComponent: renderComponentNode,
240
+ renderIcon: (packId, iconId, settings) => renderIcon(packId, iconId, settings)
241
+ });
144
242
  function renderStory(story, base) {
145
- const ctx = storyContext(story, base);
243
+ const ctx = storyContext(story, base, resolve);
146
244
  if (base && typeof base.render === "function") {
147
245
  try {
148
246
  return base.render(ctx);
@@ -208,5 +306,6 @@ function createTwigRuntime(iconPacks) {
208
306
  return { renderIcon, renderInline, makeStory, registerSdcRuntime };
209
307
  }
210
308
  export {
309
+ PrintableArray,
211
310
  createTwigRuntime
212
311
  };
@@ -1,3 +1,7 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+
1
5
  // src/runtime/twing.ts
2
6
  import {
3
7
  createSynchronousFunction,
@@ -5,6 +9,15 @@ import {
5
9
  } from "twing";
6
10
  import DrupalAttribute3 from "drupal-attribute";
7
11
 
12
+ // src/runtime/collections.ts
13
+ function entries(value) {
14
+ if (value == null) return [];
15
+ if (value instanceof Map) return [...value.entries()];
16
+ if (Array.isArray(value)) return value.map((x, i) => [i, x]);
17
+ if (typeof value === "object") return Object.entries(value);
18
+ return [];
19
+ }
20
+
8
21
  // src/runtime/iconContext.ts
9
22
  import DrupalAttribute from "drupal-attribute";
10
23
  function buildIconContext(pack, iconId, settings) {
@@ -48,27 +61,6 @@ function buildIconContext(pack, iconId, settings) {
48
61
 
49
62
  // src/runtime/attributes.ts
50
63
  import DrupalAttribute2 from "drupal-attribute";
51
-
52
- // src/runtime/collections.ts
53
- function entries(value) {
54
- if (value == null) return [];
55
- if (value instanceof Map) return [...value.entries()];
56
- if (Array.isArray(value)) return value.map((x, i) => [i, x]);
57
- if (typeof value === "object") return Object.entries(value);
58
- return [];
59
- }
60
- function toPlain(value) {
61
- if (value instanceof Map && typeof value.addClass !== "function") {
62
- const out = {};
63
- value.forEach((v, k) => {
64
- out[k] = toPlain(v);
65
- });
66
- return out;
67
- }
68
- return value;
69
- }
70
-
71
- // src/runtime/attributes.ts
72
64
  function toAttribute(value) {
73
65
  const v = value;
74
66
  if (v && typeof v.addClass === "function") return value;
@@ -131,10 +123,12 @@ function isStory(value) {
131
123
  }
132
124
  return false;
133
125
  }
134
- function storyContext(story, base) {
135
- const props = toPlain(story["#props"]) || {};
136
- const slots = toPlain(story["#slots"]) || {};
137
- return Object.assign({}, base && base.context, props, slots);
126
+ function storyContext(story, base, resolveValue = (v) => v) {
127
+ const ctx = Object.assign({}, base && base.context);
128
+ for (const src of [story["#props"], story["#slots"]]) {
129
+ for (const [k, v] of entries(src)) ctx[k] = resolveValue(v);
130
+ }
131
+ return ctx;
138
132
  }
139
133
  function createStoryFactory(renderStory) {
140
134
  const storyBase = /* @__PURE__ */ new WeakMap();
@@ -162,15 +156,117 @@ function createStoryFactory(renderStory) {
162
156
  return { makeStory, mergeStory };
163
157
  }
164
158
 
159
+ // src/runtime/nodes.ts
160
+ var PrintableArray = class {
161
+ constructor(...items) {
162
+ __publicField(this, "length");
163
+ for (let i = 0; i < items.length; i++) this[i] = items[i];
164
+ this.length = items.length;
165
+ }
166
+ [Symbol.iterator]() {
167
+ let i = 0;
168
+ return {
169
+ next: () => i < this.length ? { value: this[i++], done: false } : { value: void 0, done: true }
170
+ };
171
+ }
172
+ toString() {
173
+ return Array.prototype.join.call(this, "");
174
+ }
175
+ };
176
+ function nodeGet(node, key) {
177
+ return node instanceof Map ? node.get(key) : node[key];
178
+ }
179
+ var NODE_TYPES = /* @__PURE__ */ new Set([
180
+ "component",
181
+ "html_tag",
182
+ "element",
183
+ "image",
184
+ "icon"
185
+ ]);
186
+ function isNode(value) {
187
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
188
+ if (typeof value.addClass === "function")
189
+ return false;
190
+ const type = nodeGet(value, "type");
191
+ if (NODE_TYPES.has(type)) return true;
192
+ if (type === "markup" || typeof nodeGet(value, "markup") === "string")
193
+ return true;
194
+ const theme = nodeGet(value, "theme");
195
+ return theme === "image" || theme === "element";
196
+ }
197
+ function attributesString(node) {
198
+ return toAttribute(nodeGet(node, "attributes")).toString();
199
+ }
200
+ function createNodeResolver(renderers) {
201
+ function resolve(value) {
202
+ if (value == null) return value;
203
+ if (Array.isArray(value)) return new PrintableArray(...value.map(resolve));
204
+ if (isNode(value)) return renderNode(value);
205
+ return value;
206
+ }
207
+ function toHtml(value) {
208
+ const r = resolve(value);
209
+ return r == null ? "" : String(r);
210
+ }
211
+ function renderNode(node) {
212
+ const type = nodeGet(node, "type") ?? nodeGet(node, "theme");
213
+ if (type === "markup" || typeof nodeGet(node, "markup") === "string") {
214
+ return String(nodeGet(node, "markup") ?? "");
215
+ }
216
+ if (type === "component") return renderers.renderComponent(node);
217
+ if (type === "icon") {
218
+ return renderers.renderIcon(
219
+ String(nodeGet(node, "pack_id") ?? ""),
220
+ String(nodeGet(node, "icon_id") ?? ""),
221
+ nodeGet(node, "settings") ?? {}
222
+ );
223
+ }
224
+ if (type === "image") {
225
+ const attrs = toAttribute(nodeGet(node, "attributes"));
226
+ if (nodeGet(node, "uri") != null) {
227
+ attrs.setAttribute("src", nodeGet(node, "uri"));
228
+ }
229
+ for (const k of ["width", "height", "alt", "sizes", "title"]) {
230
+ if (nodeGet(node, k) != null)
231
+ attrs.setAttribute(k, nodeGet(node, k));
232
+ }
233
+ return `<img${attrs.toString()} />`;
234
+ }
235
+ const tag = String(nodeGet(node, "tag") ?? "div");
236
+ const children = toHtml(nodeGet(node, "value") ?? nodeGet(node, "children"));
237
+ return `<${tag}${attributesString(node)}>${children}</${tag}>`;
238
+ }
239
+ return { resolve };
240
+ }
241
+
165
242
  // src/runtime/twing.ts
166
243
  function createTwingRuntime(iconPacks) {
167
244
  let iconEnv = null;
168
245
  const inlineNames = {};
169
246
  let inlineSeq = 0;
170
247
  patchDrupalAttribute(DrupalAttribute3);
248
+ function renderComponentNode(node) {
249
+ const id = String(nodeGet(node, "component") ?? "");
250
+ if (!iconEnv || !id) return "";
251
+ const ctx = {};
252
+ for (const src of [nodeGet(node, "props"), nodeGet(node, "slots")]) {
253
+ for (const [k, v] of entries(src)) ctx[k] = resolve(v);
254
+ }
255
+ ctx["attributes"] = toAttribute(ctx["attributes"]);
256
+ try {
257
+ return iconEnv.render(id, ctx);
258
+ } catch (e) {
259
+ console.error('[SDC] render node component "' + id + '" error:', e);
260
+ return "";
261
+ }
262
+ }
263
+ const { resolve } = createNodeResolver({
264
+ renderComponent: renderComponentNode,
265
+ renderIcon: (packId, iconId, settings) => renderIcon(packId, iconId, settings)
266
+ });
171
267
  function renderStory(story, base) {
172
268
  const id = story["#component"];
173
- const ctx = storyContext(story, base);
269
+ const ctx = storyContext(story, base, resolve);
174
270
  ctx["attributes"] = toAttribute(ctx["attributes"]);
175
271
  if (iconEnv) {
176
272
  try {
@@ -284,5 +380,6 @@ function createTwingRuntime(iconPacks) {
284
380
  return { renderIcon, renderInline, makeStory, registerSdcRuntime };
285
381
  }
286
382
  export {
383
+ PrintableArray,
287
384
  createTwingRuntime
288
385
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "storybook-addon-sdc",
3
- "version": "0.24.13",
3
+ "version": "0.24.14",
4
4
  "description": "Drupal Single Directory Components as stories",
5
5
  "keywords": [
6
6
  "SDC",