@lvce-editor/find-widget-worker 1.22.0 → 2.0.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.
@@ -843,6 +843,9 @@ const createMockRpc = ({
843
843
  const invoke = (method, ...params) => {
844
844
  invocations.push([method, ...params]);
845
845
  const command = commandMap[method];
846
+ if (!command) {
847
+ throw new Error(`command ${method} not found`);
848
+ }
846
849
  return command(...params);
847
850
  };
848
851
  const mockRpc = {
@@ -983,7 +986,12 @@ const create$1 = (uid, x, y, width, height, parentUid) => {
983
986
  preserveCase: false,
984
987
  resizerPointerDown: false,
985
988
  minWidth: 400,
986
- history: []
989
+ history: [],
990
+ inputErrorMessage: '',
991
+ inputHeight: 15,
992
+ inputLineHeight: 15,
993
+ inputPaddingTop: 2,
994
+ inputPaddingBottom: 2
987
995
  };
988
996
  set$3(uid, state, state);
989
997
  return state;
@@ -994,7 +1002,7 @@ const isEqual$7 = (oldState, newState) => {
994
1002
  };
995
1003
 
996
1004
  const isEqual$6 = (oldState, newState) => {
997
- return newState.width === newState.width && oldState.height === newState.height && oldState.x === newState.x;
1005
+ return newState.width === newState.width && oldState.height === newState.height && oldState.x === newState.x && oldState.inputHeight === newState.inputHeight;
998
1006
  };
999
1007
 
1000
1008
  const isEqual$5 = (oldState, newState) => {
@@ -1040,9 +1048,18 @@ const ClientX = 'event.clientX';
1040
1048
  const ClientY = 'event.clientY';
1041
1049
  const TargetName = 'event.target.name';
1042
1050
 
1051
+ const Tab = 2;
1052
+ const Enter = 3;
1053
+ const KeyF = 34;
1054
+ const F4 = 60;
1055
+
1043
1056
  const User = 1;
1044
1057
  const Script = 2;
1045
1058
 
1059
+ const CtrlCmd = 1 << 11 >>> 0;
1060
+ const Shift = 1 << 10 >>> 0;
1061
+ const Alt = 1 << 9 >>> 0;
1062
+
1046
1063
  const EditorWorker$1 = 99;
1047
1064
 
1048
1065
  const FocusSelector = 'Viewlet.focusSelector';
@@ -1050,7 +1067,13 @@ const SetCss = 'Viewlet.setCss';
1050
1067
  const SetFocusContext = 'Viewlet.setFocusContext';
1051
1068
  const SetValueByName = 'Viewlet.setValueByName';
1052
1069
 
1070
+ const FocusFindWidget$1 = 16;
1053
1071
  const FocusFindWidgetCloseButton$1 = 48;
1072
+ const FocusFindWidgetNextMatchButton = 49;
1073
+ const FocusFindWidgetPreviousMatchButton = 50;
1074
+ const FocusFindWidgetReplace$1 = 43;
1075
+ const FocusFindWidgetReplaceAllButton$1 = 47;
1076
+ const FocusFindWidgetReplaceButton = 46;
1054
1077
  const FocusIgnoreFiles = 1012;
1055
1078
  const FocusSearchExcludeInput = 1008;
1056
1079
  const FocusSearchIncludeInput = 1007;
@@ -1340,7 +1363,18 @@ const buildRegex = (searchString, options) => {
1340
1363
  const flags = matchCase ? 'g' : 'gi';
1341
1364
  const pattern = useRegularExpression ? searchString : escapeRegExpCharacters(searchString);
1342
1365
  const wrapped = matchWholeWord ? `\\b(?:${pattern})\\b` : pattern;
1343
- return new RegExp(wrapped, flags);
1366
+ try {
1367
+ const regex = new RegExp(wrapped, flags);
1368
+ return {
1369
+ regex,
1370
+ error: ''
1371
+ };
1372
+ } catch (error) {
1373
+ return {
1374
+ regex: new RegExp(''),
1375
+ error: `${error}`
1376
+ };
1377
+ }
1344
1378
  };
1345
1379
 
1346
1380
  const findRegexMatches = (lines, regex) => {
@@ -1367,37 +1401,76 @@ const findRegexMatches = (lines, regex) => {
1367
1401
 
1368
1402
  const findMatches = (lines, searchString, options) => {
1369
1403
  if (searchString.length === 0) {
1370
- return new Uint32Array([]);
1404
+ return {
1405
+ matches: new Uint32Array([]),
1406
+ error: ''
1407
+ };
1371
1408
  }
1372
- const regex = buildRegex(searchString, options);
1373
- return findRegexMatches(lines, regex);
1409
+ const {
1410
+ regex
1411
+ } = buildRegex(searchString, options);
1412
+ const matches = findRegexMatches(lines, regex);
1413
+ return {
1414
+ matches,
1415
+ error: ''
1416
+ };
1374
1417
  };
1375
1418
 
1376
1419
  const getMatchCount = matches => {
1377
1420
  return matches.length / 2;
1378
1421
  };
1379
1422
 
1423
+ const measureTextHeight = (value, lineHeight) => {
1424
+ if (!value) {
1425
+ return lineHeight;
1426
+ }
1427
+ // TODO for single line width, can use offscreencanvas to measure text width
1428
+ // for multiline text, can ask renderer process to measure text height
1429
+ const lines = value.split('\n');
1430
+ return lines.length * lineHeight;
1431
+ };
1432
+
1380
1433
  const refresh$1 = (state, value, inputSource) => {
1381
1434
  const {
1382
1435
  lines,
1383
1436
  matchCase,
1384
1437
  useRegularExpression,
1385
- matchWholeWord
1438
+ matchWholeWord,
1439
+ inputLineHeight,
1440
+ inputPaddingTop,
1441
+ inputPaddingBottom
1386
1442
  } = state;
1387
1443
  const options = {
1388
1444
  matchCase,
1389
1445
  useRegularExpression,
1390
1446
  matchWholeWord
1391
1447
  };
1392
- const matches = findMatches(lines, value, options);
1448
+ const {
1449
+ matches,
1450
+ error
1451
+ } = findMatches(lines, value, options);
1452
+ const textHeight = measureTextHeight(value, inputLineHeight);
1453
+ const inputHeight = textHeight + inputPaddingBottom + inputPaddingTop;
1454
+ const height = inputHeight + 5;
1455
+ if (error) {
1456
+ return {
1457
+ ...state,
1458
+ inputErrorMessage: error,
1459
+ inputHeight,
1460
+ height
1461
+ };
1462
+ }
1393
1463
  const matchCount = getMatchCount(matches);
1394
1464
  return {
1395
1465
  ...state,
1466
+ inputErrorMessage: error,
1467
+ inputHeight,
1468
+ inputSource,
1469
+ matchCount,
1396
1470
  matches,
1397
1471
  matchIndex: 0,
1398
- matchCount,
1399
1472
  value,
1400
- inputSource
1473
+ height
1401
1474
  };
1402
1475
  };
1403
1476
 
@@ -1564,10 +1637,6 @@ const handleClickClose = state => {
1564
1637
  return close$1(state);
1565
1638
  };
1566
1639
 
1567
- const handleInput = (state, value, inputSource = User) => {
1568
- return refresh(state, value, inputSource);
1569
- };
1570
-
1571
1640
  const handleFindWidgetFocus = (state, focusKey) => {
1572
1641
  if (state.focus === focusKey) {
1573
1642
  return state;
@@ -1748,6 +1817,86 @@ const focusPreviousElement = state => {
1748
1817
  return focusElement(state, previousFocus);
1749
1818
  };
1750
1819
 
1820
+ const getKeyBindings = () => {
1821
+ return [{
1822
+ key: Enter,
1823
+ command: 'FindWidget.focusNext',
1824
+ when: FocusFindWidget$1
1825
+ }, {
1826
+ key: CtrlCmd | KeyF,
1827
+ command: 'FindWidget.preventDefaultBrowserFind',
1828
+ when: FocusFindWidget$1
1829
+ }, {
1830
+ key: Shift | F4,
1831
+ command: 'FindWidget.focusPrevious',
1832
+ when: FocusFindWidget$1
1833
+ }, {
1834
+ key: F4,
1835
+ command: 'FindWidget.focusNext',
1836
+ when: FocusFindWidget$1
1837
+ }, {
1838
+ key: Shift | Tab,
1839
+ command: 'FindWidget.focusToggleReplace',
1840
+ when: FocusFindWidget$1
1841
+ }, {
1842
+ key: Tab,
1843
+ command: 'FindWidget.focusReplace',
1844
+ when: FocusFindWidget$1
1845
+ }, {
1846
+ key: Tab,
1847
+ command: 'FindWidget.focusPreviousMatchButton',
1848
+ when: FocusFindWidgetReplace$1
1849
+ }, {
1850
+ key: Alt | CtrlCmd | Enter,
1851
+ command: 'FindWidget.replaceAll',
1852
+ when: FocusFindWidgetReplace$1
1853
+ }, {
1854
+ key: Tab,
1855
+ command: 'FindWidget.focusNextMatchButton',
1856
+ when: FocusFindWidgetPreviousMatchButton
1857
+ }, {
1858
+ key: Shift | Tab,
1859
+ command: 'FindWidget.focusReplace',
1860
+ when: FocusFindWidgetPreviousMatchButton
1861
+ }, {
1862
+ key: Shift | Tab,
1863
+ command: 'FindWidget.focusPreviousMatchButton',
1864
+ when: FocusFindWidgetNextMatchButton
1865
+ }, {
1866
+ key: Tab,
1867
+ command: 'FindWidget.focusCloseButton',
1868
+ when: FocusFindWidgetNextMatchButton
1869
+ }, {
1870
+ key: Shift | Tab,
1871
+ command: 'FindWidget.focusNextMatchButton',
1872
+ when: FocusFindWidgetCloseButton$1
1873
+ }, {
1874
+ key: Tab,
1875
+ command: 'FindWidget.focusReplaceButton',
1876
+ when: FocusFindWidgetCloseButton$1
1877
+ }, {
1878
+ key: Shift | Tab,
1879
+ command: 'FindWidget.focusFind',
1880
+ when: FocusFindWidgetReplace$1
1881
+ }, {
1882
+ key: Tab,
1883
+ command: 'FindWidget.focusReplaceAllButton',
1884
+ when: FocusFindWidgetReplaceButton
1885
+ }, {
1886
+ key: Shift | Tab,
1887
+ command: 'FindWidget.focusCloseButton',
1888
+ when: FocusFindWidgetReplaceButton
1889
+ }, {
1890
+ key: Shift | Tab,
1891
+ command: 'FindWidget.focusReplaceButton',
1892
+ when: FocusFindWidgetReplaceAllButton$1
1893
+ }];
1894
+ };
1895
+
1896
+ const handleInput = (state, value, inputSource = User) => {
1897
+ return refresh(state, value, inputSource);
1898
+ };
1899
+
1751
1900
  const handleReplaceInput = (state, value, inputSource = User) => {
1752
1901
  return {
1753
1902
  ...state,
@@ -1834,7 +1983,8 @@ const loadContent = async (state, savedState) => {
1834
1983
  editorUid,
1835
1984
  editorWidth,
1836
1985
  editorX,
1837
- editorY
1986
+ editorY,
1987
+ inputLineHeight
1838
1988
  } = state;
1839
1989
  const {
1840
1990
  replacement,
@@ -1854,11 +2004,13 @@ const loadContent = async (state, savedState) => {
1854
2004
  const actualValue = value || getSelectedText(lines, selections);
1855
2005
  const matches = findMatchesCaseInsensitive(lines, actualValue);
1856
2006
  const matchCount = getMatchCount(matches);
2007
+ const inputHeight = measureTextHeight(value, inputLineHeight);
1857
2008
  return {
1858
2009
  ...state,
1859
2010
  focus: FocusSearchInput,
1860
2011
  focused: true,
1861
2012
  height,
2013
+ inputHeight,
1862
2014
  inputSource: Script,
1863
2015
  lines,
1864
2016
  matchCount,
@@ -2103,13 +2255,6 @@ const SearchToggleButtonExpanded = 'SearchToggleButtonExpanded';
2103
2255
  const MaskIconChevronDown = 'MaskIconChevronDown';
2104
2256
  const MaskIconChevronRight = 'MaskIconChevronRight';
2105
2257
 
2106
- const getFindMatchCountClassName = (matchCount, value) => {
2107
- if (value && matchCount === 0) {
2108
- return mergeClassNames(FindWidgetMatchCount, FindWidgetMatchCountEmpty);
2109
- }
2110
- return FindWidgetMatchCount;
2111
- };
2112
-
2113
2258
  const getIconVirtualDom = (icon, type = Div) => {
2114
2259
  return {
2115
2260
  type,
@@ -2143,6 +2288,22 @@ const getIconButtonVirtualDom = iconButton => {
2143
2288
  }, getIconVirtualDom(icon)];
2144
2289
  };
2145
2290
 
2291
+ const emptyMatches = mergeClassNames(FindWidgetMatchCount, FindWidgetMatchCountEmpty);
2292
+ const getFindMatchCountClassName = (matchCount, hasValue) => {
2293
+ if (hasValue && matchCount === 0) {
2294
+ return emptyMatches;
2295
+ }
2296
+ return FindWidgetMatchCount;
2297
+ };
2298
+
2299
+ const getMatchCountVirtualDom = (matchCountText, matchCount, hasValue) => {
2300
+ return [{
2301
+ type: Div,
2302
+ className: getFindMatchCountClassName(matchCount, hasValue),
2303
+ childCount: 1
2304
+ }, text(matchCountText)];
2305
+ };
2306
+
2146
2307
  const checkedClassName = mergeClassNames(SearchFieldButton, SearchFieldButtonChecked);
2147
2308
  const unCheckedClassName = SearchFieldButton;
2148
2309
  const getSearchFieldButtonVirtualDom = button => {
@@ -2170,7 +2331,7 @@ const getSearchFieldButtonVirtualDom = button => {
2170
2331
  }];
2171
2332
  };
2172
2333
 
2173
- const getSearchFieldVirtualDom = (name, placeholder, onInput, insideButtons, outsideButtons, onFocus = '') => {
2334
+ const getSearchFieldVirtualDom = (name, placeholder, onInput, insideButtons, outsideButtons, onFocus = '', extraClassName = '') => {
2174
2335
  const dom = [{
2175
2336
  type: Div,
2176
2337
  className: SearchField,
@@ -2178,7 +2339,7 @@ const getSearchFieldVirtualDom = (name, placeholder, onInput, insideButtons, out
2178
2339
  childCount: 2
2179
2340
  }, {
2180
2341
  type: TextArea,
2181
- className: MultilineInputBox,
2342
+ className: mergeClassNames(MultilineInputBox, extraClassName),
2182
2343
  spellcheck: false,
2183
2344
  autocapitalize: 'off',
2184
2345
  autocorrect: 'off',
@@ -2198,16 +2359,17 @@ const getSearchFieldVirtualDom = (name, placeholder, onInput, insideButtons, out
2198
2359
  return dom;
2199
2360
  };
2200
2361
 
2201
- const getFindWidgetFindVirtualDom = (matchCountText, buttons, fieldButtons, matchCount, value) => {
2362
+ const getFindWidgetFindVirtualDom = (matchCountText, buttons, fieldButtons, matchCount, hasValue, hasError) => {
2363
+ const extraClassName = hasError ? `InputValidationError` : '';
2202
2364
  return [{
2203
2365
  type: Div,
2204
2366
  className: FindWidgetFind,
2205
- childCount: 5
2206
- }, ...getSearchFieldVirtualDom(SearchValue, find(), HandleInput, fieldButtons, [], HandleFocus), {
2367
+ childCount: 2
2368
+ }, ...getSearchFieldVirtualDom(SearchValue, find(), HandleInput, fieldButtons, [], HandleFocus, extraClassName), {
2207
2369
  type: Div,
2208
- className: getFindMatchCountClassName(matchCount, value),
2209
- childCount: 1
2210
- }, text(matchCountText), ...buttons.flatMap(getIconButtonVirtualDom)];
2370
+ className: 'FindWidgetActions',
2371
+ childCount: 1 + buttons.length
2372
+ }, ...getMatchCountVirtualDom(matchCountText, matchCount, hasValue), ...buttons.flatMap(getIconButtonVirtualDom)];
2211
2373
  };
2212
2374
 
2213
2375
  const getFindWidgetReplaceVirtualDom = (replaceExpanded, replaceButtons, replaceFieldButtons) => {
@@ -2260,12 +2422,12 @@ const parentNode = {
2260
2422
  childCount: 3,
2261
2423
  role: Group
2262
2424
  };
2263
- const getFindWidgetVirtualDom = (matchCountText, replaceExpanded, findButtons, findFieldButtons, replaceButtons, replaceFieldButtons, matchCount, value) => {
2425
+ const getFindWidgetVirtualDom = (matchCountText, replaceExpanded, findButtons, findFieldButtons, replaceButtons, replaceFieldButtons, matchCount, value, hasError) => {
2264
2426
  return [parentNode, ...getResizerVirtualDom(), ...getSearchToggleButtonVirtualDom(replaceExpanded, 'handleClickToggleReplace'), {
2265
2427
  type: Div,
2266
2428
  className: FindWidgetRight,
2267
2429
  childCount: replaceExpanded ? 2 : 1
2268
- }, ...getFindWidgetFindVirtualDom(matchCountText, findButtons, findFieldButtons, matchCount, value), ...getFindWidgetReplaceVirtualDom(replaceExpanded, replaceButtons)];
2430
+ }, ...getFindWidgetFindVirtualDom(matchCountText, findButtons, findFieldButtons, matchCount, Boolean(value), hasError), ...getFindWidgetReplaceVirtualDom(replaceExpanded, replaceButtons)];
2269
2431
  };
2270
2432
 
2271
2433
  const getMatchCountText = (matchIndex, matchCount) => {
@@ -2284,7 +2446,8 @@ const renderContent = (oldState, newState) => {
2284
2446
  matchCase,
2285
2447
  matchWholeWord,
2286
2448
  useRegularExpression,
2287
- preserveCase
2449
+ preserveCase,
2450
+ inputErrorMessage
2288
2451
  } = newState;
2289
2452
  const matchCountText = getMatchCountText(newState.matchIndex, newState.matchCount);
2290
2453
  const {
@@ -2297,7 +2460,8 @@ const renderContent = (oldState, newState) => {
2297
2460
  findFieldButtons,
2298
2461
  replaceFieldButtons
2299
2462
  } = getFindWidgetButtons(findButtonsEnabled, replaceButtonsEnabled, matchCase, matchWholeWord, useRegularExpression, preserveCase);
2300
- const dom = getFindWidgetVirtualDom(matchCountText, replaceExpanded, findButtons, findFieldButtons, replaceButtons, replaceFieldButtons, matchCount, value);
2463
+ const hasError = Boolean(inputErrorMessage);
2464
+ const dom = getFindWidgetVirtualDom(matchCountText, replaceExpanded, findButtons, findFieldButtons, replaceButtons, replaceFieldButtons, matchCount, value, hasError);
2301
2465
  return [SetDom2, uid, dom];
2302
2466
  };
2303
2467
 
@@ -2307,14 +2471,16 @@ const renderCss = (oldState, newState) => {
2307
2471
  width,
2308
2472
  height,
2309
2473
  x,
2310
- y
2474
+ y,
2475
+ inputHeight
2311
2476
  } = newState;
2312
2477
  const css = `:root {
2313
- --FindWidgetWidth: ${width}px;
2314
- --FindWidgetHeight: ${height}px;
2315
- --FindWidgetX: ${x}px;
2316
- --FindWidgetY: ${y}px;
2317
- }`;
2478
+ --FindWidgetWidth: ${width}px;
2479
+ --FindWidgetHeight: ${height}px;
2480
+ --FindWidgetX: ${x}px;
2481
+ --FindWidgetY: ${y}px;
2482
+ --FindWidgetInputHeight: ${inputHeight}px;
2483
+ }`;
2318
2484
  return [SetCss, uid, css];
2319
2485
  };
2320
2486
 
@@ -2523,17 +2689,18 @@ const commandMap = {
2523
2689
  'FindWidget.diff2': diff2,
2524
2690
  'FindWidget.dispose': dispose$1,
2525
2691
  'FindWidget.focusCloseButton': wrapCommand(focusCloseButton),
2526
- 'FindWidget.focusFirst': wrapCommand(focusFirst),
2527
2692
  'FindWidget.focusElement': wrapCommand(focusElement),
2693
+ 'FindWidget.focusFirst': wrapCommand(focusFirst),
2528
2694
  'FindWidget.focusIndex': wrapCommand(focusIndex),
2529
2695
  'FindWidget.focusLast': wrapCommand(focusLast),
2530
2696
  'FindWidget.focusNext': wrapCommand(focusNext),
2531
- 'FindWidget.focusPrevious': wrapCommand(focusPrevious),
2532
2697
  'FindWidget.focusNextElement': wrapCommand(focusNextElement),
2698
+ 'FindWidget.focusPrevious': wrapCommand(focusPrevious),
2533
2699
  'FindWidget.focusPreviousElement': wrapCommand(focusPreviousElement),
2534
2700
  'FindWidget.focusReplace': wrapCommand(focusReplace),
2535
2701
  'FindWidget.focusReplaceAllButton': wrapCommand(focusReplaceAllButton),
2536
2702
  'FindWidget.getCommandIds': getCommandIds,
2703
+ 'FindWidget.getKeyBindings': getKeyBindings,
2537
2704
  'FindWidget.handleBlur': wrapCommand(handleBlur),
2538
2705
  'FindWidget.handleClickButton': wrapCommand(handleClickButton),
2539
2706
  'FindWidget.handleClickClose': wrapCommand(handleClickClose),
@@ -2549,8 +2716,8 @@ const commandMap = {
2549
2716
  'FindWidget.replace': wrapCommand(replace$1),
2550
2717
  'FindWidget.replaceAll': wrapCommand(replaceAll$1),
2551
2718
  'FindWidget.resize': wrapCommand(resize),
2552
- 'FindWidget.terminate': terminate,
2553
2719
  'FindWidget.saveState': wrapGetter(saveState),
2720
+ 'FindWidget.terminate': terminate,
2554
2721
  'FindWidget.toggleMatchCase': wrapCommand(toggleMatchCase),
2555
2722
  'FindWidget.toggleReplace': wrapCommand(toggleReplace$1),
2556
2723
  'FindWidget.toggleUseRegularExpression': wrapCommand(toggleUseRegularExpression)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/find-widget-worker",
3
- "version": "1.22.0",
3
+ "version": "2.0.0",
4
4
  "description": "Web Worker for the find widget in Lvce Editor",
5
5
  "repository": {
6
6
  "type": "git",