-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
index.js
139 lines (122 loc) · 3.8 KB
/
index.js
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
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import addons from '@storybook/addons';
import deprecate from 'util-deprecate';
import Story from './components/Story';
import { EVENT_ID } from './config';
import { defaultOptions } from './defaults';
const channel = addons.getChannel();
const addonOptions = {
globalOptions: defaultOptions,
localOptions: {},
isGlobalScope: true,
setOptions(options) {
if (this.isGlobalScope) {
this.globalOptions = {
...this.globalOptions,
...options,
};
return this.globalOptions;
}
this.localOptions = {
...this.localOptions,
...options,
};
return this.localOptions;
},
globalScope() {
this.isGlobalScope = true;
this.localOptions = {};
},
localScope() {
this.isGlobalScope = false;
this.localOptions = {};
},
};
function catchLocalOptions(storyFn, context) {
addonOptions.localScope();
const story = storyFn(context);
const { localOptions, globalOptions } = addonOptions;
addonOptions.globalScope();
return { story, localOptions, globalOptions };
}
function sendToPanel(infoString) {
channel.emit(EVENT_ID, {
infoString,
});
}
function addInfo(storyFn, context, infoOptions) {
// Options could be overridden by setInfoOptions during storyFn execution
const { story, globalOptions, localOptions } = catchLocalOptions(storyFn, context);
const options = {
...globalOptions,
...infoOptions,
...localOptions,
};
const components = {
...options.marksyConf,
...options.components,
};
// props.propTables can only be either an array of components or null
// propTables option is allowed to be set to 'false' (a boolean)
// if the option is false, replace it with null to avoid react warnings
const props = {
summary: options.summary,
context,
showInline: Boolean(options.inline),
showHeader: Boolean(options.header),
showSource: Boolean(options.source),
hideInfoButton: Boolean(!options.infoButton),
propTables: options.propTables || null,
propTablesExclude: options.propTablesExclude,
styles: typeof options.styles === 'function' && options.styles,
components,
maxPropObjectKeys: options.maxPropObjectKeys,
maxPropArrayLength: options.maxPropArrayLength,
maxPropsIntoLine: options.maxPropsIntoLine,
maxPropStringLength: options.maxPropStringLength,
};
const infoContent = (
<Story {...props}>
{story}
</Story>
);
if (options.sendToPanel) {
const infoString = ReactDOMServer.renderToString(infoContent);
sendToPanel(infoString);
return story;
}
return infoContent;
}
const detectOptions = prop => {
if (typeof prop === 'string') {
return { summary: prop };
}
if (React.isValidElement(prop)) {
return { summary: prop };
}
return prop;
};
export const withInfo = summaryOrOptions => {
const options = detectOptions(summaryOrOptions);
return storyFn => context => addInfo(storyFn, context, options);
};
export { Story };
export default {
addWithInfo: deprecate(function addWithInfo(storyName, text, storyFn, options) {
if (typeof storyFn !== 'function') {
if (typeof text === 'function') {
options = storyFn; // eslint-disable-line
storyFn = text; // eslint-disable-line
text = ''; // eslint-disable-line
} else {
throw new Error('No story defining function has been specified');
}
}
return this.add(storyName, withInfo({ text, ...options })(storyFn));
}, '@storybook/addon-info .addWithInfo() addon is deprecated, use withInfo() from the same package instead. \nSee https://github.com/storybooks/storybook/tree/master/addons/info'),
};
export function setInfoOptions(summaryOrOptions) {
const options = detectOptions(summaryOrOptions);
return addonOptions.setOptions(options);
}