@lvce-editor/text-search-worker 2.5.0 → 2.7.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.
@@ -429,8 +429,11 @@ const states = Object.create(null);
429
429
  const get = uid => {
430
430
  return states[uid];
431
431
  };
432
- const set = (uid, state) => {
433
- states[uid] = state;
432
+ const set = (uid, oldState, newState) => {
433
+ states[uid] = {
434
+ oldState,
435
+ newState
436
+ };
434
437
  };
435
438
 
436
439
  const create$1 = ({
@@ -500,9 +503,16 @@ const create = (uid, x, y, width, height, workspacePath) => {
500
503
  focus: Empty$1,
501
504
  loaded: false,
502
505
  message: '',
503
- collapsedPaths: []
506
+ collapsedPaths: [],
507
+ icons: [],
508
+ assetDir: '',
509
+ deltaY: 0,
510
+ finalDeltaY: 0,
511
+ focused: false,
512
+ focusedIndex: -1,
513
+ handleOffset: 0
504
514
  };
505
- set(uid, state);
515
+ set(uid, state, state);
506
516
  };
507
517
 
508
518
  // based on https://github.com/microsoft/vscode/blob/c0769274fa136b45799edeccc0d0a2f645b75caf/src/vs/base/common/arrays.ts#L625 (License MIT)
@@ -822,7 +832,7 @@ const focusSearchValue$1 = state => {
822
832
  };
823
833
  };
824
834
 
825
- const focusSearchValueNext$1 = state => {
835
+ const focusSearchValueNext = state => {
826
836
  const {
827
837
  replaceExpanded
828
838
  } = state;
@@ -958,10 +968,6 @@ const getActions = () => {
958
968
  }];
959
969
  };
960
970
 
961
- const getFileIcon = () => {
962
- return '';
963
- };
964
-
965
971
  // TODO this should be in FileSystem module
966
972
  const pathBaseName = path => {
967
973
  return path.slice(path.lastIndexOf('/') + 1);
@@ -973,7 +979,7 @@ const getAbsolutePath = relativePath => {
973
979
  return `/${relativePath}`; // TODO support windows paths
974
980
  };
975
981
 
976
- const getDisplayResult = (result, itemHeight, i, setSize, searchTermLength, replacement, focusedIndex) => {
982
+ const getDisplayResult = (result, fileIcons, fileIconIndex, itemHeight, i, setSize, searchTermLength, replacement, focusedIndex) => {
977
983
  const {
978
984
  type,
979
985
  text,
@@ -992,7 +998,7 @@ const getDisplayResult = (result, itemHeight, i, setSize, searchTermLength, repl
992
998
  title: absolutePath,
993
999
  type: File$1,
994
1000
  text: baseName,
995
- icon: getFileIcon(),
1001
+ icon: fileIcons[fileIconIndex],
996
1002
  posInSet,
997
1003
  setSize,
998
1004
  top,
@@ -1043,7 +1049,7 @@ const getFilteredResults = (results, collapsedPaths) => {
1043
1049
  }
1044
1050
  return filteredResults;
1045
1051
  };
1046
- const getDisplayResults = (results, itemHeight, resultCount, searchTerm, minLineY, maxLineY, replacement, collapsedPaths, focusedIndex) => {
1052
+ const getDisplayResults = (results, itemHeight, resultCount, searchTerm, minLineY, maxLineY, replacement, collapsedPaths, fileIcons, focusedIndex) => {
1047
1053
  const displayResults = [];
1048
1054
  const filteredResults = getFilteredResults(results, collapsedPaths);
1049
1055
  const setSize = resultCount;
@@ -1051,12 +1057,14 @@ const getDisplayResults = (results, itemHeight, resultCount, searchTerm, minLine
1051
1057
  let fileResult = {
1052
1058
  matchCount: 0
1053
1059
  };
1060
+ let fileIconIndex = 0;
1054
1061
  for (let i = minLineY; i < maxLineY; i++) {
1055
1062
  const result = filteredResults[i];
1056
- const displayResult = getDisplayResult(result, itemHeight, i, setSize, searchTermLength, replacement, focusedIndex);
1063
+ const displayResult = getDisplayResult(result, fileIcons, fileIconIndex, itemHeight, i, setSize, searchTermLength, replacement, focusedIndex);
1057
1064
  displayResults.push(displayResult);
1058
1065
  if (result.type === File$1) {
1059
1066
  fileResult = displayResult;
1067
+ fileIconIndex++;
1060
1068
  } else {
1061
1069
  fileResult.matchCount++;
1062
1070
  }
@@ -1386,17 +1394,23 @@ const handleIconThemeChange = state => {
1386
1394
  };
1387
1395
  };
1388
1396
 
1389
- const first = () => {
1390
- return 0;
1391
- };
1392
- const last = items => {
1393
- return items.length - 1;
1394
- };
1395
- const next = (items, index) => {
1396
- return (index + 1) % items.length;
1397
+ const handleError = async (error, notify = true, prefix = '') => {
1398
+ console.error(`[text-search-worker] ${error}`);
1397
1399
  };
1398
- const previous = (items, index) => {
1399
- return index === 0 ? items.length - 1 : index - 1;
1400
+
1401
+ const getFileIcons = async files => {
1402
+ // TODO cache file icons
1403
+ const promises = [];
1404
+ for (const file of files) {
1405
+ if (file.type === Match) {
1406
+ continue;
1407
+ }
1408
+ promises.push(invoke$1('IconTheme.getFileIcon', {
1409
+ name: file.text
1410
+ }));
1411
+ }
1412
+ const icons = await Promise.all(promises);
1413
+ return icons;
1400
1414
  };
1401
1415
 
1402
1416
  // TODO optimize this function to return the minimum number
@@ -1406,224 +1420,29 @@ const getNumberOfVisibleItems = (listHeight, itemHeight) => {
1406
1420
  return Math.ceil(listHeight / itemHeight) + 1;
1407
1421
  };
1408
1422
 
1409
- const focusIndexScrollUp = (state, index, listHeight, itemHeight, itemsLength) => {
1410
- const newMinLineY = index;
1411
- const fittingItems = getNumberOfVisibleItems(listHeight, itemHeight);
1412
- const newMaxLineY = Math.min(newMinLineY + fittingItems, itemsLength);
1413
- const newDeltaY = newMinLineY * itemHeight;
1414
- return {
1415
- ...state,
1416
- focusedIndex: index,
1417
- minLineY: newMinLineY,
1418
- maxLineY: newMaxLineY,
1419
- focused: true,
1420
- deltaY: newDeltaY
1421
- };
1422
- };
1423
- const focusIndexScrollDown = (state, index, listHeight, itemHeight, itemsLength) => {
1424
- const newMaxLineY = Math.min(index + 1, itemsLength);
1425
- const fittingItems = getNumberOfVisibleItems(listHeight, itemHeight);
1426
- const newMinLineY = Math.max(newMaxLineY - fittingItems, 0);
1427
- const newDeltaY = itemsLength < fittingItems ? 0 : newMinLineY * itemHeight - listHeight % itemHeight + itemHeight;
1428
- return {
1429
- ...state,
1430
- focusedIndex: index,
1431
- minLineY: newMinLineY,
1432
- maxLineY: newMaxLineY,
1433
- focused: true,
1434
- deltaY: newDeltaY
1435
- };
1436
- };
1437
- const focusIndex = (state, index) => {
1438
- const {
1439
- itemHeight,
1440
- minLineY,
1441
- maxLineY,
1442
- headerHeight,
1443
- height,
1444
- items
1445
- } = state;
1446
- const itemsLength = items.length;
1447
- if (itemsLength === 0) {
1448
- return state;
1449
- }
1450
- number(itemHeight);
1451
- if (index === -1) {
1452
- return {
1453
- ...state,
1454
- focusedIndex: -1,
1455
- focused: true
1456
- };
1457
- }
1458
- const listHeight = height - headerHeight;
1459
- if (index < minLineY + 1) {
1460
- return focusIndexScrollUp(state, index, listHeight, itemHeight, itemsLength);
1461
- }
1462
- if (index >= maxLineY - 1) {
1463
- return focusIndexScrollDown(state, index, listHeight, itemHeight, itemsLength);
1464
- }
1465
- return {
1466
- ...state,
1467
- focusedIndex: index,
1468
- focused: true
1469
- };
1470
- };
1471
-
1472
- const focusFirst = state => {
1473
- const firstIndex = first();
1474
- return focusIndex(state, firstIndex);
1475
- };
1476
-
1477
- const focusLast = state => {
1478
- const {
1479
- items
1480
- } = state;
1481
- const lastIndex = last(items);
1482
- return focusIndex(state, lastIndex);
1483
- };
1484
-
1485
- const focusNext = state => {
1486
- const {
1487
- focusedIndex,
1488
- items
1489
- } = state;
1490
- const nextIndex = next(items, focusedIndex);
1491
- return focusIndex(state, nextIndex);
1492
- };
1493
-
1494
- const focusNextPage = state => {
1495
- const {
1496
- focusedIndex,
1497
- items,
1498
- maxLineY,
1499
- minLineY
1500
- } = state;
1501
- if (isLastIndex(items, focusedIndex)) {
1502
- return state;
1503
- }
1504
- const indexNextPage = Math.min(maxLineY + (maxLineY - minLineY) - 2, lastIndex(items));
1505
- return focusIndex(state, indexNextPage);
1506
- };
1507
-
1508
- const focusPreviousPage = state => {
1509
- const {
1510
- focusedIndex,
1511
- minLineY,
1512
- maxLineY
1513
- } = state;
1514
- if (focusedIndex === 0 || focusedIndex === -1) {
1515
- return state;
1516
- }
1517
- const indexPreviousPage = Math.max(minLineY - (maxLineY - minLineY) + 1, 0);
1518
- return focusIndex(state, indexPreviousPage);
1519
- };
1520
-
1521
- const focusPrevious = state => {
1522
- const {
1523
- focusedIndex,
1524
- items
1525
- } = state;
1526
- if (focusedIndex === 0 || focusedIndex === -1) {
1527
- return state;
1528
- }
1529
- const previousIndex = previous(items, focusedIndex);
1530
- return focusIndex(state, previousIndex);
1531
- };
1532
-
1533
- const getListIndex = (eventX, eventY, x, y, deltaY, itemHeight) => {
1534
- const relativeY = eventY - y + deltaY;
1535
- const index = Math.floor(relativeY / itemHeight);
1536
- return index;
1537
- };
1538
-
1539
- const openUri = async (path, focus = true, props = {}) => {
1540
- await invoke$1(/* Main.openUri */'Main.openUri', /* uri */path, /* focus */focus, props);
1541
- };
1542
-
1543
- const getFileIndex = (items, index) => {
1544
- for (let i = index; i >= 0; i--) {
1545
- const item = items[i];
1546
- if (item.type === File$1) {
1547
- return i;
1423
+ const getTextSearchResultCounts = results => {
1424
+ let resultCount = 0;
1425
+ let fileCount = 0;
1426
+ for (const result of results) {
1427
+ switch (result.type) {
1428
+ case File$1:
1429
+ fileCount++;
1430
+ break;
1431
+ case Match:
1432
+ resultCount++;
1433
+ break;
1548
1434
  }
1549
1435
  }
1550
- return -1;
1551
- };
1552
- const selectIndexFile = async (state, searchResult, index) => {
1553
- const {
1554
- collapsedPaths
1555
- } = state;
1556
- const path = getAbsolutePath(searchResult.text);
1557
- string(path);
1558
- return {
1559
- ...state,
1560
- collapsedPaths: [...collapsedPaths, path],
1561
- listFocusedIndex: index,
1562
- listFocused: true
1563
- };
1564
- };
1565
- const selectIndexPreview = async (state, searchResult, index) => {
1566
- const {
1567
- items
1568
- } = state;
1569
- const fileIndex = getFileIndex(items, index);
1570
- if (fileIndex === -1) {
1571
- throw new Error('Search result is missing file');
1572
- }
1573
- const {
1574
- lineNumber
1575
- } = searchResult;
1576
- // console.log({ searchResult })
1577
- const fileResult = items[fileIndex];
1578
- const path = getAbsolutePath(fileResult.text);
1579
- string(path);
1580
- await openUri(path, true, {
1581
- selections: new Uint32Array([lineNumber, 0, lineNumber, 0])
1582
- });
1583
1436
  return {
1584
- ...state,
1585
- listFocusedIndex: index,
1586
- listFocused: false
1437
+ fileCount,
1438
+ resultCount
1587
1439
  };
1588
1440
  };
1589
- const selectIndex = async (state, index, isClose) => {
1590
- if (index === -1) {
1591
- return {
1592
- ...state,
1593
- listFocused: true,
1594
- listFocusedIndex: -1
1595
- };
1596
- }
1597
- const {
1598
- items
1599
- } = state;
1600
- const searchResult = items[index];
1601
- switch (searchResult.type) {
1602
- case File$1:
1603
- return selectIndexFile(state, searchResult, index);
1604
- case Match:
1605
- return selectIndexPreview(state, searchResult, index);
1606
- default:
1607
- throw new Error(`unexpected search result type ${searchResult.type}`);
1608
- }
1609
- };
1610
1441
 
1611
- const handleClickAt = async (state, eventX, eventY) => {
1612
- const {
1613
- x,
1614
- y,
1615
- itemHeight,
1616
- deltaY
1617
- } = state;
1618
- const index = getListIndex(eventX, eventY, x, y, deltaY, itemHeight);
1619
- return selectIndex(state, index);
1620
- };
1442
+ const EmptyString = '';
1621
1443
 
1622
- const handleScrollBarCaptureLost = state => {
1623
- return {
1624
- ...state,
1625
- scrollBarActive: false
1626
- };
1444
+ const isEmptyString = string => {
1445
+ return string === EmptyString;
1627
1446
  };
1628
1447
 
1629
1448
  /**
@@ -1667,130 +1486,6 @@ const getNewDeltaPercent = (height, scrollBarHeight, relativeY) => {
1667
1486
  };
1668
1487
  };
1669
1488
 
1670
- const clamp = (num, min, max) => {
1671
- number(num);
1672
- number(min);
1673
- number(max);
1674
- return Math.min(Math.max(num, min), max);
1675
- };
1676
-
1677
- const setDeltaY = (state, value) => {
1678
- object(state);
1679
- number(value);
1680
- const {
1681
- itemHeight,
1682
- finalDeltaY,
1683
- deltaY,
1684
- height,
1685
- headerHeight
1686
- } = state;
1687
- const listHeight = height - headerHeight;
1688
- const newDeltaY = clamp(value, 0, finalDeltaY);
1689
- if (deltaY === newDeltaY) {
1690
- return state;
1691
- }
1692
- // TODO when it only moves by one px, extensions don't need to be rerendered, only negative margin
1693
- const minLineY = Math.floor(newDeltaY / itemHeight);
1694
- const maxLineY = minLineY + getNumberOfVisibleItems(listHeight, itemHeight);
1695
- return {
1696
- ...state,
1697
- deltaY: newDeltaY,
1698
- minLineY,
1699
- maxLineY
1700
- };
1701
- };
1702
-
1703
- const handleScrollBarClick = (state, eventY) => {
1704
- const {
1705
- y,
1706
- deltaY,
1707
- headerHeight,
1708
- finalDeltaY,
1709
- scrollBarHeight,
1710
- height
1711
- } = state;
1712
- const contentHeight = height - headerHeight;
1713
- const relativeY = eventY - y - headerHeight;
1714
- const currentScrollBarY = getScrollBarY(deltaY, finalDeltaY, contentHeight, scrollBarHeight);
1715
- const diff = relativeY - currentScrollBarY;
1716
- if (diff >= 0 && diff < scrollBarHeight) {
1717
- return {
1718
- ...state,
1719
- handleOffset: diff,
1720
- scrollBarActive: true
1721
- };
1722
- }
1723
- const {
1724
- percent,
1725
- handleOffset
1726
- } = getNewDeltaPercent(contentHeight, scrollBarHeight, relativeY);
1727
- const newDeltaY = percent * finalDeltaY;
1728
- return {
1729
- ...setDeltaY(state, newDeltaY),
1730
- handleOffset,
1731
- scrollBarActive: true
1732
- };
1733
- };
1734
-
1735
- const getNewPercent = (contentHeight, scrollBarHeight, relativeY) => {
1736
- if (relativeY <= contentHeight - scrollBarHeight / 2) {
1737
- // clicked in middle
1738
- return relativeY / (contentHeight - scrollBarHeight);
1739
- }
1740
- // clicked at bottom
1741
- return 1;
1742
- };
1743
- const handleScrollBarMove = (state, eventY) => {
1744
- const {
1745
- y,
1746
- headerHeight,
1747
- handleOffset,
1748
- finalDeltaY,
1749
- height,
1750
- scrollBarHeight
1751
- } = state;
1752
- const relativeY = eventY - y - headerHeight - handleOffset;
1753
- const contentHeight = height - headerHeight;
1754
- const newPercent = getNewPercent(contentHeight, scrollBarHeight, relativeY);
1755
- const newDeltaY = newPercent * finalDeltaY;
1756
- return setDeltaY(state, newDeltaY);
1757
- };
1758
-
1759
- const handleWheel = (state, deltaMode, deltaY) => {
1760
- number(deltaMode);
1761
- number(deltaY);
1762
- return setDeltaY(state, state.deltaY + deltaY);
1763
- };
1764
-
1765
- const handleError = async (error, notify = true, prefix = '') => {
1766
- console.error(`[text-search-worker] ${error}`);
1767
- };
1768
-
1769
- const getTextSearchResultCounts = results => {
1770
- let resultCount = 0;
1771
- let fileCount = 0;
1772
- for (const result of results) {
1773
- switch (result.type) {
1774
- case File$1:
1775
- fileCount++;
1776
- break;
1777
- case Match:
1778
- resultCount++;
1779
- break;
1780
- }
1781
- }
1782
- return {
1783
- fileCount,
1784
- resultCount
1785
- };
1786
- };
1787
-
1788
- const EmptyString = '';
1789
-
1790
- const isEmptyString = string => {
1791
- return string === EmptyString;
1792
- };
1793
-
1794
1489
  const RE_PROTOCOL = /^([a-z\-]+):\/\//;
1795
1490
  const getProtocol = uri => {
1796
1491
  const protocolMatch = uri.match(RE_PROTOCOL);
@@ -2470,6 +2165,8 @@ const handleUpdate = async (state, update) => {
2470
2165
  const numberOfVisible = getNumberOfVisibleItems(listHeight, itemHeight);
2471
2166
  const maxLineY = Math.min(numberOfVisible, total);
2472
2167
  const finalDeltaY = Math.max(contentHeight - listHeight, 0);
2168
+ const visible = results.slice(0, maxLineY);
2169
+ const icons = await getFileIcons(visible);
2473
2170
  return {
2474
2171
  ...partialNewState,
2475
2172
  minLineY: 0,
@@ -2483,7 +2180,8 @@ const handleUpdate = async (state, update) => {
2483
2180
  threads,
2484
2181
  fileCount,
2485
2182
  matchCount: resultCount,
2486
- loaded: true
2183
+ loaded: true,
2184
+ icons
2487
2185
  };
2488
2186
  } catch (error) {
2489
2187
  handleError(error);
@@ -2499,6 +2197,341 @@ const handleUpdate = async (state, update) => {
2499
2197
  }
2500
2198
  };
2501
2199
 
2200
+ const handleInput = (state, value, inputSource = Script) => {
2201
+ return handleUpdate(state, {
2202
+ value,
2203
+ inputSource
2204
+ });
2205
+ };
2206
+
2207
+ const first = () => {
2208
+ return 0;
2209
+ };
2210
+ const last = items => {
2211
+ return items.length - 1;
2212
+ };
2213
+ const next = (items, index) => {
2214
+ return (index + 1) % items.length;
2215
+ };
2216
+ const previous = (items, index) => {
2217
+ return index === 0 ? items.length - 1 : index - 1;
2218
+ };
2219
+
2220
+ const focusIndexScrollUp = (state, index, listHeight, itemHeight, itemsLength) => {
2221
+ const newMinLineY = index;
2222
+ const fittingItems = getNumberOfVisibleItems(listHeight, itemHeight);
2223
+ const newMaxLineY = Math.min(newMinLineY + fittingItems, itemsLength);
2224
+ const newDeltaY = newMinLineY * itemHeight;
2225
+ return {
2226
+ ...state,
2227
+ focusedIndex: index,
2228
+ minLineY: newMinLineY,
2229
+ maxLineY: newMaxLineY,
2230
+ focused: true,
2231
+ deltaY: newDeltaY
2232
+ };
2233
+ };
2234
+ const focusIndexScrollDown = (state, index, listHeight, itemHeight, itemsLength) => {
2235
+ const newMaxLineY = Math.min(index + 1, itemsLength);
2236
+ const fittingItems = getNumberOfVisibleItems(listHeight, itemHeight);
2237
+ const newMinLineY = Math.max(newMaxLineY - fittingItems, 0);
2238
+ const newDeltaY = itemsLength < fittingItems ? 0 : newMinLineY * itemHeight - listHeight % itemHeight + itemHeight;
2239
+ return {
2240
+ ...state,
2241
+ focusedIndex: index,
2242
+ minLineY: newMinLineY,
2243
+ maxLineY: newMaxLineY,
2244
+ focused: true,
2245
+ deltaY: newDeltaY
2246
+ };
2247
+ };
2248
+ const focusIndex = (state, index) => {
2249
+ const {
2250
+ itemHeight,
2251
+ minLineY,
2252
+ maxLineY,
2253
+ headerHeight,
2254
+ height,
2255
+ items
2256
+ } = state;
2257
+ const itemsLength = items.length;
2258
+ if (itemsLength === 0) {
2259
+ return state;
2260
+ }
2261
+ number(itemHeight);
2262
+ if (index === -1) {
2263
+ return {
2264
+ ...state,
2265
+ focusedIndex: -1,
2266
+ focused: true
2267
+ };
2268
+ }
2269
+ const listHeight = height - headerHeight;
2270
+ if (index < minLineY + 1) {
2271
+ return focusIndexScrollUp(state, index, listHeight, itemHeight, itemsLength);
2272
+ }
2273
+ if (index >= maxLineY - 1) {
2274
+ return focusIndexScrollDown(state, index, listHeight, itemHeight, itemsLength);
2275
+ }
2276
+ return {
2277
+ ...state,
2278
+ focusedIndex: index,
2279
+ focused: true
2280
+ };
2281
+ };
2282
+
2283
+ const focusFirst = state => {
2284
+ const firstIndex = first();
2285
+ return focusIndex(state, firstIndex);
2286
+ };
2287
+
2288
+ const focusLast = state => {
2289
+ const {
2290
+ items
2291
+ } = state;
2292
+ const lastIndex = last(items);
2293
+ return focusIndex(state, lastIndex);
2294
+ };
2295
+
2296
+ const focusNext = state => {
2297
+ const {
2298
+ focusedIndex,
2299
+ items
2300
+ } = state;
2301
+ const nextIndex = next(items, focusedIndex);
2302
+ return focusIndex(state, nextIndex);
2303
+ };
2304
+
2305
+ const focusNextPage = state => {
2306
+ const {
2307
+ focusedIndex,
2308
+ items,
2309
+ maxLineY,
2310
+ minLineY
2311
+ } = state;
2312
+ if (isLastIndex(items, focusedIndex)) {
2313
+ return state;
2314
+ }
2315
+ const indexNextPage = Math.min(maxLineY + (maxLineY - minLineY) - 2, lastIndex(items));
2316
+ return focusIndex(state, indexNextPage);
2317
+ };
2318
+
2319
+ const focusPreviousPage = state => {
2320
+ const {
2321
+ focusedIndex,
2322
+ minLineY,
2323
+ maxLineY
2324
+ } = state;
2325
+ if (focusedIndex === 0 || focusedIndex === -1) {
2326
+ return state;
2327
+ }
2328
+ const indexPreviousPage = Math.max(minLineY - (maxLineY - minLineY) + 1, 0);
2329
+ return focusIndex(state, indexPreviousPage);
2330
+ };
2331
+
2332
+ const focusPrevious = state => {
2333
+ const {
2334
+ focusedIndex,
2335
+ items
2336
+ } = state;
2337
+ if (focusedIndex === 0 || focusedIndex === -1) {
2338
+ return state;
2339
+ }
2340
+ const previousIndex = previous(items, focusedIndex);
2341
+ return focusIndex(state, previousIndex);
2342
+ };
2343
+
2344
+ const getListIndex = (eventX, eventY, x, y, deltaY, itemHeight) => {
2345
+ const relativeY = eventY - y + deltaY;
2346
+ const index = Math.floor(relativeY / itemHeight);
2347
+ return index;
2348
+ };
2349
+
2350
+ const openUri = async (path, focus = true, props = {}) => {
2351
+ await invoke$1(/* Main.openUri */'Main.openUri', /* uri */path, /* focus */focus, props);
2352
+ };
2353
+
2354
+ const getFileIndex = (items, index) => {
2355
+ for (let i = index; i >= 0; i--) {
2356
+ const item = items[i];
2357
+ if (item.type === File$1) {
2358
+ return i;
2359
+ }
2360
+ }
2361
+ return -1;
2362
+ };
2363
+ const selectIndexFile = async (state, searchResult, index) => {
2364
+ const {
2365
+ collapsedPaths
2366
+ } = state;
2367
+ const path = getAbsolutePath(searchResult.text);
2368
+ string(path);
2369
+ return {
2370
+ ...state,
2371
+ collapsedPaths: [...collapsedPaths, path],
2372
+ listFocusedIndex: index,
2373
+ listFocused: true
2374
+ };
2375
+ };
2376
+ const selectIndexPreview = async (state, searchResult, index) => {
2377
+ const {
2378
+ items
2379
+ } = state;
2380
+ const fileIndex = getFileIndex(items, index);
2381
+ if (fileIndex === -1) {
2382
+ throw new Error('Search result is missing file');
2383
+ }
2384
+ const {
2385
+ lineNumber
2386
+ } = searchResult;
2387
+ // console.log({ searchResult })
2388
+ const fileResult = items[fileIndex];
2389
+ const path = getAbsolutePath(fileResult.text);
2390
+ string(path);
2391
+ await openUri(path, true, {
2392
+ selections: new Uint32Array([lineNumber, 0, lineNumber, 0])
2393
+ });
2394
+ return {
2395
+ ...state,
2396
+ listFocusedIndex: index,
2397
+ listFocused: false
2398
+ };
2399
+ };
2400
+ const selectIndex = async (state, index, isClose) => {
2401
+ if (index === -1) {
2402
+ return {
2403
+ ...state,
2404
+ listFocused: true,
2405
+ listFocusedIndex: -1
2406
+ };
2407
+ }
2408
+ const {
2409
+ items
2410
+ } = state;
2411
+ const searchResult = items[index];
2412
+ switch (searchResult.type) {
2413
+ case File$1:
2414
+ return selectIndexFile(state, searchResult, index);
2415
+ case Match:
2416
+ return selectIndexPreview(state, searchResult, index);
2417
+ default:
2418
+ throw new Error(`unexpected search result type ${searchResult.type}`);
2419
+ }
2420
+ };
2421
+
2422
+ const handleClickAt = async (state, eventX, eventY) => {
2423
+ const {
2424
+ x,
2425
+ y,
2426
+ itemHeight,
2427
+ deltaY
2428
+ } = state;
2429
+ const index = getListIndex(eventX, eventY, x, y, deltaY, itemHeight);
2430
+ return selectIndex(state, index);
2431
+ };
2432
+
2433
+ const handleScrollBarCaptureLost = state => {
2434
+ return {
2435
+ ...state,
2436
+ scrollBarActive: false
2437
+ };
2438
+ };
2439
+
2440
+ const clamp = (num, min, max) => {
2441
+ number(num);
2442
+ number(min);
2443
+ number(max);
2444
+ return Math.min(Math.max(num, min), max);
2445
+ };
2446
+
2447
+ const setDeltaY = (state, value) => {
2448
+ object(state);
2449
+ number(value);
2450
+ const {
2451
+ itemHeight,
2452
+ finalDeltaY,
2453
+ deltaY,
2454
+ height,
2455
+ headerHeight
2456
+ } = state;
2457
+ const listHeight = height - headerHeight;
2458
+ const newDeltaY = clamp(value, 0, finalDeltaY);
2459
+ if (deltaY === newDeltaY) {
2460
+ return state;
2461
+ }
2462
+ // TODO when it only moves by one px, extensions don't need to be rerendered, only negative margin
2463
+ const minLineY = Math.floor(newDeltaY / itemHeight);
2464
+ const maxLineY = minLineY + getNumberOfVisibleItems(listHeight, itemHeight);
2465
+ return {
2466
+ ...state,
2467
+ deltaY: newDeltaY,
2468
+ minLineY,
2469
+ maxLineY
2470
+ };
2471
+ };
2472
+
2473
+ const handleScrollBarClick = (state, eventY) => {
2474
+ const {
2475
+ y,
2476
+ deltaY,
2477
+ headerHeight,
2478
+ finalDeltaY,
2479
+ scrollBarHeight,
2480
+ height
2481
+ } = state;
2482
+ const contentHeight = height - headerHeight;
2483
+ const relativeY = eventY - y - headerHeight;
2484
+ const currentScrollBarY = getScrollBarY(deltaY, finalDeltaY, contentHeight, scrollBarHeight);
2485
+ const diff = relativeY - currentScrollBarY;
2486
+ if (diff >= 0 && diff < scrollBarHeight) {
2487
+ return {
2488
+ ...state,
2489
+ handleOffset: diff,
2490
+ scrollBarActive: true
2491
+ };
2492
+ }
2493
+ const {
2494
+ percent,
2495
+ handleOffset
2496
+ } = getNewDeltaPercent(contentHeight, scrollBarHeight, relativeY);
2497
+ const newDeltaY = percent * finalDeltaY;
2498
+ return {
2499
+ ...setDeltaY(state, newDeltaY),
2500
+ handleOffset,
2501
+ scrollBarActive: true
2502
+ };
2503
+ };
2504
+
2505
+ const getNewPercent = (contentHeight, scrollBarHeight, relativeY) => {
2506
+ if (relativeY <= contentHeight - scrollBarHeight / 2) {
2507
+ // clicked in middle
2508
+ return relativeY / (contentHeight - scrollBarHeight);
2509
+ }
2510
+ // clicked at bottom
2511
+ return 1;
2512
+ };
2513
+ const handleScrollBarMove = (state, eventY) => {
2514
+ const {
2515
+ y,
2516
+ headerHeight,
2517
+ handleOffset,
2518
+ finalDeltaY,
2519
+ height,
2520
+ scrollBarHeight
2521
+ } = state;
2522
+ const relativeY = eventY - y - headerHeight - handleOffset;
2523
+ const contentHeight = height - headerHeight;
2524
+ const newPercent = getNewPercent(contentHeight, scrollBarHeight, relativeY);
2525
+ const newDeltaY = newPercent * finalDeltaY;
2526
+ return setDeltaY(state, newDeltaY);
2527
+ };
2528
+
2529
+ const handleWheel = (state, deltaMode, deltaY) => {
2530
+ number(deltaMode);
2531
+ number(deltaY);
2532
+ return setDeltaY(state, state.deltaY + deltaY);
2533
+ };
2534
+
2502
2535
  const getSavedValue = savedState => {
2503
2536
  if (savedState && savedState.value) {
2504
2537
  return savedState.value;
@@ -2520,8 +2553,7 @@ const getSavedCollapsedPaths = savedState => {
2520
2553
  const getThreads = () => {
2521
2554
  return 1;
2522
2555
  };
2523
- const loadContent = async (uid, savedState) => {
2524
- const state = get(uid);
2556
+ const loadContent = async (state, savedState) => {
2525
2557
  const savedValue = getSavedValue(savedState);
2526
2558
  const savedReplaceExpanded = getSavedReplaceExpanded(savedState);
2527
2559
  const savedCollapsedPaths = getSavedCollapsedPaths(savedState);
@@ -2552,10 +2584,10 @@ const renderItems = {
2552
2584
  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;
2553
2585
  },
2554
2586
  apply(oldState, newState) {
2555
- const displayResults = getDisplayResults(newState.items, newState.itemHeight, newState.fileCount, newState.value, newState.minLineY, newState.maxLineY, newState.replacement, newState.collapsedPaths, newState.focusedIndex);
2587
+ const displayResults = getDisplayResults(newState.items, newState.itemHeight, newState.fileCount, newState.value, newState.minLineY, newState.maxLineY, newState.replacement, newState.collapsedPaths, newState.icons, newState.focusedIndex);
2556
2588
  const focusOutline = newState.listFocused && newState.listFocusedIndex === -1;
2557
2589
  const dom = getSearchVirtualDom(displayResults, newState.replaceExpanded, newState.matchCase, newState.matchWholeWord, newState.useRegularExpression, newState.message, newState.detailsExpanded, focusOutline);
2558
- return ['Viewlet.setDom2', dom];
2590
+ return ['Viewlet.setDom2', newState.uid, dom];
2559
2591
  }
2560
2592
  };
2561
2593
  const getSelector = focusKey => {
@@ -2580,11 +2612,15 @@ const renderFocus = {
2580
2612
  },
2581
2613
  apply(newState) {
2582
2614
  const selector = getSelector(newState.focus);
2583
- return ['setFocus', selector];
2615
+ return ['Viewlet.send', newState.uid, 'setFocus', selector];
2584
2616
  }
2585
2617
  };
2586
2618
  const render = [renderItems, renderFocus];
2587
- const doRender = (oldState, newState) => {
2619
+ const doRender = uid => {
2620
+ const {
2621
+ oldState,
2622
+ newState
2623
+ } = get(uid);
2588
2624
  const commands = [];
2589
2625
  for (const fn of render) {
2590
2626
  if (!fn.isEqual(oldState, newState)) {
@@ -2594,21 +2630,51 @@ const doRender = (oldState, newState) => {
2594
2630
  return commands;
2595
2631
  };
2596
2632
 
2633
+ const saveState = uid => {
2634
+ const {
2635
+ newState
2636
+ } = get(uid);
2637
+ const {
2638
+ value,
2639
+ replaceExpanded
2640
+ } = newState;
2641
+ return {
2642
+ value,
2643
+ replaceExpanded
2644
+ };
2645
+ };
2646
+
2647
+ const setFocus = async focusKey => {
2648
+ await invoke$1('Focus.setFocus', focusKey);
2649
+ };
2650
+
2651
+ const getSearchFocusKey = key => {
2652
+ switch (key) {
2653
+ case 'search-value':
2654
+ return FocusSearchInput;
2655
+ case 'search-replace-value':
2656
+ return FocusSearchReplaceInput;
2657
+ case 'Match Case':
2658
+ return FocusSearchMatchCase;
2659
+ case 'Match Whole Word':
2660
+ return FocusSearchWholeWord;
2661
+ case 'Use Regular Expression':
2662
+ return FocusSearchRegex;
2663
+ case 'Replace All':
2664
+ return FocusSearchReplaceAll;
2665
+ case 'Preserve Case':
2666
+ return FocusSearchPreserveCase;
2667
+ default:
2668
+ return Empty$1;
2669
+ }
2670
+ };
2671
+
2597
2672
  const focusSearchValue = state => {
2598
2673
  return {
2599
2674
  ...state,
2600
2675
  focus: FocusSearchInput
2601
2676
  };
2602
2677
  };
2603
- const focusSearchValueNext = state => {
2604
- const {
2605
- replaceExpanded
2606
- } = state;
2607
- if (replaceExpanded) {
2608
- return focusReplaceValue(state);
2609
- }
2610
- return focusMatchCase(state);
2611
- };
2612
2678
  const focusMatchCasePrevious = state => {
2613
2679
  const {
2614
2680
  replaceExpanded
@@ -2660,6 +2726,17 @@ const focusReplaceAll = state => {
2660
2726
  focus: FocusSearchReplaceAll
2661
2727
  };
2662
2728
  };
2729
+ const handleFocusIn = (state, key) => {
2730
+ const focusKey = getSearchFocusKey(key);
2731
+ if (state.focus === focusKey) {
2732
+ return state;
2733
+ }
2734
+ setFocus(focusKey);
2735
+ return {
2736
+ ...state,
2737
+ focus: focusKey
2738
+ };
2739
+ };
2663
2740
 
2664
2741
  const submit = state => {
2665
2742
  return handleUpdate(state, {
@@ -2668,6 +2745,16 @@ const submit = state => {
2668
2745
  });
2669
2746
  };
2670
2747
 
2748
+ const toggleReplace = state => {
2749
+ const {
2750
+ replaceExpanded
2751
+ } = state;
2752
+ return {
2753
+ ...state,
2754
+ replaceExpanded: !replaceExpanded
2755
+ };
2756
+ };
2757
+
2671
2758
  const Keyboard = -1;
2672
2759
 
2673
2760
  const show = async (x, y, id, ...args) => {
@@ -2701,48 +2788,61 @@ const handleContextMenu = (state, button, x, y) => {
2701
2788
  }
2702
2789
  };
2703
2790
 
2791
+ const wrapCommand = fn => {
2792
+ const wrapped = async (uid, ...args) => {
2793
+ const {
2794
+ newState
2795
+ } = get(uid);
2796
+ const newerState = await fn(newState, ...args);
2797
+ set(uid, newState, newerState);
2798
+ };
2799
+ return wrapped;
2800
+ };
2801
+
2704
2802
  const commandMap = {
2705
- 'SearchView.focusMatchCase': focusMatchCase$1,
2706
- 'SearchView.focusReplaceValue': focusReplaceValue$1,
2707
- 'SearchView.focusSearchValue': focusSearchValue$1,
2708
- 'SearchView.focusSearchValueNext': focusSearchValueNext$1,
2709
- 'TextSearch.clearSearchResults': clearSearchResults$1,
2710
- 'TextSearch.copy': copy,
2803
+ 'TextSearch.clearSearchResults': wrapCommand(clearSearchResults$1),
2804
+ 'TextSearch.copy': wrapCommand(copy),
2805
+ 'TextSearch.handleInput': wrapCommand(handleInput),
2806
+ 'TextSearch.dismissItem': wrapCommand(dismissItem),
2807
+ 'TextSearch.focusFirst': wrapCommand(focusFirst),
2808
+ 'TextSearch.focusIndex': wrapCommand(focusIndex),
2809
+ 'TextSearch.focusLast': wrapCommand(focusLast),
2810
+ 'TextSearch.focusMatchCase': wrapCommand(focusMatchCase$1),
2811
+ 'TextSearch.focusMatchCasePrevious': wrapCommand(focusMatchCasePrevious),
2812
+ 'TextSearch.focusMatchWholeWord': wrapCommand(focusMatchWholeWord),
2813
+ 'TextSearch.focusNext': wrapCommand(focusNext),
2814
+ 'TextSearch.focusNextPage': wrapCommand(focusNextPage),
2815
+ 'TextSearch.focusPreserveCase': wrapCommand(focusPreserveCase),
2816
+ 'TextSearch.focusPrevious': wrapCommand(focusPrevious),
2817
+ 'TextSearch.focusPreviousPage': wrapCommand(focusPreviousPage),
2818
+ 'TextSearch.focusRegex': wrapCommand(focusRegex),
2819
+ 'TextSearch.focusRegexNext': wrapCommand(focusRegexNext),
2820
+ 'TextSearch.focusReplaceAll': wrapCommand(focusReplaceAll),
2821
+ 'TextSearch.focusReplaceValue': wrapCommand(focusReplaceValue$1),
2822
+ 'TextSearch.focusReplaceValueNext': wrapCommand(focusReplaceValueNext),
2823
+ 'TextSearch.focusSearchValue': wrapCommand(focusSearchValue$1),
2824
+ 'TextSearch.focusSearchValueNext': wrapCommand(focusSearchValueNext),
2825
+ 'TextSearch.getActions': wrapCommand(getActions),
2826
+ 'TextSearch.getActionsVirtualDom': wrapCommand(getActionsVirtualDom),
2827
+ 'TextSearch.getSearchDisplayResults': wrapCommand(getDisplayResults),
2828
+ 'TextSearch.getVirtualDom': wrapCommand(getSearchVirtualDom),
2829
+ 'TextSearch.handleClickAt': wrapCommand(handleClickAt),
2830
+ 'TextSearch.handleContextMenu': wrapCommand(handleContextMenu),
2831
+ 'TextSearch.handleFocusIn': wrapCommand(handleFocusIn),
2832
+ 'TextSearch.handleIconThemeChange': wrapCommand(handleIconThemeChange),
2833
+ 'TextSearch.handleScrollBarCaptureLost': wrapCommand(handleScrollBarCaptureLost),
2834
+ 'TextSearch.handleScrollBarClick': wrapCommand(handleScrollBarClick),
2835
+ 'TextSearch.handleScrollBarMove': wrapCommand(handleScrollBarMove),
2836
+ 'TextSearch.handleWheel': wrapCommand(handleWheel),
2837
+ 'TextSearch.loadContent': wrapCommand(loadContent),
2838
+ 'TextSearch.refresh': wrapCommand(refresh),
2839
+ 'TextSearch.submit': wrapCommand(submit),
2840
+ 'TextSearch.toggleReplace': wrapCommand(toggleReplace),
2841
+ // not wrapped
2711
2842
  'TextSearch.create': create,
2712
- 'TextSearch.dismissItem': dismissItem,
2713
- 'TextSearch.doRender': doRender,
2714
- 'TextSearch.focusFirst': focusFirst,
2715
- 'TextSearch.focusIndex': focusIndex,
2716
- 'TextSearch.focusLast': focusLast,
2717
- 'TextSearch.focusMatchCase': focusMatchCase,
2718
- 'TextSearch.focusMatchCasePrevious': focusMatchCasePrevious,
2719
- 'TextSearch.focusMatchWholeWord': focusMatchWholeWord,
2720
- 'TextSearch.focusNext': focusNext,
2721
- 'TextSearch.focusNextPage': focusNextPage,
2722
- 'TextSearch.focusPreserveCase': focusPreserveCase,
2723
- 'TextSearch.focusPrevious': focusPrevious,
2724
- 'TextSearch.focusPreviousPage': focusPreviousPage,
2725
- 'TextSearch.focusRegex': focusRegex,
2726
- 'TextSearch.focusRegexNext': focusRegexNext,
2727
- 'TextSearch.focusReplaceAll': focusReplaceAll,
2728
- 'TextSearch.focusReplaceValue': focusReplaceValue,
2729
- 'TextSearch.focusReplaceValueNext': focusReplaceValueNext,
2730
- 'TextSearch.focusSearchValue': focusSearchValue,
2731
- 'TextSearch.focusSearchValueNext': focusSearchValueNext,
2732
- 'TextSearch.getActions': getActions,
2733
- 'TextSearch.getActionsVirtualDom': getActionsVirtualDom,
2734
- 'TextSearch.getSearchDisplayResults': getDisplayResults,
2735
- 'TextSearch.getVirtualDom': getSearchVirtualDom,
2736
- 'TextSearch.handleClickAt': handleClickAt,
2737
- 'TextSearch.handleContextMenu': handleContextMenu,
2738
- 'TextSearch.handleIconThemeChange': handleIconThemeChange,
2739
- 'TextSearch.handleScrollBarCaptureLost': handleScrollBarCaptureLost,
2740
- 'TextSearch.handleScrollBarClick': handleScrollBarClick,
2741
- 'TextSearch.handleScrollBarMove': handleScrollBarMove,
2742
- 'TextSearch.handleWheel': handleWheel,
2743
- 'TextSearch.loadContent': loadContent,
2744
- 'TextSearch.refresh': refresh,
2745
- 'TextSearch.submit': submit,
2843
+ 'TextSearch.render': doRender,
2844
+ 'TextSearch.saveState': saveState,
2845
+ // TODO needed?
2746
2846
  'TextSearch.textSearch': textSearch
2747
2847
  };
2748
2848
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/text-search-worker",
3
- "version": "2.5.0",
3
+ "version": "2.7.0",
4
4
  "description": "Text Search Worker",
5
5
  "main": "dist/textSearchWorkerMain.js",
6
6
  "type": "module",