@lvce-editor/find-widget-worker 1.0.0 → 1.1.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.
@@ -378,26 +378,26 @@ const create$4 = (method, params) => {
378
378
  };
379
379
  };
380
380
  const callbacks = Object.create(null);
381
- const set$1 = (id, fn) => {
381
+ const set$2 = (id, fn) => {
382
382
  callbacks[id] = fn;
383
383
  };
384
- const get$1 = id => {
384
+ const get$2 = id => {
385
385
  return callbacks[id];
386
386
  };
387
387
  const remove = id => {
388
388
  delete callbacks[id];
389
389
  };
390
390
  let id = 0;
391
- const create$3 = () => {
391
+ const create$3$1 = () => {
392
392
  return ++id;
393
393
  };
394
394
  const registerPromise = () => {
395
- const id = create$3();
395
+ const id = create$3$1();
396
396
  const {
397
397
  resolve,
398
398
  promise
399
399
  } = Promise.withResolvers();
400
- set$1(id, resolve);
400
+ set$2(id, resolve);
401
401
  return {
402
402
  id,
403
403
  promise
@@ -554,7 +554,7 @@ const warn = (...args) => {
554
554
  console.warn(...args);
555
555
  };
556
556
  const resolve = (id, response) => {
557
- const fn = get$1(id);
557
+ const fn = get$2(id);
558
558
  if (!fn) {
559
559
  console.log(response);
560
560
  warn(`callback ${id} may already be disposed`);
@@ -708,7 +708,7 @@ const send = (transport, method, ...params) => {
708
708
  const message = create$4(method, params);
709
709
  transport.send(message);
710
710
  };
711
- const invoke = (ipc, method, ...params) => {
711
+ const invoke$1 = (ipc, method, ...params) => {
712
712
  return invokeHelper(ipc, method, params, false);
713
713
  };
714
714
  const invokeAndTransfer = (ipc, method, ...params) => {
@@ -741,7 +741,7 @@ const createRpc = ipc => {
741
741
  send(ipc, method, ...params);
742
742
  },
743
743
  invoke(method, ...params) {
744
- return invoke(ipc, method, ...params);
744
+ return invoke$1(ipc, method, ...params);
745
745
  },
746
746
  invokeAndTransfer(method, ...params) {
747
747
  return invokeAndTransfer(ipc, method, ...params);
@@ -782,7 +782,7 @@ const listen$1 = async (module, options) => {
782
782
  const ipc = module.wrap(rawIpc);
783
783
  return ipc;
784
784
  };
785
- const create$2 = async ({
785
+ const create$3 = async ({
786
786
  commandMap
787
787
  }) => {
788
788
  // TODO create a commandMap per rpc instance
@@ -794,10 +794,10 @@ const create$2 = async ({
794
794
  };
795
795
  const WebWorkerRpcClient = {
796
796
  __proto__: null,
797
- create: create$2
797
+ create: create$3
798
798
  };
799
799
 
800
- const create$1 = () => {
800
+ const create$2 = () => {
801
801
  const states = Object.create(null);
802
802
  return {
803
803
  get(uid) {
@@ -857,14 +857,14 @@ const create$1 = () => {
857
857
  };
858
858
 
859
859
  const {
860
- get,
861
- set,
860
+ get: get$1,
861
+ set: set$1,
862
862
  wrapCommand
863
- } = create$1();
863
+ } = create$2();
864
864
 
865
865
  const Unknown = 0;
866
866
 
867
- const create = (uid, x, y, width, height, parentUid) => {
867
+ const create$1 = (uid, x, y, width, height, parentUid) => {
868
868
  const state = {
869
869
  uid,
870
870
  value: '',
@@ -877,7 +877,7 @@ const create = (uid, x, y, width, height, parentUid) => {
877
877
  matchCase: false,
878
878
  matchWholeWord: false,
879
879
  replacement: '',
880
- editorUid: 0,
880
+ editorUid: parentUid,
881
881
  x: 0,
882
882
  y: 0,
883
883
  width: 0,
@@ -885,14 +885,42 @@ const create = (uid, x, y, width, height, parentUid) => {
885
885
  focused: false,
886
886
  focusSource: Unknown,
887
887
  focus: 0,
888
- version: 0
888
+ version: 0,
889
+ lines: [],
890
+ selections: []
889
891
  };
890
- set(uid, state, state);
892
+ set$1(uid, state, state);
891
893
  return state;
892
894
  };
893
895
 
894
- const modules = [];
895
- const numbers = [];
896
+ const isEqual$4 = (oldState, newState) => {
897
+ return oldState.x === newState.x && oldState.y === newState.y && oldState.width === newState.width && oldState.height === newState.height;
898
+ };
899
+
900
+ const isEqual$3 = (oldState, newState) => {
901
+ return oldState.version === newState.version;
902
+ };
903
+
904
+ const isEqual$2 = (oldState, newState) => {
905
+ return oldState.version === newState.version;
906
+ };
907
+
908
+ const isEqual$1 = (oldState, newState) => {
909
+ return oldState.version === newState.version;
910
+ };
911
+
912
+ const isEqual = (oldState, newState) => {
913
+ return oldState.version === newState.version;
914
+ };
915
+
916
+ const RenderBounds = 8;
917
+ const RenderEventListeners = 11;
918
+ const RenderUid = 12;
919
+ const RenderContent = 13;
920
+ const RenderFocusContext = 14;
921
+
922
+ const modules = [isEqual$3, isEqual$1, isEqual$4, isEqual, isEqual$2];
923
+ const numbers = [RenderEventListeners, RenderContent, RenderBounds, RenderUid, RenderFocusContext];
896
924
 
897
925
  const diff = (oldState, newState) => {
898
926
  const diffResult = [];
@@ -909,25 +937,252 @@ const diff2 = uid => {
909
937
  const {
910
938
  oldState,
911
939
  newState
912
- } = get(uid);
940
+ } = get$1(uid);
913
941
  const diffResult = diff(oldState, newState);
914
942
  return diffResult;
915
943
  };
916
944
 
945
+ // TODO don't call renderer worker, set editor state
946
+ // TODO this function should be synchronous
947
+ const focusIndex = (state, index) => {
948
+ const {
949
+ matchIndex
950
+ } = state;
951
+ if (index === matchIndex) {
952
+ return state;
953
+ }
954
+ // TODO update editor
955
+ // const { widgets } = editor
956
+ // const childIndex = widgets.findIndex(isFind)
957
+ // const childWidget = widgets[childIndex]
958
+
959
+ // // TODO find next match and highlight it
960
+ // const matchRowIndex = matches[index * 2]
961
+ // const matchColumnIndex = matches[index * 2 + 1]
962
+ // const newSelections = new Uint32Array([matchRowIndex, matchColumnIndex, matchRowIndex, matchColumnIndex + value.length])
963
+ // const newState: FindWidgetState = {
964
+ // ...findState,
965
+ // matchIndex: index,
966
+ // }
967
+ // const newWidget = {
968
+ // ...childWidget,
969
+ // newState,
970
+ // }
971
+ // const newWidgets = [...widgets.slice(0, childIndex), newWidget, ...widgets.slice(childIndex + 1)]
972
+ return state;
973
+ };
974
+ const focusFirst = state => {
975
+ return focusIndex(state, 0);
976
+ };
977
+ const focusLast = state => {
978
+ const {
979
+ matchCount
980
+ } = state;
981
+ return focusIndex(state, matchCount - 1);
982
+ };
983
+ const focusNext = state => {
984
+ const {
985
+ matchIndex,
986
+ matchCount
987
+ } = state;
988
+ if (matchIndex === matchCount - 1) {
989
+ return focusFirst(state);
990
+ }
991
+ return focusIndex(state, matchIndex + 1);
992
+ };
993
+ const focusPrevious = state => {
994
+ const {
995
+ matchIndex
996
+ } = state;
997
+ if (matchIndex === 0) {
998
+ return focusLast(state);
999
+ }
1000
+ return focusIndex(state, matchIndex - 1);
1001
+ };
1002
+
1003
+ const handleBlur = async state => {
1004
+ return {
1005
+ ...state,
1006
+ focus: 0,
1007
+ focused: false
1008
+ };
1009
+ };
1010
+
1011
+ const findRegexMatches = (lines, regex) => {
1012
+ if (!regex.global) {
1013
+ throw new Error('regex must be global');
1014
+ }
1015
+ const {
1016
+ length
1017
+ } = lines;
1018
+ const matches = [];
1019
+ for (let i = 0; i < length; i++) {
1020
+ const line = lines[i];
1021
+ let lastMatch;
1022
+ do {
1023
+ lastMatch = regex.exec(line);
1024
+ if (!lastMatch) {
1025
+ break;
1026
+ }
1027
+ matches.push(i, lastMatch.index);
1028
+ } while (true);
1029
+ }
1030
+ return new Uint32Array(matches);
1031
+ };
1032
+
1033
+ // copied from https://github.com/microsoft/vscode/tree/main/src/vs/base/common/strings.ts by Microsoft (License MIT)
1034
+
1035
+ const RE_ESCAPE = /[\\\{\}\*\+\?\|\^\$\.\[\]\(\)]/g;
1036
+ const escapeRegExpCharacters = value => {
1037
+ return value.replaceAll(RE_ESCAPE, String.raw`\$&`);
1038
+ };
1039
+
1040
+ const regexFlags = 'gi';
1041
+ const getSearchRegex = searchString => {
1042
+ const escaped = escapeRegExpCharacters(searchString);
1043
+ return new RegExp(escaped, regexFlags);
1044
+ };
1045
+
1046
+ const findMatchesCaseInsensitive = (lines, searchString) => {
1047
+ if (searchString.length === 0) {
1048
+ return new Uint32Array([]);
1049
+ }
1050
+ const regex = getSearchRegex(searchString);
1051
+ return findRegexMatches(lines, regex);
1052
+ };
1053
+
1054
+ const getMatchCount = matches => {
1055
+ return matches.length / 2;
1056
+ };
1057
+
1058
+ const refresh$1 = (state, value) => {
1059
+ const {
1060
+ lines
1061
+ } = state;
1062
+ const matches = findMatchesCaseInsensitive(lines, value);
1063
+ const matchCount = getMatchCount(matches);
1064
+ return {
1065
+ ...state,
1066
+ matches,
1067
+ matchIndex: 0,
1068
+ matchCount,
1069
+ value
1070
+ };
1071
+ };
1072
+
1073
+ const refresh = (state, value = state.value) => {
1074
+ return refresh$1(state, value);
1075
+ };
1076
+
1077
+ const handleInput = (state, value) => {
1078
+ return refresh(state, value);
1079
+ };
1080
+
917
1081
  const commandIds = ['handleSliderPointerDown', 'handleSliderPointerMove'];
918
1082
 
919
1083
  const getCommandIds = () => {
920
1084
  return commandIds;
921
1085
  };
922
1086
 
923
- const loadContent = state => {
1087
+ const rpcs = Object.create(null);
1088
+ const set$a = (id, rpc) => {
1089
+ rpcs[id] = rpc;
1090
+ };
1091
+ const get = id => {
1092
+ return rpcs[id];
1093
+ };
1094
+
1095
+ /* eslint-disable @typescript-eslint/explicit-function-return-type */
1096
+
1097
+ const create = rpcId => {
1098
+ return {
1099
+ // @ts-ignore
1100
+ invoke(method, ...params) {
1101
+ const rpc = get(rpcId);
1102
+ // @ts-ignore
1103
+ return rpc.invoke(method, ...params);
1104
+ },
1105
+ // @ts-ignore
1106
+ invokeAndTransfer(method, ...params) {
1107
+ const rpc = get(rpcId);
1108
+ // @ts-ignore
1109
+ return rpc.invokeAndTransfer(method, ...params);
1110
+ },
1111
+ set(rpc) {
1112
+ set$a(rpcId, rpc);
1113
+ }
1114
+ };
1115
+ };
1116
+ const EditorWorker$1 = 99;
1117
+ const {
1118
+ invoke: invoke$9,
1119
+ set: set$9
1120
+ } = create(EditorWorker$1);
1121
+ const EditorWorker = {
1122
+ __proto__: null,
1123
+ invoke: invoke$9,
1124
+ set: set$9
1125
+ };
1126
+
1127
+ const {
1128
+ invoke,
1129
+ set} = EditorWorker;
1130
+
1131
+ const getLines = async editorUid => {
1132
+ // @ts-ignore
1133
+ const lines = await invoke('Editor.getLines2', editorUid);
1134
+ // @ts-ignore
1135
+ return lines;
1136
+ };
1137
+
1138
+ const getSelections = async editorUid => {
1139
+ // @ts-ignore
1140
+ const selections = await invoke('Editor.getSelections2', editorUid);
1141
+ // @ts-ignore
1142
+ return selections;
1143
+ };
1144
+
1145
+ const loadContent = async state => {
1146
+ const {
1147
+ editorUid
1148
+ } = state;
1149
+ const lines = await getLines(editorUid);
1150
+ const selections = await getSelections(editorUid);
1151
+ if (lines.length === 0) {
1152
+ return state;
1153
+ }
1154
+ const startRowIndex = selections[0];
1155
+ const startColumnIndex = selections[1];
1156
+ const endColumnIndex = selections[3];
1157
+ const line = lines[startRowIndex];
1158
+ const value = line.slice(startColumnIndex, endColumnIndex);
1159
+ const matches = findMatchesCaseInsensitive(lines, value);
1160
+ const matchCount = getMatchCount(matches);
924
1161
  return {
925
1162
  ...state,
1163
+ value,
1164
+ matches,
1165
+ matchIndex: 0,
1166
+ matchCount,
926
1167
  version: 1
927
1168
  };
928
1169
  };
929
1170
 
930
- const RenderContent = 13;
1171
+ const SetBounds = 'Viewlet.setBounds';
1172
+ const SetDom2 = 'Viewlet.setDom2';
1173
+ const SetUid = 'Viewlet.setUid';
1174
+ const SetEventListeners = 'Viewlet.registerEventListeners';
1175
+
1176
+ const renderBounds = (oldState, newState) => {
1177
+ const {
1178
+ x,
1179
+ y,
1180
+ width,
1181
+ height,
1182
+ uid
1183
+ } = newState;
1184
+ return [SetBounds, uid, x, y, width, height];
1185
+ };
931
1186
 
932
1187
  const HandleClickClose = 'handleClickClose';
933
1188
  const HandleClickNextMatch = 'handleClickNextMatch';
@@ -1253,8 +1508,6 @@ const getMatchCountText = (matchIndex, matchCount) => {
1253
1508
  return matchOf(matchIndex + 1, matchCount);
1254
1509
  };
1255
1510
 
1256
- const SetDom2 = 'Viewlet.setDom2';
1257
-
1258
1511
  const renderContent = (oldState, newState) => {
1259
1512
  const {
1260
1513
  uid
@@ -1272,10 +1525,44 @@ const renderContent = (oldState, newState) => {
1272
1525
  return [SetDom2, uid, dom];
1273
1526
  };
1274
1527
 
1528
+ const getEventListeners = uid => {
1529
+ return [];
1530
+ };
1531
+
1532
+ const renderEventListeners = state => {
1533
+ const {
1534
+ uid
1535
+ } = state;
1536
+ const eventListeners = getEventListeners();
1537
+ return [SetEventListeners, uid, eventListeners];
1538
+ };
1539
+
1540
+ const FocusFindWidget = 16;
1541
+
1542
+ const renderFocusContext = (oldState, newState) => {
1543
+ return [/* method */'Viewlet.setFocusContext', FocusFindWidget];
1544
+ };
1545
+
1546
+ const renderUid = (oldState, newState) => {
1547
+ const {
1548
+ uid,
1549
+ editorUid
1550
+ } = newState;
1551
+ return [SetUid, uid, editorUid];
1552
+ };
1553
+
1275
1554
  const getRenderer = diffType => {
1276
1555
  switch (diffType) {
1277
1556
  case RenderContent:
1278
1557
  return renderContent;
1558
+ case RenderBounds:
1559
+ return renderBounds;
1560
+ case RenderEventListeners:
1561
+ return renderEventListeners;
1562
+ case RenderUid:
1563
+ return renderUid;
1564
+ case RenderFocusContext:
1565
+ return renderFocusContext;
1279
1566
  default:
1280
1567
  throw new Error('unknown renderer');
1281
1568
  }
@@ -1294,8 +1581,8 @@ const render2 = (uid, diffResult) => {
1294
1581
  const {
1295
1582
  oldState,
1296
1583
  newState
1297
- } = get(uid);
1298
- set(uid, newState, newState);
1584
+ } = get$1(uid);
1585
+ set$1(uid, newState, newState);
1299
1586
  const commands = applyRender(oldState, newState, diffResult);
1300
1587
  return commands;
1301
1588
  };
@@ -1305,26 +1592,26 @@ const terminate = () => {
1305
1592
  };
1306
1593
 
1307
1594
  const commandMap = {
1308
- 'ColorPicker.create': create,
1309
- 'ColorPicker.diff2': diff2,
1310
- 'ColorPicker.getCommandIds': getCommandIds,
1311
- 'ColorPicker.loadContent': wrapCommand(loadContent),
1312
- 'ColorPicker.render2': render2,
1313
- 'ColorPicker.terminate': terminate
1314
- };
1315
-
1316
- const RendererWorker = 1;
1317
-
1318
- const rpcs = Object.create(null);
1319
- const set$6 = (id, rpc) => {
1320
- rpcs[id] = rpc;
1595
+ 'FindWidget.create': create$1,
1596
+ 'FindWidget.diff2': diff2,
1597
+ 'FindWidget.focusFirst': focusFirst,
1598
+ 'FindWidget.focusIndex': focusIndex,
1599
+ 'FindWidget.focusLast': focusLast,
1600
+ 'FindWidget.focusNext': focusNext,
1601
+ 'FindWidget.focusPrevious': focusPrevious,
1602
+ 'FindWidget.getCommandIds': getCommandIds,
1603
+ 'FindWidget.handleBlur': handleBlur,
1604
+ 'FindWidget.handleInput': handleInput,
1605
+ 'FindWidget.loadContent': wrapCommand(loadContent),
1606
+ 'FindWidget.render2': render2,
1607
+ 'FindWidget.terminate': terminate
1321
1608
  };
1322
1609
 
1323
1610
  const listen = async () => {
1324
1611
  const rpc = await WebWorkerRpcClient.create({
1325
1612
  commandMap: commandMap
1326
1613
  });
1327
- set$6(RendererWorker, rpc);
1614
+ set(rpc);
1328
1615
  };
1329
1616
 
1330
1617
  const main = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/find-widget-worker",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Web Worker for the find widget in Lvce Editor",
5
5
  "repository": {
6
6
  "type": "git",