@lvce-editor/text-search-worker 2.5.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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 = ({
@@ -502,7 +505,7 @@ const create = (uid, x, y, width, height, workspacePath) => {
502
505
  message: '',
503
506
  collapsedPaths: []
504
507
  };
505
- set(uid, state);
508
+ set(uid, state, state);
506
509
  };
507
510
 
508
511
  // based on https://github.com/microsoft/vscode/blob/c0769274fa136b45799edeccc0d0a2f645b75caf/src/vs/base/common/arrays.ts#L625 (License MIT)
@@ -822,7 +825,7 @@ const focusSearchValue$1 = state => {
822
825
  };
823
826
  };
824
827
 
825
- const focusSearchValueNext$1 = state => {
828
+ const focusSearchValueNext = state => {
826
829
  const {
827
830
  replaceExpanded
828
831
  } = state;
@@ -1386,17 +1389,8 @@ const handleIconThemeChange = state => {
1386
1389
  };
1387
1390
  };
1388
1391
 
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
- };
1398
- const previous = (items, index) => {
1399
- return index === 0 ? items.length - 1 : index - 1;
1392
+ const handleError = async (error, notify = true, prefix = '') => {
1393
+ console.error(`[text-search-worker] ${error}`);
1400
1394
  };
1401
1395
 
1402
1396
  // TODO optimize this function to return the minimum number
@@ -1406,224 +1400,29 @@ const getNumberOfVisibleItems = (listHeight, itemHeight) => {
1406
1400
  return Math.ceil(listHeight / itemHeight) + 1;
1407
1401
  };
1408
1402
 
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;
1403
+ const getTextSearchResultCounts = results => {
1404
+ let resultCount = 0;
1405
+ let fileCount = 0;
1406
+ for (const result of results) {
1407
+ switch (result.type) {
1408
+ case File$1:
1409
+ fileCount++;
1410
+ break;
1411
+ case Match:
1412
+ resultCount++;
1413
+ break;
1548
1414
  }
1549
1415
  }
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
1416
  return {
1584
- ...state,
1585
- listFocusedIndex: index,
1586
- listFocused: false
1417
+ fileCount,
1418
+ resultCount
1587
1419
  };
1588
1420
  };
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
1421
 
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
- };
1422
+ const EmptyString = '';
1621
1423
 
