vzcode 1.5.0 → 1.6.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.
@@ -0,0 +1,114 @@
1
+ import { VZAction, VZState } from '.';
2
+ import {
3
+ SearchFile,
4
+ SearchFileVisibility,
5
+ SearchResult,
6
+ ShareDBDoc,
7
+ VZCodeContent,
8
+ } from '../../types';
9
+
10
+ function searchPattern(
11
+ shareDBDoc: ShareDBDoc<VZCodeContent>,
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].indexOf(pattern);
28
+
29
+ if (index !== -1) {
30
+ matches.push({
31
+ line: j + 1,
32
+ index: index,
33
+ text: lines[j],
34
+ });
35
+ }
36
+ }
37
+
38
+ if (matches.length > 0) {
39
+ results[fileIds[i]] = {
40
+ name: fileName,
41
+ matches: matches,
42
+ visibility: 'open',
43
+ };
44
+ }
45
+ }
46
+ }
47
+
48
+ return results;
49
+ }
50
+
51
+ function updateSearchFileVisibility(
52
+ results: SearchResult,
53
+ id: string,
54
+ visibility: SearchFileVisibility,
55
+ ): SearchResult {
56
+ return {
57
+ ...results,
58
+ [id]: { ...results[id], visibility: visibility },
59
+ };
60
+ }
61
+
62
+ export const setIsSearchOpenReducer = (
63
+ state: VZState,
64
+ action: VZAction,
65
+ ): VZState =>
66
+ action.type === 'set_is_search_open'
67
+ ? { ...state, isSearchOpen: action.value }
68
+ : state;
69
+
70
+ export const setSearchReducer = (
71
+ state: VZState,
72
+ action: VZAction,
73
+ ): VZState =>
74
+ action.type === 'set_search'
75
+ ? {
76
+ ...state,
77
+ search: { pattern: action.value, results: {} },
78
+ }
79
+ : state;
80
+
81
+ export const setSearchResultsReducer = (
82
+ state: VZState,
83
+ action: VZAction,
84
+ ): VZState =>
85
+ action.type === 'set_search_results'
86
+ ? {
87
+ ...state,
88
+ search: {
89
+ pattern: state.search.pattern,
90
+ results: searchPattern(
91
+ action.files,
92
+ state.search.pattern,
93
+ ),
94
+ },
95
+ }
96
+ : state;
97
+
98
+ export const setSearchFileVisibilityReducer = (
99
+ state: VZState,
100
+ action: VZAction,
101
+ ): VZState =>
102
+ action.type === 'set_search_file_visibility'
103
+ ? {
104
+ ...state,
105
+ search: {
106
+ pattern: state.search.pattern,
107
+ results: updateSearchFileVisibility(
108
+ state.search.results,
109
+ action.id,
110
+ action.visibility,
111
+ ),
112
+ },
113
+ }
114
+ : state;
package/src/types.ts CHANGED
@@ -61,6 +61,26 @@ export interface FileTreeFile {
61
61
  file: File;
62
62
  fileId: FileId;
63
63
  }
64
+ export type SearchMatch = Array<{
65
+ line: number;
66
+ index: number;
67
+ text: string;
68
+ }>;
69
+ export type SearchFileVisibility =
70
+ | 'open'
71
+ | 'flattened'
72
+ | 'closed';
73
+ export type SearchResult = { [id: string]: SearchFile };
74
+
75
+ export interface SearchFile {
76
+ name: string;
77
+ matches: SearchMatch;
78
+ visibility: SearchFileVisibility;
79
+ }
80
+ export interface SearchResults {
81
+ pattern: string;
82
+ results: SearchResult;
83
+ }
64
84
 
65
85
  export type JSONOp = any;
66
86