vzcode 2.30.0 → 2.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/dist/assets/{index-CD0x02Rq.js → index-nL6to42p.js} +137 -137
- package/dist/index.html +1 -1
- package/dist/llm-streaming-server/chatOperations.js +40 -10
- package/package.json +4 -2
- package/src/client/CodeEditor/getOrCreateEditor.tsx +6 -1
- package/src/client/CodeEditor/index.tsx +4 -0
- package/src/client/VZCodeContext/types.ts +12 -0
- package/src/client/VZCodeContext/useVZCodeState.ts +5 -1
- package/src/client/VZLeft.tsx +17 -2
- package/src/client/VZMiddle.tsx +12 -0
- package/src/client/VZSidebar/VisualEditor/VisualEditor.tsx +159 -174
- package/src/client/VZSidebar/VisualEditor/status.ts +150 -0
- package/src/client/VZSidebar/VisualEditor/updateConfig.ts +52 -0
- package/src/client/VZSidebar/VisualEditor/utils.ts +113 -0
- package/src/client/VZSidebar/index.tsx +23 -6
- package/src/client/aiChatButtonEvent.ts +61 -0
- package/src/client/index.ts +6 -0
- package/src/client/main.jsx +22 -0
- package/src/client/vzReducer/createInitialState.ts +8 -3
- package/src/llm-streaming-server/chatOperations.ts +53 -12
- package/src/llm-streaming-server/llmStreaming.ts +0 -3
- package/src/llm-streaming-ui/components/Message.tsx +0 -2
- package/src/types.ts +14 -0
|
@@ -11,15 +11,21 @@ import { VisualEditorConfigEntry } from '../../../types';
|
|
|
11
11
|
import { EmptyState } from '../EmptyState';
|
|
12
12
|
import { color, hcl, HCLColor, rgb } from 'd3-color';
|
|
13
13
|
import { CONFIG_FILE_NAME } from './constants';
|
|
14
|
+
import { getVisualEditorEmptyReason } from './status';
|
|
15
|
+
import { updateVisualEditorProperty } from './updateConfig';
|
|
14
16
|
import { SliderWidget } from './SliderWidget';
|
|
15
17
|
import { CheckboxWidget } from './CheckboxWidget';
|
|
16
18
|
import { TextInputWidget } from './TextInputWidget';
|
|
17
19
|
import { DropdownWidget } from './DropdownWidget';
|
|
18
20
|
import { ColorWidget } from './ColorWidget';
|
|
21
|
+
import { getNestedProperty } from './utils';
|
|
19
22
|
|
|
20
23
|
export const VisualEditor = () => {
|
|
21
|
-
const {
|
|
22
|
-
|
|
24
|
+
const {
|
|
25
|
+
files,
|
|
26
|
+
submitOperation,
|
|
27
|
+
renderVisualEditorEmptyState,
|
|
28
|
+
} = useContext(VZCodeContext);
|
|
23
29
|
|
|
24
30
|
// Local state to track widget values during user interaction
|
|
25
31
|
const [localValues, setLocalValues] = useState<{
|
|
@@ -66,24 +72,16 @@ export const VisualEditor = () => {
|
|
|
66
72
|
[property]: newValue,
|
|
67
73
|
}));
|
|
68
74
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
files: {
|
|
78
|
-
...files,
|
|
79
|
-
[configFileId]: {
|
|
80
|
-
name: 'config.json',
|
|
81
|
-
text: JSON.stringify(newConfigData, null, 2),
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
}));
|
|
75
|
+
submitOperation((document: VizContent) =>
|
|
76
|
+
updateVisualEditorProperty(
|
|
77
|
+
document,
|
|
78
|
+
configFileId,
|
|
79
|
+
property,
|
|
80
|
+
newValue,
|
|
81
|
+
),
|
|
82
|
+
);
|
|
85
83
|
},
|
|
86
|
-
[
|
|
84
|
+
[configFileId, submitOperation],
|
|
87
85
|
);
|
|
88
86
|
|
|
89
87
|
const onCheckboxChange = useCallback(
|
|
@@ -97,24 +95,16 @@ export const VisualEditor = () => {
|
|
|
97
95
|
[property]: newValue,
|
|
98
96
|
}));
|
|
99
97
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
files: {
|
|
109
|
-
...files,
|
|
110
|
-
[configFileId]: {
|
|
111
|
-
name: 'config.json',
|
|
112
|
-
text: JSON.stringify(newConfigData, null, 2),
|
|
113
|
-
},
|
|
114
|
-
},
|
|
115
|
-
}));
|
|
98
|
+
submitOperation((document: VizContent) =>
|
|
99
|
+
updateVisualEditorProperty(
|
|
100
|
+
document,
|
|
101
|
+
configFileId,
|
|
102
|
+
property,
|
|
103
|
+
newValue,
|
|
104
|
+
),
|
|
105
|
+
);
|
|
116
106
|
},
|
|
117
|
-
[
|
|
107
|
+
[configFileId, submitOperation],
|
|
118
108
|
);
|
|
119
109
|
|
|
120
110
|
const onTextInputChange = useCallback(
|
|
@@ -128,24 +118,16 @@ export const VisualEditor = () => {
|
|
|
128
118
|
[property]: newValue,
|
|
129
119
|
}));
|
|
130
120
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
files: {
|
|
140
|
-
...files,
|
|
141
|
-
[configFileId]: {
|
|
142
|
-
name: 'config.json',
|
|
143
|
-
text: JSON.stringify(newConfigData, null, 2),
|
|
144
|
-
},
|
|
145
|
-
},
|
|
146
|
-
}));
|
|
121
|
+
submitOperation((document: VizContent) =>
|
|
122
|
+
updateVisualEditorProperty(
|
|
123
|
+
document,
|
|
124
|
+
configFileId,
|
|
125
|
+
property,
|
|
126
|
+
newValue,
|
|
127
|
+
),
|
|
128
|
+
);
|
|
147
129
|
},
|
|
148
|
-
[
|
|
130
|
+
[configFileId, submitOperation],
|
|
149
131
|
);
|
|
150
132
|
|
|
151
133
|
const onDropdownChange = useCallback(
|
|
@@ -156,24 +138,16 @@ export const VisualEditor = () => {
|
|
|
156
138
|
[property]: newValue,
|
|
157
139
|
}));
|
|
158
140
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
files: {
|
|
168
|
-
...files,
|
|
169
|
-
[configFileId]: {
|
|
170
|
-
name: 'config.json',
|
|
171
|
-
text: JSON.stringify(newConfigData, null, 2),
|
|
172
|
-
},
|
|
173
|
-
},
|
|
174
|
-
}));
|
|
141
|
+
submitOperation((document: VizContent) =>
|
|
142
|
+
updateVisualEditorProperty(
|
|
143
|
+
document,
|
|
144
|
+
configFileId,
|
|
145
|
+
property,
|
|
146
|
+
newValue,
|
|
147
|
+
),
|
|
148
|
+
);
|
|
175
149
|
},
|
|
176
|
-
[
|
|
150
|
+
[configFileId, submitOperation],
|
|
177
151
|
);
|
|
178
152
|
|
|
179
153
|
// Custom dropdown handlers
|
|
@@ -224,9 +198,10 @@ export const VisualEditor = () => {
|
|
|
224
198
|
event.currentTarget.value,
|
|
225
199
|
);
|
|
226
200
|
|
|
227
|
-
// Get current hex color from config
|
|
201
|
+
// Get current hex color from config using nested property access
|
|
228
202
|
const currentHex =
|
|
229
|
-
configData
|
|
203
|
+
getNestedProperty<string>(configData, property) ??
|
|
204
|
+
'#000000';
|
|
230
205
|
|
|
231
206
|
// Convert current hex to LCH
|
|
232
207
|
let hclFromRGB: HCLColor;
|
|
@@ -281,26 +256,17 @@ export const VisualEditor = () => {
|
|
|
281
256
|
[`${property}_l`]: newhcl[2],
|
|
282
257
|
}));
|
|
283
258
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
files: {
|
|
293
|
-
...files,
|
|
294
|
-
[configFileId]: {
|
|
295
|
-
name: 'config.json',
|
|
296
|
-
text: JSON.stringify(newConfigData, null, 2),
|
|
297
|
-
},
|
|
298
|
-
},
|
|
299
|
-
}));
|
|
259
|
+
submitOperation((document: VizContent) =>
|
|
260
|
+
updateVisualEditorProperty(
|
|
261
|
+
document,
|
|
262
|
+
configFileId,
|
|
263
|
+
property,
|
|
264
|
+
newHex,
|
|
265
|
+
),
|
|
266
|
+
);
|
|
300
267
|
},
|
|
301
268
|
[
|
|
302
269
|
configData,
|
|
303
|
-
files,
|
|
304
270
|
configFileId,
|
|
305
271
|
submitOperation,
|
|
306
272
|
localValues,
|
|
@@ -309,7 +275,10 @@ export const VisualEditor = () => {
|
|
|
309
275
|
|
|
310
276
|
const visualEditorWidgets: VisualEditorConfigEntry[] =
|
|
311
277
|
useMemo(
|
|
312
|
-
() =>
|
|
278
|
+
() =>
|
|
279
|
+
Array.isArray(configData?.visualEditorWidgets)
|
|
280
|
+
? configData.visualEditorWidgets
|
|
281
|
+
: [],
|
|
313
282
|
[configData],
|
|
314
283
|
);
|
|
315
284
|
|
|
@@ -320,92 +289,78 @@ export const VisualEditor = () => {
|
|
|
320
289
|
} = {};
|
|
321
290
|
visualEditorWidgets.forEach((widget) => {
|
|
322
291
|
if (widget.type === 'slider') {
|
|
323
|
-
newLocalValues[widget.property] =
|
|
324
|
-
configData
|
|
292
|
+
newLocalValues[widget.property] = getNestedProperty(
|
|
293
|
+
configData,
|
|
294
|
+
widget.property,
|
|
295
|
+
);
|
|
325
296
|
} else if (widget.type === 'checkbox') {
|
|
326
|
-
newLocalValues[widget.property] =
|
|
327
|
-
configData
|
|
297
|
+
newLocalValues[widget.property] = getNestedProperty(
|
|
298
|
+
configData,
|
|
299
|
+
widget.property,
|
|
300
|
+
);
|
|
328
301
|
} else if (widget.type === 'textInput') {
|
|
329
|
-
newLocalValues[widget.property] =
|
|
330
|
-
configData
|
|
302
|
+
newLocalValues[widget.property] = getNestedProperty(
|
|
303
|
+
configData,
|
|
304
|
+
widget.property,
|
|
305
|
+
);
|
|
331
306
|
} else if (widget.type === 'dropdown') {
|
|
332
|
-
newLocalValues[widget.property] =
|
|
333
|
-
configData
|
|
307
|
+
newLocalValues[widget.property] = getNestedProperty(
|
|
308
|
+
configData,
|
|
309
|
+
widget.property,
|
|
310
|
+
);
|
|
334
311
|
} else if (widget.type === 'color') {
|
|
335
|
-
newLocalValues[widget.property] =
|
|
336
|
-
configData
|
|
312
|
+
newLocalValues[widget.property] = getNestedProperty(
|
|
313
|
+
configData,
|
|
314
|
+
widget.property,
|
|
315
|
+
);
|
|
337
316
|
|
|
338
317
|
// Convert hex to LCH for internal state
|
|
339
|
-
const hexColor =
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
newLocalValues[`${widget.property}_h`] = 0;
|
|
357
|
-
}
|
|
318
|
+
const hexColor = getNestedProperty<string>(
|
|
319
|
+
configData,
|
|
320
|
+
widget.property,
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
if (hexColor) {
|
|
324
|
+
try {
|
|
325
|
+
const rgbColor = color(hexColor);
|
|
326
|
+
const lch = hcl(rgbColor);
|
|
327
|
+
newLocalValues[`${widget.property}_h`] = lch.h;
|
|
328
|
+
newLocalValues[`${widget.property}_c`] = lch.c;
|
|
329
|
+
newLocalValues[`${widget.property}_l`] = lch.l;
|
|
330
|
+
} catch {
|
|
331
|
+
// Fallback values if conversion fails
|
|
332
|
+
newLocalValues[`${widget.property}_l`] = 0;
|
|
333
|
+
newLocalValues[`${widget.property}_c`] = 0;
|
|
334
|
+
newLocalValues[`${widget.property}_h`] = 0;
|
|
358
335
|
}
|
|
359
336
|
}
|
|
360
337
|
}
|
|
361
338
|
});
|
|
362
339
|
setLocalValues(newLocalValues);
|
|
363
|
-
}, [configData,
|
|
364
|
-
|
|
365
|
-
if (configFileId === null) {
|
|
366
|
-
return (
|
|
367
|
-
<EmptyState>
|
|
368
|
-
To begin using the visual editor, create a
|
|
369
|
-
config.json.
|
|
370
|
-
</EmptyState>
|
|
371
|
-
);
|
|
372
|
-
}
|
|
340
|
+
}, [configData, visualEditorWidgets]);
|
|
373
341
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
if (
|
|
397
|
-
configData &&
|
|
398
|
-
!Array.isArray(configData.visualEditorWidgets)
|
|
399
|
-
) {
|
|
400
|
-
return (
|
|
401
|
-
<EmptyState>
|
|
402
|
-
Your config.json file has
|
|
403
|
-
"visualEditorWidgets" but it is not an
|
|
404
|
-
array. Please make sure
|
|
405
|
-
"visualEditorWidgets" is an array of
|
|
406
|
-
widget configurations.
|
|
407
|
-
</EmptyState>
|
|
408
|
-
);
|
|
342
|
+
const emptyReason = getVisualEditorEmptyReason(
|
|
343
|
+
configFileId === null
|
|
344
|
+
? null
|
|
345
|
+
: (files?.[configFileId]?.text ?? null),
|
|
346
|
+
);
|
|
347
|
+
if (emptyReason) {
|
|
348
|
+
const hostUI =
|
|
349
|
+
renderVisualEditorEmptyState?.(emptyReason);
|
|
350
|
+
if (hostUI != null) return <>{hostUI}</>;
|
|
351
|
+
const messages = {
|
|
352
|
+
'missing-config':
|
|
353
|
+
'To begin using the visual editor, create a config.json.',
|
|
354
|
+
'invalid-config':
|
|
355
|
+
'Your config.json file is not valid JSON.',
|
|
356
|
+
'missing-widgets':
|
|
357
|
+
'Add a visualEditorWidgets array to config.json.',
|
|
358
|
+
'invalid-widgets':
|
|
359
|
+
'Your visualEditorWidgets configuration is invalid.',
|
|
360
|
+
'empty-widgets':
|
|
361
|
+
'Add at least one widget to visualEditorWidgets.',
|
|
362
|
+
};
|
|
363
|
+
return <EmptyState>{messages[emptyReason]}</EmptyState>;
|
|
409
364
|
}
|
|
410
365
|
|
|
411
366
|
return (
|
|
@@ -414,8 +369,14 @@ export const VisualEditor = () => {
|
|
|
414
369
|
if (widgetConfig.type === 'slider') {
|
|
415
370
|
// Use local value if available, otherwise fall back to config value
|
|
416
371
|
const currentValue =
|
|
417
|
-
localValues[
|
|
418
|
-
|
|
372
|
+
(localValues[
|
|
373
|
+
widgetConfig.property
|
|
374
|
+
] as number) ??
|
|
375
|
+
getNestedProperty<number>(
|
|
376
|
+
configData,
|
|
377
|
+
widgetConfig.property,
|
|
378
|
+
) ??
|
|
379
|
+
widgetConfig.min;
|
|
419
380
|
|
|
420
381
|
return (
|
|
421
382
|
<SliderWidget
|
|
@@ -434,8 +395,14 @@ export const VisualEditor = () => {
|
|
|
434
395
|
} else if (widgetConfig.type === 'checkbox') {
|
|
435
396
|
// Use local value if available, otherwise fall back to config value
|
|
436
397
|
const currentValue =
|
|
437
|
-
localValues[
|
|
438
|
-
|
|
398
|
+
(localValues[
|
|
399
|
+
widgetConfig.property
|
|
400
|
+
] as boolean) ??
|
|
401
|
+
getNestedProperty<boolean>(
|
|
402
|
+
configData,
|
|
403
|
+
widgetConfig.property,
|
|
404
|
+
) ??
|
|
405
|
+
false;
|
|
439
406
|
|
|
440
407
|
return (
|
|
441
408
|
<CheckboxWidget
|
|
@@ -451,8 +418,14 @@ export const VisualEditor = () => {
|
|
|
451
418
|
} else if (widgetConfig.type === 'textInput') {
|
|
452
419
|
// Use local value if available, otherwise fall back to config value
|
|
453
420
|
const currentValue =
|
|
454
|
-
localValues[
|
|
455
|
-
|
|
421
|
+
(localValues[
|
|
422
|
+
widgetConfig.property
|
|
423
|
+
] as string) ??
|
|
424
|
+
getNestedProperty<string>(
|
|
425
|
+
configData,
|
|
426
|
+
widgetConfig.property,
|
|
427
|
+
) ??
|
|
428
|
+
'';
|
|
456
429
|
|
|
457
430
|
return (
|
|
458
431
|
<TextInputWidget
|
|
@@ -468,8 +441,15 @@ export const VisualEditor = () => {
|
|
|
468
441
|
} else if (widgetConfig.type === 'dropdown') {
|
|
469
442
|
// Use local value if available, otherwise fall back to config value
|
|
470
443
|
const currentValue =
|
|
471
|
-
localValues[
|
|
472
|
-
|
|
444
|
+
(localValues[
|
|
445
|
+
widgetConfig.property
|
|
446
|
+
] as string) ??
|
|
447
|
+
getNestedProperty<string>(
|
|
448
|
+
configData,
|
|
449
|
+
widgetConfig.property,
|
|
450
|
+
) ??
|
|
451
|
+
widgetConfig.options?.[0] ??
|
|
452
|
+
'';
|
|
473
453
|
const isOpen =
|
|
474
454
|
openDropdown === widgetConfig.property;
|
|
475
455
|
|
|
@@ -495,8 +475,13 @@ export const VisualEditor = () => {
|
|
|
495
475
|
} else if (widgetConfig.type === 'color') {
|
|
496
476
|
// Use local value if available, otherwise fall back to config value
|
|
497
477
|
const currentHex =
|
|
498
|
-
localValues[
|
|
499
|
-
|
|
478
|
+
(localValues[
|
|
479
|
+
widgetConfig.property
|
|
480
|
+
] as string) ??
|
|
481
|
+
getNestedProperty<string>(
|
|
482
|
+
configData,
|
|
483
|
+
widgetConfig.property,
|
|
484
|
+
) ??
|
|
500
485
|
'#000000';
|
|
501
486
|
|
|
502
487
|
// Convert hex to LCH for slider values
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { color } from 'd3-color';
|
|
2
|
+
import type { VisualEditorEmptyReason } from '../../../types';
|
|
3
|
+
|
|
4
|
+
const UNSAFE_PATH_SEGMENTS = new Set([
|
|
5
|
+
'__proto__',
|
|
6
|
+
'constructor',
|
|
7
|
+
'prototype',
|
|
8
|
+
]);
|
|
9
|
+
|
|
10
|
+
const isRecord = (
|
|
11
|
+
value: unknown,
|
|
12
|
+
): value is Record<string, unknown> =>
|
|
13
|
+
value !== null &&
|
|
14
|
+
typeof value === 'object' &&
|
|
15
|
+
!Array.isArray(value);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Resolve only own properties of plain JSON objects, using the same dot-path
|
|
19
|
+
* semantics as setNestedProperty. Missing values, arrays in the path and
|
|
20
|
+
* dangerous keys are not editable targets.
|
|
21
|
+
*/
|
|
22
|
+
function editableValueAt(
|
|
23
|
+
config: Record<string, unknown>,
|
|
24
|
+
path: string,
|
|
25
|
+
): { found: boolean; value: unknown } {
|
|
26
|
+
const segments = path.split('.');
|
|
27
|
+
if (
|
|
28
|
+
segments.some(
|
|
29
|
+
(segment) =>
|
|
30
|
+
!segment || UNSAFE_PATH_SEGMENTS.has(segment),
|
|
31
|
+
)
|
|
32
|
+
) {
|
|
33
|
+
return { found: false, value: undefined };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let value: unknown = config;
|
|
37
|
+
for (const segment of segments) {
|
|
38
|
+
if (
|
|
39
|
+
!isRecord(value) ||
|
|
40
|
+
!Object.prototype.hasOwnProperty.call(value, segment)
|
|
41
|
+
) {
|
|
42
|
+
return { found: false, value: undefined };
|
|
43
|
+
}
|
|
44
|
+
value = value[segment];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return { found: true, value };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Pure classifier shared by the visual editor and host integration tests. */
|
|
51
|
+
export function getVisualEditorEmptyReason(
|
|
52
|
+
configText: string | null,
|
|
53
|
+
): VisualEditorEmptyReason | null {
|
|
54
|
+
if (configText === null) return 'missing-config';
|
|
55
|
+
let config: unknown;
|
|
56
|
+
try {
|
|
57
|
+
config = JSON.parse(configText);
|
|
58
|
+
} catch {
|
|
59
|
+
return 'invalid-config';
|
|
60
|
+
}
|
|
61
|
+
if (!isRecord(config)) return 'invalid-config';
|
|
62
|
+
if (
|
|
63
|
+
!Object.prototype.hasOwnProperty.call(
|
|
64
|
+
config,
|
|
65
|
+
'visualEditorWidgets',
|
|
66
|
+
)
|
|
67
|
+
)
|
|
68
|
+
return 'missing-widgets';
|
|
69
|
+
|
|
70
|
+
const widgets = config.visualEditorWidgets;
|
|
71
|
+
if (!Array.isArray(widgets)) return 'invalid-widgets';
|
|
72
|
+
if (!widgets.length) return 'empty-widgets';
|
|
73
|
+
|
|
74
|
+
for (const widget of widgets) {
|
|
75
|
+
if (
|
|
76
|
+
!isRecord(widget) ||
|
|
77
|
+
typeof widget.property !== 'string' ||
|
|
78
|
+
!widget.property ||
|
|
79
|
+
typeof widget.label !== 'string' ||
|
|
80
|
+
!widget.label.trim()
|
|
81
|
+
) {
|
|
82
|
+
return 'invalid-widgets';
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const { found, value } = editableValueAt(
|
|
86
|
+
config,
|
|
87
|
+
widget.property,
|
|
88
|
+
);
|
|
89
|
+
if (!found) return 'invalid-widgets';
|
|
90
|
+
|
|
91
|
+
switch (widget.type) {
|
|
92
|
+
case 'slider':
|
|
93
|
+
if (
|
|
94
|
+
!Number.isFinite(value) ||
|
|
95
|
+
!Number.isFinite(widget.min) ||
|
|
96
|
+
!Number.isFinite(widget.max) ||
|
|
97
|
+
(widget.min as number) >=
|
|
98
|
+
(widget.max as number) ||
|
|
99
|
+
(widget.step !== undefined &&
|
|
100
|
+
(!Number.isFinite(widget.step) ||
|
|
101
|
+
(widget.step as number) <= 0))
|
|
102
|
+
) {
|
|
103
|
+
return 'invalid-widgets';
|
|
104
|
+
}
|
|
105
|
+
break;
|
|
106
|
+
case 'checkbox':
|
|
107
|
+
if (typeof value !== 'boolean')
|
|
108
|
+
return 'invalid-widgets';
|
|
109
|
+
break;
|
|
110
|
+
case 'textInput':
|
|
111
|
+
if (typeof value !== 'string')
|
|
112
|
+
return 'invalid-widgets';
|
|
113
|
+
break;
|
|
114
|
+
case 'dropdown':
|
|
115
|
+
if (
|
|
116
|
+
typeof value !== 'string' ||
|
|
117
|
+
!Array.isArray(widget.options) ||
|
|
118
|
+
widget.options.length === 0 ||
|
|
119
|
+
!widget.options.every(
|
|
120
|
+
(option: unknown) => typeof option === 'string',
|
|
121
|
+
) ||
|
|
122
|
+
!widget.options.includes(value)
|
|
123
|
+
) {
|
|
124
|
+
return 'invalid-widgets';
|
|
125
|
+
}
|
|
126
|
+
break;
|
|
127
|
+
case 'color': {
|
|
128
|
+
if (typeof value !== 'string')
|
|
129
|
+
return 'invalid-widgets';
|
|
130
|
+
// A color widget converts through RGB/HCL. A mere string (or the
|
|
131
|
+
// special transparent color with NaN RGB channels) is not usable.
|
|
132
|
+
const rgb = color(value)?.rgb();
|
|
133
|
+
if (
|
|
134
|
+
!rgb ||
|
|
135
|
+
!Number.isFinite(rgb.r) ||
|
|
136
|
+
!Number.isFinite(rgb.g) ||
|
|
137
|
+
!Number.isFinite(rgb.b) ||
|
|
138
|
+
rgb.opacity !== 1
|
|
139
|
+
) {
|
|
140
|
+
return 'invalid-widgets';
|
|
141
|
+
}
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
default:
|
|
145
|
+
return 'invalid-widgets';
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
VizContent,
|
|
3
|
+
VizFileId,
|
|
4
|
+
} from '@vizhub/viz-types';
|
|
5
|
+
import { setNestedProperty } from './utils';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Apply a widget edit against the LATEST ShareDB document, not a stale React
|
|
9
|
+
* closure. Preserve unrelated files and concurrent edits to other properties.
|
|
10
|
+
*/
|
|
11
|
+
export function updateVisualEditorProperty(
|
|
12
|
+
document: VizContent,
|
|
13
|
+
configFileId: VizFileId | null,
|
|
14
|
+
property: string,
|
|
15
|
+
value: string | number | boolean,
|
|
16
|
+
): VizContent {
|
|
17
|
+
if (!configFileId) return document;
|
|
18
|
+
const original = document.files[configFileId];
|
|
19
|
+
if (!original) return document;
|
|
20
|
+
let currentConfig: unknown;
|
|
21
|
+
try {
|
|
22
|
+
currentConfig = JSON.parse(original.text);
|
|
23
|
+
} catch {
|
|
24
|
+
return document;
|
|
25
|
+
}
|
|
26
|
+
if (
|
|
27
|
+
!currentConfig ||
|
|
28
|
+
typeof currentConfig !== 'object' ||
|
|
29
|
+
Array.isArray(currentConfig)
|
|
30
|
+
)
|
|
31
|
+
return document;
|
|
32
|
+
let updated: Record<string, unknown>;
|
|
33
|
+
try {
|
|
34
|
+
updated = setNestedProperty(
|
|
35
|
+
currentConfig as Record<string, unknown>,
|
|
36
|
+
property,
|
|
37
|
+
value,
|
|
38
|
+
);
|
|
39
|
+
} catch {
|
|
40
|
+
return document;
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
...document,
|
|
44
|
+
files: {
|
|
45
|
+
...document.files,
|
|
46
|
+
[configFileId]: {
|
|
47
|
+
...original,
|
|
48
|
+
text: JSON.stringify(updated, null, 2),
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|