storybook-addon-sdc 0.22.26 → 0.23.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/README.md +117 -1
- package/dist/preset.js +405 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -14,6 +14,7 @@ This addon streamlines the integration of Drupal Single Directory Components (SD
|
|
|
14
14
|
- [Creating Stories](#creating-stories)
|
|
15
15
|
- [Support for Single Story Files (\*.story.yml)](#support-for-single-story-files-storyyml)
|
|
16
16
|
- [Namespaces](#namespaces)
|
|
17
|
+
- [Drupal Icon API](#drupal-icon-api)
|
|
17
18
|
- [Advanced Grouping and Nesting](#advanced-grouping-and-nesting)
|
|
18
19
|
- [Regular Storybook](#regular-storybook)
|
|
19
20
|
- [Configuration Options](#configuration-options)
|
|
@@ -48,6 +49,7 @@ The SDC Storybook Addon simplifies the integration of Drupal Single Directory Co
|
|
|
48
49
|
- **Custom and External Schema Definitions**: Supports custom and external JSON schema definitions to validate components based on Drupal-specific configurations (e.g., UI Patterns, Experience Builder).
|
|
49
50
|
- **Default and Custom Story Rendering**: Use `type: component` to nest components, `type: element` for HTML markup, and `type: image` for images within stories. Or create your own custom renderers.
|
|
50
51
|
- **Namespaces**: Supports multiple namespaces, allowing you to use components from different(parent/sibling) directories or packages.
|
|
52
|
+
- **Drupal Icon API**: Automatically detects `*.icons.yml` files in registered namespaces and makes `{{ icon('pack_id', 'icon_id', settings) }}` available in every Twig template. Supports `svg_sprite`, `svg` (inline), and `path` extractors with no configuration required.
|
|
51
53
|
|
|
52
54
|
## Quickstart Guide
|
|
53
55
|
|
|
@@ -365,6 +367,120 @@ And in story definitions within YAML files:
|
|
|
365
367
|
{% include '@assets/icons/example.svg' %}
|
|
366
368
|
```
|
|
367
369
|
|
|
370
|
+
## Drupal Icon API
|
|
371
|
+
|
|
372
|
+
The addon automatically supports Drupal's [Icon API](https://www.drupal.org/docs/develop/drupal-apis/icon-api) (introduced in Drupal 11.1). No configuration is required — as soon as a `{namespace}.icons.yml` file is found next to the `components/` directory of any registered namespace, the `icon()` Twig function becomes available in every template.
|
|
373
|
+
|
|
374
|
+
```twig
|
|
375
|
+
{{ icon('pack_id', 'icon_id') }}
|
|
376
|
+
{{ icon('pack_id', 'icon_id', { size: 'sm', class: 'my-icon' }) }}
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
### `{namespace}.icons.yml`
|
|
380
|
+
|
|
381
|
+
Place the file alongside the `components/` directory (i.e. the theme/module root):
|
|
382
|
+
|
|
383
|
+
```
|
|
384
|
+
my-theme/
|
|
385
|
+
├── components/
|
|
386
|
+
│ └── button/
|
|
387
|
+
│ └── button.component.yml
|
|
388
|
+
├── icons/
|
|
389
|
+
│ ├── sprite.svg
|
|
390
|
+
│ └── svg/
|
|
391
|
+
│ ├── home.svg
|
|
392
|
+
│ └── star.svg
|
|
393
|
+
└── my-theme.icons.yml ← discovered automatically
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
### Extractors
|
|
397
|
+
|
|
398
|
+
Three extractors are supported, matching Drupal core. A single pack file can contain any mix:
|
|
399
|
+
|
|
400
|
+
```yaml
|
|
401
|
+
# my-theme.icons.yml
|
|
402
|
+
|
|
403
|
+
# svg_sprite — single sprite file; icons referenced via <use href="{{ source }}#{{ icon_id }}">.
|
|
404
|
+
# content is empty — the template constructs <use> itself.
|
|
405
|
+
# Template context: icon_id, source (sprite URL), attributes (empty), group, settings.
|
|
406
|
+
my_sprite:
|
|
407
|
+
label: 'My Icons (Sprite)'
|
|
408
|
+
extractor: svg_sprite
|
|
409
|
+
config:
|
|
410
|
+
sources:
|
|
411
|
+
- icons/sprite.svg
|
|
412
|
+
settings:
|
|
413
|
+
size:
|
|
414
|
+
type: string
|
|
415
|
+
default: md
|
|
416
|
+
template: >
|
|
417
|
+
<svg {{ attributes
|
|
418
|
+
.setAttribute('width', size == 'sm' ? '16' : (size == 'lg' ? '32' : '24'))
|
|
419
|
+
.setAttribute('height', size == 'sm' ? '16' : (size == 'lg' ? '32' : '24'))
|
|
420
|
+
.setAttribute('aria-hidden', 'true')
|
|
421
|
+
}}>
|
|
422
|
+
<use href="{{ source }}#{{ icon_id }}"></use>
|
|
423
|
+
</svg>
|
|
424
|
+
|
|
425
|
+
# svg — individual SVG files inlined at build time; inner markup provided as content.
|
|
426
|
+
# Template context: icon_id, source (file URL), content (inner markup, no <svg> wrapper),
|
|
427
|
+
# attributes (DrupalAttribute pre-populated from the original <svg> element), group, settings.
|
|
428
|
+
my_svg:
|
|
429
|
+
label: 'My Icons (Inline SVG)'
|
|
430
|
+
extractor: svg
|
|
431
|
+
config:
|
|
432
|
+
sources:
|
|
433
|
+
- icons/svg
|
|
434
|
+
- icons/social/*.svg
|
|
435
|
+
settings:
|
|
436
|
+
color:
|
|
437
|
+
type: string
|
|
438
|
+
default: currentColor
|
|
439
|
+
template: >
|
|
440
|
+
<svg {{ attributes
|
|
441
|
+
.setAttribute('fill', color)
|
|
442
|
+
.setAttribute('aria-hidden', 'true')
|
|
443
|
+
}}>{{ content }}</svg>
|
|
444
|
+
|
|
445
|
+
# path — files served by URL; use source as img src or CSS background-image.
|
|
446
|
+
# content is empty. Remote URLs (http://…) are supported.
|
|
447
|
+
# Template context: icon_id, source (file URL), attributes (empty), group, settings.
|
|
448
|
+
my_path:
|
|
449
|
+
label: 'My Icons (Path)'
|
|
450
|
+
extractor: path
|
|
451
|
+
config:
|
|
452
|
+
sources:
|
|
453
|
+
- icons/png/*.png
|
|
454
|
+
template: '<img src="{{ source }}" alt="{{ icon_id }}" aria-hidden="true">'
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
### Settings
|
|
458
|
+
|
|
459
|
+
Settings defined in `*.icons.yml` are exposed as template variables. Default values are applied automatically; the third argument to `icon()` overrides them at call time.
|
|
460
|
+
|
|
461
|
+
```yaml
|
|
462
|
+
settings:
|
|
463
|
+
size:
|
|
464
|
+
type: string # string | number | integer | boolean
|
|
465
|
+
default: md
|
|
466
|
+
color:
|
|
467
|
+
type: string
|
|
468
|
+
default: currentColor
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
### Production static build
|
|
472
|
+
|
|
473
|
+
For `storybook build`, add icon asset directories to `staticDirs` in `.storybook/main.js` so the files are copied into the build output:
|
|
474
|
+
|
|
475
|
+
```js
|
|
476
|
+
staticDirs: [
|
|
477
|
+
{ from: '../components', to: '/components' },
|
|
478
|
+
{ from: '../icons', to: '/icons' }, // ← icon assets
|
|
479
|
+
],
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
---
|
|
483
|
+
|
|
368
484
|
## Advanced Grouping and Nesting
|
|
369
485
|
|
|
370
486
|
By default, the addon organizes all SDC stories under a single **SDC** [Folder](https://storybook.js.org/docs/writing-stories/naming-components-and-hierarchy#structure-and-hierarchy) at the Storybook root.
|
|
@@ -635,7 +751,7 @@ Below is a list of all available configuration options for `thirdPartySettings.s
|
|
|
635
751
|
| `disabledStories` | `array` | Optional. Array of story names to disable. Supports `basic`, `preview`, or any custom story name. Use `all` to disable all stories. **Note:** `disableBasicStory` is deprecated in favor of `disabledStories: ['basic']`. |
|
|
636
752
|
| `parameters` | `object` | Global [Storybook parameters](https://storybook.js.org/docs/writing-stories/parameters) applied to all generated stories. |
|
|
637
753
|
| `globals` | `object` | Global [Storybook globals](https://storybook.js.org/docs/essentials/toolbars-and-globals) applied to all generated stories. Story-level `globals` override component-level values. |
|
|
638
|
-
| `stories` | `object` | Allows defining or overriding specific stories. Each key (e.g. `preview`) represents a story. Within each story, you can define `props`, `slots`, `parameters`, `globals`, `description`, `name`, and `library_wrapper`.
|
|
754
|
+
| `stories` | `object` | Allows defining or overriding specific stories. Each key (e.g. `preview`) represents a story. Within each story, you can define `props`, `slots`, `parameters`, `globals`, `description`, `name`, and `library_wrapper`. |
|
|
639
755
|
|
|
640
756
|
### Disabled Stories Examples
|
|
641
757
|
|
package/dist/preset.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
// src/preset.ts
|
|
2
|
+
import { existsSync as existsSync4, statSync as statSync2 } from "fs";
|
|
3
|
+
import { join as join5, dirname as dirname5, relative as relative4 } from "path";
|
|
4
|
+
|
|
1
5
|
// src/vite-plugin-storybook-yaml-stories.ts
|
|
2
6
|
import { readdirSync as readdirSync2, readFileSync } from "fs";
|
|
3
7
|
import { parse as parseYaml } from "yaml";
|
|
@@ -406,6 +410,9 @@ var Namespaces = class {
|
|
|
406
410
|
}
|
|
407
411
|
return namespaces;
|
|
408
412
|
}
|
|
413
|
+
entries() {
|
|
414
|
+
return Object.entries(this.namespaces);
|
|
415
|
+
}
|
|
409
416
|
findPath = (namespace) => {
|
|
410
417
|
return this.namespaces[namespace] || "";
|
|
411
418
|
};
|
|
@@ -759,6 +766,377 @@ var DEFAULT_ADDON_OPTIONS = {
|
|
|
759
766
|
|
|
760
767
|
// src/preset.ts
|
|
761
768
|
import { merge as lodashMerge } from "lodash-es";
|
|
769
|
+
|
|
770
|
+
// src/vite-plugin-sdc-icon-packs.ts
|
|
771
|
+
import { existsSync as existsSync3 } from "fs";
|
|
772
|
+
import { join as join4 } from "path";
|
|
773
|
+
|
|
774
|
+
// src/icon-packs.ts
|
|
775
|
+
import { existsSync as existsSync2, readFileSync as readFileSync3, readdirSync as readdirSync3, statSync } from "fs";
|
|
776
|
+
import { join as join3, resolve as resolve2, basename as basename3, dirname as dirname4, extname as extname2, relative as relative3 } from "path";
|
|
777
|
+
import { parse as parseYaml2 } from "yaml";
|
|
778
|
+
function parseSvgFile(raw) {
|
|
779
|
+
const attrs = {};
|
|
780
|
+
const openTagMatch = raw.match(/<svg([^>]*)>/i);
|
|
781
|
+
if (openTagMatch) {
|
|
782
|
+
const attrRegex = /([\w:.-]+)=(?:"([^"]*)"|'([^']*)')/g;
|
|
783
|
+
let m;
|
|
784
|
+
while ((m = attrRegex.exec(openTagMatch[1])) !== null) {
|
|
785
|
+
attrs[m[1]] = m[2] ?? m[3] ?? "";
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
const innerMatch = raw.match(/<svg[^>]*>([\s\S]*)<\/svg>/i);
|
|
789
|
+
const content = innerMatch ? innerMatch[1].trim() : "";
|
|
790
|
+
return { content, attrs };
|
|
791
|
+
}
|
|
792
|
+
function scanDir(dir, baseDir, filterExt) {
|
|
793
|
+
const results = [];
|
|
794
|
+
let entries;
|
|
795
|
+
try {
|
|
796
|
+
entries = readdirSync3(dir, { withFileTypes: true });
|
|
797
|
+
} catch {
|
|
798
|
+
return results;
|
|
799
|
+
}
|
|
800
|
+
for (const entry of entries) {
|
|
801
|
+
const fullPath = join3(dir, entry.name);
|
|
802
|
+
if (entry.isDirectory()) {
|
|
803
|
+
results.push(...scanDir(fullPath, baseDir, filterExt));
|
|
804
|
+
} else if (entry.isFile()) {
|
|
805
|
+
const ext = extname2(entry.name);
|
|
806
|
+
if (filterExt && ext.toLowerCase() !== filterExt.toLowerCase()) continue;
|
|
807
|
+
results.push({
|
|
808
|
+
absPath: fullPath,
|
|
809
|
+
iconId: basename3(entry.name, ext),
|
|
810
|
+
group: relative3(baseDir, dir)
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
return results;
|
|
815
|
+
}
|
|
816
|
+
function resolveSourceEntry(rawSource, nsRoot) {
|
|
817
|
+
if (/^https?:\/\//.test(rawSource)) {
|
|
818
|
+
return [{ absPath: rawSource, iconId: "*", group: "" }];
|
|
819
|
+
}
|
|
820
|
+
const absPath = resolve2(nsRoot, rawSource);
|
|
821
|
+
const extMatch = rawSource.match(/\*(\.[^/{}*]+)$/);
|
|
822
|
+
const filterExt = extMatch ? extMatch[1] : null;
|
|
823
|
+
const wildcardIdx = rawSource.search(/[*{]/);
|
|
824
|
+
const dirPart = wildcardIdx >= 0 ? rawSource.substring(0, wildcardIdx) : rawSource;
|
|
825
|
+
const baseDir = resolve2(nsRoot, dirPart.replace(/[/\\]$/, ""));
|
|
826
|
+
if (!rawSource.includes("*") && !rawSource.includes("{") && existsSync2(absPath)) {
|
|
827
|
+
try {
|
|
828
|
+
if (statSync(absPath).isFile()) {
|
|
829
|
+
return [
|
|
830
|
+
{ absPath, iconId: basename3(absPath, extname2(absPath)), group: "" }
|
|
831
|
+
];
|
|
832
|
+
}
|
|
833
|
+
} catch {
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
if (!existsSync2(baseDir)) return [];
|
|
837
|
+
return scanDir(baseDir, baseDir, filterExt);
|
|
838
|
+
}
|
|
839
|
+
function loadIconPackFile(iconsFilePath) {
|
|
840
|
+
const watchFiles = [iconsFilePath];
|
|
841
|
+
const packs = {};
|
|
842
|
+
if (!existsSync2(iconsFilePath)) return { packs, watchFiles };
|
|
843
|
+
const nsRoot = dirname4(iconsFilePath);
|
|
844
|
+
const ns = basename3(iconsFilePath, ".icons.yml");
|
|
845
|
+
let raw;
|
|
846
|
+
try {
|
|
847
|
+
raw = parseYaml2(readFileSync3(iconsFilePath, "utf8"));
|
|
848
|
+
} catch {
|
|
849
|
+
return { packs, watchFiles };
|
|
850
|
+
}
|
|
851
|
+
if (!raw || typeof raw !== "object") return { packs, watchFiles };
|
|
852
|
+
const toUrl = (absPath) => `/sdc-icons/${ns}/${relative3(nsRoot, absPath).replace(/\\/g, "/")}`;
|
|
853
|
+
for (const [packId, packDef] of Object.entries(raw)) {
|
|
854
|
+
if (!packDef || typeof packDef !== "object") continue;
|
|
855
|
+
if (packDef.enabled === false) continue;
|
|
856
|
+
const extractor = packDef.extractor ?? "svg";
|
|
857
|
+
const rawSources = packDef.config?.sources ?? [];
|
|
858
|
+
const sources = [];
|
|
859
|
+
const sourceUrls = [];
|
|
860
|
+
for (const src of rawSources) {
|
|
861
|
+
if (/^https?:\/\//.test(src)) {
|
|
862
|
+
sources.push(src);
|
|
863
|
+
sourceUrls.push(src);
|
|
864
|
+
} else {
|
|
865
|
+
const absFile = resolve2(nsRoot, src);
|
|
866
|
+
const absBase = resolve2(
|
|
867
|
+
nsRoot,
|
|
868
|
+
src.replace(/[*{].*$/, "").replace(/[/\\]$/, "")
|
|
869
|
+
);
|
|
870
|
+
const isSpriteFile = extractor === "svg_sprite" && existsSync2(absFile) && statSync(absFile).isFile();
|
|
871
|
+
const finalAbs = isSpriteFile ? absFile : absBase;
|
|
872
|
+
sources.push(finalAbs);
|
|
873
|
+
sourceUrls.push(toUrl(finalAbs));
|
|
874
|
+
if (isSpriteFile) watchFiles.push(absFile);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
const svgIcons = {};
|
|
878
|
+
const pathIcons = {};
|
|
879
|
+
if (extractor === "svg") {
|
|
880
|
+
for (const src of rawSources) {
|
|
881
|
+
for (const file of resolveSourceEntry(src, nsRoot)) {
|
|
882
|
+
if (file.iconId === "*") continue;
|
|
883
|
+
watchFiles.push(file.absPath);
|
|
884
|
+
try {
|
|
885
|
+
const { content, attrs } = parseSvgFile(
|
|
886
|
+
readFileSync3(file.absPath, "utf8")
|
|
887
|
+
);
|
|
888
|
+
svgIcons[file.iconId] = {
|
|
889
|
+
content,
|
|
890
|
+
attrs,
|
|
891
|
+
sourceUrl: toUrl(file.absPath),
|
|
892
|
+
group: file.group
|
|
893
|
+
};
|
|
894
|
+
} catch {
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
} else if (extractor === "path") {
|
|
899
|
+
for (const src of rawSources) {
|
|
900
|
+
for (const file of resolveSourceEntry(src, nsRoot)) {
|
|
901
|
+
if (file.iconId === "*") continue;
|
|
902
|
+
pathIcons[file.iconId] = {
|
|
903
|
+
sourceUrl: toUrl(file.absPath),
|
|
904
|
+
group: file.group
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
packs[packId] = {
|
|
910
|
+
packId,
|
|
911
|
+
label: packDef.label ?? packId,
|
|
912
|
+
extractor,
|
|
913
|
+
sources,
|
|
914
|
+
sourceUrls,
|
|
915
|
+
settings: packDef.settings ?? {},
|
|
916
|
+
template: packDef.template ?? "",
|
|
917
|
+
svgIcons,
|
|
918
|
+
pathIcons
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
return { packs, watchFiles };
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
// src/vite-plugin-sdc-icon-packs.ts
|
|
925
|
+
var VIRTUAL_TWIG = "virtual:sdc-icon-packs:twig";
|
|
926
|
+
var RESOLVED_TWIG = "\0" + VIRTUAL_TWIG;
|
|
927
|
+
var VIRTUAL_TWING = "virtual:sdc-icon-packs:twing";
|
|
928
|
+
var RESOLVED_TWING = "\0" + VIRTUAL_TWING;
|
|
929
|
+
var PACK_PREFIX = "\0icons-pack:";
|
|
930
|
+
var SHARED_HELPERS = (
|
|
931
|
+
/* js */
|
|
932
|
+
`
|
|
933
|
+
function _sdcBuildIconContext(DrupalAttribute, pack, iconId, settings) {
|
|
934
|
+
var resolved = {};
|
|
935
|
+
var packSettings = pack.settings || {};
|
|
936
|
+
var settingKeys = Object.keys(packSettings);
|
|
937
|
+
for (var i = 0; i < settingKeys.length; i++) {
|
|
938
|
+
var key = settingKeys[i];
|
|
939
|
+
var def = packSettings[key];
|
|
940
|
+
resolved[key] =
|
|
941
|
+
settings && Object.prototype.hasOwnProperty.call(settings, key)
|
|
942
|
+
? settings[key]
|
|
943
|
+
: def && def.default !== undefined
|
|
944
|
+
? def.default
|
|
945
|
+
: '';
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
var source = '';
|
|
949
|
+
var content = '';
|
|
950
|
+
var svgAttrs = {};
|
|
951
|
+
var group = '';
|
|
952
|
+
|
|
953
|
+
if (pack.extractor === 'svg_sprite') {
|
|
954
|
+
source = pack.sourceUrls && pack.sourceUrls[0] ? pack.sourceUrls[0] : '';
|
|
955
|
+
} else if (pack.extractor === 'svg') {
|
|
956
|
+
var svgData = pack.svgIcons && pack.svgIcons[iconId];
|
|
957
|
+
if (svgData) {
|
|
958
|
+
source = svgData.sourceUrl || '';
|
|
959
|
+
content = svgData.content || '';
|
|
960
|
+
svgAttrs = svgData.attrs || {};
|
|
961
|
+
group = svgData.group || '';
|
|
962
|
+
}
|
|
963
|
+
} else {
|
|
964
|
+
var pathData = pack.pathIcons && pack.pathIcons[iconId];
|
|
965
|
+
if (pathData) {
|
|
966
|
+
source = pathData.sourceUrl || '';
|
|
967
|
+
group = pathData.group || '';
|
|
968
|
+
} else if (pack.sourceUrls && pack.sourceUrls[0]) {
|
|
969
|
+
source = pack.sourceUrls[0] + '/' + iconId;
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
var attributes = new DrupalAttribute(Object.entries(svgAttrs));
|
|
974
|
+
|
|
975
|
+
return Object.assign(
|
|
976
|
+
{ icon_id: iconId, source: source, content: content, attributes: attributes, group: group },
|
|
977
|
+
resolved
|
|
978
|
+
);
|
|
979
|
+
}
|
|
980
|
+
`
|
|
981
|
+
);
|
|
982
|
+
function packImports(paths) {
|
|
983
|
+
const imports = paths.map((p, i) => `import _p${i} from '${PACK_PREFIX}${p}';`).join("\n");
|
|
984
|
+
const args = paths.map((_, i) => `_p${i}`).join(", ");
|
|
985
|
+
return { imports, mergeExpr: args ? `Object.assign({}, ${args})` : "{}" };
|
|
986
|
+
}
|
|
987
|
+
function generateTwigModule(packFilePaths) {
|
|
988
|
+
const { imports, mergeExpr } = packImports(packFilePaths);
|
|
989
|
+
return `
|
|
990
|
+
import DrupalAttribute from 'drupal-attribute';
|
|
991
|
+
${imports}
|
|
992
|
+
|
|
993
|
+
${SHARED_HELPERS}
|
|
994
|
+
|
|
995
|
+
var _sdcIconPacks = ${mergeExpr};
|
|
996
|
+
|
|
997
|
+
export function registerIconFunction(Twig) {
|
|
998
|
+
if (Twig.__sdcIconRegistered) return;
|
|
999
|
+
Twig.__sdcIconRegistered = true;
|
|
1000
|
+
|
|
1001
|
+
Twig.extendFunction('icon', function(packId, iconId, settings) {
|
|
1002
|
+
var pack = _sdcIconPacks[packId];
|
|
1003
|
+
if (!pack || !iconId) return '';
|
|
1004
|
+
var ctx = _sdcBuildIconContext(DrupalAttribute, pack, iconId, settings || {});
|
|
1005
|
+
try {
|
|
1006
|
+
var tmpl = Twig.twig({ data: pack.template });
|
|
1007
|
+
return tmpl.render(ctx);
|
|
1008
|
+
} catch (e) {
|
|
1009
|
+
console.error('[SDC Icons] Error rendering icon "' + packId + ':' + iconId + '":', e);
|
|
1010
|
+
return '';
|
|
1011
|
+
}
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
`;
|
|
1015
|
+
}
|
|
1016
|
+
function generateTwingModule(packFilePaths) {
|
|
1017
|
+
const { imports, mergeExpr } = packImports(packFilePaths);
|
|
1018
|
+
return `
|
|
1019
|
+
import { createSynchronousFunction } from 'twing';
|
|
1020
|
+
import DrupalAttribute from 'drupal-attribute';
|
|
1021
|
+
${imports}
|
|
1022
|
+
|
|
1023
|
+
${SHARED_HELPERS}
|
|
1024
|
+
|
|
1025
|
+
var _sdcIconPacks = ${mergeExpr};
|
|
1026
|
+
|
|
1027
|
+
export function registerIconFunction(env) {
|
|
1028
|
+
// Register icon templates into the existing loader so env.render() finds them.
|
|
1029
|
+
// createSynchronousArrayLoader (used by vite-plugin-twing-drupal's SDC loader)
|
|
1030
|
+
// stores templates by reference \u2014 setTemplate() propagates to both the wrapper
|
|
1031
|
+
// loader and the base array loader.
|
|
1032
|
+
var loader = env.loader;
|
|
1033
|
+
if (loader && typeof loader.setTemplate === 'function') {
|
|
1034
|
+
var packIds = Object.keys(_sdcIconPacks);
|
|
1035
|
+
for (var i = 0; i < packIds.length; i++) {
|
|
1036
|
+
loader.setTemplate('_sdc_icon_' + packIds[i], _sdcIconPacks[packIds[i]].template);
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
var func = createSynchronousFunction(
|
|
1041
|
+
'icon',
|
|
1042
|
+
function(_twingCtx, packId, iconId, settings) {
|
|
1043
|
+
var pack = _sdcIconPacks[packId];
|
|
1044
|
+
if (!pack || !iconId) return '';
|
|
1045
|
+
var ctx = _sdcBuildIconContext(DrupalAttribute, pack, iconId, settings || {});
|
|
1046
|
+
try {
|
|
1047
|
+
return env.render('_sdc_icon_' + packId, ctx);
|
|
1048
|
+
} catch (e) {
|
|
1049
|
+
console.error('[SDC Icons] Error rendering icon "' + packId + ':' + iconId + '":', e);
|
|
1050
|
+
return '';
|
|
1051
|
+
}
|
|
1052
|
+
},
|
|
1053
|
+
[
|
|
1054
|
+
{ name: 'pack_id' },
|
|
1055
|
+
{ name: 'icon_id' },
|
|
1056
|
+
{ name: 'settings', defaultValue: {} },
|
|
1057
|
+
]
|
|
1058
|
+
);
|
|
1059
|
+
|
|
1060
|
+
env.addFunction(func);
|
|
1061
|
+
}
|
|
1062
|
+
`;
|
|
1063
|
+
}
|
|
1064
|
+
var TWIG_JS_MARKER = "from 'drupal-twig-extensions/twig'";
|
|
1065
|
+
var TWIG_JS_INJECT_AFTER = "addDrupalExtensions(Twig);";
|
|
1066
|
+
var TWING_MARKER = "createSynchronousEnvironment";
|
|
1067
|
+
var TWING_INJECT_AFTER = "addDrupalExtensions(env);";
|
|
1068
|
+
var INJECTED_GUARD = "_sdcRegisterIcon";
|
|
1069
|
+
function iconPacksPlugin(namespaces) {
|
|
1070
|
+
return {
|
|
1071
|
+
name: "vite-plugin-sdc-icon-packs",
|
|
1072
|
+
resolveId(id) {
|
|
1073
|
+
if (id === VIRTUAL_TWIG) return RESOLVED_TWIG;
|
|
1074
|
+
if (id === VIRTUAL_TWING) return RESOLVED_TWING;
|
|
1075
|
+
if (id.startsWith(PACK_PREFIX)) return id;
|
|
1076
|
+
},
|
|
1077
|
+
load(id) {
|
|
1078
|
+
if (id === RESOLVED_TWIG || id === RESOLVED_TWING) {
|
|
1079
|
+
const packFilePaths = namespaces.entries().map(([ns, root]) => join4(root, `${ns}.icons.yml`)).filter((p) => existsSync3(p));
|
|
1080
|
+
return id === RESOLVED_TWIG ? generateTwigModule(packFilePaths) : generateTwingModule(packFilePaths);
|
|
1081
|
+
}
|
|
1082
|
+
if (id.startsWith(PACK_PREFIX)) {
|
|
1083
|
+
const filePath = id.slice(PACK_PREFIX.length);
|
|
1084
|
+
const { packs, watchFiles } = loadIconPackFile(filePath);
|
|
1085
|
+
watchFiles.forEach((f) => this.addWatchFile(f));
|
|
1086
|
+
return `export default ${JSON.stringify(packs)};`;
|
|
1087
|
+
}
|
|
1088
|
+
},
|
|
1089
|
+
handleHotUpdate({ modules, server }) {
|
|
1090
|
+
const hasIconPack = modules.some(
|
|
1091
|
+
(m) => m.id === RESOLVED_TWIG || m.id === RESOLVED_TWING || (m.id?.startsWith(PACK_PREFIX) ?? false)
|
|
1092
|
+
);
|
|
1093
|
+
if (hasIconPack) {
|
|
1094
|
+
server.ws.send({ type: "full-reload" });
|
|
1095
|
+
return [];
|
|
1096
|
+
}
|
|
1097
|
+
},
|
|
1098
|
+
transform(code, id) {
|
|
1099
|
+
if (!id.match(/\.twig(\?.*)?$/)) return null;
|
|
1100
|
+
if (code.includes(INJECTED_GUARD)) return null;
|
|
1101
|
+
if (code.includes(TWING_MARKER) && code.includes(TWING_INJECT_AFTER)) {
|
|
1102
|
+
return {
|
|
1103
|
+
code: `import { registerIconFunction as ${INJECTED_GUARD} } from '${VIRTUAL_TWING}';
|
|
1104
|
+
` + code.replace(
|
|
1105
|
+
TWING_INJECT_AFTER,
|
|
1106
|
+
`${TWING_INJECT_AFTER}
|
|
1107
|
+
${INJECTED_GUARD}(env);`
|
|
1108
|
+
),
|
|
1109
|
+
map: null
|
|
1110
|
+
};
|
|
1111
|
+
}
|
|
1112
|
+
if (code.includes(TWIG_JS_MARKER) && code.includes(TWIG_JS_INJECT_AFTER)) {
|
|
1113
|
+
return {
|
|
1114
|
+
code: `import { registerIconFunction as ${INJECTED_GUARD} } from '${VIRTUAL_TWIG}';
|
|
1115
|
+
` + code.replace(
|
|
1116
|
+
TWIG_JS_INJECT_AFTER,
|
|
1117
|
+
`${TWIG_JS_INJECT_AFTER}
|
|
1118
|
+
${INJECTED_GUARD}(Twig);`
|
|
1119
|
+
),
|
|
1120
|
+
map: null
|
|
1121
|
+
};
|
|
1122
|
+
}
|
|
1123
|
+
return null;
|
|
1124
|
+
},
|
|
1125
|
+
configureServer(server) {
|
|
1126
|
+
namespaces.entries().forEach(([, root]) => server.watcher.add(root));
|
|
1127
|
+
server.watcher.on("add", (p) => {
|
|
1128
|
+
if (!p.endsWith(".icons.yml")) return;
|
|
1129
|
+
for (const rid of [RESOLVED_TWIG, RESOLVED_TWING]) {
|
|
1130
|
+
const mod = server.moduleGraph.getModuleById(rid);
|
|
1131
|
+
if (mod) server.moduleGraph.invalidateModule(mod);
|
|
1132
|
+
}
|
|
1133
|
+
server.ws.send({ type: "full-reload" });
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
1136
|
+
};
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
// src/preset.ts
|
|
762
1140
|
async function viteFinal(config, options) {
|
|
763
1141
|
options = lodashMerge({}, DEFAULT_ADDON_OPTIONS, options);
|
|
764
1142
|
const {
|
|
@@ -792,7 +1170,8 @@ async function viteFinal(config, options) {
|
|
|
792
1170
|
include: ["buffer", "stream", "path"]
|
|
793
1171
|
}),
|
|
794
1172
|
...twigPlugin ? [twigPlugin] : [],
|
|
795
|
-
vite_plugin_storybook_yaml_stories_default({ ...options, globalDefs, namespaces })
|
|
1173
|
+
vite_plugin_storybook_yaml_stories_default({ ...options, globalDefs, namespaces }),
|
|
1174
|
+
iconPacksPlugin(namespaces)
|
|
796
1175
|
],
|
|
797
1176
|
optimizeDeps: {
|
|
798
1177
|
exclude: ["vite-plugin-twig-drupal", "vite-plugin-twing-drupal"]
|
|
@@ -806,6 +1185,30 @@ var experimental_indexers = async (existingIndexers) => [
|
|
|
806
1185
|
...existingIndexers || [],
|
|
807
1186
|
yamlStoriesIndexer
|
|
808
1187
|
];
|
|
1188
|
+
var staticDirs = async (existing, options) => {
|
|
1189
|
+
const merged = lodashMerge({}, DEFAULT_ADDON_OPTIONS, options);
|
|
1190
|
+
const namespaces = toNamespaces(merged.sdcStorybookOptions);
|
|
1191
|
+
const iconDirs = [];
|
|
1192
|
+
for (const [ns, nsRoot] of namespaces.entries()) {
|
|
1193
|
+
const iconsFile = join5(nsRoot, `${ns}.icons.yml`);
|
|
1194
|
+
if (!existsSync4(iconsFile)) continue;
|
|
1195
|
+
const { packs } = loadIconPackFile(iconsFile);
|
|
1196
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1197
|
+
for (const pack of Object.values(packs)) {
|
|
1198
|
+
if (pack.extractor === "svg") continue;
|
|
1199
|
+
for (const src of pack.sources) {
|
|
1200
|
+
if (src.startsWith("http")) continue;
|
|
1201
|
+
if (!existsSync4(src)) continue;
|
|
1202
|
+
const dir = statSync2(src).isDirectory() ? src : dirname5(src);
|
|
1203
|
+
if (seen.has(dir)) continue;
|
|
1204
|
+
seen.add(dir);
|
|
1205
|
+
const relDir = relative4(nsRoot, dir).replace(/\\/g, "/");
|
|
1206
|
+
iconDirs.push({ from: dir, to: `/sdc-icons/${ns}/${relDir}` });
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
return [...existing || [], ...iconDirs];
|
|
1211
|
+
};
|
|
809
1212
|
var previewHead = (head) => `
|
|
810
1213
|
<style>
|
|
811
1214
|
.visually-hidden {
|
|
@@ -826,5 +1229,6 @@ var previewHead = (head) => `
|
|
|
826
1229
|
export {
|
|
827
1230
|
experimental_indexers,
|
|
828
1231
|
previewHead,
|
|
1232
|
+
staticDirs,
|
|
829
1233
|
viteFinal
|
|
830
1234
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "storybook-addon-sdc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.23.2",
|
|
4
4
|
"description": "Drupal Single Directory Components as stories",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"SDC",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"format": "prettier --write ."
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"json-schema-faker": "^0.6.
|
|
40
|
+
"json-schema-faker": "^0.6.2",
|
|
41
41
|
"jsonschema": "^1.5.0",
|
|
42
42
|
"lodash-es": "^4.18.1",
|
|
43
43
|
"node-fetch": "^3.3.2",
|