-
-
Notifications
You must be signed in to change notification settings - Fork 10k
Expand file tree
/
Copy pathPanel.js
More file actions
151 lines (128 loc) · 4.01 KB
/
Panel.js
File metadata and controls
151 lines (128 loc) · 4.01 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
import PropTypes from 'prop-types';
import React from 'react';
import debounce from 'lodash.debounce';
import PropForm from './PropForm';
import Types from './types';
const getTimestamp = () => +new Date();
const styles = {
panelWrapper: {
width: '100%',
},
panel: {
padding: '5px',
width: 'auto',
position: 'relative',
},
noKnobs: {
fontFamily: `
-apple-system, ".SFNSText-Regular", "San Francisco", "Roboto",
"Segoe UI", "Helvetica Neue", "Lucida Grande", sans-serif
`,
display: 'inline',
width: '100%',
textAlign: 'center',
color: 'rgb(190, 190, 190)',
padding: '10px',
},
resetButton: {
position: 'absolute',
bottom: 11,
right: 10,
border: 'none',
borderTop: 'solid 1px rgba(0, 0, 0, 0.2)',
borderLeft: 'solid 1px rgba(0, 0, 0, 0.2)',
background: 'rgba(255, 255, 255, 0.5)',
padding: '5px 10px',
borderRadius: '4px 0 0 0',
color: 'rgba(0, 0, 0, 0.5)',
outline: 'none',
},
};
export default class Panel extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.setKnobs = this.setKnobs.bind(this);
this.reset = this.reset.bind(this);
this.setOptions = this.setOptions.bind(this);
this.state = { knobs: {} };
this.options = {};
this.lastEdit = getTimestamp();
this.loadedFromUrl = false;
this.props.channel.on('addon:knobs:setKnobs', this.setKnobs);
this.props.channel.on('addon:knobs:setOptions', this.setOptions);
}
componentWillUnmount() {
this.props.channel.removeListener('addon:knobs:setKnobs', this.setKnobs);
}
setOptions(options = { debounce: false, timestamps: false }) {
this.options = options;
if (options.debounce) {
this.emitChange = debounce(this.emitChange, options.debounce.wait, { leading: options.debounce.leading });
}
}
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);
}
}
queryParams[`knob-${name}`] = Types[knob.type].serialize(knob.value);
});
}
this.loadedFromUrl = true;
api.setQueryParams(queryParams);
this.setState({ knobs });
}
reset() {
this.props.channel.emit('addon:knobs:reset');
}
emitChange(changedKnob) {
this.props.channel.emit('addon:knobs:knobChange', changedKnob);
}
handleChange(changedKnob) {
this.lastEdit = getTimestamp();
const { api } = this.props;
const { knobs } = this.state;
const { name, type, value } = changedKnob;
const newKnobs = { ...knobs };
newKnobs[name] = {
...newKnobs[name],
...changedKnob,
};
this.setState({ knobs: newKnobs });
const queryParams = {};
queryParams[`knob-${name}`] = Types[type].serialize(value);
api.setQueryParams(queryParams);
this.setState({ knobs: newKnobs }, this.emitChange(changedKnob));
}
render() {
const { knobs } = this.state;
const knobsArray = Object.keys(knobs).filter(key => knobs[key].used).map(key => knobs[key]);
if (knobsArray.length === 0) {
return <div style={styles.noKnobs}>NO KNOBS</div>;
}
return (
<div style={styles.panelWrapper}>
<div style={styles.panel}>
<PropForm knobs={knobsArray} onFieldChange={this.handleChange} />
</div>
<button style={styles.resetButton} onClick={this.reset}>RESET</button>
</div>
);
}
}
Panel.propTypes = {
channel: PropTypes.object,
onReset: PropTypes.object,
api: PropTypes.object,
};