@analogjs/storybook-angular 3.0.0-alpha.29 → 3.0.0-alpha.30
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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@analogjs/storybook-angular",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.30",
|
|
4
4
|
"description": "Storybook Integration for Angular & Vite",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
},
|
|
37
37
|
"main": "./src/index.js",
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@storybook/builder-vite": "^10.3.
|
|
39
|
+
"@storybook/builder-vite": "^10.3.5"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"@storybook/angular": "^10.0.0",
|
package/src/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export { type AngularOptions, type Parameters as AngularParameters, type AngularRenderer, type Decorator, type IStory, type Loader, type Meta, type Preview, type StoryContext, type StoryFn, type StoryObj, applicationConfig, argsToTemplate,
|
|
1
|
+
export { type AngularOptions, type Parameters as AngularParameters, type AngularRenderer, type Decorator, type IStory, type Loader, type Meta, type Preview, type StoryContext, type StoryFn, type StoryObj, applicationConfig, argsToTemplate, moduleMetadata, setProjectAnnotations } from "@storybook/angular";
|
|
2
|
+
export { componentWrapperDecorator } from "./lib/component-wrapper-decorator";
|
|
2
3
|
export { type FrameworkOptions, type StorybookConfig } from "./types";
|
package/src/index.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { componentWrapperDecorator } from "./lib/component-wrapper-decorator.js";
|
|
2
|
+
import { applicationConfig, argsToTemplate, moduleMetadata, setProjectAnnotations } from "@storybook/angular";
|
|
2
3
|
export { applicationConfig, argsToTemplate, componentWrapperDecorator, moduleMetadata, setProjectAnnotations };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type Type } from "@angular/core";
|
|
2
|
+
import { type AngularRenderer, type StoryContext } from "@storybook/angular";
|
|
3
|
+
import type { DecoratorFunction } from "storybook/internal/types";
|
|
4
|
+
type StoryProps = Record<string, unknown>;
|
|
5
|
+
export declare function componentWrapperDecorator<TArgs = any>(element: Type<unknown> | ((story: string) => string), props?: StoryProps | ((storyContext: StoryContext<TArgs>) => StoryProps)): DecoratorFunction<AngularRenderer, TArgs>;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { argsToTemplate, componentWrapperDecorator } from "@storybook/angular";
|
|
2
|
+
import { reflectComponentType } from "@angular/core";
|
|
3
|
+
//#region packages/storybook-angular/src/lib/component-wrapper-decorator.ts
|
|
4
|
+
var VOID_ELEMENTS = new Set([
|
|
5
|
+
"area",
|
|
6
|
+
"base",
|
|
7
|
+
"br",
|
|
8
|
+
"col",
|
|
9
|
+
"command",
|
|
10
|
+
"embed",
|
|
11
|
+
"hr",
|
|
12
|
+
"img",
|
|
13
|
+
"input",
|
|
14
|
+
"keygen",
|
|
15
|
+
"link",
|
|
16
|
+
"meta",
|
|
17
|
+
"param",
|
|
18
|
+
"source",
|
|
19
|
+
"track",
|
|
20
|
+
"wbr"
|
|
21
|
+
]);
|
|
22
|
+
function componentWrapperDecorator$1(element, props) {
|
|
23
|
+
const decorator = componentWrapperDecorator(element, props);
|
|
24
|
+
return (storyFn, storyContext) => decorator(() => ensureStoryTemplate(storyFn(), storyContext), storyContext);
|
|
25
|
+
}
|
|
26
|
+
function ensureStoryTemplate(story, storyContext) {
|
|
27
|
+
if (story.template || !storyContext.component) return story;
|
|
28
|
+
const component = reflectComponentType(storyContext.component);
|
|
29
|
+
if (!component) return story;
|
|
30
|
+
return {
|
|
31
|
+
...story,
|
|
32
|
+
template: createTemplate(component, story.props ?? storyContext.args),
|
|
33
|
+
userDefinedTemplate: false
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function createTemplate(component, props) {
|
|
37
|
+
const selector = component.selector.split(",")[0]?.trim();
|
|
38
|
+
if (!selector) return "<ng-container *ngComponentOutlet=\"storyComponent\"></ng-container>";
|
|
39
|
+
return buildTemplate(selector, argsToTemplate(filterTemplateBindings(component, props)));
|
|
40
|
+
}
|
|
41
|
+
function filterTemplateBindings(component, props) {
|
|
42
|
+
if (!props || typeof props !== "object") return {};
|
|
43
|
+
const supportedBindings = new Set([...component.inputs.map((input) => input.templateName), ...component.outputs.map((output) => output.templateName)]);
|
|
44
|
+
return Object.fromEntries(Object.entries(props).filter(([key, value]) => {
|
|
45
|
+
return value !== void 0 && supportedBindings.has(key);
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
function buildTemplate(selector, attributes) {
|
|
49
|
+
const tagName = selector.match(/^[A-Za-z][\w-]*/)?.[0] ?? "div";
|
|
50
|
+
const id = selector.match(/#([\w-]+)/)?.[1];
|
|
51
|
+
const classes = [...selector.matchAll(/\.([\w-]+)/g)].map((match) => match[1]);
|
|
52
|
+
const selectorAttributes = [...selector.matchAll(/\[([^\]]+)\]/g)].map((match) => match[1].trim());
|
|
53
|
+
const parts = [
|
|
54
|
+
id ? `id="${id}"` : "",
|
|
55
|
+
classes.length > 0 ? `class="${classes.join(" ")}"` : "",
|
|
56
|
+
...selectorAttributes,
|
|
57
|
+
attributes
|
|
58
|
+
].filter(Boolean);
|
|
59
|
+
const openingTag = parts.length > 0 ? `<${tagName} ${parts.join(" ")}>` : `<${tagName}>`;
|
|
60
|
+
if (VOID_ELEMENTS.has(tagName)) return openingTag.replace(/>$/, " />");
|
|
61
|
+
return `${openingTag}</${tagName}>`;
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
64
|
+
export { componentWrapperDecorator$1 as componentWrapperDecorator };
|
|
65
|
+
|
|
66
|
+
//# sourceMappingURL=component-wrapper-decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"component-wrapper-decorator.js","names":[],"sources":["../../../src/lib/component-wrapper-decorator.ts"],"sourcesContent":["import {\n reflectComponentType,\n type ComponentMirror,\n type Type,\n} from '@angular/core';\nimport {\n argsToTemplate,\n componentWrapperDecorator as originalComponentWrapperDecorator,\n type AngularRenderer,\n type IStory,\n type StoryContext,\n} from '@storybook/angular';\nimport type { DecoratorFunction } from 'storybook/internal/types';\n\ntype StoryProps = Record<string, unknown>;\n\nconst VOID_ELEMENTS = new Set([\n 'area',\n 'base',\n 'br',\n 'col',\n 'command',\n 'embed',\n 'hr',\n 'img',\n 'input',\n 'keygen',\n 'link',\n 'meta',\n 'param',\n 'source',\n 'track',\n 'wbr',\n]);\n\nexport function componentWrapperDecorator<TArgs = any>(\n element: Type<unknown> | ((story: string) => string),\n props?: StoryProps | ((storyContext: StoryContext<TArgs>) => StoryProps),\n): DecoratorFunction<AngularRenderer, TArgs> {\n const decorator = originalComponentWrapperDecorator(element, props);\n\n return (storyFn, storyContext) =>\n decorator(() => ensureStoryTemplate(storyFn(), storyContext), storyContext);\n}\n\nfunction ensureStoryTemplate<TArgs>(\n story: IStory,\n storyContext: StoryContext<TArgs>,\n): IStory {\n if (story.template || !storyContext.component) {\n return story;\n }\n\n const component = reflectComponentType(\n storyContext.component as Type<unknown>,\n );\n\n if (!component) {\n return story;\n }\n\n return {\n ...story,\n template: createTemplate(component, story.props ?? storyContext.args),\n userDefinedTemplate: false,\n };\n}\n\nfunction createTemplate(\n component: ComponentMirror<unknown>,\n props: unknown,\n): string {\n const selector = component.selector.split(',')[0]?.trim();\n if (!selector) {\n return '<ng-container *ngComponentOutlet=\"storyComponent\"></ng-container>';\n }\n\n const bindings = filterTemplateBindings(component, props);\n const attributes = argsToTemplate(bindings);\n\n return buildTemplate(selector, attributes);\n}\n\nfunction filterTemplateBindings(\n component: ComponentMirror<unknown>,\n props: unknown,\n): StoryProps {\n if (!props || typeof props !== 'object') {\n return {};\n }\n\n const supportedBindings = new Set([\n ...component.inputs.map((input) => input.templateName),\n ...component.outputs.map((output) => output.templateName),\n ]);\n\n return Object.fromEntries(\n Object.entries(props).filter(([key, value]) => {\n return value !== undefined && supportedBindings.has(key);\n }),\n );\n}\n\nfunction buildTemplate(selector: string, attributes: string): string {\n const elementMatch = selector.match(/^[A-Za-z][\\w-]*/);\n const tagName = elementMatch?.[0] ?? 'div';\n const id = selector.match(/#([\\w-]+)/)?.[1];\n const classes = [...selector.matchAll(/\\.([\\w-]+)/g)].map(\n (match) => match[1],\n );\n const selectorAttributes = [...selector.matchAll(/\\[([^\\]]+)\\]/g)].map(\n (match) => match[1].trim(),\n );\n const parts = [\n id ? `id=\"${id}\"` : '',\n classes.length > 0 ? `class=\"${classes.join(' ')}\"` : '',\n ...selectorAttributes,\n attributes,\n ].filter(Boolean);\n const openingTag =\n parts.length > 0 ? `<${tagName} ${parts.join(' ')}>` : `<${tagName}>`;\n\n if (VOID_ELEMENTS.has(tagName)) {\n return openingTag.replace(/>$/, ' />');\n }\n\n return `${openingTag}</${tagName}>`;\n}\n"],"mappings":";;;AAgBA,IAAM,gBAAgB,IAAI,IAAI;CAC5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;AAEF,SAAgB,4BACd,SACA,OAC2C;CAC3C,MAAM,YAAY,0BAAkC,SAAS,MAAM;AAEnE,SAAQ,SAAS,iBACf,gBAAgB,oBAAoB,SAAS,EAAE,aAAa,EAAE,aAAa;;AAG/E,SAAS,oBACP,OACA,cACQ;AACR,KAAI,MAAM,YAAY,CAAC,aAAa,UAClC,QAAO;CAGT,MAAM,YAAY,qBAChB,aAAa,UACd;AAED,KAAI,CAAC,UACH,QAAO;AAGT,QAAO;EACL,GAAG;EACH,UAAU,eAAe,WAAW,MAAM,SAAS,aAAa,KAAK;EACrE,qBAAqB;EACtB;;AAGH,SAAS,eACP,WACA,OACQ;CACR,MAAM,WAAW,UAAU,SAAS,MA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