forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStyleEditor.js
More file actions
297 lines (266 loc) · 7.7 KB
/
StyleEditor.js
File metadata and controls
297 lines (266 loc) · 7.7 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import * as React from 'react';
import {useContext, useMemo, useRef, useState} from 'react';
import {unstable_batchedUpdates as batchedUpdates} from 'react-dom';
import {
BridgeContext,
StoreContext,
} from 'react-devtools-shared/src/devtools/views/context';
import {copyToClipboard} from 'react-devtools-shared/src/utils';
import Button from '../../Button';
import ButtonIcon from '../../ButtonIcon';
import {serializeDataForCopy} from '../../utils';
import AutoSizeInput from './AutoSizeInput';
import styles from './StyleEditor.css';
import {sanitizeForParse} from '../../../utils';
import type {Style} from './types';
type Props = {
id: number,
style: Style,
};
type ChangeAttributeFn = (oldName: string, newName: string, value: any) => void;
type ChangeValueFn = (name: string, value: any) => void;
export default function StyleEditor({id, style}: Props): React.Node {
const bridge = useContext(BridgeContext);
const store = useContext(StoreContext);
const changeAttribute = (oldName: string, newName: string, value: any) => {
const rendererID = store.getRendererIDForElement(id);
if (rendererID !== null) {
bridge.send('NativeStyleEditor_renameAttribute', {
id,
rendererID,
oldName,
newName,
value,
});
}
};
const changeValue = (name: string, value: any) => {
const rendererID = store.getRendererIDForElement(id);
if (rendererID !== null) {
bridge.send('NativeStyleEditor_setValue', {
id,
rendererID,
name,
value,
});
}
};
const keys = useMemo(() => Array.from(Object.keys(style)), [style]);
const handleCopy = () => copyToClipboard(serializeDataForCopy(style));
return (
<div className={styles.StyleEditor}>
<div className={styles.HeaderRow}>
<div className={styles.Header}>
<div className={styles.Brackets}>{'style {'}</div>
</div>
<Button onClick={handleCopy} title="Copy to clipboard">
<ButtonIcon type="copy" />
</Button>
</div>
{keys.length > 0 &&
keys.map(attribute => (
<Row
key={attribute}
attribute={attribute}
changeAttribute={changeAttribute}
changeValue={changeValue}
validAttributes={store.nativeStyleEditorValidAttributes}
value={style[attribute]}
/>
))}
<NewRow
changeAttribute={changeAttribute}
changeValue={changeValue}
validAttributes={store.nativeStyleEditorValidAttributes}
/>
<div className={styles.Brackets}>{'}'}</div>
</div>
);
}
type NewRowProps = {
changeAttribute: ChangeAttributeFn,
changeValue: ChangeValueFn,
validAttributes: $ReadOnlyArray<string> | null,
};
function NewRow({changeAttribute, changeValue, validAttributes}: NewRowProps) {
const [key, setKey] = useState<number>(0);
const reset = () => setKey(key + 1);
const newAttributeRef = useRef<string>('');
const changeAttributeWrapper = (
oldAttribute: string,
newAttribute: string,
value: any,
) => {
// Ignore attribute changes until a value has been specified
newAttributeRef.current = newAttribute;
};
const changeValueWrapper = (attribute: string, value: any) => {
// Blur events should reset/cancel if there's no value or no attribute
if (newAttributeRef.current !== '') {
if (value !== '') {
changeValue(newAttributeRef.current, value);
}
reset();
}
};
return (
<Row
key={key}
attribute={''}
attributePlaceholder="attribute"
changeAttribute={changeAttributeWrapper}
changeValue={changeValueWrapper}
validAttributes={validAttributes}
value={''}
valuePlaceholder="value"
/>
);
}
type RowProps = {
attribute: string,
attributePlaceholder?: string,
changeAttribute: ChangeAttributeFn,
changeValue: ChangeValueFn,
validAttributes: $ReadOnlyArray<string> | null,
value: any,
valuePlaceholder?: string,
};
function Row({
attribute,
attributePlaceholder,
changeAttribute,
changeValue,
validAttributes,
value,
valuePlaceholder,
}: RowProps) {
// TODO (RN style editor) Use @reach/combobox to auto-complete attributes.
// The list of valid attributes would need to be injected by RN backend,
// which would need to require them from ReactNativeViewViewConfig "validAttributes.style" keys.
// This would need to degrade gracefully for react-native-web,
// although we could let it also inject a custom set of allowed attributes.
const [localAttribute, setLocalAttribute] = useState(attribute);
const [localValue, setLocalValue] = useState(JSON.stringify(value));
const [isAttributeValid, setIsAttributeValid] = useState(true);
const [isValueValid, setIsValueValid] = useState(true);
// $FlowFixMe[missing-local-annot]
const validateAndSetLocalAttribute = newAttribute => {
const isValid =
newAttribute === '' ||
validAttributes === null ||
validAttributes.indexOf(newAttribute) >= 0;
batchedUpdates(() => {
setLocalAttribute(newAttribute);
setIsAttributeValid(isValid);
});
};
// $FlowFixMe[missing-local-annot]
const validateAndSetLocalValue = newValue => {
let isValid = false;
try {
JSON.parse(sanitizeForParse(newValue));
isValid = true;
} catch (error) {}
batchedUpdates(() => {
setLocalValue(newValue);
setIsValueValid(isValid);
});
};
const resetAttribute = () => {
setLocalAttribute(attribute);
};
const resetValue = () => {
setLocalValue(value);
};
const submitValueChange = () => {
if (isAttributeValid && isValueValid) {
const parsedLocalValue = JSON.parse(sanitizeForParse(localValue));
if (value !== parsedLocalValue) {
changeValue(attribute, parsedLocalValue);
}
}
};
const submitAttributeChange = () => {
if (isAttributeValid && isValueValid) {
if (attribute !== localAttribute) {
changeAttribute(attribute, localAttribute, value);
}
}
};
return (
<div className={styles.Row}>
<Field
className={isAttributeValid ? styles.Attribute : styles.Invalid}
onChange={validateAndSetLocalAttribute}
onReset={resetAttribute}
onSubmit={submitAttributeChange}
placeholder={attributePlaceholder}
value={localAttribute}
/>
:
<Field
className={isValueValid ? styles.Value : styles.Invalid}
onChange={validateAndSetLocalValue}
onReset={resetValue}
onSubmit={submitValueChange}
placeholder={valuePlaceholder}
value={localValue}
/>
;
</div>
);
}
type FieldProps = {
className: string,
onChange: (value: any) => void,
onReset: () => void,
onSubmit: () => void,
placeholder?: string,
value: any,
};
function Field({
className,
onChange,
onReset,
onSubmit,
placeholder,
value,
}: FieldProps) {
// $FlowFixMe[missing-local-annot]
const onKeyDown = event => {
switch (event.key) {
case 'Enter':
onSubmit();
break;
case 'Escape':
onReset();
break;
case 'ArrowDown':
case 'ArrowLeft':
case 'ArrowRight':
case 'ArrowUp':
event.stopPropagation();
break;
default:
break;
}
};
return (
<AutoSizeInput
className={`${className} ${styles.Input}`}
onBlur={onSubmit}
onChange={(event: $FlowFixMe) => onChange(event.target.value)}
onKeyDown={onKeyDown}
placeholder={placeholder}
value={value}
/>
);
}