vzcode 3.2.0 → 3.3.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 +185 -40
- package/dist/assets/App-C9mRI7eH.js +387 -0
- package/dist/assets/App-CxiKy3Rs.css +1 -0
- package/dist/assets/bindings_wasm_bg-IWNG_AjZ.wasm +0 -0
- package/dist/assets/buildWorker-N5tFTOqM.js +794 -0
- package/dist/assets/index-COLyIkCc.js +43 -0
- package/dist/assets/index-CZT-v4sH.css +1 -0
- package/dist/assets/rolldown-runtime-COiEfhsA.js +1 -0
- package/dist/assets/worker-BpV7Ustv.js +223 -0
- package/dist/assets/worker-CLeezw6h.js +458 -0
- package/dist/assets/worker-DaSIYBQX.js +327 -0
- package/dist/cli.js +170 -99
- package/dist/index.html +3 -2
- package/dist/server/accessControl.js +104 -0
- package/dist/server/computeInitialDocument.js +4 -1
- package/dist/server/createServer.js +356 -104
- package/dist/server/index.js +4 -3
- package/dist/server/workspaceSecurity.js +115 -0
- package/dist/server/workspaceSync.js +141 -0
- package/package.json +74 -72
- package/src/cli.ts +203 -182
- package/src/client/App/index.tsx +2 -0
- package/src/client/App/usePending.ts +3 -1
- package/src/client/RunCodeWidget/index.tsx +1 -0
- package/src/client/VZCodeContext/types.ts +6 -0
- package/src/client/VZCodeContext/useVZCodeState.ts +2 -0
- package/src/client/VZSidebar/AIChatWrapper.tsx +2 -0
- package/src/client/VZSidebar/VisualEditor/CheckboxWidget.tsx +22 -30
- package/src/client/VZSidebar/VisualEditor/ColorWidget.tsx +198 -126
- package/src/client/VZSidebar/VisualEditor/DropdownWidget.tsx +25 -67
- package/src/client/VZSidebar/VisualEditor/PropertySection.tsx +41 -0
- package/src/client/VZSidebar/VisualEditor/SegmentedWidget.tsx +34 -0
- package/src/client/VZSidebar/VisualEditor/SliderWidget.tsx +60 -12
- package/src/client/VZSidebar/VisualEditor/VisualEditor.tsx +31 -393
- package/src/client/VZSidebar/VisualEditor/VisualEditorControls.tsx +478 -0
- package/src/client/VZSidebar/VisualEditor/colorUtils.ts +12 -0
- package/src/client/VZSidebar/VisualEditor/status.ts +47 -0
- package/src/client/VZSidebar/VisualEditor/updateConfig.ts +51 -24
- package/src/client/VZSidebar/VisualEditor/utils.ts +1 -1
- package/src/client/VZSidebar/styles.scss +420 -0
- package/src/client/WorkspaceConflictBanner.tsx +120 -0
- package/src/client/index.ts +2 -0
- package/src/client/main.jsx +54 -12
- package/src/llm-streaming-server/README.md +4 -16
- package/src/llm-streaming-ui/README.md +41 -2
- package/src/llm-streaming-ui/components/ChatInput.tsx +65 -13
- package/src/llm-streaming-ui/components/index.tsx +2 -0
- package/src/llm-streaming-ui/hooks/useAutoScroll.ts +1 -1
- package/src/llm-streaming-ui/index.ts +1 -0
- package/src/llm-streaming-ui/props.ts +26 -2
- package/src/server/accessControl.ts +151 -0
- package/src/server/computeInitialDocument.ts +6 -2
- package/src/server/createServer.ts +487 -138
- package/src/server/index.ts +4 -5
- package/src/server/workspaceSecurity.ts +160 -0
- package/src/server/workspaceSync.ts +203 -0
- package/src/types.ts +29 -27
- package/dist/assets/bindings_wasm_bg-9w8E-TmY.wasm +0 -0
- package/dist/assets/buildWorker-BZlya278.js +0 -687
- package/dist/assets/index-B7NRhsN6.js +0 -356
- package/dist/assets/index-D1V1AKed.css +0 -1
- package/dist/assets/worker-1oQYaz-F.js +0 -342
- package/dist/assets/worker-BfgPD8zR.js +0 -125
- package/dist/assets/worker-CoJaqtpi.js +0 -436
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
/** Reusable visual properties UI for live ShareDB and embedded review. */
|
|
2
|
+
import React, {
|
|
3
|
+
useCallback,
|
|
4
|
+
useEffect,
|
|
5
|
+
useMemo,
|
|
6
|
+
useRef,
|
|
7
|
+
useState,
|
|
8
|
+
} from 'react';
|
|
9
|
+
import type {
|
|
10
|
+
VisualEditorConfigEntry,
|
|
11
|
+
VisualEditorEmptyReason,
|
|
12
|
+
} from '../../../types';
|
|
13
|
+
import { EmptyState } from '../EmptyState';
|
|
14
|
+
import {
|
|
15
|
+
toHclChannels,
|
|
16
|
+
coerceChannelNumber,
|
|
17
|
+
hclChannelsToHex,
|
|
18
|
+
} from './colorUtils';
|
|
19
|
+
import { getVisualEditorEmptyReason } from './status';
|
|
20
|
+
import type { WidgetValue } from './updateConfig';
|
|
21
|
+
import { SliderWidget } from './SliderWidget';
|
|
22
|
+
import { CheckboxWidget } from './CheckboxWidget';
|
|
23
|
+
import { TextInputWidget } from './TextInputWidget';
|
|
24
|
+
import { DropdownWidget } from './DropdownWidget';
|
|
25
|
+
import { SegmentedWidget } from './SegmentedWidget';
|
|
26
|
+
import { ColorWidget } from './ColorWidget';
|
|
27
|
+
import { getNestedProperty } from './utils';
|
|
28
|
+
import { PropertySection } from './PropertySection';
|
|
29
|
+
|
|
30
|
+
const LIVE_EDIT_INTERVAL_MS = 75;
|
|
31
|
+
export interface VisualEditorControlsProps {
|
|
32
|
+
configText: string | null;
|
|
33
|
+
onChange: (property: string, value: WidgetValue) => void;
|
|
34
|
+
/** Optional transactional persistence for coalesced live edits. */
|
|
35
|
+
onChangeBatch?: (
|
|
36
|
+
changes: Record<string, WidgetValue>,
|
|
37
|
+
) => void;
|
|
38
|
+
renderEmptyState?: (
|
|
39
|
+
reason: VisualEditorEmptyReason,
|
|
40
|
+
) => React.ReactNode;
|
|
41
|
+
}
|
|
42
|
+
export const VisualEditorControls: React.FC<
|
|
43
|
+
VisualEditorControlsProps
|
|
44
|
+
> = ({
|
|
45
|
+
configText,
|
|
46
|
+
onChange,
|
|
47
|
+
onChangeBatch,
|
|
48
|
+
renderEmptyState,
|
|
49
|
+
}) => {
|
|
50
|
+
const [localValues, setLocalValues] = useState<
|
|
51
|
+
Record<string, WidgetValue>
|
|
52
|
+
>({});
|
|
53
|
+
const pendingRef = useRef<Record<string, WidgetValue>>(
|
|
54
|
+
{},
|
|
55
|
+
);
|
|
56
|
+
const timerRef = useRef<ReturnType<
|
|
57
|
+
typeof setTimeout
|
|
58
|
+
> | null>(null);
|
|
59
|
+
// A timer always uses the latest host callback, not a stale render closure.
|
|
60
|
+
const callbacksRef = useRef({ onChange, onChangeBatch });
|
|
61
|
+
callbacksRef.current = { onChange, onChangeBatch };
|
|
62
|
+
|
|
63
|
+
const configData = useMemo<Record<
|
|
64
|
+
string,
|
|
65
|
+
unknown
|
|
66
|
+
> | null>(() => {
|
|
67
|
+
if (configText === null) return null;
|
|
68
|
+
try {
|
|
69
|
+
const parsed: unknown = JSON.parse(configText);
|
|
70
|
+
return parsed &&
|
|
71
|
+
typeof parsed === 'object' &&
|
|
72
|
+
!Array.isArray(parsed)
|
|
73
|
+
? (parsed as Record<string, unknown>)
|
|
74
|
+
: null;
|
|
75
|
+
} catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
}, [configText]);
|
|
79
|
+
|
|
80
|
+
const visualEditorWidgets = useMemo(
|
|
81
|
+
() =>
|
|
82
|
+
Array.isArray(configData?.visualEditorWidgets)
|
|
83
|
+
? (configData.visualEditorWidgets as VisualEditorConfigEntry[])
|
|
84
|
+
: [],
|
|
85
|
+
[configData],
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
const groups = useMemo(() => {
|
|
89
|
+
const grouped = new Map<
|
|
90
|
+
string,
|
|
91
|
+
VisualEditorConfigEntry[]
|
|
92
|
+
>();
|
|
93
|
+
for (const widget of visualEditorWidgets) {
|
|
94
|
+
// Validate at render time too: invalid config must show its empty state,
|
|
95
|
+
// never throw while preparing groups.
|
|
96
|
+
if (
|
|
97
|
+
!widget ||
|
|
98
|
+
typeof widget !== 'object' ||
|
|
99
|
+
typeof widget.property !== 'string'
|
|
100
|
+
)
|
|
101
|
+
continue;
|
|
102
|
+
const section =
|
|
103
|
+
typeof widget.section === 'string' &&
|
|
104
|
+
widget.section.trim()
|
|
105
|
+
? widget.section.trim()
|
|
106
|
+
: 'Appearance';
|
|
107
|
+
const members = grouped.get(section) ?? [];
|
|
108
|
+
members.push(widget);
|
|
109
|
+
grouped.set(section, members);
|
|
110
|
+
}
|
|
111
|
+
return [...grouped].map(([title, widgets]) => ({
|
|
112
|
+
title,
|
|
113
|
+
widgets,
|
|
114
|
+
}));
|
|
115
|
+
}, [visualEditorWidgets]);
|
|
116
|
+
|
|
117
|
+
// Coalesce rapid slider/typing/color input events. Each flush reads the
|
|
118
|
+
// latest ShareDB document; independent edits remain intact.
|
|
119
|
+
const flushPending = useCallback(() => {
|
|
120
|
+
if (timerRef.current !== null) {
|
|
121
|
+
clearTimeout(timerRef.current);
|
|
122
|
+
timerRef.current = null;
|
|
123
|
+
}
|
|
124
|
+
const changes = pendingRef.current;
|
|
125
|
+
pendingRef.current = {};
|
|
126
|
+
if (Object.keys(changes).length === 0) return;
|
|
127
|
+
const {
|
|
128
|
+
onChange: writeOne,
|
|
129
|
+
onChangeBatch: writeBatch,
|
|
130
|
+
} = callbacksRef.current;
|
|
131
|
+
if (writeBatch) writeBatch(changes);
|
|
132
|
+
else
|
|
133
|
+
for (const [property, value] of Object.entries(
|
|
134
|
+
changes,
|
|
135
|
+
)) {
|
|
136
|
+
writeOne(property, value);
|
|
137
|
+
}
|
|
138
|
+
}, []);
|
|
139
|
+
|
|
140
|
+
// Flush a final pointer/typing change when the panel closes.
|
|
141
|
+
useEffect(() => () => flushPending(), [flushPending]);
|
|
142
|
+
|
|
143
|
+
const setWidgetValue = useCallback(
|
|
144
|
+
(
|
|
145
|
+
property: string,
|
|
146
|
+
value: WidgetValue,
|
|
147
|
+
extra: Record<string, number> = {},
|
|
148
|
+
continuous = false,
|
|
149
|
+
) => {
|
|
150
|
+
setLocalValues((prev) => ({
|
|
151
|
+
...prev,
|
|
152
|
+
[property]: value,
|
|
153
|
+
...extra,
|
|
154
|
+
}));
|
|
155
|
+
pendingRef.current[property] = value;
|
|
156
|
+
if (continuous) {
|
|
157
|
+
if (timerRef.current === null) {
|
|
158
|
+
timerRef.current = setTimeout(
|
|
159
|
+
flushPending,
|
|
160
|
+
LIVE_EDIT_INTERVAL_MS,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
} else flushPending();
|
|
164
|
+
},
|
|
165
|
+
[flushPending],
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const onSliderChange = useCallback(
|
|
169
|
+
(property: string) =>
|
|
170
|
+
(event: React.ChangeEvent<HTMLInputElement>) =>
|
|
171
|
+
setWidgetValue(
|
|
172
|
+
property,
|
|
173
|
+
Number(event.currentTarget.value),
|
|
174
|
+
{},
|
|
175
|
+
true,
|
|
176
|
+
),
|
|
177
|
+
[setWidgetValue],
|
|
178
|
+
);
|
|
179
|
+
const onCheckboxChange = useCallback(
|
|
180
|
+
(property: string) =>
|
|
181
|
+
(event: React.ChangeEvent<HTMLInputElement>) =>
|
|
182
|
+
setWidgetValue(
|
|
183
|
+
property,
|
|
184
|
+
event.currentTarget.checked,
|
|
185
|
+
),
|
|
186
|
+
[setWidgetValue],
|
|
187
|
+
);
|
|
188
|
+
const onTextInputChange = useCallback(
|
|
189
|
+
(property: string) =>
|
|
190
|
+
(event: React.ChangeEvent<HTMLInputElement>) =>
|
|
191
|
+
setWidgetValue(
|
|
192
|
+
property,
|
|
193
|
+
event.currentTarget.value,
|
|
194
|
+
{},
|
|
195
|
+
true,
|
|
196
|
+
),
|
|
197
|
+
[setWidgetValue],
|
|
198
|
+
);
|
|
199
|
+
const onDropdownChange = useCallback(
|
|
200
|
+
(property: string, value: string) =>
|
|
201
|
+
setWidgetValue(property, value),
|
|
202
|
+
[setWidgetValue],
|
|
203
|
+
);
|
|
204
|
+
const onHexColorChange = useCallback(
|
|
205
|
+
(property: string, value: string) =>
|
|
206
|
+
setWidgetValue(property, value),
|
|
207
|
+
[setWidgetValue],
|
|
208
|
+
);
|
|
209
|
+
const onColorChange = useCallback(
|
|
210
|
+
(property: string, component: 'l' | 'c' | 'h') =>
|
|
211
|
+
(event: React.ChangeEvent<HTMLInputElement>) => {
|
|
212
|
+
const fallback =
|
|
213
|
+
getNestedProperty<string>(configData, property) ??
|
|
214
|
+
'#000000';
|
|
215
|
+
const base = toHclChannels(fallback);
|
|
216
|
+
const channels = {
|
|
217
|
+
h:
|
|
218
|
+
(localValues[`${property}_h`] as number) ??
|
|
219
|
+
base.h,
|
|
220
|
+
c:
|
|
221
|
+
(localValues[`${property}_c`] as number) ??
|
|
222
|
+
base.c,
|
|
223
|
+
l:
|
|
224
|
+
(localValues[`${property}_l`] as number) ??
|
|
225
|
+
base.l,
|
|
226
|
+
};
|
|
227
|
+
channels[component] = Number(
|
|
228
|
+
event.currentTarget.value,
|
|
229
|
+
);
|
|
230
|
+
const hex = hclChannelsToHex(channels, fallback);
|
|
231
|
+
setWidgetValue(
|
|
232
|
+
property,
|
|
233
|
+
hex,
|
|
234
|
+
{
|
|
235
|
+
[`${property}_h`]: channels.h,
|
|
236
|
+
[`${property}_c`]: channels.c,
|
|
237
|
+
[`${property}_l`]: channels.l,
|
|
238
|
+
},
|
|
239
|
+
true,
|
|
240
|
+
);
|
|
241
|
+
},
|
|
242
|
+
[configData, localValues, setWidgetValue],
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
useEffect(() => {
|
|
246
|
+
const next: Record<string, WidgetValue> = {};
|
|
247
|
+
for (const widget of visualEditorWidgets) {
|
|
248
|
+
if (
|
|
249
|
+
!widget ||
|
|
250
|
+
typeof widget !== 'object' ||
|
|
251
|
+
typeof widget.property !== 'string'
|
|
252
|
+
)
|
|
253
|
+
continue;
|
|
254
|
+
const value = getNestedProperty<WidgetValue>(
|
|
255
|
+
configData,
|
|
256
|
+
widget.property,
|
|
257
|
+
);
|
|
258
|
+
if (value === undefined) continue;
|
|
259
|
+
next[widget.property] = value;
|
|
260
|
+
if (
|
|
261
|
+
widget.type === 'color' &&
|
|
262
|
+
typeof value === 'string'
|
|
263
|
+
) {
|
|
264
|
+
const { h, c, l } = toHclChannels(value);
|
|
265
|
+
next[`${widget.property}_h`] = h;
|
|
266
|
+
next[`${widget.property}_c`] = c;
|
|
267
|
+
next[`${widget.property}_l`] = l;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
// Keep locally pending input stable while a remote edit to a different
|
|
271
|
+
// property arrives; settled values always converge to ShareDB.
|
|
272
|
+
setLocalValues({ ...next, ...pendingRef.current });
|
|
273
|
+
}, [configData, visualEditorWidgets]);
|
|
274
|
+
|
|
275
|
+
const resetWidget = (widget: VisualEditorConfigEntry) => {
|
|
276
|
+
const value = widget.defaultValue;
|
|
277
|
+
if (value === undefined) return;
|
|
278
|
+
if (
|
|
279
|
+
widget.type === 'color' &&
|
|
280
|
+
typeof value === 'string'
|
|
281
|
+
) {
|
|
282
|
+
const { h, c, l } = toHclChannels(value);
|
|
283
|
+
setWidgetValue(widget.property, value, {
|
|
284
|
+
[`${widget.property}_h`]: h,
|
|
285
|
+
[`${widget.property}_c`]: c,
|
|
286
|
+
[`${widget.property}_l`]: l,
|
|
287
|
+
});
|
|
288
|
+
} else setWidgetValue(widget.property, value);
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
const renderControl = (
|
|
292
|
+
widget: VisualEditorConfigEntry,
|
|
293
|
+
) => {
|
|
294
|
+
const value =
|
|
295
|
+
localValues[widget.property] ??
|
|
296
|
+
getNestedProperty<WidgetValue>(
|
|
297
|
+
configData,
|
|
298
|
+
widget.property,
|
|
299
|
+
);
|
|
300
|
+
switch (widget.type) {
|
|
301
|
+
case 'slider':
|
|
302
|
+
return (
|
|
303
|
+
<SliderWidget
|
|
304
|
+
property={widget.property}
|
|
305
|
+
label={widget.label}
|
|
306
|
+
min={widget.min}
|
|
307
|
+
max={widget.max}
|
|
308
|
+
step={widget.step}
|
|
309
|
+
unit={widget.unit}
|
|
310
|
+
currentValue={
|
|
311
|
+
typeof value === 'number' ? value : widget.min
|
|
312
|
+
}
|
|
313
|
+
onChange={onSliderChange(widget.property)}
|
|
314
|
+
onNumberChange={(next) =>
|
|
315
|
+
setWidgetValue(widget.property, next)
|
|
316
|
+
}
|
|
317
|
+
/>
|
|
318
|
+
);
|
|
319
|
+
case 'checkbox':
|
|
320
|
+
return (
|
|
321
|
+
<CheckboxWidget
|
|
322
|
+
property={widget.property}
|
|
323
|
+
label={widget.label}
|
|
324
|
+
currentValue={
|
|
325
|
+
typeof value === 'boolean' ? value : false
|
|
326
|
+
}
|
|
327
|
+
onChange={onCheckboxChange(widget.property)}
|
|
328
|
+
/>
|
|
329
|
+
);
|
|
330
|
+
case 'textInput':
|
|
331
|
+
return (
|
|
332
|
+
<TextInputWidget
|
|
333
|
+
property={widget.property}
|
|
334
|
+
label={widget.label}
|
|
335
|
+
currentValue={
|
|
336
|
+
typeof value === 'string' ? value : ''
|
|
337
|
+
}
|
|
338
|
+
onChange={onTextInputChange(widget.property)}
|
|
339
|
+
/>
|
|
340
|
+
);
|
|
341
|
+
case 'dropdown':
|
|
342
|
+
return (
|
|
343
|
+
<DropdownWidget
|
|
344
|
+
property={widget.property}
|
|
345
|
+
label={widget.label}
|
|
346
|
+
options={widget.options}
|
|
347
|
+
currentValue={
|
|
348
|
+
typeof value === 'string'
|
|
349
|
+
? value
|
|
350
|
+
: widget.options[0]
|
|
351
|
+
}
|
|
352
|
+
onChange={(next) =>
|
|
353
|
+
onDropdownChange(widget.property, next)
|
|
354
|
+
}
|
|
355
|
+
/>
|
|
356
|
+
);
|
|
357
|
+
case 'segmented':
|
|
358
|
+
return (
|
|
359
|
+
<SegmentedWidget
|
|
360
|
+
label={widget.label}
|
|
361
|
+
options={widget.options}
|
|
362
|
+
currentValue={
|
|
363
|
+
typeof value === 'string'
|
|
364
|
+
? value
|
|
365
|
+
: widget.options[0]
|
|
366
|
+
}
|
|
367
|
+
onChange={(next) =>
|
|
368
|
+
onDropdownChange(widget.property, next)
|
|
369
|
+
}
|
|
370
|
+
/>
|
|
371
|
+
);
|
|
372
|
+
case 'color': {
|
|
373
|
+
const hex =
|
|
374
|
+
typeof value === 'string' ? value : '#000000';
|
|
375
|
+
const channels = toHclChannels(hex);
|
|
376
|
+
return (
|
|
377
|
+
<ColorWidget
|
|
378
|
+
property={widget.property}
|
|
379
|
+
label={widget.label}
|
|
380
|
+
currentHex={hex}
|
|
381
|
+
currentH={coerceChannelNumber(
|
|
382
|
+
localValues[`${widget.property}_h`],
|
|
383
|
+
channels.h,
|
|
384
|
+
)}
|
|
385
|
+
currentC={coerceChannelNumber(
|
|
386
|
+
localValues[`${widget.property}_c`],
|
|
387
|
+
channels.c,
|
|
388
|
+
)}
|
|
389
|
+
currentL={coerceChannelNumber(
|
|
390
|
+
localValues[`${widget.property}_l`],
|
|
391
|
+
channels.l,
|
|
392
|
+
)}
|
|
393
|
+
onColorChange={onColorChange}
|
|
394
|
+
onHexChange={onHexColorChange}
|
|
395
|
+
/>
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
const emptyReason =
|
|
402
|
+
getVisualEditorEmptyReason(configText);
|
|
403
|
+
if (emptyReason) {
|
|
404
|
+
const hostUI = renderEmptyState?.(emptyReason);
|
|
405
|
+
if (hostUI != null) return <>{hostUI}</>;
|
|
406
|
+
const messages = {
|
|
407
|
+
'missing-config':
|
|
408
|
+
'To begin using the visual editor, create a config.json.',
|
|
409
|
+
'invalid-config':
|
|
410
|
+
'Your config.json file is not valid JSON.',
|
|
411
|
+
'missing-widgets':
|
|
412
|
+
'Add a visualEditorWidgets array to config.json.',
|
|
413
|
+
'invalid-widgets':
|
|
414
|
+
'Your visualEditorWidgets configuration is invalid.',
|
|
415
|
+
'empty-widgets':
|
|
416
|
+
'Add at least one widget to visualEditorWidgets.',
|
|
417
|
+
};
|
|
418
|
+
return <EmptyState>{messages[emptyReason]}</EmptyState>;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
return (
|
|
422
|
+
<div className="visual-editor">
|
|
423
|
+
<header className="visual-properties-header">
|
|
424
|
+
<div className="visual-properties-title">
|
|
425
|
+
Visual Properties
|
|
426
|
+
</div>
|
|
427
|
+
<p className="visual-properties-hint">
|
|
428
|
+
Changes appear in the preview automatically.
|
|
429
|
+
</p>
|
|
430
|
+
</header>
|
|
431
|
+
{groups.map(({ title, widgets }) => (
|
|
432
|
+
<PropertySection key={title} title={title}>
|
|
433
|
+
{widgets.map((widget) => {
|
|
434
|
+
const actual =
|
|
435
|
+
localValues[widget.property] ??
|
|
436
|
+
getNestedProperty<WidgetValue>(
|
|
437
|
+
configData,
|
|
438
|
+
widget.property,
|
|
439
|
+
);
|
|
440
|
+
const resettable =
|
|
441
|
+
widget.defaultValue !== undefined;
|
|
442
|
+
return (
|
|
443
|
+
<div
|
|
444
|
+
className="visual-properties-field"
|
|
445
|
+
key={widget.property}
|
|
446
|
+
>
|
|
447
|
+
{renderControl(widget)}
|
|
448
|
+
{(widget.description || resettable) && (
|
|
449
|
+
<div className="visual-properties-field-footer">
|
|
450
|
+
{widget.description && (
|
|
451
|
+
<span className="visual-properties-description">
|
|
452
|
+
{widget.description}
|
|
453
|
+
</span>
|
|
454
|
+
)}
|
|
455
|
+
{resettable && (
|
|
456
|
+
<button
|
|
457
|
+
type="button"
|
|
458
|
+
className="visual-properties-reset"
|
|
459
|
+
disabled={Object.is(
|
|
460
|
+
actual,
|
|
461
|
+
widget.defaultValue,
|
|
462
|
+
)}
|
|
463
|
+
aria-label={`Reset ${widget.label} to default`}
|
|
464
|
+
onClick={() => resetWidget(widget)}
|
|
465
|
+
>
|
|
466
|
+
Reset
|
|
467
|
+
</button>
|
|
468
|
+
)}
|
|
469
|
+
</div>
|
|
470
|
+
)}
|
|
471
|
+
</div>
|
|
472
|
+
);
|
|
473
|
+
})}
|
|
474
|
+
</PropertySection>
|
|
475
|
+
))}
|
|
476
|
+
</div>
|
|
477
|
+
);
|
|
478
|
+
};
|
|
@@ -37,3 +37,15 @@ export function hclChannelsToHex(
|
|
|
37
37
|
const next = hcl(channels.h, channels.c, channels.l);
|
|
38
38
|
return next.displayable() ? next.formatHex() : fallback;
|
|
39
39
|
}
|
|
40
|
+
|
|
41
|
+
/** Normalize any opaque d3-supported CSS color to a hex string.
|
|
42
|
+
* Reject invalid/transparent values rather than writing an unusable config.
|
|
43
|
+
*/
|
|
44
|
+
export function normalizeOpaqueColorToHex(
|
|
45
|
+
value: string,
|
|
46
|
+
): string | null {
|
|
47
|
+
const parsed = color(value);
|
|
48
|
+
return parsed && parsed.opacity === 1
|
|
49
|
+
? parsed.formatHex().toLowerCase()
|
|
50
|
+
: null;
|
|
51
|
+
}
|
|
@@ -50,6 +50,52 @@ export function getVisualEditorEmptyReason(
|
|
|
50
50
|
);
|
|
51
51
|
if (!found) return 'invalid-widgets';
|
|
52
52
|
|
|
53
|
+
// Optional metadata augments the UI but does not change legacy schemas.
|
|
54
|
+
if (
|
|
55
|
+
(widget.section !== undefined &&
|
|
56
|
+
(typeof widget.section !== 'string' ||
|
|
57
|
+
!widget.section.trim() ||
|
|
58
|
+
widget.section.length > 80)) ||
|
|
59
|
+
(widget.description !== undefined &&
|
|
60
|
+
(typeof widget.description !== 'string' ||
|
|
61
|
+
widget.description.length > 240)) ||
|
|
62
|
+
(widget.unit !== undefined &&
|
|
63
|
+
(typeof widget.unit !== 'string' ||
|
|
64
|
+
widget.unit.length > 16))
|
|
65
|
+
)
|
|
66
|
+
return 'invalid-widgets';
|
|
67
|
+
if (widget.defaultValue !== undefined) {
|
|
68
|
+
const initial = widget.defaultValue;
|
|
69
|
+
if (
|
|
70
|
+
typeof initial !== typeof value ||
|
|
71
|
+
(typeof initial === 'number' &&
|
|
72
|
+
!Number.isFinite(initial))
|
|
73
|
+
)
|
|
74
|
+
return 'invalid-widgets';
|
|
75
|
+
if (
|
|
76
|
+
widget.type === 'slider' &&
|
|
77
|
+
(typeof initial !== 'number' ||
|
|
78
|
+
initial < (widget.min as number) ||
|
|
79
|
+
initial > (widget.max as number))
|
|
80
|
+
)
|
|
81
|
+
return 'invalid-widgets';
|
|
82
|
+
if (
|
|
83
|
+
(widget.type === 'dropdown' ||
|
|
84
|
+
widget.type === 'segmented') &&
|
|
85
|
+
(!Array.isArray(widget.options) ||
|
|
86
|
+
!widget.options.includes(initial))
|
|
87
|
+
)
|
|
88
|
+
return 'invalid-widgets';
|
|
89
|
+
if (widget.type === 'color') {
|
|
90
|
+
const rgb =
|
|
91
|
+
typeof initial === 'string'
|
|
92
|
+
? color(initial)?.rgb()
|
|
93
|
+
: null;
|
|
94
|
+
if (!rgb || rgb.opacity !== 1)
|
|
95
|
+
return 'invalid-widgets';
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
53
99
|
switch (widget.type) {
|
|
54
100
|
case 'slider':
|
|
55
101
|
if (
|
|
@@ -74,6 +120,7 @@ export function getVisualEditorEmptyReason(
|
|
|
74
120
|
return 'invalid-widgets';
|
|
75
121
|
break;
|
|
76
122
|
case 'dropdown':
|
|
123
|
+
case 'segmented':
|
|
77
124
|
if (
|
|
78
125
|
typeof value !== 'string' ||
|
|
79
126
|
!Array.isArray(widget.options) ||
|
|
@@ -7,55 +7,82 @@ import {
|
|
|
7
7
|
getEditablePropertyValue,
|
|
8
8
|
} from './utils';
|
|
9
9
|
|
|
10
|
+
export type WidgetValue = string | number | boolean;
|
|
11
|
+
|
|
10
12
|
/**
|
|
11
|
-
* Apply
|
|
12
|
-
*
|
|
13
|
+
* Apply one or more edits to the LATEST ShareDB content, with exactly one
|
|
14
|
+
* config.json serialization. Never overwrite unrelated properties or files.
|
|
15
|
+
* Skip invalidated paths, type changes, or no-op writes from stale widgets.
|
|
13
16
|
*/
|
|
14
|
-
export function
|
|
17
|
+
export function updateVisualEditorProperties(
|
|
15
18
|
document: VizContent,
|
|
16
19
|
configFileId: VizFileId | null,
|
|
17
|
-
|
|
18
|
-
value: string | number | boolean,
|
|
20
|
+
changes: Record<string, WidgetValue>,
|
|
19
21
|
): VizContent {
|
|
20
22
|
if (!configFileId) return document;
|
|
21
23
|
const original = document.files[configFileId];
|
|
22
24
|
if (!original) return document;
|
|
23
|
-
let
|
|
25
|
+
let data: unknown;
|
|
24
26
|
try {
|
|
25
|
-
|
|
27
|
+
data = JSON.parse(original.text);
|
|
26
28
|
} catch {
|
|
27
29
|
return document;
|
|
28
30
|
}
|
|
29
31
|
if (
|
|
30
|
-
!
|
|
31
|
-
typeof
|
|
32
|
-
Array.isArray(
|
|
32
|
+
!data ||
|
|
33
|
+
typeof data !== 'object' ||
|
|
34
|
+
Array.isArray(data)
|
|
33
35
|
)
|
|
34
36
|
return document;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
37
|
+
|
|
38
|
+
let next = data as Record<string, unknown>;
|
|
39
|
+
let changed = false;
|
|
40
|
+
for (const [property, value] of Object.entries(changes)) {
|
|
41
|
+
if (
|
|
42
|
+
typeof value === 'number' &&
|
|
43
|
+
!Number.isFinite(value)
|
|
44
|
+
)
|
|
45
|
+
continue;
|
|
46
|
+
const existing = getEditablePropertyValue(
|
|
47
|
+
next,
|
|
45
48
|
property,
|
|
46
|
-
value,
|
|
47
49
|
);
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
if (
|
|
51
|
+
!existing.found ||
|
|
52
|
+
typeof existing.value !== typeof value ||
|
|
53
|
+
Object.is(existing.value, value)
|
|
54
|
+
)
|
|
55
|
+
continue;
|
|
56
|
+
try {
|
|
57
|
+
next = setNestedProperty(next, property, value);
|
|
58
|
+
changed = true;
|
|
59
|
+
} catch {
|
|
60
|
+
// An unsafe/stale property must not reject other valid edits.
|
|
61
|
+
}
|
|
50
62
|
}
|
|
63
|
+
if (!changed) return document;
|
|
51
64
|
return {
|
|
52
65
|
...document,
|
|
53
66
|
files: {
|
|
54
67
|
...document.files,
|
|
55
68
|
[configFileId]: {
|
|
56
69
|
...original,
|
|
57
|
-
text: JSON.stringify(
|
|
70
|
+
text: JSON.stringify(next, null, 2),
|
|
58
71
|
},
|
|
59
72
|
},
|
|
60
73
|
};
|
|
61
74
|
}
|
|
75
|
+
|
|
76
|
+
/** Backward-compatible single-property helper. */
|
|
77
|
+
export function updateVisualEditorProperty(
|
|
78
|
+
document: VizContent,
|
|
79
|
+
configFileId: VizFileId | null,
|
|
80
|
+
property: string,
|
|
81
|
+
value: WidgetValue,
|
|
82
|
+
): VizContent {
|
|
83
|
+
return updateVisualEditorProperties(
|
|
84
|
+
document,
|
|
85
|
+
configFileId,
|
|
86
|
+
{ [property]: value },
|
|
87
|
+
);
|
|
88
|
+
}
|
|
@@ -144,7 +144,7 @@ export const getEditablePropertyValue = (
|
|
|
144
144
|
|
|
145
145
|
// Read widget values using exactly the same safe path semantics as writes.
|
|
146
146
|
export const getNestedProperty = <T = unknown>(
|
|
147
|
-
obj:
|
|
147
|
+
obj: unknown,
|
|
148
148
|
path: string,
|
|
149
149
|
): T | undefined => {
|
|
150
150
|
const { found, value } = getEditablePropertyValue(
|