@lvce-editor/find-widget-worker 1.12.0 → 1.14.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.
@@ -933,6 +933,9 @@ const create$2 = () => {
933
933
  }
934
934
  };
935
935
  };
936
+ const terminate = () => {
937
+ globalThis.close();
938
+ };
936
939
 
937
940
  const {
938
941
  get: get$1,
@@ -974,7 +977,8 @@ const create$1 = (uid, x, y, width, height, parentUid) => {
974
977
  editorY: y,
975
978
  editorWidth: width,
976
979
  editorHeight: height,
977
- inputSource: 0
980
+ inputSource: 0,
981
+ preserveCase: false
978
982
  };
979
983
  set$3(uid, state, state);
980
984
  return state;
@@ -1007,9 +1011,21 @@ const RenderContent = 13;
1007
1011
  const RenderFocusContext = 14;
1008
1012
  const RenderFocus = 15;
1009
1013
  const RenderValue = 16;
1014
+ const RenderReplaceValue = 17;
1015
+
1016
+ const Button = 1;
1017
+ const Div = 4;
1018
+ const Text = 12;
1019
+ const TextArea = 62;
1010
1020
 
1021
+ const User = 1;
1011
1022
  const Script = 1;
1012
- const User = 2;
1023
+
1024
+ const EditorWorker$1 = 99;
1025
+
1026
+ const FocusSelector = 'Viewlet.focusSelector';
1027
+ const SetFocusContext = 'Viewlet.setFocusContext';
1028
+ const SetValueByName = 'Viewlet.setValueByName';
1013
1029
 
1014
1030
  const isEqual = (oldState, newState) => {
1015
1031
  return newState.inputSource === User || oldState.value === newState.value;
@@ -1061,17 +1077,6 @@ const focusCloseButton = state => {
1061
1077
  return setFindWidgetFocus(state, FocusFindWidgetCloseButton);
1062
1078
  };
1063
1079
 
1064
- const Button = 1;
1065
- const Div = 4;
1066
- const Text = 12;
1067
- const TextArea = 62;
1068
-
1069
- const EditorWorker$1 = 99;
1070
-
1071
- const FocusSelector = 'Viewlet.focusSelector';
1072
- const SetFocusContext = 'Viewlet.setFocusContext';
1073
- const SetValueByName = 'Viewlet.setValueByName';
1074
-
1075
1080
  const rpcs = Object.create(null);
1076
1081
  const set$2 = (id, rpc) => {
1077
1082
  rpcs[id] = rpc;
@@ -1121,7 +1126,7 @@ const activateByEvent = async event => {
1121
1126
  // @ts-ignore
1122
1127
  await invoke$1('ActivateByEvent.activateByEvent', event);
1123
1128
  };
1124
- const applyEdit = async (editorUid, changes) => {
1129
+ const applyEdit$1 = async (editorUid, changes) => {
1125
1130
  // @ts-ignore
1126
1131
  await invoke$1('Editor.applyEdit2', editorUid, changes);
1127
1132
  };
@@ -1186,7 +1191,7 @@ const registerMockRpc = commandMap => {
1186
1191
  const EditorWorker = {
1187
1192
  __proto__: null,
1188
1193
  activateByEvent,
1189
- applyEdit,
1194
+ applyEdit: applyEdit$1,
1190
1195
  applyWorkspaceEdit,
1191
1196
  closeWidget,
1192
1197
  dispose,
@@ -1210,7 +1215,9 @@ const EditorWorker = {
1210
1215
 
1211
1216
  const {
1212
1217
  invoke,
1213
- set} = EditorWorker;
1218
+ set,
1219
+ applyEdit
1220
+ } = EditorWorker;
1214
1221
 
1215
1222
  const setSelections = async (editorUid, selections) => {
1216
1223
  // @ts-ignore
@@ -1283,6 +1290,10 @@ const handleBlur = async state => {
1283
1290
  };
1284
1291
  };
1285
1292
 
1293
+ const handleClickButton = async (state, name) => {
1294
+ return state;
1295
+ };
1296
+
1286
1297
  const close$1 = async state => {
1287
1298
  const {
1288
1299
  editorUid
@@ -1341,7 +1352,7 @@ const getMatchCount = matches => {
1341
1352
  return matches.length / 2;
1342
1353
  };
1343
1354
 
1344
- const refresh$1 = (state, value) => {
1355
+ const refresh$1 = (state, value, inputSource) => {
1345
1356
  const {
1346
1357
  lines
1347
1358
  } = state;
@@ -1353,16 +1364,27 @@ const refresh$1 = (state, value) => {
1353
1364
  matchIndex: 0,
1354
1365
  matchCount,
1355
1366
  value,
1356
- inputSource: User
1367
+ inputSource
1357
1368
  };
1358
1369
  };
1359
1370
 
1360
- const refresh = (state, value = state.value) => {
1361
- return refresh$1(state, value);
1371
+ const refresh = (state, value = state.value, inputSource = User) => {
1372
+ return refresh$1(state, value, inputSource);
1373
+ };
1374
+
1375
+ const handleInput = (state, value, inputSource = User) => {
1376
+ return refresh(state, value, inputSource);
1362
1377
  };
1363
1378
 
1364
- const handleInput = (state, value) => {
1365
- return refresh(state, value);
1379
+ const toggleMatchCase = state => {
1380
+ const {
1381
+ matchCase
1382
+ } = state;
1383
+ // TODO update search results
1384
+ return {
1385
+ ...state,
1386
+ matchCase: !matchCase
1387
+ };
1366
1388
  };
1367
1389
 
1368
1390
  const getFindWidgetHeight = replaceExpanded => {
@@ -1385,6 +1407,24 @@ const toggleReplace = state => {
1385
1407
  };
1386
1408
  };
1387
1409
 
1410
+ const toggleUseRegularExpression = state => {
1411
+ const {
1412
+ useRegularExpression
1413
+ } = state;
1414
+ return {
1415
+ ...state,
1416
+ useRegularExpression: !useRegularExpression
1417
+ };
1418
+ };
1419
+
1420
+ const handleReplaceInput = (state, value, inputSource = User) => {
1421
+ return {
1422
+ ...state,
1423
+ replacement: value,
1424
+ inputSource
1425
+ };
1426
+ };
1427
+
1388
1428
  const getFindWidgetPosition = (editorX, editorY, editorWidth) => {
1389
1429
  const width = 300;
1390
1430
  const height = 30;
@@ -1489,6 +1529,7 @@ const HandleClickPreviousMatch = 'handleClickPreviousMatch';
1489
1529
  const HandleClickReplace = 'handleClickReplace';
1490
1530
  const HandleClickToggleReplace = 'handleClickToggleReplace';
1491
1531
  const HandleClickReplaceAll = 'handleClickReplaceAll';
1532
+ const HandleClickButton = 'handleClickButton';
1492
1533
  const HandleFocus = 'handleFocus';
1493
1534
  const HandleInput = 'handleInput';
1494
1535
  const HandleReplaceFocus = 'handleReplaceFocus';
@@ -1538,27 +1579,33 @@ const close = () => {
1538
1579
  const find = () => {
1539
1580
  return i18nString(Find$1);
1540
1581
  };
1541
- const replace = () => {
1582
+ const replace$1 = () => {
1542
1583
  return i18nString(Replace$2);
1543
1584
  };
1544
- const replaceAll = () => {
1585
+ const replaceAll$1 = () => {
1545
1586
  return i18nString(ReplaceAll$2);
1546
1587
  };
1547
1588
 
1589
+ const MatchCase$1 = 'CaseSensitive';
1590
+ const UseRegularExpression$1 = 'Regex';
1591
+ const MatchWholeWord$1 = 'WholeWord';
1548
1592
  const ArrowDown = 'ArrowDown';
1549
1593
  const ArrowUp = 'ArrowUp';
1550
1594
  const Close$1 = 'Close';
1551
1595
  const Replace$1 = 'Replace';
1552
1596
  const ReplaceAll$1 = 'ReplaceAll';
1553
1597
 
1554
- const ToggleReplace = 'ToggleReplace';
1555
- const SearchValue = 'search-value';
1556
- const ReplaceValue = 'replace-value';
1557
- const ReplaceAll = 'ReplaceAll';
1558
1598
  const Close = 'Close';
1559
1599
  const FocusNext = 'FocusNext';
1560
1600
  const FocusPrevious = 'FocusPrevious';
1601
+ const MatchCase = 'MatchCase';
1602
+ const MatchWholeWord = 'MatchWholeWord';
1561
1603
  const Replace = 'Replace';
1604
+ const ReplaceAll = 'ReplaceAll';
1605
+ const ReplaceValue = 'replace-value';
1606
+ const SearchValue = 'search-value';
1607
+ const ToggleReplace = 'ToggleReplace';
1608
+ const UseRegularExpression = 'UseRegularExpression';
1562
1609
 
1563
1610
  const getFindWidgetButtons = (findButtonsEnabled, replaceButtonsEnabled) => {
1564
1611
  const findButtons = [{
@@ -1581,13 +1628,13 @@ const getFindWidgetButtons = (findButtonsEnabled, replaceButtonsEnabled) => {
1581
1628
  name: Close
1582
1629
  }];
1583
1630
  const replaceButtons = [{
1584
- label: replace(),
1631
+ label: replace$1(),
1585
1632
  icon: Replace$1,
1586
1633
  disabled: !replaceButtonsEnabled,
1587
1634
  onClick: HandleClickReplace,
1588
1635
  name: Replace
1589
1636
  }, {
1590
- label: replaceAll(),
1637
+ label: replaceAll$1(),
1591
1638
  icon: ReplaceAll$1,
1592
1639
  disabled: !replaceButtonsEnabled,
1593
1640
  onClick: HandleClickReplaceAll,
@@ -1642,6 +1689,25 @@ const ViewletFind = 'ViewletFind';
1642
1689
  const FindWidget = 'FindWidget';
1643
1690
  const ViewletFindWidget = 'ViewletFindWidget';
1644
1691
 
1692
+ const getFindButtons = () => {
1693
+ return [{
1694
+ checked: false,
1695
+ icon: MatchCase$1,
1696
+ title: 'Match Case',
1697
+ name: MatchCase
1698
+ }, {
1699
+ checked: false,
1700
+ icon: MatchWholeWord$1,
1701
+ title: 'Match Whole Word',
1702
+ name: MatchWholeWord
1703
+ }, {
1704
+ checked: false,
1705
+ icon: UseRegularExpression$1,
1706
+ title: 'Use Regular Expression',
1707
+ name: UseRegularExpression
1708
+ }];
1709
+ };
1710
+
1645
1711
  const getFindMatchCountClassName = (matchCount, value) => {
1646
1712
  if (value && matchCount === 0) {
1647
1713
  return mergeClassNames(FindWidgetMatchCount, FindWidgetMatchCountEmpty);
@@ -1698,7 +1764,7 @@ const getSearchFieldButtonVirtualDom = button => {
1698
1764
  childCount: 1
1699
1765
  }, {
1700
1766
  type: Div,
1701
- className: mergeClassNames(MaskIcon, icon),
1767
+ className: mergeClassNames(MaskIcon, `MaskIcon${icon}`),
1702
1768
  childCount: 0
1703
1769
  }];
1704
1770
  };
@@ -1738,7 +1804,7 @@ const getFindWidgetFindVirtualDom = (matchCountText, buttons, matchCount, value)
1738
1804
  className: FindWidgetFind,
1739
1805
  childCount: 5
1740
1806
  });
1741
- dom.push(...getSearchFieldVirtualDom('search-value', find(), HandleInput, [], [], HandleFocus));
1807
+ dom.push(...getSearchFieldVirtualDom(SearchValue, find(), HandleInput, getFindButtons(), [], HandleFocus));
1742
1808
  const findClassName = getFindMatchCountClassName(matchCount, value);
1743
1809
  dom.push({
1744
1810
  type: Div,
@@ -1755,7 +1821,7 @@ const getFindWidgetReplaceVirtualDom = (replaceExpanded, replaceButtons) => {
1755
1821
  type: Div,
1756
1822
  className: FindWidgetReplace,
1757
1823
  childCount: 1 + replaceButtons.length
1758
- }, ...getSearchFieldVirtualDom(ReplaceValue, replace(), HandleReplaceInput, [], [], HandleReplaceFocus), ...replaceButtons.flatMap(getIconButtonVirtualDom));
1824
+ }, ...getSearchFieldVirtualDom(ReplaceValue, replace$1(), HandleReplaceInput, [], [], HandleReplaceFocus), ...replaceButtons.flatMap(getIconButtonVirtualDom));
1759
1825
  }
1760
1826
  return dom;
1761
1827
  };
@@ -1864,6 +1930,9 @@ const getEventListeners = (uid, widgetId) => {
1864
1930
  }, {
1865
1931
  name: HandleClickReplaceAll,
1866
1932
  params: ['executeWidgetCommand', 'FindWidget', 'FindWidget.handleClickReplaceAll', 0, widgetId]
1933
+ }, {
1934
+ name: HandleClickButton,
1935
+ params: ['executeWidgetCommand', 'FindWidget', 'FindWidget.handleClickButton', 0, widgetId, `event.target.name`]
1867
1936
  }];
1868
1937
  };
1869
1938
 
@@ -1890,6 +1959,14 @@ const renderFocusContext = (oldState, newState) => {
1890
1959
  return [/* method */SetFocusContext, FocusFindWidget];
1891
1960
  };
1892
1961
 
1962
+ const renderReplaceValue = (oldState, newState) => {
1963
+ const {
1964
+ uid,
1965
+ replacement
1966
+ } = newState;
1967
+ return [SetValueByName, uid, Replace, replacement];
1968
+ };
1969
+
1893
1970
  const renderUid = (oldState, newState) => {
1894
1971
  const {
1895
1972
  uid,
@@ -1903,7 +1980,7 @@ const renderValue = (oldState, newState) => {
1903
1980
  uid,
1904
1981
  value
1905
1982
  } = newState;
1906
- return [/* method */SetValueByName, uid, SearchValue, value];
1983
+ return [SetValueByName, uid, SearchValue, value];
1907
1984
  };
1908
1985
 
1909
1986
  const getRenderer = diffType => {
@@ -1922,6 +1999,8 @@ const getRenderer = diffType => {
1922
1999
  return renderFocus;
1923
2000
  case RenderValue:
1924
2001
  return renderValue;
2002
+ case RenderReplaceValue:
2003
+ return renderReplaceValue;
1925
2004
  default:
1926
2005
  throw new Error('unknown renderer');
1927
2006
  }
@@ -1946,8 +2025,61 @@ const render2 = (uid, diffResult) => {
1946
2025
  return commands;
1947
2026
  };
1948
2027
 
1949
- const terminate = () => {
1950
- globalThis.close();
2028
+ const applyEdits = async (editorId, edits) => {
2029
+ await applyEdit(editorId, edits);
2030
+ };
2031
+
2032
+ const getEdits = (matches, value, replacement, startIndex, replaceAll) => {
2033
+ if (value.length === 0 || matches.length === 0) {
2034
+ return [];
2035
+ }
2036
+ const edits = [];
2037
+ const totalMatches = Math.floor(matches.length / 2);
2038
+ const fromIndex = replaceAll ? 0 : Math.max(0, startIndex);
2039
+ const toIndex = replaceAll ? totalMatches : Math.min(totalMatches, fromIndex + 1);
2040
+ for (let i = fromIndex; i < toIndex; i++) {
2041
+ const rowIndex = matches[i * 2];
2042
+ const startColumnIndex = matches[i * 2 + 1];
2043
+ const endColumnIndex = startColumnIndex + value.length;
2044
+ edits.push({
2045
+ rowIndex,
2046
+ startColumnIndex,
2047
+ endColumnIndex,
2048
+ newText: replacement
2049
+ });
2050
+ }
2051
+ return edits;
2052
+ };
2053
+
2054
+ const replace = async state => {
2055
+ const {
2056
+ matches,
2057
+ value,
2058
+ replacement,
2059
+ matchIndex,
2060
+ editorUid
2061
+ } = state;
2062
+ const edits = getEdits(matches, value, replacement, matchIndex, false);
2063
+ await applyEdits(editorUid, edits);
2064
+ // TODO ask editor worker to apply edit
2065
+ // TODO ask editor worker for new lines?
2066
+ return {
2067
+ ...state
2068
+ };
2069
+ };
2070
+
2071
+ const replaceAll = state => {
2072
+ const {
2073
+ matches,
2074
+ value,
2075
+ replacement,
2076
+ editorUid
2077
+ } = state;
2078
+ const edits = getEdits(matches, value, replacement, 0, true);
2079
+ void applyEdits(editorUid, edits);
2080
+ return {
2081
+ ...state
2082
+ };
1951
2083
  };
1952
2084
 
1953
2085
  const commandMap = {
@@ -1958,6 +2090,7 @@ const commandMap = {
1958
2090
  'FindWidget.focusFirst': wrapCommand(focusFirst),
1959
2091
  'FindWidget.focusIndex': wrapCommand(focusIndex),
1960
2092
  'FindWidget.focusLast': wrapCommand(focusLast),
2093
+ 'FindWidget.toggleMatchCase': wrapCommand(toggleMatchCase),
1961
2094
  'FindWidget.focusNext': wrapCommand(focusNext),
1962
2095
  'FindWidget.focusPrevious': wrapCommand(focusPrevious),
1963
2096
  'FindWidget.focusReplace': wrapCommand(focusReplace),
@@ -1966,11 +2099,16 @@ const commandMap = {
1966
2099
  'FindWidget.handleBlur': wrapCommand(handleBlur),
1967
2100
  'FindWidget.handleClickClose': wrapCommand(handleClickClose),
1968
2101
  'FindWidget.handleInput': wrapCommand(handleInput),
2102
+ 'FindWidget.handleReplaceInput': wrapCommand(handleReplaceInput),
1969
2103
  'FindWidget.loadContent': wrapCommand(loadContent),
1970
2104
  'FindWidget.preventDefaultBrowserFind': wrapCommand(preventDefaultBrowserFind),
1971
2105
  'FindWidget.render2': render2,
1972
2106
  'FindWidget.terminate': terminate,
1973
- 'FindWidget.toggleReplace': wrapCommand(toggleReplace)
2107
+ 'FindWidget.toggleReplace': wrapCommand(toggleReplace),
2108
+ 'FindWidget.replace': wrapCommand(replace),
2109
+ 'FindWidget.replaceAll': wrapCommand(replaceAll),
2110
+ 'FindWidget.handleClickButton': wrapCommand(handleClickButton),
2111
+ 'FindWidget.toggleUseRegularExpression': wrapCommand(toggleUseRegularExpression)
1974
2112
  };
1975
2113
 
1976
2114
  const listen = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/find-widget-worker",
3
- "version": "1.12.0",
3
+ "version": "1.14.0",
4
4
  "description": "Web Worker for the find widget in Lvce Editor",
5
5
  "repository": {
6
6
  "type": "git",