@lvce-editor/text-search-worker 2.2.0 → 2.3.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/README.md CHANGED
@@ -1,3 +1,3 @@
1
- # text search worker
1
+ # Text Search Worker
2
2
 
3
3
  Web Worker for the text search functionality in LVCE Editor.
@@ -18,6 +18,45 @@ const execute = (command, ...args) => {
18
18
  return fn(...args);
19
19
  };
20
20
 
21
+ const ListItem = 22;
22
+
23
+ const User = 1;
24
+ const Script = 2;
25
+
26
+ const minimumSliderSize = 20;
27
+
28
+ const states = Object.create(null);
29
+ const get$2 = uid => {
30
+ return states[uid];
31
+ };
32
+ const set$2 = (uid, state) => {
33
+ states[uid] = state;
34
+ };
35
+
36
+ const create$5 = ({
37
+ itemHeight,
38
+ headerHeight = 0,
39
+ minimumSliderSize = 20
40
+ }) => {
41
+ return {
42
+ deltaY: 0,
43
+ minLineY: 0,
44
+ maxLineY: 0,
45
+ finalDeltaY: 0,
46
+ itemHeight,
47
+ headerHeight,
48
+ items: [],
49
+ minimumSliderSize,
50
+ focusedIndex: -1,
51
+ touchOffsetY: 0,
52
+ touchTimeStamp: 0,
53
+ touchDifference: 0,
54
+ scrollBarHeight: 0,
55
+ scrollBarActive: false
56
+ };
57
+ };
58
+
59
+ const Empty$1 = 0;
21
60
  const FocusSearchInput = 21;
22
61
  const FocusSearchReplaceInput = 31;
23
62
  const FocusSearchMatchCase = 32;
@@ -26,6 +65,46 @@ const FocusSearchWholeWord = 34;
26
65
  const FocusSearchReplaceAll = 35;
27
66
  const FocusSearchPreserveCase = 36;
28
67
 
