@lvce-editor/file-search-worker 3.6.0 → 3.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.
@@ -100,6 +100,12 @@ const array = value => {
100
100
  throw new AssertionError('expected value to be of type array');
101
101
  }
102
102
  };
103
+ const string = value => {
104
+ const type = getType(value);
105
+ if (type !== 'string') {
106
+ throw new AssertionError('expected value to be of type string');
107
+ }
108
+ };
103
109
 
104
110
  const isMessagePort = value => {
105
111
  return value && value instanceof MessagePort;
@@ -1288,7 +1294,7 @@ const getFiles = () => {
1288
1294
  return state$3.files;
1289
1295
  };
1290
1296
 
1291
- const searchFile$4 = async () => {
1297
+ const searchFile$5 = async () => {
1292
1298
  const files = await getFiles();
1293
1299
  const keys = Object.keys(files);
1294
1300
  return keys;
@@ -1296,7 +1302,7 @@ const searchFile$4 = async () => {
1296
1302
 
1297
1303
  const SearchFileMemfs = {
1298
1304
  __proto__: null,
1299
- searchFile: searchFile$4
1305
+ searchFile: searchFile$5
1300
1306
  };
1301
1307
 
1302
1308
  const assetDir = '';
@@ -1326,7 +1332,7 @@ const removeLeadingSlash = path => {
1326
1332
  // TODO simplify code
1327
1333
  // 1. don't have playground prefix in fileMap json
1328
1334
  // 2. remove code here that removes the prefix
1329
- const searchFile$3 = async path => {
1335
+ const searchFile$4 = async path => {
1330
1336
  const fileList = await getJson(fileMapUrl);
1331
1337
  const result = fileList.map(removeLeadingSlash);
1332
1338
  const prefixLength = path.length - 'file:///'.length;
@@ -1339,7 +1345,7 @@ const searchFile$3 = async path => {
1339
1345
 
1340
1346
  const SearchFileFetch = {
1341
1347
  __proto__: null,
1342
- searchFile: searchFile$3
1348
+ searchFile: searchFile$4
1343
1349
  };
1344
1350
 
1345
1351
  const Directory = 'directory';
@@ -1686,7 +1692,7 @@ const searchFilesRecursively = async (all, parent, handle) => {
1686
1692
  }
1687
1693
  await Promise.all(promises);
1688
1694
  };
1689
- const searchFile$2 = async uri => {
1695
+ const searchFile$3 = async uri => {
1690
1696
  const path = uri.slice('html://'.length);
1691
1697
  const handle = await getDirectoryHandle(path);
1692
1698
  if (!handle) {
@@ -1700,7 +1706,7 @@ const searchFile$2 = async uri => {
1700
1706
 
1701
1707
  const SearchFileHtml = {
1702
1708
  __proto__: null,
1703
- searchFile: searchFile$2
1709
+ searchFile: searchFile$3
1704
1710
  };
1705
1711
 
1706
1712
  const emptyMatches = [];
@@ -1911,7 +1917,7 @@ const splitLines = lines => {
1911
1917
 
1912
1918
  // TODO create direct connection from electron to file search worker using message ports
1913
1919
 
1914
- const searchFile$1 = async (path, value, prepare) => {
1920
+ const searchFile$2 = async (path, value, prepare) => {
1915
1921
  const ripGrepArgs = getFileSearchRipGrepArgs();
1916
1922
  const options = {
1917
1923
  ripGrepArgs,
@@ -1929,7 +1935,7 @@ const searchFile$1 = async (path, value, prepare) => {
1929
1935
 
1930
1936
  const SearchFileRipGrep = {
1931
1937
  __proto__: null,
1932
- searchFile: searchFile$1
1938
+ searchFile: searchFile$2
1933
1939
  };
1934
1940
 
1935
1941
  const getModule = protocol => {
@@ -1944,7 +1950,8 @@ const getModule = protocol => {
1944
1950
  return SearchFileRipGrep;
1945
1951
  }
1946
1952
  };
1947
- const searchFile = async (path, value, prepare, assetDir) => {
1953
+
1954
+ const searchFile$1 = async (path, value, prepare, assetDir) => {
1948
1955
  const protocol = getProtocol(path);
1949
1956
  // TODO call different providers depending on protocol
1950
1957
  const module = await getModule(protocol);
@@ -1967,6 +1974,12 @@ const pathDirName = path => {
1967
1974
  return path.slice(0, index);
1968
1975
  };
1969
1976
 
1977
+ const searchFile = async (path, value) => {
1978
+ const prepare = true;
1979
+ // @ts-ignore
1980
+ const files = await searchFile$1(/* path */path, /* searchTerm */value, prepare);
1981
+ return files;
1982
+ };
1970
1983
  const name$7 = 'file';
1971
1984
  const getPlaceholder$a = () => {
1972
1985
  return '';
@@ -1988,9 +2001,14 @@ const getNoResults$9 = () => {
1988
2001
  };
1989
2002
  };
1990
2003
  const getPicks$a = async searchValue => {
1991
- {
2004
+ // TODO cache workspace path
2005
+ const workspace = await invoke$1('Workspace.getPath');
2006
+ if (!workspace) {
1992
2007
  return [];
1993
2008
  }
2009
+ const files = await searchFile(workspace, searchValue);
2010
+ // const picks = files.map(toPick)
2011
+ return files;
1994
2012
  };
1995
2013
  const selectPick$a = async pick => {
1996
2014
  if (typeof pick === 'object') {
@@ -2697,9 +2715,29 @@ const focusIndex = async (state, index) => {
2697
2715
  };
2698
2716
  };
2699
2717
 
2718
+ const first = () => {
2719
+ return 0;
2720
+ };
2721
+ const last = items => {
2722
+ return items.length - 1;
2723
+ };
2700
2724
  const next = (items, index) => {
2701
2725
  return (index + 1) % items.length;
2702
2726
  };
2727
+ const previous = (items, index) => {
2728
+ return index === 0 ? items.length - 1 : index - 1;
2729
+ };
2730
+
2731
+ const focusFirst = state => {
2732
+ return focusIndex(state, first());
2733
+ };
2734
+
2735
+ const focusLast = state => {
2736
+ const {
2737
+ items
2738
+ } = state;
2739
+ return focusIndex(state, last(items));
2740
+ };
2703
2741
 
2704
2742
  const focusNext = state => {
2705
2743
  const {
@@ -2710,6 +2748,20 @@ const focusNext = state => {
2710
2748
  return focusIndex(state, nextIndex);
2711
2749
  };
2712
2750
 
2751
+ const focusPrevious = state => {
2752
+ const {
2753
+ items,
2754
+ focusedIndex
2755
+ } = state;
2756
+ const previousIndex = previous(items, focusedIndex);
2757
+ return focusIndex(state, previousIndex);
2758
+ };
2759
+
2760
+ const commandIds = ['handleBlur', 'handleClickAt', 'handleWheel', 'selectCurrentIndex', 'selectIndex', 'selectItem', 'focusFirst', 'focusIndex', 'focusLast', 'focusNext', 'focusPrevious', 'handleInput', 'handleBeforeInput'];
2761
+ const getCommandIds = () => {
2762
+ return commandIds;
2763
+ };
2764
+
2713
2765
  const Enter = 3;
2714
2766
  const Escape = 8;
2715
2767
  const PageUp = 10;
@@ -2748,6 +2800,182 @@ const getKeyBindings = () => {
2748
2800
  }];
2749
2801
  };
2750
2802
 
2803
+ const InsertText = 'insertText';
2804
+ const DeleteContentBackward = 'deleteContentBackward';
2805
+ const DeleteContentForward = 'deleteContentForward';
2806
+ const DeleteWordForward = 'deleteWordForward';
2807
+ const DeleteWordBackward = 'deleteWordBackward';
2808
+ const InsertLineBreak = 'insertLineBreak';
2809
+ const InsertCompositionText = 'insertCompositionText';
2810
+ const InsertFromPaste = 'insertFromPaste';
2811
+
2812
+ const getNewValueInsertText = (value, selectionStart, selectionEnd, data) => {
2813
+ if (selectionStart === value.length) {
2814
+ const newValue = value + data;
2815
+ return {
2816
+ newValue,
2817
+ cursorOffset: newValue.length
2818
+ };
2819
+ }
2820
+ const before = value.slice(0, selectionStart);
2821
+ const after = value.slice(selectionEnd);
2822
+ const newValue = before + data + after;
2823
+ return {
2824
+ newValue,
2825
+ cursorOffset: selectionStart + data.length
2826
+ };
2827
+ };
2828
+ const getNewValueDeleteContentBackward = (value, selectionStart, selectionEnd, data) => {
2829
+ const after = value.slice(selectionEnd);
2830
+ if (selectionStart === selectionEnd) {
2831
+ const before = value.slice(0, selectionStart - 1);
2832
+ const newValue = before + after;
2833
+ return {
2834
+ newValue,
2835
+ cursorOffset: before.length
2836
+ };
2837
+ }
2838
+ const before = value.slice(0, selectionStart);
2839
+ const newValue = before + after;
2840
+ return {
2841
+ newValue,
2842
+ cursorOffset: selectionStart
2843
+ };
2844
+ };
2845
+ const RE_ALPHA_NUMERIC = /[a-z\d]/i;
2846
+ const isAlphaNumeric = character => {
2847
+ return RE_ALPHA_NUMERIC.test(character);
2848
+ };
2849
+ const getNewValueDeleteWordBackward = (value, selectionStart, selectionEnd, data) => {
2850
+ const after = value.slice(selectionEnd);
2851
+ if (selectionStart === selectionEnd) {
2852
+ let startIndex = Math.max(selectionStart - 1, 0);
2853
+ while (startIndex > 0 && isAlphaNumeric(value[startIndex])) {
2854
+ startIndex--;
2855
+ }
2856
+ const before = value.slice(0, startIndex);
2857
+ const newValue = before + after;
2858
+ return {
2859
+ newValue,
2860
+ cursorOffset: before.length
2861
+ };
2862
+ }
2863
+ const before = value.slice(0, selectionStart);
2864
+ const newValue = before + after;
2865
+ return {
2866
+ newValue,
2867
+ cursorOffset: selectionStart
2868
+ };
2869
+ };
2870
+ const getNewValueDeleteContentForward = (value, selectionStart, selectionEnd, data) => {
2871
+ const before = value.slice(0, selectionStart);
2872
+ if (selectionStart === selectionEnd) {
2873
+ const after = value.slice(selectionEnd + 1);
2874
+ const newValue = before + after;
2875
+ return {
2876
+ newValue,
2877
+ cursorOffset: selectionStart
2878
+ };
2879
+ }
2880
+ const after = value.slice(selectionEnd);
2881
+ const newValue = before + after;
2882
+ return {
2883
+ newValue,
2884
+ cursorOffset: selectionStart
2885
+ };
2886
+ };
2887
+ const getNewValueDeleteWordForward = (value, selectionStart, selectionEnd, data) => {
2888
+ const before = value.slice(0, selectionStart);
2889
+ if (selectionStart === selectionEnd) {
2890
+ let startIndex = Math.min(selectionStart + 1, value.length - 1);
2891
+ while (startIndex < value.length && isAlphaNumeric(value[startIndex])) {
2892
+ startIndex++;
2893
+ }
2894
+ const after = value.slice(startIndex);
2895
+ const newValue = before + after;
2896
+ return {
2897
+ newValue,
2898
+ cursorOffset: before.length
2899
+ };
2900
+ }
2901
+ const after = value.slice(selectionEnd);
2902
+ const newValue = before + after;
2903
+ return {
2904
+ newValue,
2905
+ cursorOffset: selectionStart
2906
+ };
2907
+ };
2908
+ const getNewValueInsertCompositionText = (value, selectionStart, selectionEnd, data) => {
2909
+ return getNewValueInsertText(value, selectionStart, selectionEnd, data);
2910
+ };
2911
+ const getNewValueInsertLineBreak = (value, selectionStart, selectionEnd, data) => {
2912
+ return {
2913
+ newValue: value,
2914
+ cursorOffset: selectionEnd
2915
+ };
2916
+ };
2917
+ const getFn = inputType => {
2918
+ switch (inputType) {
2919
+ case InsertFromPaste:
2920
+ case InsertText:
2921
+ return getNewValueInsertText;
2922
+ case DeleteContentBackward:
2923
+ return getNewValueDeleteContentBackward;
2924
+ case DeleteContentForward:
2925
+ return getNewValueDeleteContentForward;
2926
+ case DeleteWordForward:
2927
+ return getNewValueDeleteWordForward;
2928
+ case DeleteWordBackward:
2929
+ return getNewValueDeleteWordBackward;
2930
+ case InsertLineBreak:
2931
+ return getNewValueInsertLineBreak;
2932
+ case InsertCompositionText:
2933
+ return getNewValueInsertCompositionText;
2934
+ default:
2935
+ throw new Error(`unsupported input type ${inputType}`);
2936
+ }
2937
+ };
2938
+ const getNewValue = (value, inputType, data, selectionStart, selectionEnd) => {
2939
+ const fn = getFn(inputType);
2940
+ return fn(value, selectionStart, selectionEnd, data);
2941
+ };
2942
+
2943
+ // TODO when user types letters -> no need to query provider again -> just filter existing results
2944
+ const handleInput = async (state, newValue, cursorOffset, inputSource = Script) => {
2945
+ if (state.value === newValue) {
2946
+ return state;
2947
+ }
2948
+ // @ts-ignore
2949
+ state.value = newValue;
2950
+ // @ts-ignore
2951
+ state.inputSource = inputSource;
2952
+ const newPicks = await state.provider.getPicks(newValue);
2953
+ const filterValue = state.provider.getFilterValue(newValue);
2954
+ const items = filterQuickPickItems(newPicks, filterValue);
2955
+ const focusedIndex = items.length === 0 ? -1 : 0;
2956
+ return {
2957
+ ...state,
2958
+ picks: newPicks,
2959
+ items,
2960
+ focusedIndex,
2961
+ cursorOffset
2962
+ };
2963
+ };
2964
+
2965
+ const handleBeforeInput = (state, inputType, data, selectionStart, selectionEnd) => {
2966
+ string(inputType);
2967
+ number(selectionStart);
2968
+ number(selectionEnd);
2969
+ const {
2970
+ value
2971
+ } = state;
2972
+ const {
2973
+ newValue,
2974
+ cursorOffset
2975
+ } = getNewValue(value, inputType, data, selectionStart, selectionEnd);
2976
+ return handleInput(state, newValue, cursorOffset, User);
2977
+ };
2978
+
2751
2979
  const closeWidget = async id => {
2752
2980
  await invoke$1('Viewlet.closeWidget', id);
2753
2981
  };
@@ -3099,7 +3327,7 @@ const loadContent = async state => {
3099
3327
  array(newPicks);
3100
3328
  // @ts-ignore
3101
3329
  const filterValue = provider.getFilterValue(value);
3102
- const items = filterQuickPickItems(state.items, filterValue);
3330
+ const items = filterQuickPickItems(newPicks, filterValue);
3103
3331
  // @ts-ignore
3104
3332
  provider.getLabel();
3105
3333
  const minLineY = 0;
@@ -3311,29 +3539,29 @@ const SetItemsHeight = 'setItemsHeight';
3311
3539
  const SetValue = 'setValue';
3312
3540
 
3313
3541
  const renderValue = (oldState, newState) => {
3314
- return [/* method */SetValue, /* value */newState.value];
3542
+ return ['Viewlet.send', newState.uid, /* method */SetValue, /* value */newState.value];
3315
3543
  };
3316
3544
  const renderCursorOffset = (oldState, newState) => {
3317
- return [/* method */SetCursorOffset, /* cursorOffset */newState.cursorOffset];
3545
+ return ['Viewlet.send', newState.uid, /* method */SetCursorOffset, /* cursorOffset */newState.cursorOffset];
3318
3546
  };
3319
3547
  const renderItems = (oldState, newState) => {
3320
3548
  const visibleItems = getVisible(newState.provider, newState.items, newState.minLineY, newState.maxLineY, newState.focusedIndex);
3321
3549
  const dom = getQuickPickItemsVirtualDom(visibleItems);
3322
- return [/* method */'setItemsDom', dom];
3550
+ return ['Viewlet.send', newState.uid, /* method */'setItemsDom', dom];
3323
3551
  };
3324
3552
  const renderFocusedIndex = (oldState, newState) => {
3325
3553
  const oldFocusedIndex = oldState.focusedIndex - oldState.minLineY;
3326
3554
  const newFocusedIndex = newState.focusedIndex - newState.minLineY;
3327
- return [/* method */SetFocusedIndex, /* oldFocusedIndex */oldFocusedIndex, /* newFocusedIndex */newFocusedIndex];
3555
+ return ['Viewlet.send', newState.uid, /* method */SetFocusedIndex, /* oldFocusedIndex */oldFocusedIndex, /* newFocusedIndex */newFocusedIndex];
3328
3556
  };
3329
3557
  const renderHeight = (oldState, newState) => {
3330
3558
  if (newState.items.length === 0) {
3331
- return [/* method */SetItemsHeight, /* height */newState.itemHeight];
3559
+ return ['Viewlet.send', newState.uid, /* method */SetItemsHeight, /* height */newState.itemHeight];
3332
3560
  }
3333
3561
  const maxLineY = Math.min(newState.maxLineY, newState.items.length);
3334
3562
  const itemCount = maxLineY - newState.minLineY;
3335
3563
  const height = itemCount * newState.itemHeight;
3336
- return [/* method */SetItemsHeight, /* height */height];
3564
+ return ['Viewlet.send', newState.uid, /* method */SetItemsHeight, /* height */height];
3337
3565
  };
3338
3566
  const renderFocus = (oldState, newState) => {
3339
3567
  const selector = newState.focused ? '.InputBox' : '';
@@ -3443,6 +3671,74 @@ const doRender = uid => {
3443
3671
  return applyRender(oldState, newState, diffResult);
3444
3672
  };
3445
3673
 
3674
+ const getPick = (items, index) => {
3675
+ array(items);
3676
+ number(index);
3677
+ // if (index < state.recentPicks.length) {
3678
+ // return state.recentPicks[index]
3679
+ // }
3680
+ // index -= state.recentPicks.length
3681
+ if (index < items.length) {
3682
+ return items[index].pick;
3683
+ }
3684
+ console.warn('no pick matching index', index);
3685
+ };
3686
+
3687
+ const selectIndex = async (state, index, button = /* left */0) => {
3688
+ const {
3689
+ minLineY,
3690
+ provider,
3691
+ items
3692
+ } = state;
3693
+ const actualIndex = index + minLineY;
3694
+ const pick = getPick(items, actualIndex);
3695
+ const selectPickResult = await provider.selectPick(pick, actualIndex, button);
3696
+ object(selectPickResult);
3697
+ string(selectPickResult.command);
3698
+ const {
3699
+ command
3700
+ } = selectPickResult;
3701
+ switch (command) {
3702
+ case Hide:
3703
+ await closeWidget(state.uid);
3704
+ return state;
3705
+ default:
3706
+ return state;
3707
+ }
3708
+
3709
+ // TODO recent picks should be per provider
3710
+ // if (!state.recentPickIds.has(pick.id)) {
3711
+ // state.recentPicks.unshift(pick)
3712
+ // state.recentPickIds.add(pick.id)
3713
+ // }
3714
+ // if (state.recentPicks.length > RECENT_PICKS_MAX_SIZE) {
3715
+ // const last = state.recentPicks.pop()
3716
+ // state.recentPickIds.delete(last.id)
3717
+ // }
3718
+ };
3719
+
3720
+ const selectCurrentIndex = state => {
3721
+ return selectIndex(state, state.focusedIndex);
3722
+ };
3723
+
3724
+ const findLabelIndex = (items, label) => {
3725
+ for (let i = 0; i < items.length; i++) {
3726
+ if (items[i].pick.label === label) {
3727
+ return i;
3728
+ }
3729
+ }
3730
+ return -1;
3731
+ };
3732
+
3733
+ const selectItem = async (state, label) => {
3734
+ string(label);
3735
+ const index = findLabelIndex(state.items, label);
3736
+ if (index === -1) {
3737
+ return state;
3738
+ }
3739
+ return selectIndex(state, index);
3740
+ };
3741
+
3446
3742
  const wrapCommand = fn => {
3447
3743
  const wrapped = async (uid, ...args) => {
3448
3744
  const {
@@ -3455,23 +3751,31 @@ const wrapCommand = fn => {
3455
3751
  };
3456
3752
 
3457
3753
  const commandMap = {
3458
- 'QuickPick.create': create,
3459
3754
  'QuickPick.create2': create$1,
3755
+ 'QuickPick.focusFirst': wrapCommand(focusFirst),
3460
3756
  'QuickPick.focusIndex': wrapCommand(focusIndex),
3757
+ 'QuickPick.focusLast': wrapCommand(focusLast),
3461
3758
  'QuickPick.focusNext': wrapCommand(focusNext),
3759
+ 'QuickPick.focusPrevious': wrapCommand(focusPrevious),
3760
+ 'QuickPick.getCommandIds': getCommandIds,
3462
3761
  'QuickPick.getKeyBindings': getKeyBindings,
3762
+ 'QuickPick.handleBeforeInput': wrapCommand(handleBeforeInput),
3463
3763
  'QuickPick.handleBlur': wrapCommand(handleBlur),
3464
3764
  'QuickPick.handleWheel': wrapCommand(handleWheel),
3465
- 'QuickPick.setDeltaY': wrapCommand(setDeltaY),
3466
3765
  'QuickPick.loadContent': wrapCommand(loadContent),
3467
3766
  'QuickPick.loadEntries2': load,
3468
3767
  'QuickPick.render': doRender,
3768
+ 'QuickPick.selectCurrentIndex': wrapCommand(selectCurrentIndex),
3769
+ 'QuickPick.selectIndex': wrapCommand(selectIndex),
3770
+ 'QuickPick.selectItem': wrapCommand(selectItem),
3771
+ 'QuickPick.setDeltaY': wrapCommand(setDeltaY),
3469
3772
  'SearchFile.filter': filterQuickPickItems,
3470
- 'SearchFile.searchFile': searchFile,
3471
- 'SearchFile.searchFileWithFetch': searchFile$3,
3472
- 'SearchFile.searchFileWithHtml': searchFile$2,
3473
- 'SearchFile.searchFileWithRipGrep': searchFile$1,
3773
+ 'SearchFile.searchFile': searchFile$1,
3774
+ 'SearchFile.searchFileWithFetch': searchFile$4,
3775
+ 'SearchFile.searchFileWithHtml': searchFile$3,
3776
+ 'SearchFile.searchFileWithRipGrep': searchFile$2,
3474
3777
  // deprecated
3778
+ 'QuickPick.create': create,
3475
3779
  'QuickPick.loadEntries': loadQuickPickEntries,
3476
3780
  'FileSystemFetch.chmod': chmod,
3477
3781
  'FileSystemFetch.getBlob': getBlob,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/file-search-worker",
3
- "version": "3.6.0",
3
+ "version": "3.7.0",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "text-search"