@lvce-editor/text-search-worker 3.25.0 → 3.27.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,16 @@
1
1
  # Text Search Worker
2
2
 
3
3
  Web Worker for the text search functionality in LVCE Editor.
4
+
5
+ ## Contributing
6
+
7
+ ```sh
8
+ git clone git@github.com:lvce-editor/text-search-worker.git &&
9
+ cd text-search-worker &&
10
+ npm ci &&
11
+ npm test
12
+ ```
13
+
14
+ ## Gitpod
15
+
16
+ [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/lvce-editor/text-search-worker)
@@ -1673,7 +1673,6 @@ const Highlight = 'Highlight';
1673
1673
  const HighlightDeleted = 'HighlightDeleted';
1674
1674
  const HighlightInserted = 'HighlightInserted';
1675
1675
  const IconButton = 'IconButton';
1676
- const SearchInputError = 'SearchInputError';
1677
1676
  const Label = 'Label';
1678
1677
  const List = 'List';
1679
1678
  const MaskIcon = 'MaskIcon';
@@ -1694,6 +1693,7 @@ const SearchFieldButton = 'SearchFieldButton';
1694
1693
  const SearchFieldButtonChecked = 'SearchFieldButtonChecked';
1695
1694
  const SearchFieldButtons = 'SearchFieldButtons';
1696
1695
  const SearchFieldContainer = 'SearchFieldContainer';
1696
+ const SearchFieldError = 'SearchFieldError';
1697
1697
  const SearchHeader = 'SearchHeader';
1698
1698
  const SearchHeaderDetails = 'SearchHeaderDetails';
1699
1699
  const SearchHeaderDetailsExpanded = 'SearchHeaderDetailsExpanded';
@@ -1701,6 +1701,7 @@ const SearchHeaderDetailsExpandedTop = 'SearchHeaderDetailsExpandedTop';
1701
1701
  const SearchHeaderDetailsHeading = 'SearchHeaderDetailsHeading';
1702
1702
  const SearchHeaderTop = 'SearchHeaderTop';
1703
1703
  const SearchHeaderTopRight = 'SearchHeaderTopRight';
1704
+ const SearchInputError = 'SearchInputError';
1704
1705
  const SearchRemove = 'SearchRemove';
1705
1706
  const SearchToggleButton = 'SearchToggleButton';
1706
1707
  const SearchToggleButtonExpanded = 'SearchToggleButtonExpanded';
@@ -1890,7 +1891,7 @@ const getAbsolutePath = relativePath => {
1890
1891
  return `/${relativePath}`; // TODO support windows paths
1891
1892
  };
1892
1893
 
