@lvce-editor/find-widget-worker 1.0.0 → 1.2.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,46 @@ 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: [],
891
+ editorX: x,
892
+ editorY: y,
893
+ editorWidth: width,
894
+ editorHeight: height
889
895
  };
890
- set(uid, state, state);
896
+ set$1(uid, state, state);
891
897
  return state;
892
898
  };
893
899
 
894
- const modules = [];
895
- const numbers = [];
900
+ const isEqual$4 = (oldState, newState) => {
901
+ return oldState.x === newState.x && oldState.y === newState.y && oldState.width === newState.width && oldState.height === newState.height;
902
+ };
903
+
904
+ const isEqual$3 = (oldState, newState) => {
905
+ return oldState.version === newState.version;
906
+ };
907
+
908
+ const isEqual$2 = (oldState, newState) => {
909
+ return oldState.version === newState.version;
910
+ };
911
+
912
+ const isEqual$1 = (oldState, newState) => {
913
+ return oldState.version === newState.version;
914
+ };
915
+
916
+ const isEqual = (oldState, newState) => {
917
+ return oldState.version === newState.version;
918
+ };
919
+
920
+ const RenderBounds = 8;
921
+ const RenderEventListeners = 11;
922
+ const RenderUid = 12;
923
+ const RenderContent = 13;
924
+ const RenderFocusContext = 14;
925
+
926
+ const modules = [isEqual$3, isEqual$1, isEqual$4, isEqual, isEqual$2];
927
+ const numbers = [RenderEventListeners, RenderContent, RenderBounds, RenderUid, RenderFocusContext];
896
928
 
897
929
  const diff = (oldState, newState) => {
898
930
  const diffResult = [];
@@ -909,25 +941,280 @@ const diff2 = uid => {
909
941
  const {
910
942
  oldState,
911
943
  newState
912
- } = get(uid);
944
+ } = get$1(uid);
913
945
  const diffResult = diff(oldState, newState);
914
946
  return diffResult;
915
947
  };
916
948
 
