forked from storybookjs/addon-jsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
205 lines (170 loc) · 5.4 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import {
addons,
ClientStoryApi,
makeDecorator,
StoryApi,
StoryContext,
StoryFn
} from '@storybook/addons';
import reactElementToJSXString, {
Options
} from '@styled/react-element-to-jsx-string';
import { html as beautifyHTML, HTMLBeautifyOptions } from 'js-beautify';
import React from 'react';
import { EVENTS } from './constants';
import { ComponentMap } from './renderer';
export declare const addDecorator: ClientStoryApi<any>['addDecorator'];
export declare type DecoratorFn = Parameters<typeof addDecorator>[0];
type VueComponent = {
/** The template for the Vue component */
template?: string;
};
interface JSXOptions extends HTMLBeautifyOptions {
/** How many wrappers to skip when rendering the jsx */
skip?: number;
/** Whether to show the function in the jsx tab */
showFunctions?: boolean;
/** Whether to format HTML or Vue markup */
enableBeautify?: boolean;
/** Override the display name used for a component */
displayName?: string | Options['displayName'];
/** A function ran before the story is rendered */
onBeforeRender?(dom: string): string;
}
/** Run the user supplied onBeforeRender function if it exists */
const applyBeforeRender = (domString: string, options: JSXOptions) => {
if (typeof options.onBeforeRender !== 'function') {
return domString;
}
return options.onBeforeRender(domString);
};
/** Apply the users parameters and render the jsx for a story */
const renderJsx = (code: React.ReactElement, options: Required<JSXOptions>) => {
let renderedJSX = code;
let Type = renderedJSX.type;
for (let i = 0; i < options.skip; i++) {
if (typeof renderedJSX === 'undefined') {
// eslint-disable-next-line no-console
console.warn('Cannot skip undefined element');
return;
}
if (React.Children.count(renderedJSX) > 1) {
// eslint-disable-next-line no-console
console.warn('Trying to skip an array of elements');
return;
}
if (typeof renderedJSX.props.children === 'undefined') {
// eslint-disable-next-line no-console
console.warn('Not enough children to skip elements.');
if (typeof Type === 'function' && Type.name === '') {
renderedJSX = <Type {...renderedJSX.props} />;
}
} else if (typeof renderedJSX.props.children === 'function') {
renderedJSX = renderedJSX.props.children();
} else {
renderedJSX = renderedJSX.props.children;
}
}
if (typeof renderedJSX === 'undefined') {
// eslint-disable-next-line no-console
return console.warn('Too many skip or undefined component');
}
while (typeof Type === 'function' && Type.name === '') {
renderedJSX = <Type {...renderedJSX.props} />;
Type = renderedJSX.type;
}
const ooo =
typeof options.displayName === 'string'
? {
...options,
showFunctions: true,
displayName: () => options.displayName
}
: options;
return React.Children.map(renderedJSX, c => {
let string = applyBeforeRender(
reactElementToJSXString(c, ooo as Options),
options
);
const matches = string.match(/\S+=\\"([^"]*)\\"/g);
if (matches) {
matches.forEach(match => {
string = string.replace(match, match.replace(/"/g, "'"));
});
}
return string;
}).join('\n');
};
/** Extract the docs for all components used in a story */
const getDocs = (story: React.ReactElement) => {
const types: ComponentMap = {};
/** Walk the story for components */
function extract(innerChildren: React.ReactElement) {
if (!innerChildren) {
return;
}
if (Array.isArray(innerChildren)) {
innerChildren.forEach(extract);
return;
}
if (innerChildren.props && innerChildren.props.children) {
extract(innerChildren.props.children);
}
Object.values(innerChildren.props || {}).forEach(prop => {
extract(prop as React.ReactElement);
});
if (typeof innerChildren.type !== 'string' && innerChildren.type) {
const childType = innerChildren.type as any;
const name: string = childType.displayName || childType.name;
if (name && !types[name]) {
types[name] = childType.__docgenInfo;
}
}
}
extract(story);
return types;
};
const defaultOpts = {
skip: 0,
showDefaultProps: true,
showFunctions: true,
enableBeautify: true
};
/** Extract components from story and emit them to the panel */
export const jsxDecorator = makeDecorator({
name: 'jsx',
parameterName: 'jsx',
wrapper: (storyFn: any, parameters: StoryContext) => {
const story: ReturnType<typeof storyFn> & VueComponent = storyFn();
const channel = addons.getChannel();
const options = {
...defaultOpts,
...((parameters && parameters.parameters.jsx) || {})
} as Required<JSXOptions>;
let components: ComponentMap = {};
let jsx = '';
if (story.template) {
if (options.enableBeautify) {
jsx = beautifyHTML(story.template, options);
} else {
jsx = story.template;
}
} else {
const rendered = renderJsx(story, options);
if (rendered) {
jsx = rendered;
components = getDocs(story);
}
}
channel.emit(EVENTS.ADD_JSX, (parameters || {}).id, jsx, components);
return story;
}
});
export default {
addWithJSX(this: StoryApi, kind: string, storyFn: StoryFn<any>) {
return this.add(
kind,
(context: any) => jsxDecorator(storyFn, context) as StoryFn
);
}
};