vzcode 2.31.0 → 2.35.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.
@@ -101,38 +101,57 @@ export const renderSliderBackground = (
101
101
  ctx.putImageData(imageData, 0, 0);
102
102
  };
103
103
 
104
- // Helper function to get nested property value using dot notation
105
- export const getNestedProperty = <T = unknown>(
106
- obj: Record<string, unknown>,
104
+ /**
105
+ * Widget paths are own-property paths through JSON objects. This parser is
106
+ * shared by reads, validation and writes to avoid accidental prototype access.
107
+ */
108
+ export const splitEditablePropertyPath = (
107
109
  path: string,
108
- ): T | undefined => {
109
- // Validate path to prevent empty strings, consecutive dots, or leading/trailing dots
110
- if (
111
- !path ||
112
- path.includes('..') ||
113
- path.startsWith('.') ||
114
- path.endsWith('.')
115
- ) {
116
- return undefined;
117
- }
110
+ ): string[] | null => {
111
+ if (typeof path !== 'string' || !path) return null;
112
+ const segments = path.split('.');
113
+ return segments.every(
114
+ (key) =>
115
+ key.length > 0 &&
116
+ key !== '__proto__' &&
117
+ key !== 'constructor' &&
118
+ key !== 'prototype',
119
+ )
120
+ ? segments
121
+ : null;
122
+ };
118
123
 
119
- const keys = path.split('.');
120
- let value: unknown = obj;
124
+ export const getEditablePropertyValue = (
125
+ obj: unknown,
126
+ path: string,
127
+ ): { found: boolean; value: unknown } => {
128
+ const keys = splitEditablePropertyPath(path);
129
+ if (!keys) return { found: false, value: undefined };
130
+ let current: unknown = obj;
121
131
  for (const key of keys) {
122
- // Skip empty keys that might result from splitting
123
- if (!key) {
124
- return undefined;
125
- }
126
- if (value === null || value === undefined) {
127
- return undefined;
128
- }
129
- // Type guard to ensure value is an object before accessing property
130
- if (typeof value !== 'object') {
131
- return undefined;
132
+ if (
133
+ current === null ||
134
+ typeof current !== 'object' ||
135
+ Array.isArray(current) ||
136
+ !Object.prototype.hasOwnProperty.call(current, key)
137
+ ) {
138
+ return { found: false, value: undefined };
132
139
  }
133
- value = (value as Record<string, unknown>)[key];
140
+ current = (current as Record<string, unknown>)[key];
134
141
  }
135
- return value as T | undefined;
142
+ return { found: true, value: current };
143
+ };
144
+
145
+ // Read widget values using exactly the same safe path semantics as writes.
146
+ export const getNestedProperty = <T = unknown>(
147
+ obj: Record<string, unknown>,
148
+ path: string,
149
+ ): T | undefined => {
150
+ const { found, value } = getEditablePropertyValue(
151
+ obj,
152
+ path,
153
+ );
154
+ return found ? (value as T) : undefined;
136
155
  };
137
156
 
138
157
  // Helper function to set nested property value using dot notation
@@ -143,47 +162,18 @@ export const setNestedProperty = <
143
162
  path: string,
144
163
  value: unknown,
145
164
  ): T => {
146
- // Validate path to prevent empty strings, consecutive dots, or leading/trailing dots
147
- if (
148
- !path ||
149
- path.includes('..') ||
150
- path.startsWith('.') ||
151
- path.endsWith('.')
152
- ) {
153
- throw new Error(
154
- `Invalid property path "${path}" - path cannot be empty, contain consecutive dots, or have leading/trailing dots`,
155
- );
156
- }
157
-
158
- const keys = path.split('.');
159
-
160
- // Check for empty keys
161
- if (keys.some((key) => !key)) {
165
+ const keys = splitEditablePropertyPath(path);
166
+ if (!keys)
162
167
  throw new Error(
163
- `Invalid property path "${path}" - path contains empty segments`,
168
+ 'Invalid or unsafe property path: ' + path,
164
169
  );
165
- }
166
170
 
167
171
  const newObj = { ...obj };
168
172
  let current: Record<string, unknown> = newObj;
169
173
 
170
- // List of dangerous keys that could lead to prototype pollution
171
- const dangerousKeys = [
172
- '__proto__',
173
- 'constructor',
174
- 'prototype',
175
- ];
176
-
177
174
  for (let i = 0; i < keys.length - 1; i++) {
178
175
  const key = keys[i];
179
176
 
180
- // Guard against prototype pollution
181
- if (dangerousKeys.includes(key)) {
182
- throw new Error(
183
- `Cannot set property "${key}" - potential prototype pollution`,
184
- );
185
- }
186
-
187
177
  // Create nested object if it doesn't exist or isn't a plain object
188
178
  if (
189
179
  current[key] == null ||
@@ -200,13 +190,7 @@ export const setNestedProperty = <
200
190
  current = current[key] as Record<string, unknown>;
201
191
  }
202
192
 
203
- // Guard against prototype pollution for the final key
204
193
  const finalKey = keys[keys.length - 1];
205
- if (dangerousKeys.includes(finalKey)) {
206
- throw new Error(
207
- `Cannot set property "${finalKey}" - potential prototype pollution`,
208
- );
209
- }
210
194
 
211
195
  // Set the final value
212
196
  current[finalKey] = value;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * An editor owns the temporary Ctrl-hover and Ctrl-click listeners.
3
+ * Keeping their lifecycle together prevents stale DOM handlers when panes
4
+ * switch or VZCode is unmounted.
5
+ */
6
+ export function createDefinitionJumpListeners(
7
+ target: EventTarget,
8
+ onHover: (event: MouseEvent) => void,
9
+ onClick: (event: MouseEvent) => void,
10
+ ) {
11
+ let hovering = false;
12
+ let armed = false;
13
+
14
+ const hoverListener: EventListener = (event) => {
15
+ onHover(event as MouseEvent);
16
+ };
17
+ const clickListener: EventListener = (event) => {
18
+ armed = false;
19
+ target.removeEventListener('mousedown', clickListener);
20
+ onClick(event as MouseEvent);
21
+ };
22
+
23
+ return {
24
+ start() {
25
+ if (hovering) return;
26
+ hovering = true;
27
+ target.addEventListener('mouseover', hoverListener);
28
+ },
29
+ armClick() {
30
+ if (armed) return;
31
+ armed = true;
32
+ target.addEventListener('mousedown', clickListener);
33
+ },
34
+ stop() {
35
+ hovering = false;
36
+ armed = false;
37
+ target.removeEventListener(
38
+ 'mouseover',
39
+ hoverListener,
40
+ );
41
+ target.removeEventListener(
42
+ 'mousedown',
43
+ clickListener,
44
+ );
45
+ },
46
+ };
47
+ }
@@ -7,18 +7,13 @@ import { EditorState } from '@codemirror/state';
7
7
  import { VizFileId } from '@vizhub/viz-types';
8
8
  import { PaneId } from '../types';
9
9
  import { editorCacheKey } from './useEditorCache';
10
+ import { createDefinitionJumpListeners } from './definitionJumpListeners';
10
11
 
11
12
  /*
12
13
  The following is a helpful resource for the following Code Mirror Syntax Tree methods below
13
14
  https://lezer.codemirror.net/docs/ref/#common
14
15
  */
15
16
 
16
- // Store the element in the current editor DOM to highlight, indicating a potential jump to definition is possible
17
- let activeJumpingElement: HTMLSpanElement = null;
18
-
19
- // Store the syntax node representing the destination within the syntax tree
20
- let definingNode: SyntaxNode = null;
21
-
22
17
  // Example nesting types for the specific language to find level in the syntax tree
23
18
  const nestingTypes = new Set<string>([
24
19
  'FunctionDeclaration',
@@ -207,6 +202,9 @@ export const useKeyboardShortcuts = ({
207
202
  activeFileId,
208
203
  activePaneId,
209
204
  );
205
+ // Each mounted editor owns its jump target and listeners.
206
+ let activeJumpingElement: HTMLSpanElement | null = null;
207
+ let definingNode: SyntaxNode | null = null;
210
208
 
211
209
  const handleKeyPress = (event: KeyboardEvent) => {
212
210
  if (shouldTriggerRun(event)) {
@@ -245,10 +243,7 @@ export const useKeyboardShortcuts = ({
245
243
 
246
244
  if (event.ctrlKey === true) {
247
245
  // On holding CTRL key, search for a potential definition jump using mouse location
248
- document.addEventListener(
249
- 'mouseover',
250
- handleMouseOver,
251
- );
246
+ jumpListeners.start();
252
247
  }
253
248
 
254
249
  if (event.altKey === true) {
@@ -299,17 +294,11 @@ export const useKeyboardShortcuts = ({
299
294
  if (activeJumpingElement) {
300
295
  activeJumpingElement.style.cursor = 'initial';
301
296
  activeJumpingElement.style.textDecoration = 'none';
302
- activeJumpingElement = definingNode = null;
297
+ activeJumpingElement = null;
298
+ definingNode = null;
303
299
  }
304
300
 
305
- document.removeEventListener(
306
- 'mouseover',
307
- handleMouseOver,
308
- );
309
- document.removeEventListener(
310
- 'mousedown',
311
- jumpToDefinitionHandler,
312
- );
301
+ jumpListeners.stop();
313
302
  };
314
303
 
315
304
  const jumpToDefinitionHandler = (
@@ -332,8 +321,11 @@ export const useKeyboardShortcuts = ({
332
321
 
333
322
  // Move current cursor and center view in the editor to destination node
334
323
 
335
- const editor: EditorView =
336
- editorCache.get(cacheKey).editor;
324
+ const editor = editorCache.get(cacheKey)?.editor;
325
+ if (!editor) {
326
+ resetActiveJumpingElement();
327
+ return;
328
+ }
337
329
  const closestDefinition: SyntaxNode = definingNode;
338
330
 
339
331
  editor.dispatch({
@@ -365,8 +357,8 @@ export const useKeyboardShortcuts = ({
365
357
  return;
366
358
  }
367
359
 
368
- const editor: EditorView =
369
- editorCache.get(cacheKey).editor;
360
+ const editor = editorCache.get(cacheKey)?.editor;
361
+ if (!editor) return;
370
362
  const tree = syntaxTree(editor.state);
371
363
  const element = event.target as HTMLSpanElement;
372
364
 
@@ -423,19 +415,22 @@ export const useKeyboardShortcuts = ({
423
415
  'underline';
424
416
 
425
417
  // CTRL + Click: Jump to relative definition, which is removed on CTRL key release
426
- document.addEventListener(
427
- 'mousedown',
428
- jumpToDefinitionHandler,
429
- { once: true },
430
- );
418
+ jumpListeners.armClick();
431
419
  }
432
420
  }
433
421
  };
434
422
 
423
+ const jumpListeners = createDefinitionJumpListeners(
424
+ document,
425
+ handleMouseOver,
426
+ jumpToDefinitionHandler,
427
+ );
428
+
435
429
  document.addEventListener('keydown', handleKeyPress);
436
430
  document.addEventListener('keyup', handleKeyRelease);
437
431
 
438
432
  return () => {
433
+ resetActiveJumpingElement();
439
434
  document.removeEventListener(
440
435
  'keydown',
441
436
  handleKeyPress,
@@ -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';