UNPKG

7.98 kBJavaScriptView Raw
1function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
3function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
5function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
6
7function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
8
9function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
10
11function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
12
13import "core-js/modules/es.array.concat.js";
14import "core-js/modules/es.function.name.js";
15import "core-js/modules/es.string.ends-with.js";
16import "core-js/modules/es.array.join.js";
17import "core-js/modules/es.array.filter.js";
18import "core-js/modules/es.object.to-string.js";
19import "core-js/modules/es.array.map.js";
20import "core-js/modules/es.object.entries.js";
21import "core-js/modules/es.array.find.js";
22import "core-js/modules/es.symbol.js";
23import "core-js/modules/es.symbol.description.js";
24import "core-js/modules/es.symbol.iterator.js";
25import "core-js/modules/es.array.iterator.js";
26import "core-js/modules/es.string.iterator.js";
27import "core-js/modules/web.dom-collections.iterator.js";
28import "core-js/modules/es.array.slice.js";
29import "core-js/modules/es.array.from.js";
30import "core-js/modules/es.regexp.exec.js";
31import { addons, useEffect } from '@storybook/addons';
32import { once } from '@storybook/client-logger';
33import { SourceType, SNIPPET_RENDERED } from '@storybook/docs-tools';
34/**
35 * Check if the sourcecode should be generated.
36 *
37 * @param context StoryContext
38 */
39
40var skipSourceRender = function skipSourceRender(context) {
41 var _context$parameters$d;
42
43 var sourceParams = context === null || context === void 0 ? void 0 : (_context$parameters$d = context.parameters.docs) === null || _context$parameters$d === void 0 ? void 0 : _context$parameters$d.source;
44 var isArgsStory = context === null || context === void 0 ? void 0 : context.parameters.__isArgsStory; // always render if the user forces it
45
46 if ((sourceParams === null || sourceParams === void 0 ? void 0 : sourceParams.type) === SourceType.DYNAMIC) {
47 return false;
48 } // never render if the user is forcing the block to render code, or
49 // if the user provides code, or if it's not an args story.
50
51
52 return !isArgsStory || (sourceParams === null || sourceParams === void 0 ? void 0 : sourceParams.code) || (sourceParams === null || sourceParams === void 0 ? void 0 : sourceParams.type) === SourceType.CODE;
53};
54/**
55 * Transform a key/value to a svelte declaration as string.
56 *
57 * Default values are ommited
58 *
59 * @param key Key
60 * @param value Value
61 * @param argTypes Component ArgTypes
62 */
63
64
65function toSvelteProperty(key, value, argTypes) {
66 if (value === undefined || value === null) {
67 return null;
68 } // default value ?
69
70
71 if (argTypes[key] && argTypes[key].defaultValue === value) {
72 return null;
73 }
74
75 if (value === true) {
76 return key;
77 }
78
79 if (typeof value === 'string') {
80 return "".concat(key, "=").concat(JSON.stringify(value));
81 }
82
83 return "".concat(key, "={").concat(JSON.stringify(value), "}");
84}
85/**
86 * Extract a component name.
87 *
88 * @param component Component
89 */
90
91
92function getComponentName(component) {
93 if (component == null) {
94 return null;
95 }
96
97 var _component$__docgen = component.__docgen,
98 __docgen = _component$__docgen === void 0 ? {} : _component$__docgen;
99
100 var name = __docgen.name;
101
102 if (!name) {
103 return component.name;
104 }
105
106 if (name.endsWith('.svelte')) {
107 name = name.substring(0, name.length - 7);
108 }
109
110 return name;
111}
112/**
113 * Generate a svelte template.
114 *
115 * @param component Component
116 * @param args Args
117 * @param argTypes ArgTypes
118 * @param slotProperty Property used to simulate a slot
119 */
120
121
122export function generateSvelteSource(component, args, argTypes, slotProperty) {
123 var name = getComponentName(component);
124
125 if (!name) {
126 return null;
127 }
128
129 var props = Object.entries(args).filter(function (_ref) {
130 var _ref2 = _slicedToArray(_ref, 1),
131 k = _ref2[0];
132
133 return k !== slotProperty;
134 }).map(function (_ref3) {
135 var _ref4 = _slicedToArray(_ref3, 2),
136 k = _ref4[0],
137 v = _ref4[1];
138
139 return toSvelteProperty(k, v, argTypes);
140 }).filter(function (p) {
141 return p;
142 }).join(' ');
143 var slotValue = slotProperty ? args[slotProperty] : null;
144
145 if (slotValue) {
146 return "<".concat(name, " ").concat(props, ">\n ").concat(slotValue, "\n</").concat(name, ">");
147 }
148
149 return "<".concat(name, " ").concat(props, "/>");
150}
151/**
152 * Check if the story component is a wrapper to the real component.
153 *
154 * A component can be annoted with @wrapper to indicate that
155 * it's just a wrapper for the real tested component. If it's the case
156 * then the code generated references the real component, not the wrapper.
157 *
158 * moreover, a wrapper can annotate a property with @slot : this property
159 * is then assumed to be an alias to the default slot.
160 *
161 * @param component Component
162 */
163
164function getWrapperProperties(component) {
165 var __docgen = component.__docgen;
166
167 if (!__docgen) {
168 return {
169 wrapper: false
170 };
171 } // the component should be declared as a wrapper
172
173
174 if (!__docgen.keywords.find(function (kw) {
175 return kw.name === 'wrapper';
176 })) {
177 return {
178 wrapper: false
179 };
180 }
181
182 var slotProp = __docgen.data.find(function (prop) {
183 return prop.keywords.find(function (kw) {
184 return kw.name === 'slot';
185 });
186 });
187
188 return {
189 wrapper: true,
190 slotProperty: slotProp === null || slotProp === void 0 ? void 0 : slotProp.name
191 };
192}
193/**
194 * Svelte source decorator.
195 * @param storyFn Fn
196 * @param context StoryContext
197 */
198
199
200export var sourceDecorator = function sourceDecorator(storyFn, context) {
201 var channel = addons.getChannel();
202 var skip = skipSourceRender(context);
203 var story = storyFn();
204 var source;
205 useEffect(function () {
206 if (!skip && source) {
207 channel.emit(SNIPPET_RENDERED, (context || {}).id, source);
208 }
209 });
210
211 if (skip) {
212 return story;
213 }
214
215 var _ref5 = context || {},
216 _ref5$parameters = _ref5.parameters,
217 parameters = _ref5$parameters === void 0 ? {} : _ref5$parameters,
218 _ref5$args = _ref5.args,
219 args = _ref5$args === void 0 ? {} : _ref5$args,
220 ctxtComponent = _ref5.component;
221
222 var _story$Component = story.Component,
223 component = _story$Component === void 0 ? {} : _story$Component;
224
225 var _getWrapperProperties = getWrapperProperties(component),
226 wrapper = _getWrapperProperties.wrapper,
227 slotProperty = _getWrapperProperties.slotProperty;
228
229 if (wrapper) {
230 if (parameters.component) {
231 once.warn('parameters.component is deprecated. Using context.component instead.');
232 }
233
234 component = ctxtComponent;
235 }
236
237 source = generateSvelteSource(component, args, context === null || context === void 0 ? void 0 : context.argTypes, slotProperty);
238 return story;
239};
\No newline at end of file