1622
- const handleScrollBarCaptureLost = state => {
1623
- return {
1624
- ...state,
1625
- scrollBarActive: false
1626
- };
1424
+ const isEmptyString = string => {
1425
+ return string === EmptyString;
1627
1426
  };
1628
1427
 
1629
1428
  /**
@@ -1667,137 +1466,13 @@ const getNewDeltaPercent = (height, scrollBarHeight, relativeY) => {
1667
1466
  };
1668
1467
  };
1669
1468
 
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
- const RE_PROTOCOL = /^([a-z\-]+):\/\//;
1795
- const getProtocol = uri => {
1796
- const protocolMatch = uri.match(RE_PROTOCOL);
1797
- if (protocolMatch) {
1798
- return protocolMatch[1];
1799
- }
1800
- return '';
1469
+ const RE_PROTOCOL = /^([a-z\-]+):\/\//;
1470
+ const getProtocol = uri => {
1471
+ const protocolMatch = uri.match(RE_PROTOCOL);
1472
+ if (protocolMatch) {
1473
+ return protocolMatch[1];
1474
+ }
1475
+ return '';
1801
1476
  };
1802
1477
 
1803
1478
  const textSearch$5 = async (scheme, root, query) => {
@@ -2499,6 +2174,341 @@ const handleUpdate = async (state, update) => {
2499
2174
  }
2500
2175
  };
2501
2176
 
2177
+ const handleInput = (state, value, inputSource = Script) => {
2178
+ return handleUpdate(state, {
2179
+ value,
2180
+ inputSource
2181
+ });
2182
+ };
2183
+
2184
+ const first = () => {
2185
+ return 0;
2186
+ };
2187
+ const last = items => {
2188
+ return items.length - 1;
2189
+ };
2190
+ const next = (items, index) => {
2191
+ return (index + 1) % items.length;
2192
+ };
2193
+ const previous = (items, index) => {
2194
+ return index === 0 ? items.length - 1 : index - 1;
2195
+ };
2196
+
2197
+ const focusIndexScrollUp = (state, index, listHeight, itemHeight, itemsLength) => {
2198
+ const newMinLineY = index;
2199
+ const fittingItems = getNumberOfVisibleItems(listHeight, itemHeight);
2200
+ const newMaxLineY = Math.min(newMinLineY + fittingItems, itemsLength);
2201
+ const newDeltaY = newMinLineY * itemHeight;
2202
+ return {
2203
+ ...state,
2204
+ focusedIndex: index,
2205
+ minLineY: newMinLineY,
2206
+ maxLineY: newMaxLineY,
2207
+ focused: true,
2208
+ deltaY: newDeltaY
2209
+ };
2210
+ };
2211
+ const focusIndexScrollDown = (state, index, listHeight, itemHeight, itemsLength) => {
2212
+ const newMaxLineY = Math.min(index + 1, itemsLength);
2213
+ const fittingItems = getNumberOfVisibleItems(listHeight, itemHeight);
2214
+ const newMinLineY = Math.max(newMaxLineY - fittingItems, 0);
2215
+ const newDeltaY = itemsLength < fittingItems ? 0 : newMinLineY * itemHeight - listHeight % itemHeight + itemHeight;
2216
+ return {
2217
+ ...state,
2218
+ focusedIndex: index,
2219
+ minLineY: newMinLineY,
2220
+ maxLineY: newMaxLineY,
2221
+ focused: true,
2222
+ deltaY: newDeltaY
2223
+ };
2224
+ };
2225
+ const focusIndex = (state, index) => {
2226
+ const {
2227
+ itemHeight,
2228
+ minLineY,
2229
+ maxLineY,
2230
+ headerHeight,
2231
+ height,
2232
+ items
2233
+ } = state;
2234
+ const itemsLength = items.length;
2235
+ if (itemsLength === 0) {
2236
+ return state;
2237
+ }
2238
+ number(itemHeight);
2239
+ if (index === -1) {
2240
+ return {
2241
+ ...state,
2242
+ focusedIndex: -1,
2243
+ focused: true
2244
+ };
2245
+ }
2246
+ const listHeight = height - headerHeight;
2247
+ if (index < minLineY + 1) {
2248
+ return focusIndexScrollUp(state, index, listHeight, itemHeight, itemsLength);
2249
+ }
2250
+ if (index >= maxLineY - 1) {
2251
+ return focusIndexScrollDown(state, index, listHeight, itemHeight, itemsLength);
2252
+ }
2253
+ return {
2254
+ ...state,
2255
+ focusedIndex: index,
2256
+ focused: true
2257
+ };
2258
+ };
2259
+
2260
+ const focusFirst = state => {
2261
+ const firstIndex = first();
2262
+ return focusIndex(state, firstIndex);
2263
+ };
2264
+
2265
+ const focusLast = state => {
2266
+ const {
2267
+ items
2268
+ } = state;
2269
+ const lastIndex = last(items);
2270
+ return focusIndex(state, lastIndex);
2271
+ };
2272
+
2273
+ const focusNext = state => {
2274
+ const {
2275
+ focusedIndex,
2276
+ items
2277
+ } = state;
2278
+ const nextIndex = next(items, focusedIndex);
2279
+ return focusIndex(state, nextIndex);
2280
+ };
2281
+
2282
+ const focusNextPage = state => {
2283
+ const {
2284
+ focusedIndex,
2285
+ items,
2286
+ maxLineY,
2287
+ minLineY
2288
+ } = state;
2289
+ if (isLastIndex(items, focusedIndex)) {
2290
+ return state;
2291
+ }
2292
+ const indexNextPage = Math.min(maxLineY + (maxLineY - minLineY) - 2, lastIndex(items));
2293
+ return focusIndex(state, indexNextPage);
2294
+ };
2295
+
2296
+ const focusPreviousPage = state => {
2297
+ const {
2298
+ focusedIndex,
2299
+ minLineY,
2300
+ maxLineY
2301
+ } = state;
2302
+ if (focusedIndex === 0 || focusedIndex === -1) {
2303
+ return state;
2304
+ }
2305
+ const indexPreviousPage = Math.max(minLineY - (maxLineY - minLineY) + 1, 0);
2306
+ return focusIndex(state, indexPreviousPage);
2307
+ };
2308
+
2309
+ const focusPrevious = state => {
2310
+ const {
2311
+ focusedIndex,
2312
+ items
2313
+ } = state;
2314
+ if (focusedIndex === 0 || focusedIndex === -1) {
2315
+ return state;
2316
+ }
2317
+ const previousIndex = previous(items, focusedIndex);
2318
+ return focusIndex(state, previousIndex);
2319
+ };
2320
+
2321
+ const getListIndex = (eventX, eventY, x, y, deltaY, itemHeight) => {
2322
+ const relativeY = eventY - y + deltaY;
2323
+ const index = Math.floor(relativeY / itemHeight);
2324
+ return index;
2325
+ };
2326
+
2327
+ const openUri = async (path, focus = true, props = {}) => {
2328
+ await invoke$1(/* Main.openUri */'Main.openUri', /* uri */path, /* focus */focus, props);
2329
+ };
2330
+
2331
+ const getFileIndex = (items, index) => {
2332
+ for (let i = index; i >= 0; i--) {
2333
+ const item = items[i];
2334
+ if (item.type === File$1) {
2335
+ return i;
2336
+ }
2337
+ }
2338
+ return -1;
2339
+ };
2340
+ const selectIndexFile = async (state, searchResult, index) => {
2341
+ const {
2342
+ collapsedPaths
2343
+ } = state;
2344
+ const path = getAbsolutePath(searchResult.text);
2345
+ string(path);
2346
+ return {
2347
+ ...state,
2348
+ collapsedPaths: [...collapsedPaths, path],
2349
+ listFocusedIndex: index,
2350
+ listFocused: true
2351
+ };
2352
+ };
2353
+ const selectIndexPreview = async (state, searchResult, index) => {
2354
+ const {
2355
+ items
2356
+ } = state;
2357
+ const fileIndex = getFileIndex(items, index);
2358
+ if (fileIndex === -1) {
2359
+ throw new Error('Search result is missing file');
2360
+ }
2361
+ const {
2362
+ lineNumber
2363
+ } = searchResult;
2364
+ // console.log({ searchResult })
2365
+ const fileResult = items[fileIndex];
2366
+ const path = getAbsolutePath(fileResult.text);
2367
+ string(path);
2368
+ await openUri(path, true, {
2369
+ selections: new Uint32Array([lineNumber, 0, lineNumber, 0])
2370
+ });
2371
+ return {
2372
+ ...state,
2373
+ listFocusedIndex: index,
2374
+ listFocused: false
2375
+ };
2376
+ };
2377
+ const selectIndex = async (state, index, isClose) => {
2378
+ if (index === -1) {
2379
+ return {
2380
+ ...state,
2381
+ listFocused: true,
2382
+ listFocusedIndex: -1
2383
+ };
2384
+ }
2385
+ const {
2386
+ items
2387
+ } = state;
2388
+ const searchResult = items[index];
2389
+ switch (searchResult.type) {
2390
+ case File$1:
2391
+ return selectIndexFile(state, searchResult, index);
2392
+ case Match:
2393
+ return selectIndexPreview(state, searchResult, index);
2394
+ default:
2395
+ throw new Error(`unexpected search result type ${searchResult.type}`);
2396
+ }
2397
+ };
2398
+
2399
+ const handleClickAt = async (state, eventX, eventY) => {
2400
+ const {
2401
+ x,
2402
+ y,
2403
+ itemHeight,
2404
+ deltaY
2405
+ } = state;
2406
+ const index = getListIndex(eventX, eventY, x, y, deltaY, itemHeight);
2407
+ return selectIndex(state, index);
2408
+ };
2409
+
2410
+ const handleScrollBarCaptureLost = state => {
2411
+ return {
2412
+ ...state,
2413
+ scrollBarActive: false
2414
+ };
2415
+ };
2416
+
2417
+ const clamp = (num, min, max) => {
2418
+ number(num);
2419
+ number(min);
2420
+ number(max);
2421
+ return Math.min(Math.max(num, min), max);
2422
+ };
2423
+
2424
+ const setDeltaY = (state, value) => {
2425
+ object(state);
2426
+ number(value);
2427
+ const {
2428
+ itemHeight,
2429
+ finalDeltaY,
2430
+ deltaY,
2431
+ height,
2432
+ headerHeight
2433
+ } = state;
2434
+ const listHeight = height - headerHeight;
2435
+ const newDeltaY = clamp(value, 0, finalDeltaY);
2436
+ if (deltaY === newDeltaY) {
2437
+ return state;
2438
+ }
2439
+ // TODO when it only moves by one px, extensions don't need to be rerendered, only negative margin
2440
+ const minLineY = Math.floor(newDeltaY / itemHeight);
2441
+ const maxLineY = minLineY + getNumberOfVisibleItems(listHeight, itemHeight);
2442
+ return {
2443
+ ...state,
2444
+ deltaY: newDeltaY,
2445
+ minLineY,
2446
+ maxLineY
2447
+ };
2448
+ };
2449
+
2450
+ const handleScrollBarClick = (state, eventY) => {
2451
+ const {
2452
+ y,
2453
+ deltaY,
2454
+ headerHeight,
2455
+ finalDeltaY,
2456
+ scrollBarHeight,
2457
+ height
2458
+ } = state;
2459
+ const contentHeight = height - headerHeight;
2460
+ const relativeY = eventY - y - headerHeight;
2461
+ const currentScrollBarY = getScrollBarY(deltaY, finalDeltaY, contentHeight, scrollBarHeight);
2462
+ const diff = relativeY - currentScrollBarY;
2463
+ if (diff >= 0 && diff < scrollBarHeight) {
2464
+ return {
2465
+ ...state,
2466
+ handleOffset: diff,
2467
+ scrollBarActive: true
2468
+ };
2469
+ }
2470
+ const {
2471
+ percent,
2472
+ handleOffset
2473
+ } = getNewDeltaPercent(contentHeight, scrollBarHeight, relativeY);
2474
+ const newDeltaY = percent * finalDeltaY;
2475
+ return {
2476
+ ...setDeltaY(state, newDeltaY),
2477
+ handleOffset,
2478
+ scrollBarActive: true
2479
+ };
2480
+ };
2481
+
2482
+ const getNewPercent = (contentHeight, scrollBarHeight, relativeY) => {
2483
+ if (relativeY <= contentHeight - scrollBarHeight / 2) {
2484
+ // clicked in middle
2485
+ return relativeY / (contentHeight - scrollBarHeight);
2486
+ }
2487
+ // clicked at bottom
2488
+ return 1;
2489
+ };
2490
+ const handleScrollBarMove = (state, eventY) => {
2491
+ const {
2492
+ y,
2493
+ headerHeight,
2494
+ handleOffset,
2495
+ finalDeltaY,
2496
+ height,
2497
+ scrollBarHeight
2498
+ } = state;
2499
+ const relativeY = eventY - y - headerHeight - handleOffset;
2500
+ const contentHeight = height - headerHeight;
2501
+ const newPercent = getNewPercent(contentHeight, scrollBarHeight, relativeY);
2502
+ const newDeltaY = newPercent * finalDeltaY;
2503
+ return setDeltaY(state, newDeltaY);
2504
+ };
2505
+
2506
+ const handleWheel = (state, deltaMode, deltaY) => {
2507
+ number(deltaMode);
2508
+ number(deltaY);
2509
+ return setDeltaY(state, state.deltaY + deltaY);
2510
+ };
2511
+
2502
2512
  const getSavedValue = savedState => {
2503
2513
  if (savedState && savedState.value) {
2504
2514
  return savedState.value;
@@ -2520,8 +2530,7 @@ const getSavedCollapsedPaths = savedState => {
2520
2530
  const getThreads = () => {
2521
2531
  return 1;
2522
2532
  };
2523
- const loadContent = async (uid, savedState) => {
2524
- const state = get(uid);
2533
+ const loadContent = async (state, savedState) => {
2525
2534
  const savedValue = getSavedValue(savedState);
2526
2535
  const savedReplaceExpanded = getSavedReplaceExpanded(savedState);
2527
2536
  const savedCollapsedPaths = getSavedCollapsedPaths(savedState);
@@ -2555,7 +2564,7 @@ const renderItems = {
2555
2564
  const displayResults = getDisplayResults(newState.items, newState.itemHeight, newState.fileCount, newState.value, newState.minLineY, newState.maxLineY, newState.replacement, newState.collapsedPaths, newState.focusedIndex);
2556
2565
  const focusOutline = newState.listFocused && newState.listFocusedIndex === -1;
2557
2566
  const dom = getSearchVirtualDom(displayResults, newState.replaceExpanded, newState.matchCase, newState.matchWholeWord, newState.useRegularExpression, newState.message, newState.detailsExpanded, focusOutline);
2558
- return ['Viewlet.setDom2', dom];
2567
+ return ['Viewlet.setDom2', newState.uid, dom];
2559
2568
  }
2560
2569
  };
2561
2570
  const getSelector = focusKey => {
@@ -2584,7 +2593,11 @@ const renderFocus = {
2584
2593
  }
2585
2594
  };
2586
2595
  const render = [renderItems, renderFocus];
2587
- const doRender = (oldState, newState) => {
2596
+ const doRender = uid => {
2597
+ const {
2598
+ oldState,
2599
+ newState
2600
+ } = get(uid);
2588
2601
  const commands = [];
2589
2602
  for (const fn of render) {
2590
2603
  if (!fn.isEqual(oldState, newState)) {
@@ -2594,21 +2607,51 @@ const doRender = (oldState, newState) => {
2594
2607
  return commands;
2595
2608
  };
2596
2609
 
2610
+ const saveState = uid => {
2611
+ const {
2612
+ newState
2613
+ } = get(uid);
2614
+ const {
2615
+ value,
2616
+ replaceExpanded
2617
+ } = newState;
2618
+ return {
2619
+ value,
2620
+ replaceExpanded
2621
+ };
2622
+ };
2623
+
2624
+ const setFocus = async focusKey => {
2625
+ await invoke$1('Focus.setFocus', focusKey);
2626
+ };
2627
+
2628
+ const getSearchFocusKey = key => {
2629
+ switch (key) {
2630
+ case 'search-value':
2631
+ return FocusSearchInput;
2632
+ case 'search-replace-value':
2633
+ return FocusSearchReplaceInput;
2634
+ case 'Match Case':
2635
+ return FocusSearchMatchCase;
2636
+ case 'Match Whole Word':
2637
+ return FocusSearchWholeWord;
2638
+ case 'Use Regular Expression':
2639
+ return FocusSearchRegex;
2640
+ case 'Replace All':
2641
+ return FocusSearchReplaceAll;
2642
+ case 'Preserve Case':
2643
+ return FocusSearchPreserveCase;
2644
+ default:
2645
+ return Empty$1;
2646
+ }
2647
+ };
2648
+
2597
2649
  const focusSearchValue = state => {
2598
2650
  return {
2599
2651
  ...state,
2600
2652
  focus: FocusSearchInput
2601
2653
  };
2602
2654
  };
2603
- const focusSearchValueNext = state => {
2604
- const {
2605
- replaceExpanded
2606
- } = state;
2607
- if (replaceExpanded) {
2608
- return focusReplaceValue(state);
2609
- }
2610
- return focusMatchCase(state);
2611
- };
2612
2655
  const focusMatchCasePrevious = state => {
2613
2656
  const {
2614
2657
  replaceExpanded
@@ -2660,6 +2703,17 @@ const focusReplaceAll = state => {
2660
2703
  focus: FocusSearchReplaceAll
2661
2704
  };
2662
2705
  };
2706
+ const handleFocusIn = (state, key) => {
2707
+ const focusKey = getSearchFocusKey(key);
2708
+ if (state.focus === focusKey) {
2709
+ return state;
2710
+ }
2711
+ setFocus(focusKey);
2712
+ return {
2713
+ ...state,
2714
+ focus: focusKey
2715
+ };
2716
+ };
2663
2717
 
2664
2718
  const submit = state => {
2665
2719
  return handleUpdate(state, {
@@ -2668,6 +2722,16 @@ const submit = state => {
2668
2722
  });
2669
2723
  };
2670
2724
 
2725
+ const toggleReplace = state => {
2726
+ const {
2727
+ replaceExpanded
2728
+ } = state;
2729
+ return {
2730
+ ...state,
2731
+ replaceExpanded: !replaceExpanded
2732
+ };
2733
+ };
2734
+
2671
2735
  const Keyboard = -1;
2672
2736
 
2673
2737
  const show = async (x, y, id, ...args) => {
@@ -2701,48 +2765,61 @@ const handleContextMenu = (state, button, x, y) => {
2701
2765
  }
2702
2766
  };
2703
2767
 
2768
+ const wrapCommand = fn => {
2769
+ const wrapped = async (uid, ...args) => {
2770
+ const {
2771
+ newState
2772
+ } = get(uid);
2773
+ const newerState = await fn(newState, ...args);
2774
+ set(uid, newState, newerState);
2775
+ };
2776
+ return wrapped;
2777
+ };
2778
+
2704
2779
  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,
2780
+ 'TextSearch.clearSearchResults': wrapCommand(clearSearchResults$1),
2781
+ 'TextSearch.copy': wrapCommand(copy),
2782
+ 'TextSearch.handleInput': wrapCommand(handleInput),
2783
+ 'TextSearch.dismissItem': wrapCommand(dismissItem),
2784
+ 'TextSearch.focusFirst': wrapCommand(focusFirst),
2785
+ 'TextSearch.focusIndex': wrapCommand(focusIndex),
2786
+ 'TextSearch.focusLast': wrapCommand(focusLast),
2787
+ 'TextSearch.focusMatchCase': wrapCommand(focusMatchCase$1),
2788
+ 'TextSearch.focusMatchCasePrevious': wrapCommand(focusMatchCasePrevious),
2789
+ 'TextSearch.focusMatchWholeWord': wrapCommand(focusMatchWholeWord),
2790
+ 'TextSearch.focusNext': wrapCommand(focusNext),
2791
+ 'TextSearch.focusNextPage': wrapCommand(focusNextPage),
2792
+ 'TextSearch.focusPreserveCase': wrapCommand(focusPreserveCase),
2793
+ 'TextSearch.focusPrevious': wrapCommand(focusPrevious),
2794
+ 'TextSearch.focusPreviousPage': wrapCommand(focusPreviousPage),
2795
+ 'TextSearch.focusRegex': wrapCommand(focusRegex),
2796
+ 'TextSearch.focusRegexNext': wrapCommand(focusRegexNext),
2797
+ 'TextSearch.focusReplaceAll': wrapCommand(focusReplaceAll),
2798
+ 'TextSearch.focusReplaceValue': wrapCommand(focusReplaceValue$1),
2799
+ 'TextSearch.focusReplaceValueNext': wrapCommand(focusReplaceValueNext),
2800
+ 'TextSearch.focusSearchValue': wrapCommand(focusSearchValue$1),
2801
+ 'TextSearch.focusSearchValueNext': wrapCommand(focusSearchValueNext),
2802
+ 'TextSearch.getActions': wrapCommand(getActions),
2803
+ 'TextSearch.getActionsVirtualDom': wrapCommand(getActionsVirtualDom),
2804
+ 'TextSearch.getSearchDisplayResults': wrapCommand(getDisplayResults),
2805
+ 'TextSearch.getVirtualDom': wrapCommand(getSearchVirtualDom),
2806
+ 'TextSearch.handleClickAt': wrapCommand(handleClickAt),
2807
+ 'TextSearch.handleContextMenu': wrapCommand(handleContextMenu),
2808
+ 'TextSearch.handleFocusIn': wrapCommand(handleFocusIn),
2809
+ 'TextSearch.handleIconThemeChange': wrapCommand(handleIconThemeChange),
2810
+ 'TextSearch.handleScrollBarCaptureLost': wrapCommand(handleScrollBarCaptureLost),
2811
+ 'TextSearch.handleScrollBarClick': wrapCommand(handleScrollBarClick),
2812
+ 'TextSearch.handleScrollBarMove': wrapCommand(handleScrollBarMove),
2813
+ 'TextSearch.handleWheel': wrapCommand(handleWheel),
2814
+ 'TextSearch.loadContent': wrapCommand(loadContent),
2815
+ 'TextSearch.refresh': wrapCommand(refresh),
2816
+ 'TextSearch.submit': wrapCommand(submit),
2817
+ 'TextSearch.toggleReplace': wrapCommand(toggleReplace),
2818
+ // not wrapped
2711
2819
  '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,
2820
+ 'TextSearch.render': doRender,
2821
+ 'TextSearch.saveState': saveState,
2822
+ // TODO needed?
2746
2823
  'TextSearch.textSearch': textSearch
2747
2824
  };
2748
2825
 
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.6.0",
4
4
  "description": "Text Search Worker",
5
5
  "main": "dist/textSearchWorkerMain.js",
6
6
  "type": "module",