949
+ // TODO don't call renderer worker, set editor state
950
+ // TODO this function should be synchronous
951
+ const focusIndex = (state, index) => {
952
+ const {
953
+ matchIndex
954
+ } = state;
955
+ if (index === matchIndex) {
956
+ return state;
957
+ }
958
+ // TODO update editor
959
+ // const { widgets } = editor
960
+ // const childIndex = widgets.findIndex(isFind)
961
+ // const childWidget = widgets[childIndex]
962
+
963
+ // // TODO find next match and highlight it
964
+ // const matchRowIndex = matches[index * 2]
965
+ // const matchColumnIndex = matches[index * 2 + 1]
966
+ // const newSelections = new Uint32Array([matchRowIndex, matchColumnIndex, matchRowIndex, matchColumnIndex + value.length])
967
+ // const newState: FindWidgetState = {
968
+ // ...findState,
969
+ // matchIndex: index,
970
+ // }
971
+ // const newWidget = {
972
+ // ...childWidget,
973
+ // newState,
974
+ // }
975
+ // const newWidgets = [...widgets.slice(0, childIndex), newWidget, ...widgets.slice(childIndex + 1)]
976
+ return state;
977
+ };
978
+ const focusFirst = state => {
979
+ return focusIndex(state, 0);
980
+ };
981
+ const focusLast = state => {
982
+ const {
983
+ matchCount
984
+ } = state;
985
+ return focusIndex(state, matchCount - 1);
986
+ };
987
+ const focusNext = state => {
988
+ const {
989
+ matchIndex,
990
+ matchCount
991
+ } = state;
992
+ if (matchIndex === matchCount - 1) {
993
+ return focusFirst(state);
994
+ }
995
+ return focusIndex(state, matchIndex + 1);
996
+ };
997
+ const focusPrevious = state => {
998
+ const {
999
+ matchIndex
1000
+ } = state;
1001
+ if (matchIndex === 0) {
1002
+ return focusLast(state);
1003
+ }
1004
+ return focusIndex(state, matchIndex - 1);
1005
+ };
1006
+
1007
+ const handleBlur = async state => {
1008
+ return {
1009
+ ...state,
1010
+ focus: 0,
1011
+ focused: false
1012
+ };
1013
+ };
1014
+
1015
+ const findRegexMatches = (lines, regex) => {
1016
+ if (!regex.global) {
1017
+ throw new Error('regex must be global');
1018
+ }
1019
+ const {
1020
+ length
1021
+ } = lines;
1022
+ const matches = [];
1023
+ for (let i = 0; i < length; i++) {
1024
+ const line = lines[i];
1025
+ let lastMatch;
1026
+ do {
1027
+ lastMatch = regex.exec(line);
1028
+ if (!lastMatch) {
1029
+ break;
1030
+ }
1031
+ matches.push(i, lastMatch.index);
1032
+ } while (true);
1033
+ }
1034
+ return new Uint32Array(matches);
1035
+ };
1036
+
1037
+ // copied from https://github.com/microsoft/vscode/tree/main/src/vs/base/common/strings.ts by Microsoft (License MIT)
1038
+
1039
+ const RE_ESCAPE = /[\\\{\}\*\+\?\|\^\$\.\[\]\(\)]/g;
1040
+ const escapeRegExpCharacters = value => {
1041
+ return value.replaceAll(RE_ESCAPE, String.raw`\$&`);
1042
+ };
1043
+
1044
+ const regexFlags = 'gi';
1045
+ const getSearchRegex = searchString => {
1046
+ const escaped = escapeRegExpCharacters(searchString);
1047
+ return new RegExp(escaped, regexFlags);
1048
+ };
1049
+
1050
+ const findMatchesCaseInsensitive = (lines, searchString) => {
1051
+ if (searchString.length === 0) {
1052
+ return new Uint32Array([]);
1053
+ }
1054
+ const regex = getSearchRegex(searchString);
1055
+ return findRegexMatches(lines, regex);
1056
+ };
1057
+
1058
+ const getMatchCount = matches => {
1059
+ return matches.length / 2;
1060
+ };
1061
+
1062
+ const refresh$1 = (state, value) => {
1063
+ const {
1064
+ lines
1065
+ } = state;
1066
+ const matches = findMatchesCaseInsensitive(lines, value);
1067
+ const matchCount = getMatchCount(matches);
1068
+ return {
1069
+ ...state,
1070
+ matches,
1071
+ matchIndex: 0,
1072
+ matchCount,
1073
+ value
1074
+ };
1075
+ };
1076
+
1077
+ const refresh = (state, value = state.value) => {
1078
+ return refresh$1(state, value);
1079
+ };
1080
+
1081
+ const handleInput = (state, value) => {
1082
+ return refresh(state, value);
1083
+ };
1084
+
917
1085
  const commandIds = ['handleSliderPointerDown', 'handleSliderPointerMove'];
918
1086
 
919
1087
  const getCommandIds = () => {
920
1088
  return commandIds;
921
1089
  };
922
1090
 