68
+ const create$4 = (uid, x, y, width, height, workspacePath) => {
69
+ const state = {
70
+ uid,
71
+ searchResults: [],
72
+ stats: {},
73
+ searchId: -1,
74
+ value: '',
75
+ disposed: false,
76
+ fileCount: 0,
77
+ x,
78
+ y,
79
+ width,
80
+ height,
81
+ ...create$5({
82
+ itemHeight: ListItem,
83
+ minimumSliderSize: minimumSliderSize,
84
+ headerHeight: 61 // TODO
85
+ }),
86
+ threads: 0,
87
+ replaceExpanded: false,
88
+ useRegularExpression: false,
89
+ matchCase: false,
90
+ matchWholeWord: false,
91
+ replacement: '',
92
+ matchCount: 0,
93
+ listFocused: false,
94
+ listFocusedIndex: -1,
95
+ inputSource: User,
96
+ workspacePath: workspacePath,
97
+ includeValue: '',
98
+ excludeValue: '',
99
+ detailsExpanded: false,
100
+ focus: Empty$1,
101
+ loaded: false,
102
+ message: '',
103
+ collapsedPaths: []
104
+ };
105
+ set$2(uid, state);
106
+ };
107
+
29
108
  const focusMatchCase$1 = state => {
30
109
  return {
31
110
  ...state,
@@ -80,7 +159,7 @@ const MaskIcon = 'MaskIcon';
80
159
  const MaskIconChevronDown = 'MaskIconChevronDown';
81
160
  const MaskIconChevronRight = 'MaskIconChevronRight';
82
161
  const MultilineInputBox = 'MultilineInputBox';
83
- const Search = 'Search';
162
+ const Search$1 = 'Search';
84
163
  const SearchField = 'SearchField';
85
164
  const SearchFieldButtons = 'SearchFieldButtons';
86
165
  const SearchFieldContainer = 'SearchFieldContainer';
@@ -182,6 +261,12 @@ const getType$1 = value => {
182
261
  return 'unknown';
183
262
  }
184
263
  };
264
+ const number$1 = value => {
265
+ const type = getType$1(value);
266
+ if (type !== 'number') {
267
+ throw new AssertionError$1('expected value to be of type number');
268
+ }
269
+ };
185
270
  const string = value => {
186
271
  const type = getType$1(value);
187
272
  if (type !== 'string') {
@@ -230,6 +315,23 @@ const UiStrings = {
230
315
  ViewAsTree: 'View as Tree',
231
316
  CollapseAll: 'Collapse All'
232
317
  };
318
+ const noResults = () => {
319
+ return i18nString(UiStrings.NoResults);
320
+ };
321
+ const oneResult = () => {
322
+ return i18nString(UiStrings.Oneresult);
323
+ };
324
+ const manyResultsInOneFile = resultCount => {
325
+ return i18nString(UiStrings.ManyResultsInOneFile, {
326
+ PH1: resultCount
327
+ });
328
+ };
329
+ const manyResultsInManyFiles = (resultCount, fileResultCount) => {
330
+ return i18nString(UiStrings.ManyResultsInManyFiles, {
331
+ PH1: resultCount,
332
+ PH2: fileResultCount
333
+ });
334
+ };
233
335
  const refresh = () => {
234
336
  return i18nString(UiStrings.Refresh);
235
337
  };
@@ -671,7 +773,7 @@ const getSearchVirtualDom = (visibleItems, replaceExpanded, matchCase, matchWhol
671
773
  const dom = [];
672
774
  dom.push({
673
775
  type: Div,
674
- className: mergeClassNames(Viewlet, Search),
776
+ className: mergeClassNames(Viewlet, Search$1),
675
777
  childCount: 2
676
778
  });
677
779
  dom.push(...getSearchHeaderVirtualDom(replaceExpanded, matchCase, matchWholeWord, useRegularExpression, detailsExpanded));
@@ -695,6 +797,76 @@ const getSearchVirtualDom = (visibleItems, replaceExpanded, matchCase, matchWhol
695
797
  return dom;
696
798
  };
697
799
 
800
+ const handleIconThemeChange = state => {
801
+ const {
802
+ items
803
+ } = state;
804
+ const newItems = [...items];
805
+ return {
806
+ ...state,
807
+ items: newItems
808
+ };
809
+ };
810
+
811
+ const handleError = async (error, notify = true, prefix = '') => {
812
+ console.error(`[text-search-worker] ${error}`);
813
+ };
814
+
815
+ // TODO optimize this function to return the minimum number
816
+ // of visible items needed, e.g. when not scrolled 5 items with
817
+ // 20px fill 100px but when scrolled 6 items are needed
818
+ const getNumberOfVisibleItems = (listHeight, itemHeight) => {
819
+ return Math.ceil(listHeight / itemHeight) + 1;
820
+ };
821
+
822
+ const getTextSearchResultCounts = results => {
823
+ let resultCount = 0;
824
+ let fileCount = 0;
825
+ for (const result of results) {
826
+ switch (result.type) {
827
+ case File$1:
828
+ fileCount++;
829
+ break;
830
+ case Match:
831
+ resultCount++;
832
+ break;
833
+ }
834
+ }
835
+ return {
836
+ fileCount,
837
+ resultCount
838
+ };
839
+ };
840
+
841
+ const EmptyString = '';
842
+
843
+ const isEmptyString = string => {
844
+ return string === EmptyString;
845
+ };
846
+
847
+ /**
848
+ *
849
+ * @param {number} size
850
+ * @param {number} contentSize
851
+ * @param {number} minimumSliderSize
852
+ * @returns
853
+ */
854
+ const getScrollBarSize = (size, contentSize, minimumSliderSize) => {
855
+ if (size >= contentSize) {
856
+ return 0;
857
+ }
858
+ return Math.max(Math.round(size ** 2 / contentSize), minimumSliderSize);
859
+ };
860
+
861
+ const RE_PROTOCOL = /^([a-z\-]+):\/\//;
862
+ const getProtocol = uri => {
863
+ const protocolMatch = uri.match(RE_PROTOCOL);
864
+ if (protocolMatch) {
865
+ return protocolMatch[1];
866
+ }
867
+ return '';
868
+ };
869
+
698
870
  const state$3 = {
699
871
  /**
700
872
  * @type {any}
@@ -1071,82 +1243,6 @@ const listen$2 = ipc => {
1071
1243
  set$1(ipc);
1072
1244
  };
1073
1245
 
1074
- const focusSearchValue = state => {
1075
- return {
1076
- ...state,
1077
- focus: FocusSearchInput
1078
- };
1079
- };
1080
- const focusSearchValueNext = state => {
1081
- const {
1082
- replaceExpanded
1083
- } = state;
1084
- if (replaceExpanded) {
1085
- return focusReplaceValue(state);
1086
- }
1087
- return focusMatchCase(state);
1088
- };
1089
- const focusMatchCasePrevious = state => {
1090
- const {
1091
- replaceExpanded
1092
- } = state;
1093
- if (replaceExpanded) {
1094
- return focusReplaceValue(state);
1095
- }
1096
- return focusSearchValue(state);
1097
- };
1098
- const focusReplaceValueNext = state => {
1099
- return focusMatchCase(state);
1100
- };
1101
- const focusRegexNext = state => {
1102
- return focusPreserveCase(state);
1103
- };
1104
- const focusReplaceValue = state => {
1105
- return {
1106
- ...state,
1107
- focus: FocusSearchReplaceInput
1108
- };
1109
- };
1110
- const focusMatchCase = state => {
1111
- return {
1112
- ...state,
1113
- focus: FocusSearchMatchCase
1114
- };
1115
- };
1116
- const focusPreserveCase = state => {
1117
- return {
1118
- ...state,
1119
- focus: FocusSearchPreserveCase
1120
- };
1121
- };
1122
- const focusMatchWholeWord = state => {
1123
- return {
1124
- ...state,
1125
- focus: FocusSearchWholeWord
1126
- };
1127
- };
1128
- const focusRegex = state => {
1129
- return {
1130
- ...state,
1131
- focus: FocusSearchRegex
1132
- };
1133
- };
1134
- const focusReplaceAll = state => {
1135
- return {
1136
- ...state,
1137
- focus: FocusSearchReplaceAll
1138
- };
1139
- };
1140
-
1141
- const RE_PROTOCOL = /^([a-z\-]+):\/\//;
1142
- const getProtocol = uri => {
1143
- const protocolMatch = uri.match(RE_PROTOCOL);
1144
- if (protocolMatch) {
1145
- return protocolMatch[1];
1146
- }
1147
- return '';
1148
- };
1149
-
1150
1246
  const textSearch$5 = async (scheme, root, query) => {
1151
1247
  string(scheme);
1152
1248
  string(query);
@@ -1785,11 +1881,305 @@ const textSearch = async (root, query, options, assetDir) => {
1785
1881
  return results;
1786
1882
  };
1787
1883
 
1884
+ const getStatusMessage = (resultCount, fileResultCount) => {
1885
+ number$1(resultCount);
1886
+ number$1(fileResultCount);
1887
+ if (resultCount === 0) {
1888
+ return noResults();
1889
+ }
1890
+ if (resultCount === 1) {
1891
+ return oneResult();
1892
+ }
1893
+ if (fileResultCount === 1) {
1894
+ return manyResultsInOneFile(resultCount);
1895
+ }
1896
+ return manyResultsInManyFiles(resultCount, fileResultCount);
1897
+ };
1898
+
1899
+ const handleUpdate = async (state, update) => {
1900
+ const partialNewState = {
1901
+ ...state,
1902
+ ...update
1903
+ };
1904
+ try {
1905
+ const {
1906
+ height,
1907
+ itemHeight,
1908
+ minimumSliderSize,
1909
+ headerHeight,
1910
+ matchCase,
1911
+ value,
1912
+ threads,
1913
+ useRegularExpression
1914
+ } = partialNewState;
1915
+ if (isEmptyString(value)) {
1916
+ return {
1917
+ ...partialNewState,
1918
+ minLineY: 0,
1919
+ maxLineY: 0,
1920
+ deltaY: 0,
1921
+ items: [],
1922
+ matchIndex: 0,
1923
+ matchCount: 0,
1924
+ message: '',
1925
+ loaded: true
1926
+ };
1927
+ }
1928
+ const root = state.workspacePath;
1929
+ const results = await textSearch(root, value, {
1930
+ threads,
1931
+ isCaseSensitive: matchCase,
1932
+ useRegularExpression
1933
+ }, state.assetDir);
1934
+ if (!Array.isArray(results)) {
1935
+ throw new TypeError('results must be of type array');
1936
+ }
1937
+ const {
1938
+ fileCount,
1939
+ resultCount
1940
+ } = getTextSearchResultCounts(results);
1941
+ const message = getStatusMessage(resultCount, fileCount);
1942
+ const total = results.length;
1943
+ const contentHeight = total * itemHeight;
1944
+ const listHeight = height - headerHeight;
1945
+ const scrollBarHeight = getScrollBarSize(height, contentHeight, minimumSliderSize);
1946
+ const numberOfVisible = getNumberOfVisibleItems(listHeight, itemHeight);
1947
+ const maxLineY = Math.min(numberOfVisible, total);
1948
+ const finalDeltaY = Math.max(contentHeight - listHeight, 0);
1949
+ return {
1950
+ ...partialNewState,
1951
+ minLineY: 0,
1952
+ deltaY: 0,
1953
+ value,
1954
+ items: results,
1955
+ message,
1956
+ maxLineY: maxLineY,
1957
+ scrollBarHeight,
1958
+ finalDeltaY,
1959
+ threads,
1960
+ fileCount,
1961
+ matchCount: resultCount,
1962
+ loaded: true
1963
+ };
1964
+ } catch (error) {
1965
+ handleError(error);
1966
+ return {
1967
+ ...partialNewState,
1968
+ message: `${error}`,
1969
+ items: [],
1970
+ matchCount: 0,
1971
+ fileCount: 0,
1972
+ minLineY: 0,
1973
+ maxLineY: 0
1974
+ };
1975
+ }
1976
+ };
1977
+
1978
+ const getSavedValue = savedState => {
1979
+ if (savedState && savedState.value) {
1980
+ return savedState.value;
1981
+ }
1982
+ return '';
1983
+ };
1984
+ const getSavedReplaceExpanded = savedState => {
1985
+ if (savedState && 'replaceExpanded' in savedState) {
1986
+ return savedState.replaceExpanded;
1987
+ }
1988
+ return false;
1989
+ };
1990
+ const getSavedCollapsedPaths = savedState => {
1991
+ if (savedState && 'collapsedPaths' in savedState && Array.isArray(savedState.collapsedPaths) && savedState.collapsedPaths.every(path => typeof path === 'string')) {
1992
+ return savedState.collapsedPaths;
1993
+ }
1994
+ return [];
1995
+ };
1996
+ const getThreads = () => {
1997
+ return 1;
1998
+ };
1999
+ const loadContent = async (uid, savedState) => {
2000
+ const state = get$2(uid);
2001
+ const savedValue = getSavedValue(savedState);
2002
+ const savedReplaceExpanded = getSavedReplaceExpanded(savedState);
2003
+ const savedCollapsedPaths = getSavedCollapsedPaths(savedState);
2004
+ const threads = getThreads();
2005
+ if (savedValue) {
2006
+ return handleUpdate(state, {
2007
+ value: savedValue,
2008
+ threads,
2009
+ replaceExpanded: savedReplaceExpanded,
2010
+ inputSource: Script,
2011
+ collapsedPaths: savedCollapsedPaths
2012
+ });
2013
+ }
2014
+ return {
2015
+ ...state,
2016
+ threads,
2017
+ replaceExpanded: savedReplaceExpanded,
2018
+ loaded: true
2019
+ };
2020
+ };
2021
+
2022
+ const renderItems = {
2023
+ isEqual(oldState, newState) {
2024
+ return oldState.items === newState.items && oldState.minLineY === newState.minLineY && oldState.maxLineY === newState.maxLineY && oldState.replacement === newState.replacement && oldState.replaceExpanded === newState.replaceExpanded && oldState.matchCase === newState.matchCase && oldState.matchWholeWord === newState.matchWholeWord && oldState.useRegularExpression === newState.useRegularExpression && oldState.message === newState.message && oldState.detailsExpanded === newState.detailsExpanded && oldState.loaded === newState.loaded && oldState.collapsedPaths === newState.collapsedPaths && oldState.listFocusedIndex === newState.listFocusedIndex && oldState.listFocused === newState.listFocused;
2025
+ },
2026
+ apply(oldState, newState) {
2027
+ const displayResults = getDisplayResults(newState.items, newState.itemHeight, newState.fileCount, newState.value, newState.minLineY, newState.maxLineY, newState.replacement, newState.collapsedPaths, newState.focusedIndex);
2028
+ const focusOutline = newState.listFocused && newState.listFocusedIndex === -1;
2029
+ const dom = getSearchVirtualDom(displayResults, newState.replaceExpanded, newState.matchCase, newState.matchWholeWord, newState.useRegularExpression, newState.message, newState.detailsExpanded, focusOutline);
2030
+ return ['Viewlet.setDom2', dom];
2031
+ }
2032
+ };
2033
+ const getSelector = focusKey => {
2034
+ switch (focusKey) {
2035
+ case FocusSearchInput:
2036
+ return '[name="search-value"]';
2037
+ case FocusSearchReplaceInput:
2038
+ return '[name="search-replace-value"]';
2039
+ case FocusSearchMatchCase:
2040
+ return '[title="Match Case"]';
2041
+ case FocusSearchPreserveCase:
2042
+ return '[title="Preserve Case"]';
2043
+ case FocusSearchRegex:
2044
+ return '[title="Use Regular Expression"]';
2045
+ default:
2046
+ return '';
2047
+ }
2048
+ };
2049
+ const renderFocus = {
2050
+ isEqual(oldState, newState) {
2051
+ return oldState.focus === newState.focus;
2052
+ },
2053
+ apply(oldState, newState) {
2054
+ const selector = getSelector(newState.focus);
2055
+ return ['setFocus', selector];
2056
+ }
2057
+ };
2058
+ const render = [renderItems, renderFocus];
2059
+ const doRender = (oldState, newState) => {
2060
+ const commands = [];
2061
+ for (const fn of render) {
2062
+ if (!fn.isEqual(oldState, newState)) {
2063
+ commands.push(fn.apply(oldState, newState));
2064
+ }
2065
+ }
2066
+ return commands;
2067
+ };
2068
+
2069
+ const focusSearchValue = state => {
2070
+ return {
2071
+ ...state,
2072
+ focus: FocusSearchInput
2073
+ };
2074
+ };
2075
+ const focusSearchValueNext = state => {
2076
+ const {
2077
+ replaceExpanded
2078
+ } = state;
2079
+ if (replaceExpanded) {
2080
+ return focusReplaceValue(state);
2081
+ }
2082
+ return focusMatchCase(state);
2083
+ };
2084
+ const focusMatchCasePrevious = state => {
2085
+ const {
2086
+ replaceExpanded
2087
+ } = state;
2088
+ if (replaceExpanded) {
2089
+ return focusReplaceValue(state);
2090
+ }
2091
+ return focusSearchValue(state);
2092
+ };
2093
+ const focusReplaceValueNext = state => {
2094
+ return focusMatchCase(state);
2095
+ };
2096
+ const focusRegexNext = state => {
2097
+ return focusPreserveCase(state);
2098
+ };
2099
+ const focusReplaceValue = state => {
2100
+ return {
2101
+ ...state,
2102
+ focus: FocusSearchReplaceInput
2103
+ };
2104
+ };
2105
+ const focusMatchCase = state => {
2106
+ return {
2107
+ ...state,
2108
+ focus: FocusSearchMatchCase
2109
+ };
2110
+ };
2111
+ const focusPreserveCase = state => {
2112
+ return {
2113
+ ...state,
2114
+ focus: FocusSearchPreserveCase
2115
+ };
2116
+ };
2117
+ const focusMatchWholeWord = state => {
2118
+ return {
2119
+ ...state,
2120
+ focus: FocusSearchWholeWord
2121
+ };
2122
+ };
2123
+ const focusRegex = state => {
2124
+ return {
2125
+ ...state,
2126
+ focus: FocusSearchRegex
2127
+ };
2128
+ };
2129
+ const focusReplaceAll = state => {
2130
+ return {
2131
+ ...state,
2132
+ focus: FocusSearchReplaceAll
2133
+ };
2134
+ };
2135
+
2136
+ const submit = state => {
2137
+ return handleUpdate(state, {
2138
+ value: state.value,
2139
+ inputSource: User
2140
+ });
2141
+ };
2142
+
2143
+ const Keyboard = -1;
2144
+
2145
+ const show = async (x, y, id, ...args) => {
2146
+ // TODO ask renderer worker to show context menu
2147
+ number$1(x);
2148
+ number$1(y);
2149
+ number$1(id);
2150
+ // TODO
2151
+ };
2152
+
2153
+ const Search = 18;
2154
+
2155
+ const handleContextMenuKeyboard = async state => {
2156
+ const x = state.x; // TODO
2157
+ const y = state.y; // TODO
2158
+ await show(x, y, Search);
2159
+ return state;
2160
+ };
2161
+
2162
+ const handleContextMenuMouseAt = async (state, x, y) => {
2163
+ await show(x, y, Search);
2164
+ return state;
2165
+ };
2166
+
2167
+ const handleContextMenu = (state, button, x, y) => {
2168
+ switch (button) {
2169
+ case Keyboard:
2170
+ return handleContextMenuKeyboard(state);
2171
+ default:
2172
+ return handleContextMenuMouseAt(state, x, y);
2173
+ }
2174
+ };
2175
+
1788
2176
  const commandMap = {
1789
2177
  'SearchView.focusMatchCase': focusMatchCase$1,
1790
2178
  'SearchView.focusReplaceValue': focusReplaceValue$1,
1791
2179
  'SearchView.focusSearchValue': focusSearchValue$1,
1792
2180
  'SearchView.focusSearchValueNext': focusSearchValueNext$1,
2181
+ 'TextSearch.create': create$4,
2182
+ 'TextSearch.doRender': doRender,
1793
2183
  'TextSearch.focusMatchCase': focusMatchCase,
1794
2184
  'TextSearch.focusMatchCasePrevious': focusMatchCasePrevious,
1795
2185
  'TextSearch.focusMatchWholeWord': focusMatchWholeWord,
@@ -1805,6 +2195,10 @@ const commandMap = {
1805
2195
  'TextSearch.getActionsVirtualDom': getActionsVirtualDom,
1806
2196
  'TextSearch.getSearchDisplayResults': getDisplayResults,
1807
2197
  'TextSearch.getVirtualDom': getSearchVirtualDom,
2198
+ 'TextSearch.handleContextMenu': handleContextMenu,
2199
+ 'TextSearch.handleIconThemeChange': handleIconThemeChange,
2200
+ 'TextSearch.loadContent': loadContent,
2201
+ 'TextSearch.submit': submit,
1808
2202
  'TextSearch.textSearch': textSearch
1809
2203
  };
1810
2204
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/text-search-worker",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "",
5
5
  "main": "dist/textSearchWorkerMain.js",
6
6
  "type": "module",