1893
- const getDisplayResult = (result, fileIcons, fileIconIndex, itemHeight, i, setSize, searchTermLength, replacement, focusedIndex) => {
1894
+ const getDisplayResult = (result, fileIcons, fileIconIndex, itemHeight, i, setSize, searchTermLength, replacement, focusedIndex, collapsedPaths) => {
1894
1895
  const {
1895
1896
  type,
1896
1897
  text,
@@ -1919,7 +1920,8 @@ const getDisplayResult = (result, fileIcons, fileIconIndex, itemHeight, i, setSi
1919
1920
  replacement: '',
1920
1921
  depth: 0,
1921
1922
  matchCount: 0,
1922
- focused
1923
+ focused,
1924
+ expanded: !collapsedPaths.includes(path)
1923
1925
  };
1924
1926
  case Match:
1925
1927
  return {
@@ -1936,14 +1938,15 @@ const getDisplayResult = (result, fileIcons, fileIconIndex, itemHeight, i, setSi
1936
1938
  replacement,
1937
1939
  depth: 1,
1938
1940
  matchCount: 0,
1939
- focused
1941
+ focused,
1942
+ expanded: false
1940
1943
  };
1941
1944
  default:
1942
1945
  throw new Error('unexpected search result type');
1943
1946
  }
1944
1947
  };
1945
1948
 
1946
- const getDisplayResults = (results, itemHeight, resultCount, searchTerm, minLineY, maxLineY, replacement, fileIcons, focusedIndex) => {
1949
+ const getDisplayResults = (results, itemHeight, resultCount, searchTerm, minLineY, maxLineY, replacement, fileIcons, focusedIndex, collapsedPaths) => {
1947
1950
  const displayResults = [];
1948
1951
  const setSize = resultCount;
1949
1952
  const searchTermLength = searchTerm.length;
@@ -1953,7 +1956,7 @@ const getDisplayResults = (results, itemHeight, resultCount, searchTerm, minLine
1953
1956
  let fileIconIndex = 0;
1954
1957
  for (let i = minLineY; i < maxLineY; i++) {
1955
1958
  const result = results[i];
1956
- const displayResult = getDisplayResult(result, fileIcons, fileIconIndex, itemHeight, i, setSize, searchTermLength, replacement, focusedIndex);
1959
+ const displayResult = getDisplayResult(result, fileIcons, fileIconIndex, itemHeight, i, setSize, searchTermLength, replacement, focusedIndex, collapsedPaths);
1957
1960
  displayResults.push(displayResult);
1958
1961
  if (result.type === File) {
1959
1962
  fileResult = displayResult;
@@ -2101,14 +2104,16 @@ const getOutSideButtonsDom = outsideButtons => {
2101
2104
  postNodes: outsideButtons.flatMap(getSearchFieldButtonVirtualDom)
2102
2105
  };
2103
2106
  };
2104
- const getSearchFieldVirtualDom = (name, placeholder, onInput, insideButtons, outsideButtons, onFocus = '') => {
2107
+
2108
+ const getSearchFieldVirtualDom = (name, placeholder, onInput, insideButtons, outsideButtons, onFocus = '', hasError = false) => {
2105
2109
  const {
2106
2110
  preNodes,
2107
2111
  postNodes
2108
2112
  } = getOutSideButtonsDom(outsideButtons);
2113
+ const searchFieldClassName = hasError ? mergeClassNames(SearchField, SearchFieldError) : SearchField;
2109
2114
  const dom = [...preNodes, {
2110
2115
  type: Div,
2111
- className: SearchField,
2116
+ className: searchFieldClassName,
2112
2117
  role: None,
2113
2118
  childCount: 2
2114
2119
  }, {
@@ -2219,7 +2224,7 @@ const getSearchToggleVirtualDom = flags => {
2219
2224
  }];
2220
2225
  };
2221
2226
 
2222
- const getSearchHeaderTopVirtualDom = flags => {
2227
+ const getSearchHeaderTopVirtualDom = (flags, searchInputErrorMessage = '') => {
2223
2228
  const inputActions = getInputActionsInput(flags);
2224
2229
  const replaceActions = getInputActionsReplace(flags);
2225
2230
  const dom = [{
@@ -2232,14 +2237,14 @@ const getSearchHeaderTopVirtualDom = flags => {
2232
2237
  className: SearchHeaderTopRight,
2233
2238
  role: None,
2234
2239
  childCount: flags & ReplaceExpanded ? 2 : 1
2235
- }, ...getSearchFieldVirtualDom(SearchValue, 'Search', HandleInput, inputActions.inside, inputActions.outside)];
2240
+ }, ...getSearchFieldVirtualDom(SearchValue, 'Search', HandleInput, inputActions.inside, inputActions.outside, '', Boolean(searchInputErrorMessage))];
2236
2241
  if (flags & ReplaceExpanded) {
2237
2242
  dom.push(...getSearchFieldVirtualDom(ReplaceValue, 'Replace', HandleReplaceInput, replaceActions.inside, replaceActions.outside));
2238
2243
  }
2239
2244
  return dom;
2240
2245
  };
2241
2246
 
2242
- const getSearchHeaderVirtualDom = (flags, message) => {
2247
+ const getSearchHeaderVirtualDom = (flags, message, searchInputErrorMessage) => {
2243
2248
  const dom = [{
2244
2249
  type: Div,
2245
2250
  className: SearchHeader,
@@ -2247,7 +2252,7 @@ const getSearchHeaderVirtualDom = (flags, message) => {
2247
2252
  childCount: 2,
2248
2253
  onClick: HandleHeaderClick,
2249
2254
  onFocusIn: HandleHeaderFocusIn
2250
- }, ...getSearchHeaderTopVirtualDom(flags), ...getSearchHeaderDetailsVirtualDom(flags, message)];
2255
+ }, ...getSearchHeaderTopVirtualDom(flags, searchInputErrorMessage), ...getSearchHeaderDetailsVirtualDom(flags, message)];
2251
2256
  return dom;
2252
2257
  };
2253
2258
 
@@ -2276,11 +2281,11 @@ const chevronDownVirtualDom = {
2276
2281
  className: mergeClassNames(Chevron, MaskIconChevronDown),
2277
2282
  childCount: 0
2278
2283
  };
2279
- ({
2284
+ const chevronRightVirtualDom = {
2280
2285
  type: Div,
2281
2286
  className: mergeClassNames(Chevron, MaskIconChevronRight),
2282
2287
  childCount: 0
2283
- });
2288
+ };
2284
2289
 
2285
2290
  const getFileIconVirtualDom = icon => {
2286
2291
  return {
@@ -2348,7 +2353,8 @@ const getSearchResultVirtualDom = rowInfo => {
2348
2353
  depth,
2349
2354
  replacement,
2350
2355
  matchCount,
2351
- focused
2356
+ focused,
2357
+ expanded
2352
2358
  } = rowInfo;
2353
2359
  const treeItem = {
2354
2360
  type: Div,
@@ -2370,14 +2376,18 @@ const getSearchResultVirtualDom = rowInfo => {
2370
2376
  }
2371
2377
  switch (type) {
2372
2378
  case File:
2373
- treeItem.ariaExpanded = 'true';
2379
+ treeItem.ariaExpanded = `${expanded}`;
2374
2380
  break;
2375
2381
  }
2376
2382
  const dom = [];
2377
2383
  dom.push(treeItem);
2378
2384
  if (type === File) {
2379
2385
  treeItem.childCount += 2;
2380
- dom.push(chevronDownVirtualDom, getFileIconVirtualDom(icon));
2386
+ if (expanded) {
2387
+ dom.push(chevronDownVirtualDom, getFileIconVirtualDom(icon));
2388
+ } else {
2389
+ dom.push(chevronRightVirtualDom, getFileIconVirtualDom(icon));
2390
+ }
2381
2391
  }
2382
2392
  dom.push(...getLabelVirtualDom(displayText, matchLength, matchStart, replacement));
2383
2393
  if (matchCount) {
@@ -2416,7 +2426,7 @@ const getSearchVirtualDom = (visibleItems, flags, message, focusOutline, searchI
2416
2426
  type: Div,
2417
2427
  className: mergeClassNames(Viewlet, Search$1),
2418
2428
  childCount
2419
- }, ...getSearchHeaderVirtualDom(flags, message), ...errorDom, ...getSearchResultsVirtualDom(visibleItems, focusOutline)];
2429
+ }, ...getSearchHeaderVirtualDom(flags, message, searchInputErrorMessage), ...errorDom, ...getSearchResultsVirtualDom(visibleItems, focusOutline)];
2420
2430
  };
2421
2431
 
2422
2432
  const handleError = error => {
@@ -2824,7 +2834,8 @@ const handleUpdate = async (state, update) => {
2824
2834
  fileCount,
2825
2835
  matchCount: resultCount,
2826
2836
  loaded: true,
2827
- icons
2837
+ icons,
2838
+ searchInputErrorMessage: ''
2828
2839
  };
2829
2840
  } catch (error) {
2830
2841
  handleError(error);
@@ -3087,11 +3098,11 @@ const handleSharedInput = (state, name, value, inputSource = Script) => {
3087
3098
  return fn(state, value, inputSource);
3088
3099
  };
3089
3100
 
3090
- const focusIndexScrollUp = (state, index, listHeight, itemHeight, itemsLength) => {
3091
- const newMinLineY = index;
3101
+ const focusIndexScrollDown = (state, index, listHeight, itemHeight, itemsLength) => {
3102
+ const newMaxLineY = Math.min(index + 1, itemsLength);
3092
3103
  const fittingItems = getNumberOfVisibleItems(listHeight, itemHeight);
3093
- const newMaxLineY = Math.min(newMinLineY + fittingItems, itemsLength);
3094
- const newDeltaY = newMinLineY * itemHeight;
3104
+ const newMinLineY = Math.max(newMaxLineY - fittingItems, 0);
3105
+ const newDeltaY = itemsLength < fittingItems ? 0 : newMinLineY * itemHeight - listHeight % itemHeight + itemHeight;
3095
3106
  return {
3096
3107
  ...state,
3097
3108
  focusedIndex: index,
@@ -3101,11 +3112,12 @@ const focusIndexScrollUp = (state, index, listHeight, itemHeight, itemsLength) =
3101
3112
  deltaY: newDeltaY
3102
3113
  };
3103
3114
  };
3104
- const focusIndexScrollDown = (state, index, listHeight, itemHeight, itemsLength) => {
3105
- const newMaxLineY = Math.min(index + 1, itemsLength);
3115
+
3116
+ const focusIndexScrollUp = (state, index, listHeight, itemHeight, itemsLength) => {
3117
+ const newMinLineY = index;
3106
3118
  const fittingItems = getNumberOfVisibleItems(listHeight, itemHeight);
3107
- const newMinLineY = Math.max(newMaxLineY - fittingItems, 0);
3108
- const newDeltaY = itemsLength < fittingItems ? 0 : newMinLineY * itemHeight - listHeight % itemHeight + itemHeight;
3119
+ const newMaxLineY = Math.min(newMinLineY + fittingItems, itemsLength);
3120
+ const newDeltaY = newMinLineY * itemHeight;
3109
3121
  return {
3110
3122
  ...state,
3111
3123
  focusedIndex: index,
@@ -3115,6 +3127,7 @@ const focusIndexScrollDown = (state, index, listHeight, itemHeight, itemsLength)
3115
3127
  deltaY: newDeltaY
3116
3128
  };
3117
3129
  };
3130
+
3118
3131
  const focusIndex = (state, index) => {
3119
3132
  const {
3120
3133
  itemHeight,
@@ -3251,6 +3264,7 @@ const getFilteredResults = (results, collapsedPaths) => {
3251
3264
  let isExcluded = false;
3252
3265
  for (const result of results) {
3253
3266
  if (result.type === File) {
3267
+ filteredResults.push(result);
3254
3268
  if (collapsedPaths.includes(result.text)) {
3255
3269
  isExcluded = true;
3256
3270
  } else {
@@ -3264,25 +3278,36 @@ const getFilteredResults = (results, collapsedPaths) => {
3264
3278
  return filteredResults;
3265
3279
  };
3266
3280
 
3267
- const selectIndexFile = (state, searchResult, index) => {
3281
+ const toggleCollapsedPath = (collapsedPaths, path) => {
3282
+ if (collapsedPaths.includes(path)) {
3283
+ const index = collapsedPaths.indexOf(path);
3284
+ return collapsedPaths.toSpliced(index, 1);
3285
+ }
3286
+ return [...collapsedPaths, path];
3287
+ };
3288
+
3289
+ const selectIndexFile = async (state, searchResult, index) => {
3268
3290
  const {
3269
3291
  collapsedPaths,
3270
3292
  items,
3271
- maxLineY
3293
+ itemHeight,
3294
+ headerHeight,
3295
+ height
3272
3296
  } = state;
3273
- const path = getAbsolutePath(searchResult.text);
3274
- string(path);
3275
- const pathWithDot = `.${path}`;
3276
- const newCollapsedPaths = [...collapsedPaths, path, pathWithDot];
3277
- const filteredResults = getFilteredResults(items, collapsedPaths);
3278
- const newMaxLineY = Math.min(maxLineY, filteredResults.length);
3297
+ const path = searchResult.text;
3298
+ const newCollapsedPaths = toggleCollapsedPath(collapsedPaths, path);
3299
+ const filteredResults = getFilteredResults(items, newCollapsedPaths);
3300
+ const total = filteredResults.length;
3301
+ const listHeight = height - headerHeight;
3302
+ const numberOfVisible = getNumberOfVisibleItems(listHeight, itemHeight);
3303
+ const maxLineY = Math.min(numberOfVisible, total);
3279
3304
  return {
3280
3305
  ...state,
3281
3306
  collapsedPaths: newCollapsedPaths,
3282
3307
  listFocusedIndex: index,
3283
3308
  listFocused: true,
3284
3309
  listItems: filteredResults,
3285
- maxLineY: newMaxLineY
3310
+ maxLineY
3286
3311
  };
3287
3312
  };
3288
3313
 
@@ -3323,6 +3348,17 @@ const selectIndexPreview = async (state, searchResult, index) => {
3323
3348
  };
3324
3349
  };
3325
3350
 
3351
+ const getSelectHandler = type => {
3352
+ switch (type) {
3353
+ case File:
3354
+ return selectIndexFile;
3355
+ case Match:
3356
+ return selectIndexPreview;
3357
+ default:
3358
+ throw new Error(`unexpected search result type ${type}`);
3359
+ }
3360
+ };
3361
+
3326
3362
  const selectIndex = async (state, index) => {
3327
3363
  if (index === -1) {
3328
3364
  return {
@@ -3335,14 +3371,8 @@ const selectIndex = async (state, index) => {
3335
3371
  items
3336
3372
  } = state;
3337
3373
  const searchResult = items[index];
3338
- switch (searchResult.type) {
3339
- case File:
3340
- return selectIndexFile(state, searchResult, index);
3341
- case Match:
3342
- return selectIndexPreview(state, searchResult, index);
3343
- default:
3344
- throw new Error(`unexpected search result type ${searchResult.type}`);
3345
- }
3374
+ const fn = getSelectHandler(searchResult.type);
3375
+ return fn(state, searchResult, index);
3346
3376
  };
3347
3377
 
3348
3378
  const handleClickAt = (state, eventX, eventY) => {
@@ -3571,7 +3601,7 @@ const renderItems = {
3571
3601
  return oldState.items === newState.items && oldState.minLineY === newState.minLineY && oldState.maxLineY === newState.maxLineY && oldState.replacement === newState.replacement && oldState.flags === newState.flags && oldState.message === newState.message && oldState.loaded === newState.loaded && oldState.collapsedPaths === newState.collapsedPaths && oldState.listFocusedIndex === newState.listFocusedIndex && oldState.listFocused === newState.listFocused && oldState.icons === newState.icons && oldState.searchInputErrorMessage === newState.searchInputErrorMessage;
3572
3602
  },
3573
3603
  apply(oldState, newState) {
3574
- const displayResults = getDisplayResults(newState.listItems, newState.itemHeight, newState.fileCount, newState.value, newState.minLineY, newState.maxLineY, newState.replacement, newState.icons, newState.listFocusedIndex);
3604
+ const displayResults = getDisplayResults(newState.listItems, newState.itemHeight, newState.fileCount, newState.value, newState.minLineY, newState.maxLineY, newState.replacement, newState.icons, newState.listFocusedIndex, newState.collapsedPaths);
3575
3605
  const focusOutline = newState.listFocused && newState.listFocusedIndex === -1;
3576
3606
  const dom = getSearchVirtualDom(displayResults, newState.flags, newState.message, focusOutline, newState.searchInputErrorMessage);
3577
3607
  return ['Viewlet.setDom2', newState.uid, dom];
@@ -3890,6 +3920,7 @@ const commandMap = {
3890
3920
  'TextSearch.refresh': wrapCommand(refresh),
3891
3921
  'TextSearch.replaceAll': wrapCommand(replaceAll),
3892
3922
  'TextSearch.replaceAllAndPrompt': wrapCommand(replaceAllAndPrompt),
3923
+ 'TextSearch.selectIndex': wrapCommand(selectIndex),
3893
3924
  'TextSearch.submit': wrapCommand(submit),
3894
3925
  'TextSearch.toggleMatchCase': wrapCommand(toggleMatchCase),
3895
3926
  'TextSearch.toggleMatchWholeWord': wrapCommand(toggleMatchWholeWord),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/text-search-worker",
3
- "version": "3.25.0",
3
+ "version": "3.27.0",
4
4
  "description": "Text Search Worker",
5
5
  "main": "dist/textSearchWorkerMain.js",
6
6
  "type": "module",