923
- const loadContent = state => {
1091
+ const getFindWidgetPosition = (editorX, editorY, editorWidth) => {
1092
+ const width = 300;
1093
+ const height = 30;
1094
+ const paddingTop = 10;
1095
+ const paddingRight = 20;
1096
+ const x = editorX + editorWidth - width - paddingRight;
1097
+ const y = editorY + paddingTop;
1098
+ return {
1099
+ y,
1100
+ x,
1101
+ width,
1102
+ height
1103
+ };
1104
+ };
1105
+
1106
+ const rpcs = Object.create(null);
1107
+ const set$a = (id, rpc) => {
1108
+ rpcs[id] = rpc;
1109
+ };
1110
+ const get = id => {
1111
+ return rpcs[id];
1112
+ };
1113
+
1114
+ /* eslint-disable @typescript-eslint/explicit-function-return-type */
1115
+
1116
+ const create = rpcId => {
1117
+ return {
1118
+ // @ts-ignore
1119
+ invoke(method, ...params) {
1120
+ const rpc = get(rpcId);
1121
+ // @ts-ignore
1122
+ return rpc.invoke(method, ...params);
1123
+ },
1124
+ // @ts-ignore
1125
+ invokeAndTransfer(method, ...params) {
1126
+ const rpc = get(rpcId);
1127
+ // @ts-ignore
1128
+ return rpc.invokeAndTransfer(method, ...params);
1129
+ },
1130
+ set(rpc) {
1131
+ set$a(rpcId, rpc);
1132
+ }
1133
+ };
1134
+ };
1135
+ const EditorWorker$1 = 99;
1136
+ const {
1137
+ invoke: invoke$9,
1138
+ set: set$9
1139
+ } = create(EditorWorker$1);
1140
+ const EditorWorker = {
1141
+ __proto__: null,
1142
+ invoke: invoke$9,
1143
+ set: set$9
1144
+ };
1145
+
1146
+ const {
1147
+ invoke,
1148
+ set} = EditorWorker;
1149
+
1150
+ const getLines = async editorUid => {
1151
+ // @ts-ignore
1152
+ const lines = await invoke('Editor.getLines2', editorUid);
1153
+ // @ts-ignore
1154
+ return lines;
1155
+ };
1156
+
1157
+ const getSelections = async editorUid => {
1158
+ // @ts-ignore
1159
+ const selections = await invoke('Editor.getSelections2', editorUid);
1160
+ // @ts-ignore
1161
+ return selections;
1162
+ };
1163
+
1164
+ const loadContent = async state => {
1165
+ const {
1166
+ editorUid,
1167
+ editorWidth,
1168
+ editorX,
1169
+ editorY
1170
+ } = state;
1171
+ const lines = await getLines(editorUid);
1172
+ const selections = await getSelections(editorUid);
1173
+ const {
1174
+ x,
1175
+ y,
1176
+ width,
1177
+ height
1178
+ } = getFindWidgetPosition(editorX, editorY, editorWidth);
1179
+ if (lines.length === 0) {
1180
+ return state;
1181
+ }
1182
+ const startRowIndex = selections[0];
1183
+ const startColumnIndex = selections[1];
1184
+ const endColumnIndex = selections[3];
1185
+ const line = lines[startRowIndex];
1186
+ const value = line.slice(startColumnIndex, endColumnIndex);
1187
+ const matches = findMatchesCaseInsensitive(lines, value);
1188
+ const matchCount = getMatchCount(matches);
924
1189
  return {
925
1190
  ...state,
926
- version: 1
1191
+ value,
1192
+ matches,
1193
+ matchIndex: 0,
1194
+ matchCount,
1195
+ version: 1,
1196
+ x,
1197
+ y,
1198
+ width,
1199
+ height
927
1200
  };
928
1201
  };
929
1202
 
930
- const RenderContent = 13;
1203
+ const SetBounds = 'Viewlet.setBounds';
1204
+ const SetDom2 = 'Viewlet.setDom2';
1205
+ const SetUid = 'Viewlet.setUid';
1206
+ const SetEventListeners = 'Viewlet.registerEventListeners';
1207
+
1208
+ const renderBounds = (oldState, newState) => {
1209
+ const {
1210
+ x,
1211
+ y,
1212
+ width,
1213
+ height,
1214
+ uid
1215
+ } = newState;
1216
+ return [SetBounds, uid, x, y, width, height];
1217
+ };
931
1218
 
932
1219
  const HandleClickClose = 'handleClickClose';
933
1220
  const HandleClickNextMatch = 'handleClickNextMatch';
@@ -1253,8 +1540,6 @@ const getMatchCountText = (matchIndex, matchCount) => {
1253
1540
  return matchOf(matchIndex + 1, matchCount);
1254
1541
  };
1255
1542
 
1256
- const SetDom2 = 'Viewlet.setDom2';
1257
-
1258
1543
  const renderContent = (oldState, newState) => {
1259
1544
  const {
1260
1545
  uid
@@ -1272,10 +1557,44 @@ const renderContent = (oldState, newState) => {
1272
1557
  return [SetDom2, uid, dom];
1273
1558
  };
1274
1559
 
1560
+ const getEventListeners = uid => {
1561
+ return [];
1562
+ };
1563
+
1564
+ const renderEventListeners = state => {
1565
+ const {
1566
+ uid
1567
+ } = state;
1568
+ const eventListeners = getEventListeners();
1569
+ return [SetEventListeners, uid, eventListeners];
1570
+ };
1571
+
1572
+ const FocusFindWidget = 16;
1573
+
1574
+ const renderFocusContext = (oldState, newState) => {
1575
+ return [/* method */'Viewlet.setFocusContext', FocusFindWidget];
1576
+ };
1577
+
1578
+ const renderUid = (oldState, newState) => {
1579
+ const {
1580
+ uid,
1581
+ editorUid
1582
+ } = newState;
1583
+ return [SetUid, uid, editorUid];
1584
+ };
1585
+
1275
1586
  const getRenderer = diffType => {
1276
1587
  switch (diffType) {
1277
1588
  case RenderContent:
1278
1589
  return renderContent;
1590
+ case RenderBounds:
1591
+ return renderBounds;
1592
+ case RenderEventListeners:
1593
+ return renderEventListeners;
1594
+ case RenderUid:
1595
+ return renderUid;
1596
+ case RenderFocusContext:
1597
+ return renderFocusContext;
1279
1598
  default:
1280
1599
  throw new Error('unknown renderer');
1281
1600
  }
@@ -1294,8 +1613,8 @@ const render2 = (uid, diffResult) => {
1294
1613
  const {
1295
1614
  oldState,
1296
1615
  newState
1297
- } = get(uid);
1298
- set(uid, newState, newState);
1616
+ } = get$1(uid);
1617
+ set$1(uid, newState, newState);
1299
1618
  const commands = applyRender(oldState, newState, diffResult);
1300
1619
  return commands;
1301
1620
  };
@@ -1305,26 +1624,26 @@ const terminate = () => {
1305
1624
  };
1306
1625
 
1307
1626
  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;
1627
+ 'FindWidget.create': create$1,
1628
+ 'FindWidget.diff2': diff2,
1629
+ 'FindWidget.focusFirst': focusFirst,
1630
+ 'FindWidget.focusIndex': focusIndex,
1631
+ 'FindWidget.focusLast': focusLast,
1632
+ 'FindWidget.focusNext': focusNext,
1633
+ 'FindWidget.focusPrevious': focusPrevious,
1634
+ 'FindWidget.getCommandIds': getCommandIds,
1635
+ 'FindWidget.handleBlur': handleBlur,
1636
+ 'FindWidget.handleInput': handleInput,
1637
+ 'FindWidget.loadContent': wrapCommand(loadContent),
1638
+ 'FindWidget.render2': render2,
1639
+ 'FindWidget.terminate': terminate
1321
1640
  };
1322
1641
 
1323
1642
  const listen = async () => {
1324
1643
  const rpc = await WebWorkerRpcClient.create({
1325
1644
  commandMap: commandMap
1326
1645
  });
1327
- set$6(RendererWorker, rpc);
1646
+ set(rpc);
1328
1647
  };
1329
1648
 
1330
1649
  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.2.0",
4
4
  "description": "Web Worker for the find widget in Lvce Editor",
5
5
  "repository": {
6
6
  "type": "git",