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.
- package/package.json +3 -2
- package/src/cli.ts +208 -0
- package/src/client/AIAssist/AIAssistCodeMirrorKeyMap.ts +42 -0
- package/src/client/AIAssist/AIAssistWidget/index.tsx +135 -0
- package/src/client/AIAssist/AIAssistWidget/style.scss +47 -0
- package/src/client/AIAssist/Spinner.jsx +76 -0
- package/src/client/AIAssist/formatFile.ts +27 -0
- package/src/client/AIAssist/generateFilesContext.ts +25 -0
- package/src/client/AIAssist/index.ts +2 -0
- package/src/client/AIAssist/startAIAssist.ts +95 -0
- package/src/client/App/index.tsx +152 -0
- package/src/client/App/style.scss +82 -0
- package/src/client/App/usePending.ts +51 -0
- package/src/client/App/useShareDB.ts +161 -0
- package/src/client/CodeEditor/Copilot/index.ts +74 -0
- package/src/client/CodeEditor/InteractiveWidgets/boolToggler.ts +12 -0
- package/src/client/CodeEditor/InteractiveWidgets/colorPicker.ts +68 -0
- package/src/client/CodeEditor/InteractiveWidgets/colorsInTextPlugin.ts +174 -0
- package/src/client/CodeEditor/InteractiveWidgets/highlightWidgets.ts +79 -0
- package/src/client/CodeEditor/InteractiveWidgets/index.ts +44 -0
- package/src/client/CodeEditor/InteractiveWidgets/markdownCheckboxToggler.ts +12 -0
- package/src/client/CodeEditor/InteractiveWidgets/numberDragger.ts +20 -0
- package/src/client/CodeEditor/InteractiveWidgets/rotateWidget.ts +143 -0
- package/src/client/CodeEditor/InteractiveWidgets/urlClicker.test.ts +48 -0
- package/src/client/CodeEditor/InteractiveWidgets/urlClicker.ts +12 -0
- package/src/client/CodeEditor/InteractiveWidgets/vec2Slider.ts +25 -0
- package/src/client/CodeEditor/basicSetup.ts +73 -0
- package/src/client/CodeEditor/getOrCreateEditor.tsx +615 -0
- package/src/client/CodeEditor/index.tsx +202 -0
- package/src/client/CodeEditor/json1PresenceBroadcast.ts +41 -0
- package/src/client/CodeEditor/json1PresenceDisplay.ts +350 -0
- package/src/client/CodeEditor/rainbowBrackets.ts +122 -0
- package/src/client/CodeEditor/style.scss +156 -0
- package/src/client/CodeEditor/typescriptExtension/worker.ts +32 -0
- package/src/client/CodeErrorOverlay/index.tsx +77 -0
- package/src/client/CodeErrorOverlay/style.scss +33 -0
- package/src/client/Fonts/fonts.ts +8 -0
- package/src/client/Icons/AdjustmentSVG.tsx +19 -0
- package/src/client/Icons/BugSVG.tsx +20 -0
- package/src/client/Icons/CloseSVG.tsx +19 -0
- package/src/client/Icons/CssSVG.tsx +16 -0
- package/src/client/Icons/DirectoryArrowSVG.tsx +19 -0
- package/src/client/Icons/EditSVG.tsx +19 -0
- package/src/client/Icons/FileSVG.tsx +19 -0
- package/src/client/Icons/FolderSVG.tsx +20 -0
- package/src/client/Icons/GearSVG.tsx +16 -0
- package/src/client/Icons/HtmlSVG.tsx +16 -0
- package/src/client/Icons/JavaScriptSVG.tsx +16 -0
- package/src/client/Icons/JsonSVG.tsx +16 -0
- package/src/client/Icons/MarkdownSVG.tsx +16 -0
- package/src/client/Icons/MicOffSVG.tsx +35 -0
- package/src/client/Icons/MicSVG.tsx +25 -0
- package/src/client/Icons/NewSVG.tsx +19 -0
- package/src/client/Icons/PinSVG.tsx +14 -0
- package/src/client/Icons/PlaySVG.tsx +16 -0
- package/src/client/Icons/QuestionMarkSVG.tsx +18 -0
- package/src/client/Icons/ReactSVG.tsx +16 -0
- package/src/client/Icons/SearchSVG.tsx +20 -0
- package/src/client/Icons/SparklesSVG.tsx +23 -0
- package/src/client/Icons/SplitEditorSVG.tsx +34 -0
- package/src/client/Icons/StopSVG.tsx +15 -0
- package/src/client/Icons/SvelteSVG.tsx +16 -0
- package/src/client/Icons/TrashSVG.tsx +19 -0
- package/src/client/Icons/TypeScriptSVG.tsx +16 -0
- package/src/client/Icons/index.tsx +26 -0
- package/src/client/ImageViewer/index.tsx +91 -0
- package/src/client/ImageViewer/style.scss +41 -0
- package/src/client/PresenceNotifications/PresenceNotifications.test.ts +134 -0
- package/src/client/PresenceNotifications/index.tsx +158 -0
- package/src/client/PresenceNotifications/style.scss +17 -0
- package/src/client/RunCodeWidget/index.tsx +117 -0
- package/src/client/RunCodeWidget/style.scss +24 -0
- package/src/client/SplitPaneResizeContext.tsx +159 -0
- package/src/client/TabList/Tab.tsx +73 -0
- package/src/client/TabList/index.tsx +35 -0
- package/src/client/TabList/style.scss +51 -0
- package/src/client/VZCodeContext/index.tsx +29 -0
- package/src/client/VZCodeContext/types.ts +248 -0
- package/src/client/VZCodeContext/useVZCodeState.ts +602 -0
- package/src/client/VZKeyboardShortcutsDoc.tsx +148 -0
- package/src/client/VZLeft.tsx +28 -0
- package/src/client/VZMiddle.tsx +172 -0
- package/src/client/VZResizer/index.tsx +128 -0
- package/src/client/VZResizer/styles.scss +33 -0
- package/src/client/VZRight.tsx +113 -0
- package/src/client/VZSettings.tsx +275 -0
- package/src/client/VZSidebar/AIChat/ChatInput.tsx +243 -0
- package/src/client/VZSidebar/AIChat/DiffView.scss +136 -0
- package/src/client/VZSidebar/AIChat/DiffView.tsx +176 -0
- package/src/client/VZSidebar/AIChat/Message.tsx +79 -0
- package/src/client/VZSidebar/AIChat/MessageList.tsx +190 -0
- package/src/client/VZSidebar/AIChat/StreamingMessage.tsx +35 -0
- package/src/client/VZSidebar/AIChat/ThinkingScratchpad.tsx +156 -0
- package/src/client/VZSidebar/AIChat/TypingIndicator.tsx +19 -0
- package/src/client/VZSidebar/AIChat/index.tsx +357 -0
- package/src/client/VZSidebar/AIChat/styles.scss +646 -0
- package/src/client/VZSidebar/AIChat/useSpeechRecognition.ts +117 -0
- package/src/client/VZSidebar/CreateDirModal.tsx +111 -0
- package/src/client/VZSidebar/CreateFileModal.tsx +111 -0
- package/src/client/VZSidebar/DeleteConfirmationModal.tsx +76 -0
- package/src/client/VZSidebar/DirectoryListing.tsx +89 -0
- package/src/client/VZSidebar/EmptyState.tsx +11 -0
- package/src/client/VZSidebar/FileListing.tsx +91 -0
- package/src/client/VZSidebar/FileTypeIcon.tsx +49 -0
- package/src/client/VZSidebar/Item.tsx +237 -0
- package/src/client/VZSidebar/Listing.tsx +47 -0
- package/src/client/VZSidebar/Search.tsx +527 -0
- package/src/client/VZSidebar/VisualEditor.tsx +282 -0
- package/src/client/VZSidebar/VoiceChatModal.tsx +121 -0
- package/src/client/VZSidebar/aiCopyPaste.ts +199 -0
- package/src/client/VZSidebar/index.tsx +728 -0
- package/src/client/VZSidebar/styles.scss +612 -0
- package/src/client/VZSidebar/useDragAndDrop.tsx +408 -0
- package/src/client/bootstrap.ts +26 -0
- package/src/client/buildWorker.ts +3 -0
- package/src/client/featureFlags.ts +8 -0
- package/src/client/generateRequestId.ts +6 -0
- package/src/client/getFileTree.ts +53 -0
- package/src/client/index.ts +19 -0
- package/src/client/main.jsx +10 -0
- package/src/client/presenceColor.ts +8 -0
- package/src/client/shouldTriggerRun.ts +31 -0
- package/src/client/sortFileTree.ts +21 -0
- package/src/client/tabsSearchParameters.test.ts +79 -0
- package/src/client/tabsSearchParameters.ts +137 -0
- package/src/client/themes/index.ts +7 -0
- package/src/client/themes/themes.ts +77 -0
- package/src/client/themes/uiwImports.ts +32 -0
- package/src/client/themes/useDynamicTheme.ts +29 -0
- package/src/client/themes/vizhubTheme/colors.ts +80 -0
- package/src/client/themes/vizhubTheme/index.ts +193 -0
- package/src/client/useActions.ts +288 -0
- package/src/client/useESLint/index.ts +72 -0
- package/src/client/useESLint/worker.ts +113 -0
- package/src/client/useEditorCache.ts +35 -0
- package/src/client/useFileCRUD.ts +228 -0
- package/src/client/useKeyboardShortcuts.ts +451 -0
- package/src/client/useOpenDirectories.ts +99 -0
- package/src/client/usePresenceAutoFollow.ts +85 -0
- package/src/client/usePrettier/index.ts +178 -0
- package/src/client/usePrettier/worker.ts +109 -0
- package/src/client/useRunCode.tsx +15 -0
- package/src/client/useRuntimeError.ts +32 -0
- package/src/client/useSubmitOperation.ts +15 -0
- package/src/client/useURLSync.ts +137 -0
- package/src/client/usernameLocalStorage.ts +43 -0
- package/src/client/utils/fileExtension.ts +10 -0
- package/src/client/utils/isImageFile.ts +12 -0
- package/src/client/vzReducer/aiChatReducer.ts +44 -0
- package/src/client/vzReducer/closeTabReducer.test.ts +205 -0
- package/src/client/vzReducer/closeTabsReducer.ts +96 -0
- package/src/client/vzReducer/createInitialState.ts +38 -0
- package/src/client/vzReducer/editorNoLongerWantsFocusReducer.ts +9 -0
- package/src/client/vzReducer/findPane.ts +19 -0
- package/src/client/vzReducer/index.ts +250 -0
- package/src/client/vzReducer/openTabReducer.test.ts +127 -0
- package/src/client/vzReducer/openTabReducer.ts +74 -0
- package/src/client/vzReducer/searchReducer.test.ts +292 -0
- package/src/client/vzReducer/searchReducer.ts +181 -0
- package/src/client/vzReducer/setActiveFileIdReducer.ts +28 -0
- package/src/client/vzReducer/setActiveFileLeftRightReducer.ts +76 -0
- package/src/client/vzReducer/setIsDocOpenReducer.ts +9 -0
- package/src/client/vzReducer/setIsSettingsOpenReducer.ts +9 -0
- package/src/client/vzReducer/setThemeReducer.ts +9 -0
- package/src/client/vzReducer/setUsernameReducer.ts +9 -0
- package/src/client/vzReducer/splitCurrentPaneReducer.ts +66 -0
- package/src/client/vzReducer/toggleAutoFollowReducer.ts +12 -0
- package/src/client/vzReducer/updatePane.ts +40 -0
- package/src/client/vzReducer/updatePresenceIndicatorReducer.ts +30 -0
- package/src/client/vzReducer/visualEditorReducer.ts +9 -0
- package/src/ot.ts +24 -0
- package/src/randomId.ts +15 -0
- package/src/reactRouterExports.ts +15 -0
- package/src/runCode.ts +27 -0
- package/src/server/aiChatHandler/aiEditing.ts +151 -0
- package/src/server/aiChatHandler/chatOperations.ts +476 -0
- package/src/server/aiChatHandler/errorHandling.ts +66 -0
- package/src/server/aiChatHandler/index.ts +199 -0
- package/src/server/aiChatHandler/llmStreaming.ts +419 -0
- package/src/server/aiChatHandler/validation.ts +24 -0
- package/src/server/computeInitialDocument.ts +220 -0
- package/src/server/config.ts +24 -0
- package/src/server/featureFlags.ts +8 -0
- package/src/server/generateAIResponse.ts +188 -0
- package/src/server/handleAIAssist.ts +53 -0
- package/src/server/handleAIChatMessage.ts +2 -0
- package/src/server/handleAICopilot.ts +97 -0
- package/src/server/index.ts +379 -0
- package/src/server/isDirectory.ts +1 -0
- package/src/server/livekit.ts +24 -0
- package/src/server/prettier.ts +115 -0
- package/src/server/setupEnv.ts +8 -0
- package/src/submitOperation.ts +25 -0
- package/src/types.ts +205 -0
- package/src/utils/fileDiff.ts +117 -0
- package/src/vite-env.d.ts +10 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { setSearchResultsReducer } from './searchReducer';
|
|
3
|
+
import { VZAction, VZState, createInitialState } from '.';
|
|
4
|
+
import { defaultTheme } from '../themes';
|
|
5
|
+
import { ShareDBDoc } from '../../types';
|
|
6
|
+
import { VizContent } from '@vizhub/viz-types';
|
|
7
|
+
|
|
8
|
+
// Mock ShareDBDoc for testing
|
|
9
|
+
const createMockShareDBDoc = (
|
|
10
|
+
files: any,
|
|
11
|
+
): ShareDBDoc<VizContent> => ({
|
|
12
|
+
data: { files, id: 'mock-id' },
|
|
13
|
+
ingestSnapshot: (snapshot: any, callback: any) => {
|
|
14
|
+
// No-op for testing
|
|
15
|
+
if (callback) callback();
|
|
16
|
+
},
|
|
17
|
+
subscribe: (callback: any) => {
|
|
18
|
+
// No-op for testing
|
|
19
|
+
if (callback) callback();
|
|
20
|
+
},
|
|
21
|
+
on: (
|
|
22
|
+
event: string,
|
|
23
|
+
callback: (op: any, source: boolean) => void,
|
|
24
|
+
) => {
|
|
25
|
+
// No-op for testing
|
|
26
|
+
},
|
|
27
|
+
off: (
|
|
28
|
+
event: string,
|
|
29
|
+
callback: (op: any, source: boolean) => void,
|
|
30
|
+
) => {
|
|
31
|
+
// No-op for testing
|
|
32
|
+
},
|
|
33
|
+
removeListener: (
|
|
34
|
+
event: string,
|
|
35
|
+
callback: (op: any, source: boolean) => void,
|
|
36
|
+
) => {
|
|
37
|
+
// No-op for testing
|
|
38
|
+
},
|
|
39
|
+
submitOp: (
|
|
40
|
+
op: any,
|
|
41
|
+
options?: any,
|
|
42
|
+
callback?: () => void,
|
|
43
|
+
) => {
|
|
44
|
+
// No-op for testing
|
|
45
|
+
if (callback) callback();
|
|
46
|
+
},
|
|
47
|
+
whenNothingPending: (callback: () => void) => {
|
|
48
|
+
// No-op for testing
|
|
49
|
+
if (callback) callback();
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const initialState: VZState = createInitialState({
|
|
54
|
+
defaultTheme,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe('searchReducer', () => {
|
|
58
|
+
describe('setSearchResultsReducer', () => {
|
|
59
|
+
it('should find case-insensitive matches', () => {
|
|
60
|
+
const mockFiles = {
|
|
61
|
+
'file1.js': {
|
|
62
|
+
name: 'file1.js',
|
|
63
|
+
text: 'const Hello = "world";\nconst goodbye = "HELLO";',
|
|
64
|
+
},
|
|
65
|
+
'file2.js': {
|
|
66
|
+
name: 'file2.js',
|
|
67
|
+
text: 'function hello() {\n return "Hello World";\n}',
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const stateWithSearch = {
|
|
72
|
+
...initialState,
|
|
73
|
+
search: {
|
|
74
|
+
...initialState.search,
|
|
75
|
+
pattern: 'hello', // lowercase search pattern
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const action: VZAction = {
|
|
80
|
+
type: 'set_search_results',
|
|
81
|
+
files: createMockShareDBDoc(mockFiles),
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const newState = setSearchResultsReducer(
|
|
85
|
+
stateWithSearch,
|
|
86
|
+
action,
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
// Should find matches in both files despite different cases
|
|
90
|
+
expect(
|
|
91
|
+
Object.keys(newState.search.results),
|
|
92
|
+
).toHaveLength(2);
|
|
93
|
+
|
|
94
|
+
// Check file1.js results
|
|
95
|
+
const file1Results =
|
|
96
|
+
newState.search.results['file1.js'];
|
|
97
|
+
expect(file1Results).toBeDefined();
|
|
98
|
+
expect(file1Results.matches).toHaveLength(2); // "Hello" and "HELLO"
|
|
99
|
+
expect(file1Results.matches[0].line).toBe(1);
|
|
100
|
+
expect(file1Results.matches[0].index).toBe(6); // position of "Hello"
|
|
101
|
+
expect(file1Results.matches[1].line).toBe(2);
|
|
102
|
+
expect(file1Results.matches[1].index).toBe(17); // position of "HELLO"
|
|
103
|
+
|
|
104
|
+
// Check file2.js results
|
|
105
|
+
const file2Results =
|
|
106
|
+
newState.search.results['file2.js'];
|
|
107
|
+
expect(file2Results).toBeDefined();
|
|
108
|
+
expect(file2Results.matches).toHaveLength(2); // "hello" and "Hello"
|
|
109
|
+
expect(file2Results.matches[0].line).toBe(1);
|
|
110
|
+
expect(file2Results.matches[0].index).toBe(9); // position of "hello"
|
|
111
|
+
expect(file2Results.matches[1].line).toBe(2);
|
|
112
|
+
expect(file2Results.matches[1].index).toBe(10); // position of "Hello"
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should find exact case matches', () => {
|
|
116
|
+
const mockFiles = {
|
|
117
|
+
'file1.js': {
|
|
118
|
+
name: 'file1.js',
|
|
119
|
+
text: 'const hello = "world";',
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const stateWithSearch = {
|
|
124
|
+
...initialState,
|
|
125
|
+
search: {
|
|
126
|
+
...initialState.search,
|
|
127
|
+
pattern: 'hello', // exact case match
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const action: VZAction = {
|
|
132
|
+
type: 'set_search_results',
|
|
133
|
+
files: createMockShareDBDoc(mockFiles),
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const newState = setSearchResultsReducer(
|
|
137
|
+
stateWithSearch,
|
|
138
|
+
action,
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
expect(
|
|
142
|
+
Object.keys(newState.search.results),
|
|
143
|
+
).toHaveLength(1);
|
|
144
|
+
const file1Results =
|
|
145
|
+
newState.search.results['file1.js'];
|
|
146
|
+
expect(file1Results.matches).toHaveLength(1);
|
|
147
|
+
expect(file1Results.matches[0].line).toBe(1);
|
|
148
|
+
expect(file1Results.matches[0].index).toBe(6);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('should not find matches when pattern does not exist', () => {
|
|
152
|
+
const mockFiles = {
|
|
153
|
+
'file1.js': {
|
|
154
|
+
name: 'file1.js',
|
|
155
|
+
text: 'const world = "test";',
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const stateWithSearch = {
|
|
160
|
+
...initialState,
|
|
161
|
+
search: {
|
|
162
|
+
...initialState.search,
|
|
163
|
+
pattern: 'hello',
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const action: VZAction = {
|
|
168
|
+
type: 'set_search_results',
|
|
169
|
+
files: createMockShareDBDoc(mockFiles),
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const newState = setSearchResultsReducer(
|
|
173
|
+
stateWithSearch,
|
|
174
|
+
action,
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
expect(
|
|
178
|
+
Object.keys(newState.search.results),
|
|
179
|
+
).toHaveLength(0);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('should handle uppercase search pattern', () => {
|
|
183
|
+
const mockFiles = {
|
|
184
|
+
'file1.js': {
|
|
185
|
+
name: 'file1.js',
|
|
186
|
+
text: 'const Hello = "world";\nfunction hello() {}',
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const stateWithSearch = {
|
|
191
|
+
...initialState,
|
|
192
|
+
search: {
|
|
193
|
+
...initialState.search,
|
|
194
|
+
pattern: 'HELLO', // uppercase search pattern
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const action: VZAction = {
|
|
199
|
+
type: 'set_search_results',
|
|
200
|
+
files: createMockShareDBDoc(mockFiles),
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const newState = setSearchResultsReducer(
|
|
204
|
+
stateWithSearch,
|
|
205
|
+
action,
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
expect(
|
|
209
|
+
Object.keys(newState.search.results),
|
|
210
|
+
).toHaveLength(1);
|
|
211
|
+
const file1Results =
|
|
212
|
+
newState.search.results['file1.js'];
|
|
213
|
+
expect(file1Results.matches).toHaveLength(2); // Should find both "Hello" and "hello"
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('should handle mixed case search pattern', () => {
|
|
217
|
+
const mockFiles = {
|
|
218
|
+
'file1.js': {
|
|
219
|
+
name: 'file1.js',
|
|
220
|
+
text: 'const HeLLo = "world";\nconst hello = "test";\nconst HELLO = "end";',
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const stateWithSearch = {
|
|
225
|
+
...initialState,
|
|
226
|
+
search: {
|
|
227
|
+
...initialState.search,
|
|
228
|
+
pattern: 'HeLLo', // mixed case search pattern
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const action: VZAction = {
|
|
233
|
+
type: 'set_search_results',
|
|
234
|
+
files: createMockShareDBDoc(mockFiles),
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const newState = setSearchResultsReducer(
|
|
238
|
+
stateWithSearch,
|
|
239
|
+
action,
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
expect(
|
|
243
|
+
Object.keys(newState.search.results),
|
|
244
|
+
).toHaveLength(1);
|
|
245
|
+
const file1Results =
|
|
246
|
+
newState.search.results['file1.js'];
|
|
247
|
+
expect(file1Results.matches).toHaveLength(3); // Should find all three variations
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it('should skip files without text content', () => {
|
|
251
|
+
const mockFiles = {
|
|
252
|
+
'file1.js': {
|
|
253
|
+
name: 'file1.js',
|
|
254
|
+
text: 'const hello = "world";',
|
|
255
|
+
},
|
|
256
|
+
'file2.js': {
|
|
257
|
+
name: 'file2.js',
|
|
258
|
+
// no text property
|
|
259
|
+
},
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
const stateWithSearch = {
|
|
263
|
+
...initialState,
|
|
264
|
+
search: {
|
|
265
|
+
...initialState.search,
|
|
266
|
+
pattern: 'hello',
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
const action: VZAction = {
|
|
271
|
+
type: 'set_search_results',
|
|
272
|
+
files: createMockShareDBDoc(mockFiles),
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const newState = setSearchResultsReducer(
|
|
276
|
+
stateWithSearch,
|
|
277
|
+
action,
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
// Should only find results in file1.js
|
|
281
|
+
expect(
|
|
282
|
+
Object.keys(newState.search.results),
|
|
283
|
+
).toHaveLength(1);
|
|
284
|
+
expect(
|
|
285
|
+
newState.search.results['file1.js'],
|
|
286
|
+
).toBeDefined();
|
|
287
|
+
expect(
|
|
288
|
+
newState.search.results['file2.js'],
|
|
289
|
+
).toBeUndefined();
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
});
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { VZAction, VZState } from '.';
|
|
2
|
+
import {
|
|
3
|
+
SearchFile,
|
|
4
|
+
SearchFileVisibility,
|
|
5
|
+
SearchResult,
|
|
6
|
+
ShareDBDoc,
|
|
7
|
+
} from '../../types';
|
|
8
|
+
import { VizContent } from '@vizhub/viz-types';
|
|
9
|
+
|
|
10
|
+
function searchPattern(
|
|
11
|
+
shareDBDoc: ShareDBDoc<VizContent>,
|
|
12
|
+
pattern: string,
|
|
13
|
+
): SearchResult {
|
|
14
|
+
const files = shareDBDoc.data.files;
|
|
15
|
+
const fileIds = Object.keys(shareDBDoc.data.files);
|
|
16
|
+
let results: { [id: string]: SearchFile } = {};
|
|
17
|
+
|
|
18
|
+
for (let i = 0; i < fileIds.length; i++) {
|
|
19
|
+
const file = files[fileIds[i]];
|
|
20
|
+
|
|
21
|
+
if (files[fileIds[i]].text) {
|
|
22
|
+
const fileName = file.name;
|
|
23
|
+
const lines = file.text.split('\n');
|
|
24
|
+
const matches = [];
|
|
25
|
+
|
|
26
|
+
for (let j = 0; j < lines.length; j++) {
|
|
27
|
+
const index = lines[j]
|
|
28
|
+
.toLowerCase()
|
|
29
|
+
.indexOf(pattern.toLowerCase());
|
|
30
|
+
|
|
31
|
+
if (index !== -1) {
|
|
32
|
+
matches.push({
|
|
33
|
+
line: j + 1,
|
|
34
|
+
index: index,
|
|
35
|
+
text: lines[j],
|
|
36
|
+
isClosed: false,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (matches.length > 0) {
|
|
42
|
+
results[fileIds[i]] = {
|
|
43
|
+
name: fileName,
|
|
44
|
+
matches: matches,
|
|
45
|
+
visibility: 'open',
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return results;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function updateSearchFileVisibility(
|
|
55
|
+
results: SearchResult,
|
|
56
|
+
id: string,
|
|
57
|
+
visibility: SearchFileVisibility,
|
|
58
|
+
): SearchResult {
|
|
59
|
+
return {
|
|
60
|
+
...results,
|
|
61
|
+
[id]: { ...results[id], visibility: visibility },
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const setIsSearchOpenReducer = (
|
|
66
|
+
state: VZState,
|
|
67
|
+
action: VZAction,
|
|
68
|
+
): VZState =>
|
|
69
|
+
action.type === 'set_is_search_open'
|
|
70
|
+
? { ...state, isSearchOpen: action.value }
|
|
71
|
+
: state;
|
|
72
|
+
|
|
73
|
+
export const setSearchReducer = (
|
|
74
|
+
state: VZState,
|
|
75
|
+
action: VZAction,
|
|
76
|
+
): VZState =>
|
|
77
|
+
action.type === 'set_search'
|
|
78
|
+
? {
|
|
79
|
+
...state,
|
|
80
|
+
search: {
|
|
81
|
+
...state.search,
|
|
82
|
+
pattern: action.value,
|
|
83
|
+
results: {},
|
|
84
|
+
focusedIndex: 0,
|
|
85
|
+
focusedChildIndex: null,
|
|
86
|
+
focused: true,
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
: state;
|
|
90
|
+
|
|
91
|
+
export const setSearchResultsReducer = (
|
|
92
|
+
state: VZState,
|
|
93
|
+
action: VZAction,
|
|
94
|
+
): VZState =>
|
|
95
|
+
action.type === 'set_search_results'
|
|
96
|
+
? {
|
|
97
|
+
...state,
|
|
98
|
+
search: {
|
|
99
|
+
...state.search,
|
|
100
|
+
pattern: state.search.pattern,
|
|
101
|
+
results: searchPattern(
|
|
102
|
+
action.files,
|
|
103
|
+
state.search.pattern,
|
|
104
|
+
),
|
|
105
|
+
focused: state.search.focused,
|
|
106
|
+
},
|
|
107
|
+
}
|
|
108
|
+
: state;
|
|
109
|
+
|
|
110
|
+
export const setSearchFileVisibilityReducer = (
|
|
111
|
+
state: VZState,
|
|
112
|
+
action: VZAction,
|
|
113
|
+
): VZState =>
|
|
114
|
+
action.type === 'set_search_file_visibility'
|
|
115
|
+
? {
|
|
116
|
+
...state,
|
|
117
|
+
search: {
|
|
118
|
+
...state.search,
|
|
119
|
+
pattern: state.search.pattern,
|
|
120
|
+
results: updateSearchFileVisibility(
|
|
121
|
+
state.search.results,
|
|
122
|
+
action.id,
|
|
123
|
+
action.visibility,
|
|
124
|
+
),
|
|
125
|
+
},
|
|
126
|
+
}
|
|
127
|
+
: state;
|
|
128
|
+
|
|
129
|
+
export const setSearchLineVisibilityReducer = (
|
|
130
|
+
state: VZState,
|
|
131
|
+
action: VZAction,
|
|
132
|
+
): VZState =>
|
|
133
|
+
action.type === 'hide_search_results_line'
|
|
134
|
+
? {
|
|
135
|
+
...state,
|
|
136
|
+
search: {
|
|
137
|
+
...state.search,
|
|
138
|
+
results: {
|
|
139
|
+
...state.search.results,
|
|
140
|
+
[action.id]: {
|
|
141
|
+
...state.search.results[action.id],
|
|
142
|
+
matches: state.search.results[
|
|
143
|
+
action.id
|
|
144
|
+
].matches.filter(
|
|
145
|
+
(match, index) =>
|
|
146
|
+
match.line !== action.line,
|
|
147
|
+
),
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
}
|
|
152
|
+
: state;
|
|
153
|
+
|
|
154
|
+
export const setSearchFocusedIndexReducer = (
|
|
155
|
+
state: VZState,
|
|
156
|
+
action: VZAction,
|
|
157
|
+
): VZState =>
|
|
158
|
+
action.type === 'set_active_search_index'
|
|
159
|
+
? {
|
|
160
|
+
...state,
|
|
161
|
+
search: {
|
|
162
|
+
...state.search,
|
|
163
|
+
focusedIndex: action.focusedIndex,
|
|
164
|
+
focusedChildIndex: action.childIndex,
|
|
165
|
+
},
|
|
166
|
+
}
|
|
167
|
+
: state;
|
|
168
|
+
|
|
169
|
+
export const toggleSearchFocusedReducer = (
|
|
170
|
+
state: VZState,
|
|
171
|
+
action: VZAction,
|
|
172
|
+
): VZState =>
|
|
173
|
+
action.type === 'toggle_search_focused'
|
|
174
|
+
? {
|
|
175
|
+
...state,
|
|
176
|
+
search: {
|
|
177
|
+
...state.search,
|
|
178
|
+
focused: !state.search.focused,
|
|
179
|
+
},
|
|
180
|
+
}
|
|
181
|
+
: state;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { VZAction, VZState } from '.';
|
|
2
|
+
import { Pane } from '../../types';
|
|
3
|
+
import { findPane } from './findPane';
|
|
4
|
+
import { updatePane } from './updatePane';
|
|
5
|
+
|
|
6
|
+
export const setActiveFileIdReducer = (
|
|
7
|
+
state: VZState,
|
|
8
|
+
action: VZAction,
|
|
9
|
+
): VZState => {
|
|
10
|
+
if (action.type !== 'set_active_file_id') return state;
|
|
11
|
+
|
|
12
|
+
const { pane, activePaneId } = state;
|
|
13
|
+
const activePane: Pane = findPane(pane, activePaneId);
|
|
14
|
+
|
|
15
|
+
if (activePane.type !== 'leafPane') {
|
|
16
|
+
throw new Error(
|
|
17
|
+
'closeTabsReducer: activePane is not a leafPane',
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
...state,
|
|
22
|
+
pane: updatePane({
|
|
23
|
+
pane,
|
|
24
|
+
activePaneId,
|
|
25
|
+
newActiveFileId: action.activeFileId,
|
|
26
|
+
}),
|
|
27
|
+
};
|
|
28
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { VZAction, VZState } from '.';
|
|
2
|
+
import { Pane } from '../../types';
|
|
3
|
+
import { findPane } from './findPane';
|
|
4
|
+
import { updatePane } from './updatePane';
|
|
5
|
+
|
|
6
|
+
export const setActiveFileLeftReducer = (
|
|
7
|
+
state: VZState,
|
|
8
|
+
action: VZAction,
|
|
9
|
+
): VZState => {
|
|
10
|
+
if (action.type !== 'set_active_file_left') {
|
|
11
|
+
return state;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const { pane, activePaneId } = state;
|
|
15
|
+
const activePane: Pane = findPane(pane, activePaneId);
|
|
16
|
+
|
|
17
|
+
if (activePane.type !== 'leafPane') {
|
|
18
|
+
throw new Error(
|
|
19
|
+
'closeTabsReducer: activePane is not a leafPane',
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let index = activePane.tabList.findIndex(
|
|
24
|
+
(tab) => tab.fileId === activePane.activeFileId,
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
index -= 1;
|
|
28
|
+
if (index < 0) {
|
|
29
|
+
index = activePane.tabList.length - 1;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
...state,
|
|
34
|
+
pane: updatePane({
|
|
35
|
+
pane,
|
|
36
|
+
activePaneId,
|
|
37
|
+
newActiveFileId: activePane.tabList[index].fileId,
|
|
38
|
+
}),
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const setActiveFileRightReducer = (
|
|
43
|
+
state: VZState,
|
|
44
|
+
action: VZAction,
|
|
45
|
+
): VZState => {
|
|
46
|
+
if (action.type !== 'set_active_file_right') {
|
|
47
|
+
return state;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const { pane, activePaneId } = state;
|
|
51
|
+
const activePane: Pane = findPane(pane, activePaneId);
|
|
52
|
+
|
|
53
|
+
if (activePane.type !== 'leafPane') {
|
|
54
|
+
throw new Error(
|
|
55
|
+
'closeTabsReducer: activePane is not a leafPane',
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let index = activePane.tabList.findIndex(
|
|
60
|
+
(tab) => tab.fileId === activePane.activeFileId,
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
index += 1;
|
|
64
|
+
if (index > activePane.tabList.length - 1) {
|
|
65
|
+
index = 0;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
...state,
|
|
70
|
+
pane: updatePane({
|
|
71
|
+
pane,
|
|
72
|
+
activePaneId,
|
|
73
|
+
newActiveFileId: activePane.tabList[index].fileId,
|
|
74
|
+
}),
|
|
75
|
+
};
|
|
76
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { PaneId, Pane } from '../../types';
|
|
2
|
+
import { VZAction, VZState } from '.';
|
|
3
|
+
import { updatePane } from './updatePane';
|
|
4
|
+
|
|
5
|
+
export const splitCurrentPaneReducer = (
|
|
6
|
+
state: VZState,
|
|
7
|
+
action: VZAction,
|
|
8
|
+
): VZState => {
|
|
9
|
+
// console.log('splitCurrentPaneReducer');
|
|
10
|
+
if (action.type !== 'split_current_pane') {
|
|
11
|
+
return state;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const activePaneId = state.activePaneId;
|
|
15
|
+
|
|
16
|
+
const newLeafPane1: Pane = {
|
|
17
|
+
id: `${activePaneId}-1`, // Generate unique IDs for new panes
|
|
18
|
+
type: 'leafPane',
|
|
19
|
+
tabList: [],
|
|
20
|
+
activeFileId: null,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const newLeafPane2: Pane = {
|
|
24
|
+
id: `${activePaneId}-2`,
|
|
25
|
+
type: 'leafPane',
|
|
26
|
+
tabList: [],
|
|
27
|
+
activeFileId: null,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const newSplitPane: Pane = {
|
|
31
|
+
id: activePaneId,
|
|
32
|
+
type: 'splitPane',
|
|
33
|
+
orientation: 'horizontal',
|
|
34
|
+
children: [newLeafPane1, newLeafPane2],
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const updatedPane = updatePane({
|
|
38
|
+
pane: state.pane,
|
|
39
|
+
activePaneId,
|
|
40
|
+
newTabList: undefined,
|
|
41
|
+
newActiveFileId: undefined,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const updateSplitPane = (pane: Pane, id: PaneId): Pane =>
|
|
45
|
+
pane.id === id
|
|
46
|
+
? newSplitPane
|
|
47
|
+
: pane.type === 'splitPane'
|
|
48
|
+
? {
|
|
49
|
+
...pane,
|
|
50
|
+
children: pane.children.map((child) =>
|
|
51
|
+
updateSplitPane(child, id),
|
|
52
|
+
),
|
|
53
|
+
}
|
|
54
|
+
: pane;
|
|
55
|
+
|
|
56
|
+
const finalPane = updateSplitPane(
|
|
57
|
+
updatedPane,
|
|
58
|
+
activePaneId,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
...state,
|
|
63
|
+
pane: finalPane,
|
|
64
|
+
activePaneId: newLeafPane1.id,
|
|
65
|
+
};
|
|
66
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { VZAction, VZState } from '.';
|
|
2
|
+
|
|
3
|
+
export const toggleAutoFollowReducer = (
|
|
4
|
+
state: VZState,
|
|
5
|
+
action: VZAction,
|
|
6
|
+
): VZState =>
|
|
7
|
+
action.type === 'toggle_auto_follow'
|
|
8
|
+
? {
|
|
9
|
+
...state,
|
|
10
|
+
enableAutoFollow: !state.enableAutoFollow,
|
|
11
|
+
}
|
|
12
|
+
: state;
|