vzcode 2.31.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/dist/index.html CHANGED
@@ -20,7 +20,7 @@
20
20
  href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap"
21
21
  rel="stylesheet"
22
22
  />
23
- <script type="module" crossorigin src="/assets/index-BfDRE_ou.js"></script>
23
+ <script type="module" crossorigin src="/assets/index-nL6to42p.js"></script>
24
24
  <link rel="stylesheet" crossorigin href="/assets/index-Dyt9tpmu.css">
25
25
  </head>
26
26
  <body>
package/package.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "vzcode",
3
- "version": "2.31.0",
3
+ "version": "2.34.0",
4
4
  "description": "Multiplayer code editor system",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
7
- "bin": "dist/cli.js",
7
+ "bin": {
8
+ "vzcode": "dist/cli.js"
9
+ },
8
10
  "files": [
9
11
  "dist/**",
10
12
  "src/**"
@@ -1,4 +1,6 @@
1
1
  import {
2
+ VZInitialSidebarView,
3
+ VisualEditorEmptyReason,
2
4
  ItemId,
3
5
  LeafPane,
4
6
  Pane,
@@ -91,6 +93,10 @@ export type VZCodeContextValue = {
91
93
  setIsSearchOpen: (isSearchOpen: boolean) => void;
92
94
  setSearch: (pattern: string) => void;
93
95
 
96
+ /** Host-provided replacement for the built-in visual-editor empty states. */
97
+ renderVisualEditorEmptyState?: (
98
+ reason: VisualEditorEmptyReason,
99
+ ) => React.ReactNode;
94
100
  isVisualEditorOpen: boolean;
95
101
  setIsVisualEditorOpen: (
96
102
  isVisualEditorOpen: boolean,
@@ -226,6 +232,12 @@ export interface VZCodeProviderProps {
226
232
  docPresence: any;
227
233
  prettierWorker: Worker;
228
234
  initialUsername: Username;
235
+ /** Initial sidebar selection; defaults to VZCode's historical AI chat. */
236
+ initialSidebarView?: VZInitialSidebarView;
237
+ /** Optional host UI for a missing/invalid/unconfigured visual editor. */
238
+ renderVisualEditorEmptyState?: (
239
+ reason: VisualEditorEmptyReason,
240
+ ) => React.ReactNode;
229
241
  children: React.ReactNode;
230
242
  codeError?: string | null;
231
243
  connected?: boolean;
@@ -37,6 +37,8 @@ export const useVZCodeState = ({
37
37
  docPresence,
38
38
  prettierWorker,
39
39
  initialUsername,
40
+ initialSidebarView,
41
+ renderVisualEditorEmptyState,
40
42
  codeError = null,
41
43
  connected,
42
44
  pending,
@@ -101,7 +103,7 @@ export const useVZCodeState = ({
101
103
  // See https://react.dev/reference/react/useReducer
102
104
  const [state, dispatch] = useReducer(
103
105
  vzReducer,
104
- { defaultTheme, initialUsername },
106
+ { defaultTheme, initialUsername, initialSidebarView },
105
107
  createInitialState,
106
108
  );
107
109
 
@@ -670,6 +672,7 @@ export const useVZCodeState = ({
670
672
  setSearchFocusedIndex,
671
673
  toggleSearchFocused,
672
674
 
675
+ renderVisualEditorEmptyState,
673
676
  isVisualEditorOpen,
674
677
  setIsVisualEditorOpen,
675
678
 
@@ -11,19 +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';
19
- import {
20
- getNestedProperty,
21
- setNestedProperty,
22
- } from './utils';
21
+ import { getNestedProperty } from './utils';
23
22
 
24
23
  export const VisualEditor = () => {
25
- const { files, submitOperation } =
26
- useContext(VZCodeContext);
24
+ const {
25
+ files,
26
+ submitOperation,
27
+ renderVisualEditorEmptyState,
28
+ } = useContext(VZCodeContext);
27
29
 
28
30
  // Local state to track widget values during user interaction
29
31
  const [localValues, setLocalValues] = useState<{
@@ -70,25 +72,16 @@ export const VisualEditor = () => {
70
72
  [property]: newValue,
71
73
  }));
72
74
 
73
- // Update config.json with support for nested properties
74
- const newConfigData = setNestedProperty(
75
- configData,
76
- property,
77
- newValue,
75
+ submitOperation((document: VizContent) =>
76
+ updateVisualEditorProperty(
77
+ document,
78
+ configFileId,
79
+ property,
80
+ newValue,
81
+ ),
78
82
  );
79
-
80
- submitOperation((document: VizContent) => ({
81
- ...document,
82
- files: {
83
- ...files,
84
- [configFileId]: {
85
- name: 'config.json',
86
- text: JSON.stringify(newConfigData, null, 2),
87
- },
88
- },
89
- }));
90
83
  },
91
- [configData, files, configFileId, submitOperation],
84
+ [configFileId, submitOperation],
92
85
  );
93
86
 
94
87
  const onCheckboxChange = useCallback(
@@ -102,25 +95,16 @@ export const VisualEditor = () => {
102
95
  [property]: newValue,
103
96
  }));
104
97
 
105
- // Update config.json with support for nested properties
106
- const newConfigData = setNestedProperty(
107
- configData,
108
- property,
109
- newValue,
98
+ submitOperation((document: VizContent) =>
99
+ updateVisualEditorProperty(
100
+ document,
101
+ configFileId,
102
+ property,
103
+ newValue,
104
+ ),
110
105
  );
111
-
112
- submitOperation((document: VizContent) => ({
113
- ...document,
114
- files: {
115
- ...files,
116
- [configFileId]: {
117
- name: 'config.json',
118
- text: JSON.stringify(newConfigData, null, 2),
119
- },
120
- },
121
- }));
122
106
  },
123
- [configData, files, configFileId, submitOperation],
107
+ [configFileId, submitOperation],
124
108
  );
125
109
 
126
110
  const onTextInputChange = useCallback(
@@ -134,25 +118,16 @@ export const VisualEditor = () => {
134
118
  [property]: newValue,
135
119
  }));
136
120
 
137
- // Update config.json with support for nested properties
138
- const newConfigData = setNestedProperty(
139
- configData,
140
- property,
141
- newValue,
121
+ submitOperation((document: VizContent) =>
122
+ updateVisualEditorProperty(
123
+ document,
124
+ configFileId,
125
+ property,
126
+ newValue,
127
+ ),
142
128
  );
143
-
144
- submitOperation((document: VizContent) => ({
145
- ...document,
146
- files: {
147
- ...files,
148
- [configFileId]: {
149
- name: 'config.json',
150
- text: JSON.stringify(newConfigData, null, 2),
151
- },
152
- },
153
- }));
154
129
  },
155
- [configData, files, configFileId, submitOperation],
130
+ [configFileId, submitOperation],
156
131
  );
157
132
 
158
133
  const onDropdownChange = useCallback(
@@ -163,25 +138,16 @@ export const VisualEditor = () => {
163
138
  [property]: newValue,
164
139
  }));
165
140
 
166
- // Update config.json with support for nested properties
167
- const newConfigData = setNestedProperty(
168
- configData,
169
- property,
170
- newValue,
141
+ submitOperation((document: VizContent) =>
142
+ updateVisualEditorProperty(
143
+ document,
144
+ configFileId,
145
+ property,
146
+ newValue,
147
+ ),
171
148
  );
172
-
173
- submitOperation((document: VizContent) => ({
174
- ...document,
175
- files: {
176
- ...files,
177
- [configFileId]: {
178
- name: 'config.json',
179
- text: JSON.stringify(newConfigData, null, 2),
180
- },
181
- },
182
- }));
183
149
  },
184
- [configData, files, configFileId, submitOperation],
150
+ [configFileId, submitOperation],
185
151
  );
186
152
 
187
153
  // Custom dropdown handlers
@@ -290,27 +256,17 @@ export const VisualEditor = () => {
290
256
  [`${property}_l`]: newhcl[2],
291
257
  }));
292
258
 
293
- // Update config.json with hex value using nested property access
294
- const newConfigData = setNestedProperty(
295
- configData,
296
- property,
297
- newHex,
259
+ submitOperation((document: VizContent) =>
260
+ updateVisualEditorProperty(
261
+ document,
262
+ configFileId,
263
+ property,
264
+ newHex,
265
+ ),
298
266
  );
299
-
300
- submitOperation((document: VizContent) => ({
301
- ...document,
302
- files: {
303
- ...files,
304
- [configFileId]: {
305
- name: 'config.json',
306
- text: JSON.stringify(newConfigData, null, 2),
307
- },
308
- },
309
- }));
310
267
  },
311
268
  [
312
269
  configData,
313
- files,
314
270
  configFileId,
315
271
  submitOperation,
316
272
  localValues,
@@ -319,7 +275,10 @@ export const VisualEditor = () => {
319
275
 
320
276
  const visualEditorWidgets: VisualEditorConfigEntry[] =
321
277
  useMemo(
322
- () => configData?.visualEditorWidgets ?? [],
278
+ () =>
279
+ Array.isArray(configData?.visualEditorWidgets)
280
+ ? configData.visualEditorWidgets
281
+ : [],
323
282
  [configData],
324
283
  );
325
284
 
@@ -361,74 +320,47 @@ export const VisualEditor = () => {
361
320
  widget.property,
362
321
  );
363
322
 
364
- if (localValues[widget.property] !== hexColor) {
365
- if (hexColor) {
366
- try {
367
- const rgbColor = color(hexColor);
368
- const lch = hcl(rgbColor);
369
- newLocalValues[`${widget.property}_h`] =
370
- lch.h;
371
- newLocalValues[`${widget.property}_c`] =
372
- lch.c;
373
- newLocalValues[`${widget.property}_l`] =
374
- lch.l;
375
- } catch {
376
- // Fallback values if conversion fails
377
- newLocalValues[`${widget.property}_l`] = 0;
378
- newLocalValues[`${widget.property}_c`] = 0;
379
- newLocalValues[`${widget.property}_h`] = 0;
380
- }
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;
381
335
  }
382
336
  }
383
337
  }
384
338
  });
385
339
  setLocalValues(newLocalValues);
386
- }, [configData, localValues, visualEditorWidgets]);
387
-
388
- if (configFileId === null) {
389
- return (
390
- <EmptyState>
391
- To begin using the visual editor, create a
392
- config.json.
393
- </EmptyState>
394
- );
395
- }
340
+ }, [configData, visualEditorWidgets]);
396
341
 
