-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathValueNodes.tsx
280 lines (259 loc) · 7.16 KB
/
ValueNodes.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
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
import React, { useEffect, useRef, useState } from 'react'
import { AutogrowTextArea } from './AutogrowTextArea'
import { insertCharInTextArea, toPathString } from './helpers'
import { useTheme } from './contexts'
import { type NodeData, type InputProps } from './types'
import { type TranslateFunction } from './localisation'
export const INVALID_FUNCTION_STRING = '**INVALID_FUNCTION**'
interface StringDisplayProps {
nodeData: NodeData
styles: React.CSSProperties
pathString: string
showStringQuotes?: boolean
stringTruncate?: number
canEdit: boolean
setIsEditing: (value: React.SetStateAction<boolean>) => void
translate: TranslateFunction
}
export const StringDisplay: React.FC<StringDisplayProps> = ({
nodeData,
showStringQuotes = true,
stringTruncate = 200,
pathString,
canEdit,
setIsEditing,
styles,
translate,
}) => {
const value = nodeData.value as string
const [isExpanded, setIsExpanded] = useState(false)
const quoteChar = showStringQuotes ? '"' : ''
const requiresTruncation = value.length > stringTruncate
const handleMaybeEdit = () => {
canEdit ? setIsEditing(true) : setIsExpanded(!isExpanded)
}
return (
<div
id={`${pathString}_display`}
onDoubleClick={handleMaybeEdit}
onClick={(e) => {
if (e.getModifierState('Control') || e.getModifierState('Meta')) handleMaybeEdit()
}}
className="jer-value-string"
style={styles}
>
{quoteChar}
{!requiresTruncation ? (
`${value}${quoteChar}`
) : isExpanded ? (
<>
<span>
{value}
{quoteChar}
</span>
<span className="jer-string-expansion jer-show-less" onClick={() => setIsExpanded(false)}>
{' '}
{translate('SHOW_LESS', nodeData)}
</span>
</>
) : (
<>
<span>{value.slice(0, stringTruncate - 2).trimEnd()}</span>
<span className="jer-string-expansion jer-ellipsis" onClick={() => setIsExpanded(true)}>
...
</span>
{quoteChar}
</>
)}
</div>
)
}
export const StringValue: React.FC<InputProps & { value: string }> = ({
value,
setValue,
isEditing,
path,
handleEdit,
nodeData,
handleKeyboard,
keyboardCommon,
...props
}) => {
const { getStyles } = useTheme()
const textAreaRef = useRef<HTMLTextAreaElement>(null)
const pathString = toPathString(path)
return isEditing ? (
<AutogrowTextArea
className="jer-input-text"
textAreaRef={textAreaRef}
name={pathString}
value={value}
setValue={setValue as React.Dispatch<React.SetStateAction<string>>}
isEditing={isEditing}
handleKeyPress={(e) => {
handleKeyboard(e, {
stringConfirm: handleEdit,
stringLineBreak: () => {
// Simulates standard text-area line break behaviour. Only
// required when control key is not "standard" text-area
// behaviour ("Shift-Enter" or "Enter")
const newValue = insertCharInTextArea(
textAreaRef as React.MutableRefObject<HTMLTextAreaElement>,
'\n'
)
setValue(newValue)
},
...keyboardCommon,
})
}}
styles={getStyles('input', nodeData)}
/>
) : (
<StringDisplay
nodeData={nodeData}
pathString={pathString}
styles={getStyles('string', nodeData)}
{...props}
/>
)
}
export const NumberValue: React.FC<InputProps & { value: number }> = ({
value,
setValue,
isEditing,
path,
setIsEditing,
handleEdit,
nodeData,
handleKeyboard,
keyboardCommon,
}) => {
const { getStyles } = useTheme()
const validateNumber = (input: string) => {
return input.replace(/[^0-9.-]/g, '')
}
return isEditing ? (
<input
className="jer-input-number"
type="text"
name={toPathString(path)}
value={value}
onChange={(e) => setValue(validateNumber(e.target.value))}
autoFocus
onFocus={(e) => setTimeout(() => e.target.select(), 10)}
onKeyDown={(e) =>
handleKeyboard(e, {
numberConfirm: handleEdit,
numberUp: () => setValue(Number(value) + 1),
numberDown: () => setValue(Number(value) - 1),
...keyboardCommon,
})
}
style={{ width: `${String(value).length / 1.5 + 2}em`, ...getStyles('input', nodeData) }}
/>
) : (
<span
onDoubleClick={() => setIsEditing(true)}
className="jer-value-number"
style={getStyles('number', nodeData)}
>
{value}
</span>
)
}
export const BooleanValue: React.FC<InputProps & { value: boolean }> = ({
value,
setValue,
isEditing,
path,
setIsEditing,
handleEdit,
nodeData,
handleKeyboard,
keyboardCommon,
}) => {
const { getStyles } = useTheme()
return isEditing ? (
<input
className="jer-input-boolean"
type="checkbox"
name={toPathString(path)}
checked={value}
onChange={() => setValue(!value)}
onKeyDown={(e) => {
// If we don't explicitly suppress normal checkbox keyboard behaviour,
// the default key (Space) will continue to work even if re-defined
if (e.key === ' ') e.preventDefault()
handleKeyboard(e, {
booleanConfirm: handleEdit,
booleanToggle: () => setValue(!value),
...keyboardCommon,
})
}}
autoFocus
/>
) : (
<span
onDoubleClick={() => setIsEditing(true)}
className="jer-value-boolean"
style={getStyles('boolean', nodeData)}
>
{String(value)}
</span>
)
}
export const NullValue: React.FC<InputProps> = ({
value,
isEditing,
setIsEditing,
handleEdit,
nodeData,
handleKeyboard,
keyboardCommon,
}) => {
const { getStyles } = useTheme()
const timer = useRef<number | undefined>()
useEffect(() => {
if (!isEditing) {
// The listener messes with other elements when switching rapidly (e.g. when "getNext" called repeatedly on inaccessible elements), so we cancel the listener load before it even happens if this node gets switched from isEditing to not in less than 100ms
window.clearTimeout(timer.current)
return
}
// Small delay to prevent registering keyboard input from previous element
// if switched using "Tab"
timer.current = window.setTimeout(
() => window.addEventListener('keydown', listenForSubmit),
100
)
return () => window.removeEventListener('keydown', listenForSubmit)
}, [isEditing])
const listenForSubmit = (e: unknown) =>
handleKeyboard(e as React.KeyboardEvent, {
confirm: handleEdit,
...keyboardCommon,
})
return (
<div
onDoubleClick={() => setIsEditing(true)}
className="jer-value-null"
style={getStyles('null', nodeData)}
>
{String(value)}
</div>
)
}
export const InvalidValue: React.FC<InputProps> = ({ value }) => {
let message = 'Error!'
switch (typeof value) {
case 'string':
if (value === INVALID_FUNCTION_STRING) message = 'Function'
break
case 'undefined':
message = 'Undefined'
break
case 'symbol':
message = 'Symbol'
break
}
return <span className="jer-value-invalid">{message}</span>
}