Panel.js
6.1 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import qs from 'qs';
import { document } from 'global';
import styled from '@emotion/styled';
import copy from 'copy-to-clipboard';
import { Placeholder, TabWrapper, TabsState, ActionBar, ActionButton } from '@storybook/components';
import Types from './types';
import PropForm from './PropForm';
const getTimestamp = () => +new Date();
const DEFAULT_GROUP_ID = 'ALL';
const PanelWrapper = styled.div({
height: '100%',
overflow: 'auto',
width: '100%',
});
export default class Panel extends PureComponent {
constructor(props) {
super(props);
this.state = {
knobs: {},
};
this.options = {};
this.lastEdit = getTimestamp();
this.loadedFromUrl = false;
}
componentDidMount() {
const { channel, api } = this.props;
channel.on('addon:knobs:setKnobs', this.setKnobs);
channel.on('addon:knobs:setOptions', this.setOptions);
this.stopListeningOnStory = api.onStory(() => {
this.setState({ knobs: {} });
channel.emit('addon:knobs:reset');
});
}
componentWillUnmount() {
const { channel } = this.props;
channel.removeListener('addon:knobs:setKnobs', this.setKnobs);
this.stopListeningOnStory();
}
setOptions = (options = { timestamps: false }) => {
this.options = options;
};
setKnobs = ({ knobs, timestamp }) => {
const queryParams = {};
const { api, channel } = this.props;
if (!this.options.timestamps || !timestamp || this.lastEdit <= timestamp) {
Object.keys(knobs).forEach(name => {
const knob = knobs[name];
// For the first time, get values from the URL and set them.
if (!this.loadedFromUrl) {
const urlValue = api.getQueryParam(`knob-${name}`);
if (urlValue !== undefined) {
// If the knob value present in url
knob.value = Types[knob.type].deserialize(urlValue);
channel.emit('addon:knobs:knobChange', knob);
}
}
// set all knobsquery params to be deleted from URL
queryParams[`knob-${name}`] = null;
});
api.setQueryParams(queryParams);
this.setState({ knobs });
this.loadedFromUrl = true;
}
};
reset = () => {
const { channel } = this.props;
channel.emit('addon:knobs:reset');
};
copy = () => {
const { location } = document;
const query = qs.parse(location.search.replace('?', ''));
const { knobs } = this.state;
Object.entries(knobs).forEach(([name, knob]) => {
query[`knob-${name}`] = Types[knob.type].serialize(knob.value);
});
copy(`${location.origin + location.pathname}?${qs.stringify(query)}`);
// TODO: show some notification of this
};
emitChange = changedKnob => {
const { channel } = this.props;
channel.emit('addon:knobs:knobChange', changedKnob);
};
handleChange = changedKnob => {
this.lastEdit = getTimestamp();
const { knobs } = this.state;
const { name } = changedKnob;
const newKnobs = { ...knobs };
newKnobs[name] = {
...newKnobs[name],
...changedKnob,
};
this.setState({ knobs: newKnobs }, this.emitChange(changedKnob));
};
handleClick = knob => {
const { channel } = this.props;
channel.emit('addon:knobs:knobClick', knob);
};
render() {
const { knobs } = this.state;
const { active } = this.props;
if (!active) {
return null;
}
const groups = {};
const groupIds = [];
const knobKeysArray = Object.keys(knobs).filter(key => knobs[key].used);
knobKeysArray
.filter(key => knobs[key].groupId)
.forEach(key => {
const knobKeyGroupId = knobs[key].groupId;
groupIds.push(knobKeyGroupId);
groups[knobKeyGroupId] = {
render: ({ active: groupActive, selected }) => (
<TabWrapper active={groupActive || selected === DEFAULT_GROUP_ID}>
<PropForm
// false positive
// eslint-disable-next-line no-use-before-define
knobs={knobsArray.filter(knob => knob.groupId === knobKeyGroupId)}
onFieldChange={this.handleChange}
onFieldClick={this.handleClick}
/>
</TabWrapper>
),
title: knobKeyGroupId,
};
});
groups[DEFAULT_GROUP_ID] = {
render: ({ active: groupActive }) => {
// false positive
// eslint-disable-next-line no-use-before-define
const defaultKnobs = knobsArray.filter(
knob => !knob.groupId || knob.groupId === DEFAULT_GROUP_ID
);
if (defaultKnobs.length === 0) {
return null;
}
return (
<TabWrapper active={groupActive}>
<PropForm
knobs={defaultKnobs}
onFieldChange={this.handleChange}
onFieldClick={this.handleClick}
/>
</TabWrapper>
);
},
title: DEFAULT_GROUP_ID,
};
const knobsArray = knobKeysArray.map(key => knobs[key]);
if (knobsArray.length === 0) {
return <Placeholder>NO KNOBS</Placeholder>;
}
return (
<PanelWrapper>
{groupIds.length > 0 ? (
<TabsState>
{Object.entries(groups).map(([k, v]) => (
<div id={k} key={k} title={v.title}>
{v.render}
</div>
))}
</TabsState>
) : (
<PropForm
knobs={knobsArray}
onFieldChange={this.handleChange}
onFieldClick={this.handleClick}
/>
)}
<ActionBar>
<ActionButton onClick={this.copy}>COPY</ActionButton>
<ActionButton onClick={this.reset}>RESET</ActionButton>
</ActionBar>
</PanelWrapper>
);
}
}
Panel.propTypes = {
active: PropTypes.bool.isRequired,
onReset: PropTypes.object, // eslint-disable-line
channel: PropTypes.shape({
emit: PropTypes.func,
on: PropTypes.func,
removeListener: PropTypes.func,
}).isRequired,
api: PropTypes.shape({
onStory: PropTypes.func,
getQueryParam: PropTypes.func,
setQueryParams: PropTypes.func,
}).isRequired,
};