397
- if (!configData) {
398
- return (
399
- <EmptyState>
400
- Your config.json file is not valid json.
401
- </EmptyState>
402
- );
403
- }
404
-
405
- if (
406
- configData &&
407
- !('visualEditorWidgets' in configData)
408
- ) {
409
- return (
410
- <EmptyState>
411
- To begin using the visual editor, make sure your
412
- config.json has a key called
413
- &quot;visualEditorWidgets&quot;, whose value is the
414
- config for the visual editor.
415
- </EmptyState>
416
- );
417
- }
418
-
419
- if (
420
- configData &&
421
- !Array.isArray(configData.visualEditorWidgets)
422
- ) {
423
- return (
424
- <EmptyState>
425
- Your config.json file has
426
- &quot;visualEditorWidgets&quot; but it is not an
427
- array. Please make sure
428
- &quot;visualEditorWidgets&quot; is an array of
429
- widget configurations.
430
- </EmptyState>
431
- );
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>;
432
364
  }
433
365
 
434
366
  return (
@@ -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
+ }
@@ -1,13 +1,18 @@
1
1
  import { VZState } from '.';
2
- import { Username } from '../../types';
2
+ import {
3
+ Username,
4
+ VZInitialSidebarView,
5
+ } from '../../types';
3
6
  import { ThemeLabel } from '../themes';
