@lvce-editor/text-search-worker 3.2.0 → 3.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.
@@ -835,8 +835,6 @@ const copy = async state => {
835
835
  return state;
836
836
  };
837
837
 
838
- const ListItem = 22;
839
-
840
838
  const User = 1;
841
839
  const Script = 2;
842
840
 
@@ -885,7 +883,7 @@ const FocusSearchWholeWord = 34;
885
883
  const FocusSearchReplaceAll = 35;
886
884
  const FocusSearchPreserveCase = 36;
887
885
 
888
- const create = (uid, x, y, width, height, workspacePath, assetDir) => {
886
+ const create = (uid, x, y, width, height, workspacePath, assetDir, itemHeight) => {
889
887
  const state = {
890
888
  uid,
891
889
  searchResults: [],
@@ -899,7 +897,7 @@ const create = (uid, x, y, width, height, workspacePath, assetDir) => {
899
897
  width,
900
898
  height,
901
899
  ...create$1({
902
- itemHeight: ListItem,
900
+ itemHeight: itemHeight || 22,
903
901
  minimumSliderSize: minimumSliderSize,
904
902
  headerHeight: 61 // TODO
905
903
  }),
@@ -958,11 +956,15 @@ const isLastIndex = (array, index) => {
958
956
  const lastIndex = array => {
959
957
  return array.length - 1;
960
958
  };
961
- const toSpliced = (array, index, deleteCount, ...inserted) => {
962
- return [...array.slice(0, index), ...inserted, ...array.slice(index + deleteCount)];
963
- };
964
959
  const remove = (array, index, deleteCount) => {
965
- return toSpliced(array, index, deleteCount);
960
+ return array.toSpliced(index, deleteCount);
961
+ };
962
+ const fromAsync = async asyncIterable => {
963
+ const children = [];
964
+ for await (const value of asyncIterable) {
965
+ children.push(value);
966
+ }
967
+ return children;
966
968
  };
967
969
 
968
970
  class AssertionError extends Error {
@@ -1035,33 +1037,34 @@ const i18nString = (key, placeholders = emptyObject) => {
1035
1037
  * @enum {string}
1036
1038
  */
1037
1039
  const UiStrings = {
1038
- NoResults: 'No results found',
1039
- Oneresult: '1 result in 1 file',
1040
- ManyResultsInOneFile: '{PH1} results in 1 file',
1041
- ManyResultsInManyFiles: '{PH1} results in {PH2} files',
1042
- ReplaceAll: 'Replace All',
1043
- Replace: 'Replace',
1040
+ ClearSearchResults: 'Clear Search Results',
1041
+ CollapseAll: 'Collapse All',
1044
1042
  ConfirmReplaceAll: 'Replace All?',
1045
- ConfirmReplaceOneOccurrenceInOneFile: "Replace 1 occurrence across 1 file with '{PH1}'",
1046
- ConfirmReplaceOneOccurrenceInOneFileNoValue: 'Replace 1 occurrence across 1 file',
1047
- ConfirmReplaceManyOccurrencesInOneFile: "Replace {PH1} occurrences across 1 file with '{PH2}'",
1048
- ConfirmReplaceManyOccurrencesInOneFileNoValue: 'Replace {PH1} occurrences across 1 file',
1049
1043
  ConfirmReplaceManyOccurrencesInManyFiles: "Replace {PH1} occurrences across {PH2} files with '{PH3}'",
1050
1044
  ConfirmReplaceManyOccurrencesInManyFilesNoValue: 'Replace {PH1} occurrences across {PH2} files',
1051
- ReplacedOneOccurrenceInOneFile: "Replaced 1 occurrence across 1 file with '{PH1}'",
1052
- ReplacedManyOccurrencesInOneFile: "Replaced {PH1} occurrences across 1 file with '{PH2}'",
1053
- ReplacedManyOccurrencesInManyFiles: "Replaced {PH1} occurrences across {PH2} files with '{PH3}'",
1045
+ ConfirmReplaceManyOccurrencesInOneFile: "Replace {PH1} occurrences across 1 file with '{PH2}'",
1046
+ ConfirmReplaceManyOccurrencesInOneFileNoValue: 'Replace {PH1} occurrences across 1 file',
1047
+ ConfirmReplaceOneOccurrenceInOneFile: "Replace 1 occurrence across 1 file with '{PH1}'",
1048
+ ConfirmReplaceOneOccurrenceInOneFileNoValue: 'Replace 1 occurrence across 1 file',
1054
1049
  CopyPath: 'Copy Path',
1055
1050
  Dismiss: 'Dismiss',
1056
- Refresh: 'Refresh',
1057
- ClearSearchResults: 'Clear Search Results',
1058
- OpenNewSearchEditor: 'Open New Search Editor',
1059
- ViewAsTree: 'View as Tree',
1060
- CollapseAll: 'Collapse All',
1061
- ToggleReplace: 'Toggle Replace',
1051
+ ManyResultsInManyFiles: '{PH1} results in {PH2} files',
1052
+ ManyResultsInOneFile: '{PH1} results in 1 file',
1062
1053
  MatchCase: 'Match Case',
1063
1054
  MatchWholeWord: 'Match Whole Word',
1064
- UseRegularExpression: 'Use Regular Expression'
1055
+ NoResults: 'No results found',
1056
+ Oneresult: '1 result in 1 file',
1057
+ OpenNewSearchEditor: 'Open New Search Editor',
1058
+ PreserveCase: 'Preserve Case',
1059
+ Refresh: 'Refresh',
1060
+ Replace: 'Replace',
1061
+ ReplaceAll: 'Replace All',
1062
+ ReplacedManyOccurrencesInManyFiles: "Replaced {PH1} occurrences across {PH2} files with '{PH3}'",
1063
+ ReplacedManyOccurrencesInOneFile: "Replaced {PH1} occurrences across 1 file with '{PH2}'",
1064
+ ReplacedOneOccurrenceInOneFile: "Replaced 1 occurrence across 1 file with '{PH1}'",
1065
+ ToggleReplace: 'Toggle Replace',
1066
+ UseRegularExpression: 'Use Regular Expression',
1067
+ ViewAsTree: 'View as Tree'
1065
1068
  };
1066
1069
  const noResults = () => {
1067
1070
  return i18nString(UiStrings.NoResults);
@@ -1075,6 +1078,9 @@ const toggleReplace$1 = () => {
1075
1078
  const matchCase = () => {
1076
1079
  return i18nString(UiStrings.MatchCase);
1077
1080
  };
1081
+ const preserveCase = () => {
1082
+ return i18nString(UiStrings.PreserveCase);
1083
+ };
1078
1084
  const matchWholeWord = () => {
1079
1085
  return i18nString(UiStrings.MatchWholeWord);
1080
1086
  };
@@ -1349,8 +1355,13 @@ const IconButton = 'IconButton';
1349
1355
  const Label = 'Label';
1350
1356
  const List = 'List';
1351
1357
  const MaskIcon = 'MaskIcon';
1358
+ const MaskIconCaseSensitive = 'MaskIconCaseSensitive';
1352
1359
  const MaskIconChevronDown = 'MaskIconChevronDown';
1353
1360
  const MaskIconChevronRight = 'MaskIconChevronRight';
1361
+ const MaskIconPreserveCase = 'MaskIconPreserveCase';
1362
+ const MaskIconRegex = 'MaskIconRegex';
1363
+ const MaskIconReplaceAll = 'MaskIconReplaceAll';
1364
+ const MaskIconWholeWord = 'MaskIconWholeWord';
1354
1365
  const MultilineInputBox = 'MultilineInputBox';
1355
1366
  const Search$1 = 'Search';
1356
1367
  const SearchField = 'SearchField';
@@ -1594,8 +1605,29 @@ const getSearchFieldButtonVirtualDom = button => {
1594
1605
  }];
1595
1606
  };
1596
1607
 
1608
+ const getOutSideButtonsDom = outsideButtons => {
1609
+ if (outsideButtons.length === 0) {
1610
+ return {
1611
+ preNodes: [],
1612
+ postNodes: []
1613
+ };
1614
+ }
1615
+ return {
1616
+ preNodes: [{
1617
+ type: Div,
1618
+ className: SearchFieldContainer,
1619
+ role: None,
1620
+ childCount: 1 + outsideButtons.length
1621
+ }],
1622
+ postNodes: outsideButtons.flatMap(getSearchFieldButtonVirtualDom)
1623
+ };
1624
+ };
1597
1625
  const getSearchFieldVirtualDom = (name, placeholder, onInput, insideButtons, outsideButtons, onFocus = '') => {
1598
- const dom = [{
1626
+ const {
1627
+ preNodes,
1628
+ postNodes
1629
+ } = getOutSideButtonsDom(outsideButtons);
1630
+ const dom = [...preNodes, {
1599
1631
  type: Div,
1600
1632
  className: SearchField,
1601
1633
  role: None,
@@ -1615,16 +1647,7 @@ const getSearchFieldVirtualDom = (name, placeholder, onInput, insideButtons, out
1615
1647
  type: Div,
1616
1648
  className: SearchFieldButtons,
1617
1649
  childCount: insideButtons.length
1618
- }, ...insideButtons.flatMap(getSearchFieldButtonVirtualDom)];
1619
- if (outsideButtons.length > 0) {
1620
- dom.unshift({
1621
- type: Div,
1622
- className: SearchFieldContainer,
1623
- role: None,
1624
- childCount: 1 + outsideButtons.length
1625
- });
1626
- dom.push(...outsideButtons.flatMap(getSearchFieldButtonVirtualDom));
1627
- }
1650
+ }, ...insideButtons.flatMap(getSearchFieldButtonVirtualDom), ...postNodes];
1628
1651
  return dom;
1629
1652
  };
1630
1653
 
@@ -1667,27 +1690,27 @@ const getSearchHeaderVirtualDom = (replaceExpanded, matchCase$1, matchWholeWord$
1667
1690
  role: None,
1668
1691
  childCount: replaceExpanded ? 2 : 1
1669
1692
  }, ...getSearchFieldVirtualDom('search-value', 'Search', 'handleInput', [{
1670
- icon: 'MaskIconCaseSensitive',
1693
+ icon: MaskIconCaseSensitive,
1671
1694
  checked: matchCase$1,
1672
1695
  title: matchCase()
1673
1696
  }, {
1674
- icon: 'MaskIconWholeWord',
1697
+ icon: MaskIconWholeWord,
1675
1698
  checked: matchWholeWord$1,
1676
1699
  title: matchWholeWord()
1677
1700
  }, {
1678
- icon: 'MaskIconRegex',
1701
+ icon: MaskIconRegex,
1679
1702
  checked: useRegularExpression$1,
1680
1703
  title: useRegularExpression()
1681
1704
  }], [])];
1682
1705
  if (replaceExpanded) {
1683
1706
  dom.push(...getSearchFieldVirtualDom('search-replace-value', 'Replace', 'handleReplaceInput', [{
1684
- icon: 'MaskIconPreserveCase',
1707
+ icon: MaskIconPreserveCase,
1685
1708
  checked: false,
1686
- title: 'Preserve Case'
1709
+ title: preserveCase()
1687
1710
  }], [{
1688
- icon: 'MaskIconReplaceAll',
1711
+ icon: MaskIconReplaceAll,
1689
1712
  checked: false,
1690
- title: 'Replace All'
1713
+ title: replaceAll$1()
1691
1714
  }]));
1692
1715
  }
1693
1716
  if (detailsExpanded) {
@@ -1719,12 +1742,18 @@ const getBadgeVirtualDom = (className, count) => {
1719
1742
  }, text(`${count}`)];
1720
1743
  };
1721
1744
 
1722
- const getChevronDownVirtualDom = (extraClassName = '') => {
1723
- return {
1724
- type: Div,
1725
- className: mergeClassNames(Chevron, MaskIconChevronDown, extraClassName),
1726
- childCount: 0
1727
- };
1745
+ const chevronDownVirtualDom = {
1746
+ type: Div,
1747
+ className: mergeClassNames(Chevron, MaskIconChevronDown),
1748
+ childCount: 0
1749
+ };
1750
+ ({
1751
+ type: Div,
1752
+ className: mergeClassNames(Chevron, MaskIconChevronRight),
1753
+ childCount: 0
1754
+ });
1755
+ const getChevronDownVirtualDom = () => {
1756
+ return chevronDownVirtualDom;
1728
1757
  };
1729
1758
 
1730
1759
  const getFileIconVirtualDom = icon => {
@@ -1847,21 +1876,17 @@ const getSearchResultsVirtualDom = visibleItems => {
1847
1876
  };
1848
1877
 
1849
1878
  const getSearchVirtualDom = (visibleItems, replaceExpanded, matchCase, matchWholeWord, useRegularExpression, message, detailsExpanded, focusOutline) => {
1850
- const dom = [];
1851
- dom.push({
1879
+ const dom = [{
1852
1880
  type: Div,
1853
1881
  className: mergeClassNames(Viewlet, Search$1),
1854
1882
  childCount: 2
1855
- });
1856
- dom.push(...getSearchHeaderVirtualDom(replaceExpanded, matchCase, matchWholeWord, useRegularExpression, detailsExpanded));
1857
- dom.push({
1883
+ }, ...getSearchHeaderVirtualDom(replaceExpanded, matchCase, matchWholeWord, useRegularExpression, detailsExpanded), {
1858
1884
  type: Div,
1859
1885
  className: ViewletSearchMessage,
1860
1886
  role: Status,
1861
1887
  tabIndex: 0,
1862
1888
  childCount: 1
1863
- }, text(message));
1864
- dom.push({
1889
+ }, text(message), {
1865
1890
  type: Div,
1866
1891
  className: mergeClassNames(Viewlet, List, focusOutline ? FocusOutline : Empty),
1867
1892
  role: Tree,
@@ -1869,8 +1894,7 @@ const getSearchVirtualDom = (visibleItems, replaceExpanded, matchCase, matchWhol
1869
1894
  childCount: visibleItems.length,
1870
1895
  onClick: HandleClick,
1871
1896
  onBlur: HandleListBlur
1872
- });
1873
- dom.push(...getSearchResultsVirtualDom(visibleItems));
1897
+ }, ...getSearchResultsVirtualDom(visibleItems)];
1874
1898
  return dom;
1875
1899
  };
1876
1900
 
@@ -2072,7 +2096,7 @@ const splitLines = lines => {
2072
2096
  return lines.split('\n');
2073
2097
  };
2074
2098
 
2075
- const textSearchInFile$1 = (file, content, query) => {
2099
+ const textSearchInText = (file, content, query) => {
2076
2100
  const results = [];
2077
2101
  const lines = splitLines(content);
2078
2102
  for (let i = 0; i < lines.length; i++) {
@@ -2099,6 +2123,7 @@ const textSearchInFile$1 = (file, content, query) => {
2099
2123
  }
2100
2124
  return results;
2101
2125
  };
2126
+
2102
2127
  const textSearch$4 = async (scheme, root, query, options, assetDir) => {
2103
2128
  string(scheme);
2104
2129
  string(root);
@@ -2111,15 +2136,38 @@ const textSearch$4 = async (scheme, root, query, options, assetDir) => {
2111
2136
  const fetchUri = `${assetDir}${uri}`;
2112
2137
  const content = await getText(fetchUri);
2113
2138
  const relativeUri = uri.slice(relativeRoot.length + 1);
2114
- const results = textSearchInFile$1(relativeUri, content, query);
2139
+ const results = textSearchInText(relativeUri, content, query);
2115
2140
  allResults.push(...results);
2116
2141
  }
2117
2142
  return allResults;
2118
2143
  };
2119
2144
 
2145
+ class FileNotFoundError extends Error {
2146
+ constructor(message) {
2147
+ super(message);
2148
+ this.name = 'FileNotFoundError';
2149
+ }
2150
+ }
2151
+
2120
2152
  const Directory = 'directory';
2121
2153
  const File = 'file';
2122
2154
 
2155
+ const getChildHandles$1 = async handle => {
2156
+ object(handle);
2157
+ // @ts-ignore
2158
+ const childHandles = handle.values();
2159
+ const handles = await fromAsync(childHandles);
2160
+ return handles;
2161
+ };
2162
+
2163
+ const getChildHandles = async handle => {
2164
+ try {
2165
+ return await getChildHandles$1(handle);
2166
+ } catch (error) {
2167
+ throw new VError(error, 'failed to get child handles');
2168
+ }
2169
+ };
2170
+
2123
2171
  const dirname = (pathSeparator, path) => {
2124
2172
  const index = path.lastIndexOf(pathSeparator);
2125
2173
  if (index === -1) {
@@ -2441,34 +2489,6 @@ const getFile = handle => {
2441
2489
  return handle.getFile();
2442
2490
  };
2443
2491
 
2444
- const textSearchInText = (file, content, query) => {
2445
- const results = [];
2446
- const lines = splitLines(content);
2447
- for (let i = 0; i < lines.length; i++) {
2448
- const line = lines[i];
2449
- const index = line.indexOf(query);
2450
- if (index !== -1) {
2451
- results.push({
2452
- type: Match,
2453
- text: line,
2454
- start: index,
2455
- end: index + query.length,
2456
- lineNumber: i
2457
- });
2458
- }
2459
- }
2460
- if (results.length > 0) {
2461
- results.unshift({
2462
- type: File$1,
2463
- text: file,
2464
- start: 0,
2465
- end: 0,
2466
- lineNumber: 0
2467
- });
2468
- }
2469
- return results;
2470
- };
2471
-
2472
2492
  const textSearchInFile = async (all, handle, absolutePath, query) => {
2473
2493
  try {
2474
2494
  const file = await getFile(handle);
@@ -2485,9 +2505,7 @@ const textSearchInFile = async (all, handle, absolutePath, query) => {
2485
2505
  };
2486
2506
 
2487
2507
  const textSearchRecursively = async (all, parent, handle, query) => {
2488
- const childHandles = [];
2489
- // TODO
2490
- // await FileSystemHtml.getChildHandles(handle)
2508
+ const childHandles = await getChildHandles(handle);
2491
2509
  const promises = [];
2492
2510
  for (const childHandle of childHandles) {
2493
2511
  const absolutePath = parent + '/' + childHandle.name;
@@ -2496,6 +2514,7 @@ const textSearchRecursively = async (all, parent, handle, query) => {
2496
2514
  promises.push(textSearchRecursively(all, absolutePath, childHandle, query));
2497
2515
  break;
2498
2516
  case File:
2517
+ // @ts-ignore
2499
2518
  promises.push(textSearchInFile(all, childHandle, absolutePath, query));
2500
2519
  break;
2501
2520
  }
@@ -2509,7 +2528,7 @@ const textSearch$3 = async (scheme, root, query) => {
2509
2528
  const relativeRoot = root.slice('html://'.length);
2510
2529
  const handle = await getDirectoryHandle(relativeRoot);
2511
2530
  if (!handle) {
2512
- throw new VError(`Folder not found ${relativeRoot}`);
2531
+ throw new FileNotFoundError(`Folder not found: ${relativeRoot}`);
2513
2532
  }
2514
2533
  const all = [];
2515
2534
  await textSearchRecursively(all, '', handle, query);
@@ -3023,19 +3042,19 @@ const handleWheel = (state, deltaMode, deltaY) => {
3023
3042
  };
3024
3043
 
3025
3044
  const getSavedValue = savedState => {
3026
- if (savedState && savedState.value) {
3045
+ if (savedState && typeof savedState === 'object' && 'value' in savedState && typeof savedState.value === 'string') {
3027
3046
  return savedState.value;
3028
3047
  }
3029
3048
  return '';
3030
3049
  };
3031
3050
  const getSavedReplaceExpanded = savedState => {
3032
- if (savedState && 'replaceExpanded' in savedState) {
3051
+ if (savedState && typeof savedState === 'object' && 'replaceExpanded' in savedState && typeof savedState.replaceExpanded === 'boolean') {
3033
3052
  return savedState.replaceExpanded;
3034
3053
  }
3035
3054
  return false;
3036
3055
  };
3037
3056
  const getSavedCollapsedPaths = savedState => {
3038
- if (savedState && 'collapsedPaths' in savedState && Array.isArray(savedState.collapsedPaths) && savedState.collapsedPaths.every(path => typeof path === 'string')) {
3057
+ if (savedState && typeof savedState === 'object' && 'collapsedPaths' in savedState && Array.isArray(savedState.collapsedPaths) && savedState.collapsedPaths.every(path => typeof path === 'string')) {
3039
3058
  return savedState.collapsedPaths;
3040
3059
  }
3041
3060
  return [];
@@ -3043,11 +3062,26 @@ const getSavedCollapsedPaths = savedState => {
3043
3062
  const getThreads = () => {
3044
3063
  return 1;
3045
3064
  };
3046
- const loadContent = async (state, savedState) => {
3065
+ const restoreState = savedState => {
3047
3066
  const savedValue = getSavedValue(savedState);
3048
3067
  const savedReplaceExpanded = getSavedReplaceExpanded(savedState);
3049
3068
  const savedCollapsedPaths = getSavedCollapsedPaths(savedState);
3050
3069
  const threads = getThreads();
3070
+ return {
3071
+ savedCollapsedPaths,
3072
+ savedReplaceExpanded,
3073
+ savedValue,
3074
+ threads
3075
+ };
3076
+ };
3077
+
3078
+ const loadContent = async (state, savedState) => {
3079
+ const {
3080
+ savedValue,
3081
+ savedCollapsedPaths,
3082
+ savedReplaceExpanded,
3083
+ threads
3084
+ } = restoreState(savedState);
3051
3085
  if (savedValue) {
3052
3086
  return handleUpdate(state, {
3053
3087
  value: savedValue,
@@ -3136,7 +3170,7 @@ const getBulkReplacementEdits = (workspacePath, matches) => {
3136
3170
  ranges.push(currentRanges.length);
3137
3171
  push(ranges, currentRanges);
3138
3172
  const absolutePath = `${workspacePath}/${text.slice(2)}`;
3139
- files.push(absolutePath); // TODO
3173
+ files.push(absolutePath);
3140
3174
  currentRanges = [];
3141
3175
  break;
3142
3176
  case Match:
@@ -3152,12 +3186,18 @@ const getBulkReplacementEdits = (workspacePath, matches) => {
3152
3186
  };
3153
3187
  };
3154
3188
 
3155
- const replaceAll = async (workspacePath, items, replacement) => {
3189
+ const replaceAll = async state => {
3190
+ const {
3191
+ workspacePath,
3192
+ items,
3193
+ replacement
3194
+ } = state;
3156
3195
  const {
3157
3196
  files,
3158
3197
  ranges
3159
3198
  } = getBulkReplacementEdits(workspacePath, items);
3160
3199
  await invoke$1('BulkReplacement.applyBulkReplacement', files, ranges, replacement);
3200
+ return state;
3161
3201
  };
3162
3202
 
3163
3203
  const getReplaceAllConfirmText = (matchCount, fileCount, replacement) => {
@@ -3284,8 +3324,8 @@ const focusReplaceAll = state => {
3284
3324
  return focusElement(state, FocusSearchReplaceAll);
3285
3325
  };
3286
3326
  const handleFocusIn = (state, key) => {
3287
- const focusKey = getSearchFocusKey(key.dataset.focusKey);
3288
- if (focusKey === state.focus) {
3327
+ const focusKey = getSearchFocusKey(key);
3328
+ if (state.focus === focusKey) {
3289
3329
  return state;
3290
3330
  }
3291
3331
  void setFocus(focusKey);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/text-search-worker",
3
- "version": "3.2.0",
3
+ "version": "3.4.0",
4
4
  "description": "Text Search Worker",
5
5
  "main": "dist/textSearchWorkerMain.js",
6
6
  "type": "module",