vzcode 2.3.0 → 2.4.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.
Files changed (196) hide show
  1. package/package.json +3 -2
  2. package/src/cli.ts +208 -0
  3. package/src/client/AIAssist/AIAssistCodeMirrorKeyMap.ts +42 -0
  4. package/src/client/AIAssist/AIAssistWidget/index.tsx +135 -0
  5. package/src/client/AIAssist/AIAssistWidget/style.scss +47 -0
  6. package/src/client/AIAssist/Spinner.jsx +76 -0
  7. package/src/client/AIAssist/formatFile.ts +27 -0
  8. package/src/client/AIAssist/generateFilesContext.ts +25 -0
  9. package/src/client/AIAssist/index.ts +2 -0
  10. package/src/client/AIAssist/startAIAssist.ts +95 -0
  11. package/src/client/App/index.tsx +152 -0
  12. package/src/client/App/style.scss +82 -0
  13. package/src/client/App/usePending.ts +51 -0
  14. package/src/client/App/useShareDB.ts +161 -0
  15. package/src/client/CodeEditor/Copilot/index.ts +74 -0
  16. package/src/client/CodeEditor/InteractiveWidgets/boolToggler.ts +12 -0
  17. package/src/client/CodeEditor/InteractiveWidgets/colorPicker.ts +68 -0
  18. package/src/client/CodeEditor/InteractiveWidgets/colorsInTextPlugin.ts +174 -0
  19. package/src/client/CodeEditor/InteractiveWidgets/highlightWidgets.ts +79 -0
  20. package/src/client/CodeEditor/InteractiveWidgets/index.ts +44 -0
  21. package/src/client/CodeEditor/InteractiveWidgets/markdownCheckboxToggler.ts +12 -0
  22. package/src/client/CodeEditor/InteractiveWidgets/numberDragger.ts +20 -0
  23. package/src/client/CodeEditor/InteractiveWidgets/rotateWidget.ts +143 -0
  24. package/src/client/CodeEditor/InteractiveWidgets/urlClicker.test.ts +48 -0
  25. package/src/client/CodeEditor/InteractiveWidgets/urlClicker.ts +12 -0
  26. package/src/client/CodeEditor/InteractiveWidgets/vec2Slider.ts +25 -0
  27. package/src/client/CodeEditor/basicSetup.ts +73 -0
  28. package/src/client/CodeEditor/getOrCreateEditor.tsx +615 -0
  29. package/src/client/CodeEditor/index.tsx +202 -0
  30. package/src/client/CodeEditor/json1PresenceBroadcast.ts +41 -0
  31. package/src/client/CodeEditor/json1PresenceDisplay.ts +350 -0
  32. package/src/client/CodeEditor/rainbowBrackets.ts +122 -0
  33. package/src/client/CodeEditor/style.scss +156 -0
  34. package/src/client/CodeEditor/typescriptExtension/worker.ts +32 -0
  35. package/src/client/CodeErrorOverlay/index.tsx +77 -0
  36. package/src/client/CodeErrorOverlay/style.scss +33 -0
  37. package/src/client/Fonts/fonts.ts +8 -0
  38. package/src/client/Icons/AdjustmentSVG.tsx +19 -0
  39. package/src/client/Icons/BugSVG.tsx +20 -0
  40. package/src/client/Icons/CloseSVG.tsx +19 -0
  41. package/src/client/Icons/CssSVG.tsx +16 -0
  42. package/src/client/Icons/DirectoryArrowSVG.tsx +19 -0
  43. package/src/client/Icons/EditSVG.tsx +19 -0
  44. package/src/client/Icons/FileSVG.tsx +19 -0
  45. package/src/client/Icons/FolderSVG.tsx +20 -0
  46. package/src/client/Icons/GearSVG.tsx +16 -0
  47. package/src/client/Icons/HtmlSVG.tsx +16 -0
  48. package/src/client/Icons/JavaScriptSVG.tsx +16 -0
  49. package/src/client/Icons/JsonSVG.tsx +16 -0
  50. package/src/client/Icons/MarkdownSVG.tsx +16 -0
  51. package/src/client/Icons/MicOffSVG.tsx +35 -0
  52. package/src/client/Icons/MicSVG.tsx +25 -0
  53. package/src/client/Icons/NewSVG.tsx +19 -0
  54. package/src/client/Icons/PinSVG.tsx +14 -0
  55. package/src/client/Icons/PlaySVG.tsx +16 -0
  56. package/src/client/Icons/QuestionMarkSVG.tsx +18 -0
  57. package/src/client/Icons/ReactSVG.tsx +16 -0
  58. package/src/client/Icons/SearchSVG.tsx +20 -0
  59. package/src/client/Icons/SparklesSVG.tsx +23 -0
  60. package/src/client/Icons/SplitEditorSVG.tsx +34 -0
  61. package/src/client/Icons/StopSVG.tsx +15 -0
  62. package/src/client/Icons/SvelteSVG.tsx +16 -0
  63. package/src/client/Icons/TrashSVG.tsx +19 -0
  64. package/src/client/Icons/TypeScriptSVG.tsx +16 -0
  65. package/src/client/Icons/index.tsx +26 -0
  66. package/src/client/ImageViewer/index.tsx +91 -0
  67. package/src/client/ImageViewer/style.scss +41 -0
  68. package/src/client/PresenceNotifications/PresenceNotifications.test.ts +134 -0
  69. package/src/client/PresenceNotifications/index.tsx +158 -0
  70. package/src/client/PresenceNotifications/style.scss +17 -0
  71. package/src/client/RunCodeWidget/index.tsx +117 -0
  72. package/src/client/RunCodeWidget/style.scss +24 -0
  73. package/src/client/SplitPaneResizeContext.tsx +159 -0
  74. package/src/client/TabList/Tab.tsx +73 -0
  75. package/src/client/TabList/index.tsx +35 -0
  76. package/src/client/TabList/style.scss +51 -0
  77. package/src/client/VZCodeContext/index.tsx +29 -0
  78. package/src/client/VZCodeContext/types.ts +248 -0
  79. package/src/client/VZCodeContext/useVZCodeState.ts +602 -0
  80. package/src/client/VZKeyboardShortcutsDoc.tsx +148 -0
  81. package/src/client/VZLeft.tsx +28 -0
  82. package/src/client/VZMiddle.tsx +172 -0
  83. package/src/client/VZResizer/index.tsx +128 -0
  84. package/src/client/VZResizer/styles.scss +33 -0
  85. package/src/client/VZRight.tsx +113 -0
  86. package/src/client/VZSettings.tsx +275 -0
  87. package/src/client/VZSidebar/AIChat/ChatInput.tsx +243 -0
  88. package/src/client/VZSidebar/AIChat/DiffView.scss +136 -0
  89. package/src/client/VZSidebar/AIChat/DiffView.tsx +176 -0
  90. package/src/client/VZSidebar/AIChat/Message.tsx +79 -0
  91. package/src/client/VZSidebar/AIChat/MessageList.tsx +190 -0
  92. package/src/client/VZSidebar/AIChat/StreamingMessage.tsx +35 -0
  93. package/src/client/VZSidebar/AIChat/ThinkingScratchpad.tsx +156 -0
  94. package/src/client/VZSidebar/AIChat/TypingIndicator.tsx +19 -0
  95. package/src/client/VZSidebar/AIChat/index.tsx +357 -0
  96. package/src/client/VZSidebar/AIChat/styles.scss +646 -0
  97. package/src/client/VZSidebar/AIChat/useSpeechRecognition.ts +117 -0
  98. package/src/client/VZSidebar/CreateDirModal.tsx +111 -0
  99. package/src/client/VZSidebar/CreateFileModal.tsx +111 -0
  100. package/src/client/VZSidebar/DeleteConfirmationModal.tsx +76 -0
  101. package/src/client/VZSidebar/DirectoryListing.tsx +89 -0
  102. package/src/client/VZSidebar/EmptyState.tsx +11 -0
  103. package/src/client/VZSidebar/FileListing.tsx +91 -0
  104. package/src/client/VZSidebar/FileTypeIcon.tsx +49 -0
  105. package/src/client/VZSidebar/Item.tsx +237 -0
  106. package/src/client/VZSidebar/Listing.tsx +47 -0
  107. package/src/client/VZSidebar/Search.tsx +527 -0
  108. package/src/client/VZSidebar/VisualEditor.tsx +282 -0
  109. package/src/client/VZSidebar/VoiceChatModal.tsx +121 -0
  110. package/src/client/VZSidebar/aiCopyPaste.ts +199 -0
  111. package/src/client/VZSidebar/index.tsx +728 -0
  112. package/src/client/VZSidebar/styles.scss +612 -0
  113. package/src/client/VZSidebar/useDragAndDrop.tsx +408 -0
  114. package/src/client/bootstrap.ts +26 -0
  115. package/src/client/buildWorker.ts +3 -0
  116. package/src/client/featureFlags.ts +8 -0
  117. package/src/client/generateRequestId.ts +6 -0
  118. package/src/client/getFileTree.ts +53 -0
  119. package/src/client/index.ts +19 -0
  120. package/src/client/main.jsx +10 -0
  121. package/src/client/presenceColor.ts +8 -0
  122. package/src/client/shouldTriggerRun.ts +31 -0
  123. package/src/client/sortFileTree.ts +21 -0
  124. package/src/client/tabsSearchParameters.test.ts +79 -0
  125. package/src/client/tabsSearchParameters.ts +137 -0
  126. package/src/client/themes/index.ts +7 -0
  127. package/src/client/themes/themes.ts +77 -0
  128. package/src/client/themes/uiwImports.ts +32 -0
  129. package/src/client/themes/useDynamicTheme.ts +29 -0
  130. package/src/client/themes/vizhubTheme/colors.ts +80 -0
  131. package/src/client/themes/vizhubTheme/index.ts +193 -0
  132. package/src/client/useActions.ts +288 -0
  133. package/src/client/useESLint/index.ts +72 -0
  134. package/src/client/useESLint/worker.ts +113 -0
  135. package/src/client/useEditorCache.ts +35 -0
  136. package/src/client/useFileCRUD.ts +228 -0
  137. package/src/client/useKeyboardShortcuts.ts +451 -0
  138. package/src/client/useOpenDirectories.ts +99 -0
  139. package/src/client/usePresenceAutoFollow.ts +85 -0
  140. package/src/client/usePrettier/index.ts +178 -0
  141. package/src/client/usePrettier/worker.ts +109 -0
  142. package/src/client/useRunCode.tsx +15 -0
  143. package/src/client/useRuntimeError.ts +32 -0
  144. package/src/client/useSubmitOperation.ts +15 -0
  145. package/src/client/useURLSync.ts +137 -0
  146. package/src/client/usernameLocalStorage.ts +43 -0
  147. package/src/client/utils/fileExtension.ts +10 -0
  148. package/src/client/utils/isImageFile.ts +12 -0
  149. package/src/client/vzReducer/aiChatReducer.ts +44 -0
  150. package/src/client/vzReducer/closeTabReducer.test.ts +205 -0
  151. package/src/client/vzReducer/closeTabsReducer.ts +96 -0
  152. package/src/client/vzReducer/createInitialState.ts +38 -0
  153. package/src/client/vzReducer/editorNoLongerWantsFocusReducer.ts +9 -0
  154. package/src/client/vzReducer/findPane.ts +19 -0
  155. package/src/client/vzReducer/index.ts +250 -0
  156. package/src/client/vzReducer/openTabReducer.test.ts +127 -0
  157. package/src/client/vzReducer/openTabReducer.ts +74 -0
  158. package/src/client/vzReducer/searchReducer.test.ts +292 -0
  159. package/src/client/vzReducer/searchReducer.ts +181 -0
  160. package/src/client/vzReducer/setActiveFileIdReducer.ts +28 -0
  161. package/src/client/vzReducer/setActiveFileLeftRightReducer.ts +76 -0
  162. package/src/client/vzReducer/setIsDocOpenReducer.ts +9 -0
  163. package/src/client/vzReducer/setIsSettingsOpenReducer.ts +9 -0
  164. package/src/client/vzReducer/setThemeReducer.ts +9 -0
  165. package/src/client/vzReducer/setUsernameReducer.ts +9 -0
  166. package/src/client/vzReducer/splitCurrentPaneReducer.ts +66 -0
  167. package/src/client/vzReducer/toggleAutoFollowReducer.ts +12 -0
  168. package/src/client/vzReducer/updatePane.ts +40 -0
  169. package/src/client/vzReducer/updatePresenceIndicatorReducer.ts +30 -0
  170. package/src/client/vzReducer/visualEditorReducer.ts +9 -0
  171. package/src/ot.ts +24 -0
  172. package/src/randomId.ts +15 -0
  173. package/src/reactRouterExports.ts +15 -0
  174. package/src/runCode.ts +27 -0
  175. package/src/server/aiChatHandler/aiEditing.ts +151 -0
  176. package/src/server/aiChatHandler/chatOperations.ts +476 -0
  177. package/src/server/aiChatHandler/errorHandling.ts +66 -0
  178. package/src/server/aiChatHandler/index.ts +199 -0
  179. package/src/server/aiChatHandler/llmStreaming.ts +419 -0
  180. package/src/server/aiChatHandler/validation.ts +24 -0
  181. package/src/server/computeInitialDocument.ts +220 -0
  182. package/src/server/config.ts +24 -0
  183. package/src/server/featureFlags.ts +8 -0
  184. package/src/server/generateAIResponse.ts +188 -0
  185. package/src/server/handleAIAssist.ts +53 -0
  186. package/src/server/handleAIChatMessage.ts +2 -0
  187. package/src/server/handleAICopilot.ts +97 -0
  188. package/src/server/index.ts +379 -0
  189. package/src/server/isDirectory.ts +1 -0
  190. package/src/server/livekit.ts +24 -0
  191. package/src/server/prettier.ts +115 -0
  192. package/src/server/setupEnv.ts +8 -0
  193. package/src/submitOperation.ts +25 -0
  194. package/src/types.ts +205 -0
  195. package/src/utils/fileDiff.ts +117 -0
  196. package/src/vite-env.d.ts +10 -0