4
7
 
5
8
  export const createInitialState = ({
6
9
  defaultTheme,
7
10
  initialUsername = 'Anonymous',
11
+ initialSidebarView = 'ai-chat',
8
12
  }: {
9
13
  defaultTheme: ThemeLabel;
10
14
  initialUsername?: Username;
15
+ initialSidebarView?: VZInitialSidebarView;
11
16
  }): VZState => ({
12
17
  pane: {
13
18
  id: 'root',
@@ -24,9 +29,9 @@ export const createInitialState = ({
24
29
  focusedIndex: null,
25
30
  focusedChildIndex: null,
26
31
  },
27
- isVisualEditorOpen: false,
32
+ isVisualEditorOpen: initialSidebarView === 'visual',
28
33
  isSearchOpen: false,
29
- isAIChatOpen: true,
34
+ isAIChatOpen: initialSidebarView === 'ai-chat',
30
35
  aiChatFocused: false,
31
36
  aiChatMode: 'edit',
32
37
  isSettingsOpen: false,
package/src/types.ts CHANGED
@@ -305,3 +305,17 @@ export interface StreamingChatMessage {
305
305
  currentStatus?: string;
306
306
  isComplete?: boolean;
307
307
  }
308
+
309
+ /** Embedding hosts can choose which sidebar VZCode opens initially. */
310
+ export type VZInitialSidebarView =
311
+ | 'ai-chat'
312
+ | 'files'
313
+ | 'visual';
314
+
315
+ /** Reason a visualization cannot yet render visual-editor controls. */
316
+ export type VisualEditorEmptyReason =
317
+ | 'missing-config'
318
+ | 'invalid-config'
319
+ | 'missing-widgets'
320
+ | 'invalid-widgets'
321
+ | 'empty-widgets';