@@ -0,0 +1,288 @@
1
+ import { useCallback } from 'react';
2
+ import { ThemeLabel } from './themes';
3
+ import {
4
+ PresenceIndicator,
5
+ SearchFileVisibility,
6
+ ShareDBDoc,
7
+ TabState,
8
+ Username,
9
+ } from '../types';
10
+ import { VizFileId, VizContent } from '@vizhub/viz-types';
11
+ import { VZAction } from './vzReducer';
12
+
13
+ // This is a custom hook that returns a set of functions
14
+ // that can be used to dispatch actions to the reducer.
15
+ export const useActions = (
16
+ dispatch: (action: VZAction) => void,
17
+ ) => {
18
+ const setActiveFileId = useCallback(
19
+ (activeFileId: VizFileId) => {
20
+ dispatch({
21
+ type: 'set_active_file_id',
22
+ activeFileId,
23
+ });
24
+ },
25
+ [dispatch],
26
+ );
27
+
28
+ const setActiveFileLeft = useCallback(() => {
29
+ dispatch({ type: 'set_active_file_left' });
30
+ }, [dispatch]);
31
+
32
+ const setActiveFileRight = useCallback(() => {
33
+ dispatch({ type: 'set_active_file_right' });
34
+ }, [dispatch]);
35
+
36
+ const openTab = useCallback(
37
+ (tabState: TabState): void => {
38
+ dispatch({
39
+ type: 'open_tab',
40
+ fileId: tabState.fileId,
41
+ isTransient: tabState.isTransient,
42
+ });
43
+ },
44
+ [dispatch],
45
+ );
46
+
47
+ const closeTabs = useCallback(
48
+ (fileIdsToClose: Array<VizFileId>) => {
49
+ dispatch({
50
+ type: 'close_tabs',
51
+ fileIdsToClose,
52
+ });
53
+ },
54
+ [dispatch],
55
+ );
56
+
57
+ const setTheme = useCallback(
58
+ (themeLabel: ThemeLabel) => {
59
+ dispatch({
60
+ type: 'set_theme',
61
+ themeLabel: themeLabel,
62
+ });
63
+ },
64
+ [dispatch],
65
+ );
66
+
67
+ // True to show the visual editor
68
+ const setIsVisualEditorOpen = useCallback(
69
+ (value: boolean) => {
70
+ dispatch({
71
+ type: 'set_is_visual_editor_open',
72
+ value: value,
73
+ });
74
+ },
75
+ [dispatch],
76
+ );
77
+
78
+ // True to show the settings modal.
79
+ const setIsSearchOpen = useCallback(
80
+ (value: boolean) => {
81
+ dispatch({
82
+ type: 'set_is_search_open',
83
+ value: value,
84
+ });
85
+ },
86
+ [dispatch],
87
+ );
88
+
89
+ // Update search pattern
90
+ const setSearch = useCallback(
91
+ (pattern: string) => {
92
+ dispatch({
93
+ type: 'set_search',
94
+ value: pattern,
95
+ });
96
+ },
97
+ [dispatch],
98
+ );
99
+
100
+ // Update search results based on current pattern
101
+ const setSearchResults = useCallback(
102
+ (files: ShareDBDoc<VizContent>) => {
103
+ dispatch({
104
+ type: 'set_search_results',
105
+ files: files,
106
+ });
107
+ },
108
+ [dispatch],
109
+ );
110
+
111
+ // Update search results file visibility based on current pattern
112
+ const setSearchFileVisibility = useCallback(
113
+ (
114
+ files: ShareDBDoc<VizContent>,
115
+ id: string,
116
+ visibility: SearchFileVisibility,
117
+ ) => {
118
+ dispatch({
119
+ type: 'set_search_file_visibility',
120
+ files: files,
121
+ id: id,
122
+ visibility: visibility,
123
+ });
124
+ },
125
+ [dispatch],
126
+ );
127
+
128
+ // Update search results file matching line visibility to be
129
+ const setSearchLineVisibility = useCallback(
130
+ (
131
+ files: ShareDBDoc<VizContent>,
132
+ id: string,
133
+ line: number,
134
+ ) => {
135
+ dispatch({
136
+ type: 'hide_search_results_line',
137
+ files: files,
138
+ id: id,
139
+ line: line,
140
+ });
141
+ },
142
+ [dispatch],
143
+ );
144
+
145
+ // Update search results active element
146
+ const setSearchFocusedIndex = useCallback(
147
+ (focusedIndex: number, childIndex: number) => {
148
+ dispatch({
149
+ type: 'set_active_search_index',
150
+ focusedIndex: focusedIndex,
151
+ childIndex: childIndex,
152
+ });
153
+ },
154
+ [dispatch],
155
+ );
156
+
157
+ // Toggle the focused variable, which should focus the search input
158
+ const toggleSearchFocused = useCallback(() => {
159
+ dispatch({
160
+ type: 'toggle_search_focused',
161
+ });
162
+ }, [dispatch]);
163
+
164
+ // True to show the AI chat instead of files
165
+ const setIsAIChatOpen = useCallback(
166
+ (value: boolean) => {
167
+ dispatch({
168
+ type: 'set_is_ai_chat_open',
169
+ value: value,
170
+ });
171
+ },
172
+ [dispatch],
173
+ );
174
+
175
+ // Toggle the focused variable, which should focus the AI chat input
176
+ const toggleAIChatFocused = useCallback(() => {
177
+ dispatch({
178
+ type: 'toggle_ai_chat_focused',
179
+ });
180
+ }, [dispatch]);
181
+
182
+ // Set the AI chat mode (ask or edit)
183
+ const setAIChatMode = useCallback(
184
+ (mode: 'ask' | 'edit') => {
185
+ dispatch({
186
+ type: 'set_ai_chat_mode',
187
+ mode,
188
+ });
189
+ },
190
+ [dispatch],
191
+ );
192
+
193
+ // True to show the settings modal.
194
+ const setIsSettingsOpen = useCallback(
195
+ (value: boolean) => {
196
+ dispatch({
197
+ type: 'set_is_settings_open',
198
+ value: value,
199
+ });
200
+ },
201
+ [dispatch],
202
+ );
203
+
204
+ const setIsDocOpen = useCallback(
205
+ (value: boolean) => {
206
+ dispatch({
207
+ type: 'set_is_doc_open',
208
+ value: value,
209
+ });
210
+ },
211
+ [dispatch],
212
+ );
213
+
214
+ const closeSettings = useCallback(() => {
215
+ setIsSettingsOpen(false);
216
+ }, [setIsSettingsOpen]);
217
+
218
+ const closeDoc = useCallback(() => {
219
+ setIsDocOpen(false);
220
+ }, [setIsDocOpen]);
221
+
222
+ const editorNoLongerWantsFocus = useCallback(() => {
223
+ dispatch({
224
+ type: 'editor_no_longer_wants_focus',
225
+ });
226
+ }, [dispatch]);
227
+
228
+ const setUsername = useCallback(
229
+ (username: Username) => {
230
+ dispatch({
231
+ type: 'set_username',
232
+ username,
233
+ });
234
+ },
235
+ [dispatch],
236
+ );
237
+
238
+ const toggleAutoFollow = useCallback(() => {
239
+ dispatch({
240
+ type: 'toggle_auto_follow',
241
+ });
242
+ }, [dispatch]);
243
+
244
+ const updatePresenceIndicator = useCallback(
245
+ (presenceIndicator: PresenceIndicator) => {
246
+ dispatch({
247
+ type: 'update_presence_indicator',
248
+ presenceIndicator,
249
+ });
250
+ },
251
+ [dispatch],
252
+ );
253
+
254
+ const splitCurrentPane = useCallback(() => {
255
+ dispatch({
256
+ type: 'split_current_pane',
257
+ });
258
+ }, [dispatch]);
259
+
260
+ return {
261
+ setActiveFileId,
262
+ setActiveFileLeft,
263
+ setActiveFileRight,
264
+ openTab,
265
+ closeTabs,
266
+ setTheme,
267
+ setIsVisualEditorOpen,
268
+ setIsSearchOpen,
269
+ setSearch,
270
+ setSearchResults,
271
+ setSearchFileVisibility,
272
+ setSearchLineVisibility,
273
+ setSearchFocusedIndex,
274
+ toggleSearchFocused,
275
+ setIsAIChatOpen,
276
+ toggleAIChatFocused,
277
+ setAIChatMode,
278
+ setIsSettingsOpen,
279
+ setIsDocOpen,
280
+ closeSettings,
281
+ closeDoc,
282
+ editorNoLongerWantsFocus,
283
+ setUsername,
284
+ toggleAutoFollow,
285
+ updatePresenceIndicator,
286
+ splitCurrentPane,
287
+ };
288
+ };
@@ -0,0 +1,72 @@
1
+ import { useState, useEffect, useCallback } from 'react';
2
+ import { EditorView } from '@codemirror/view';
3
+ import { Diagnostic } from '@codemirror/lint';
4
+ // @ts-ignore - Worker import
5
+ import ESLintWorker from './worker?worker';
6
+ import { enableESLint } from '../../server/featureFlags';
7
+ import { fileNameStateField } from '../CodeEditor/getOrCreateEditor';
8
+
9
+ let requestCounter = 0;
10
+ const pendingRequests = new Map();
11
+
12
+ export const useESLint = () => {
13
+ const [worker, setWorker] = useState(null);
14
+
15
+ // Initialize the work in an effect so it does not run during SSR.
16
+ useEffect(() => {
17
+ setWorker(new ESLintWorker());
18
+ }, []);
19
+
20
+ useEffect(() => {
21
+ // Wait for the worker to be initialized
22
+ if (!worker) return;
23
+
24
+ const handleMessage = (event: MessageEvent) => {
25
+ const { diagnostics, requestId, error } = event.data;
26
+
27
+ if (error) {
28
+ console.error('[ESLint] Error:', error);
29
+ }
30
+
31
+ const resolve = pendingRequests.get(requestId);
32
+ if (resolve) {
33
+ resolve(diagnostics);
34
+ pendingRequests.delete(requestId);
35
+ }
36
+ };
37
+
38
+ worker.addEventListener('message', handleMessage);
39
+
40
+ return () => {
41
+ worker.removeEventListener('message', handleMessage);
42
+ worker.terminate();
43
+ };
44
+ }, [worker]);
45
+
46
+ const esLintSource = useCallback(
47
+ async (
48
+ view: EditorView,
49
+ ): Promise<readonly Diagnostic[]> => {
50
+ if (!enableESLint) return [];
51
+
52
+ // Retrieve the file name from the editor's state
53
+ const fileName = view.state.field(fileNameStateField);
54
+
55
+ try {
56
+ const code = view.state.doc.toString();
57
+ const requestId = requestCounter++;
58
+
59
+ return new Promise<Diagnostic[]>((resolve) => {
60
+ pendingRequests.set(requestId, resolve);
61
+ worker.postMessage({ code, requestId, fileName });
62
+ });
63
+ } catch (e) {
64
+ console.error('[ESLint] Error in linter:', e);
65
+ return [];
66
+ }
67
+ },
68
+ [worker],
69
+ );
70
+
71
+ return { esLintSource };
72
+ };
@@ -0,0 +1,113 @@
1
+ import * as eslint from 'eslint-linter-browserify';
2
+ import globals from 'globals';
3
+
4
+ // Feature flag to enable or disable JSX linting
5
+ // It's disabled for now since it doesn't work.
6
+ // See issue:
7
+ // https://github.com/vizhub-core/vzcode/issues/921
8
+ const enableJSXLinting = false;
9
+
10
+ const linter = new eslint.Linter();
11
+
12
+ const config = {
13
+ languageOptions: {
14
+ globals: {
15
+ ...globals.browser,
16
+ ...globals.es2021,
17
+ },
18
+ parserOptions: {
19
+ ecmaVersion: 2022,
20
+ sourceType: 'module',
21
+ ecmaFeatures: {
22
+ // Add this to enable JSX parsing
23
+ jsx: true,
24
+ },
25
+ },
26
+ },
27
+ rules: {
28
+ 'no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
29
+ 'no-undef': 'error',
30
+ semi: 'off',
31
+ },
32
+ };
33
+
34
+ // Helper function to convert line/column to character offset
35
+ function getOffset(
36
+ docLines: string[],
37
+ line: number,
38
+ column: number,
39
+ ): number {
40
+ let offset = 0;
41
+ for (let i = 0; i < line - 1; i++) {
42
+ offset += (docLines[i] ? docLines[i].length : 0) + 1; // +1 for newline
43
+ }
44
+ return offset + column - 1;
45
+ }
46
+
47
+ self.onmessage = (event) => {
48
+ const { code, requestId, fileName } = event.data;
49
+ if (typeof code !== 'string') {
50
+ self.postMessage({
51
+ diagnostics: [],
52
+ requestId,
53
+ error: 'Invalid code received',
54
+ });
55
+ return;
56
+ }
57
+
58
+ // Only lint .js files, and .jsx files if enableJSXLinting is true
59
+ if (!fileName) {
60
+ self.postMessage({ diagnostics: [], requestId });
61
+ return;
62
+ }
63
+
64
+ const isJsFile = /\.js$/i.test(fileName);
65
+ const isJsxFile = /\.jsx$/i.test(fileName);
66
+
67
+ if (!isJsFile && (!isJsxFile || !enableJSXLinting)) {
68
+ self.postMessage({ diagnostics: [], requestId });
69
+ return;
70
+ }
71
+
72
+ try {
73
+ const messages = linter.verify(code, config as any);
74
+ const docLines = code.split('\n');
75
+
76
+ const diagnostics = messages.map((msg) => {
77
+ const from = getOffset(
78
+ docLines,
79
+ msg.line,
80
+ msg.column,
81
+ );
82
+ // endLine and endColumn may not always exist
83
+ const to =
84
+ msg.endLine && msg.endColumn
85
+ ? getOffset(docLines, msg.endLine, msg.endColumn)
86
+ : from +
87
+ (msg.fix?.range[1] - msg.fix?.range[0] || 1);
88
+
89
+ return {
90
+ from,
91
+ to,
92
+ severity:
93
+ msg.severity === 2
94
+ ? 'error'
95
+ : msg.severity === 1
96
+ ? 'warning'
97
+ : 'info',
98
+ message: msg.message,
99
+ source: msg.ruleId
100
+ ? `eslint(${msg.ruleId})`
101
+ : 'eslint',
102
+ };
103
+ });
104
+ self.postMessage({ diagnostics, requestId });
105
+ } catch (e: any) {
106
+ console.error('Error linting in ESLint worker:', e);
107
+ self.postMessage({
108
+ error: e.message,
109
+ diagnostics: [],
110
+ requestId,
111
+ });
112
+ }
113
+ };
@@ -0,0 +1,35 @@
1
+ import { useRef } from 'react';
2
+ import { VizFileId } from '@vizhub/viz-types';
3
+ import { PaneId } from '../types';
4
+ import { EditorView } from 'codemirror';
5
+
6
+ export type EditorCacheValue = {
7
+ editor: EditorView;
8
+ themeCompartment: any;
9
+ languageCompartment?: any;
10
+ scrollPosition?: number;
11
+ };
12
+
13
+ export type EditorCacheKey = string;
14
+
15
+ export const editorCacheKey = (
16
+ fileId: VizFileId,
17
+ paneId: PaneId,
18
+ ): EditorCacheKey => fileId + '|' + paneId;
19
+
20
+ export type EditorCache = Map<
21
+ EditorCacheKey,
22
+ EditorCacheValue
23
+ >;
24
+
25
+ export const useEditorCache = () => {
26
+ // Singleton cache of CodeMirror instances
27
+ // These are created, but never destroyed.
28
+ // TODO consider invalidating this cache at some point.
29
+ // Right now these editor instances just keep accumulating,
30
+ // and they all retain their connections to ShareDB.
31
+ // This has not been an issue so far, but it could become one.
32
+ const editorCacheRef = useRef<EditorCache>(new Map());
33
+
34
+ return editorCacheRef.current;
35
+ };