@lvce-editor/menu-worker 1.7.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.
@@ -785,10 +785,10 @@ const send = (transport, method, ...params) => {
785
785
  const message = create$4(method, params);
786
786
  transport.send(message);
787
787
  };
788
- const invoke$2 = (ipc, method, ...params) => {
788
+ const invoke$3 = (ipc, method, ...params) => {
789
789
  return invokeHelper(ipc, method, params, false);
790
790
  };
791
- const invokeAndTransfer = (ipc, method, ...params) => {
791
+ const invokeAndTransfer$1 = (ipc, method, ...params) => {
792
792
  return invokeHelper(ipc, method, params, true);
793
793
  };
794
794
 
@@ -824,10 +824,10 @@ const createRpc = ipc => {
824
824
  send(ipc, method, ...params);
825
825
  },
826
826
  invoke(method, ...params) {
827
- return invoke$2(ipc, method, ...params);
827
+ return invoke$3(ipc, method, ...params);
828
828
  },
829
829
  invokeAndTransfer(method, ...params) {
830
- return invokeAndTransfer(ipc, method, ...params);
830
+ return invokeAndTransfer$1(ipc, method, ...params);
831
831
  },
832
832
  async dispose() {
833
833
  await ipc?.dispose();
@@ -879,6 +879,25 @@ const WebWorkerRpcClient = {
879
879
  __proto__: null,
880
880
  create: create$2
881
881
  };
882
+ const createMockRpc = ({
883
+ commandMap
884
+ }) => {
885
+ const invocations = [];
886
+ const invoke = (method, ...params) => {
887
+ invocations.push([method, ...params]);
888
+ const command = commandMap[method];
889
+ if (!command) {
890
+ throw new Error(`command ${method} not found`);
891
+ }
892
+ return command(...params);
893
+ };
894
+ const mockRpc = {
895
+ invoke,
896
+ invokeAndTransfer: invoke,
897
+ invocations
898
+ };
899
+ return mockRpc;
900
+ };
882
901
 
883
902
  const Text = 12;
884
903
 
@@ -1152,7 +1171,11 @@ const CtrlCmd = 1 << 11 >>> 0;
1152
1171
  const Shift = 1 << 10 >>> 0;
1153
1172
 
1154
1173
  const Separator$2 = 1;
1174
+ const None$1 = 0;
1175
+ const SubMenu$1 = 4;
1155
1176
  const Disabled$1 = 5;
1177
+ const RestoreFocus$1 = 6;
1178
+ const Ignore$1 = 7;
1156
1179
 
1157
1180
  const parseKey = rawKey => {
1158
1181
  const isCtrl = Boolean(rawKey & CtrlCmd);
@@ -1166,7 +1189,8 @@ const parseKey = rawKey => {
1166
1189
  };
1167
1190
  };
1168
1191
 
1169
- const RendererWorker = 1;
1192
+ const DebugWorker = 55;
1193
+ const RendererWorker$1 = 1;
1170
1194
 
1171
1195
  const rpcs = Object.create(null);
1172
1196
  const set$3 = (id, rpc) => {
@@ -1201,8 +1225,359 @@ const create$1 = rpcId => {
1201
1225
  };
1202
1226
 
1203
1227
  const {
1204
- invoke: invoke$1,
1205
- set: set$2} = create$1(RendererWorker);
1228
+ invoke: invoke$2,
1229
+ invokeAndTransfer,
1230
+ set: set$2,
1231
+ dispose
1232
+ } = create$1(RendererWorker$1);
1233
+ const searchFileHtml = async uri => {
1234
+ return invoke$2('ExtensionHost.searchFileWithHtml', uri);
1235
+ };
1236
+ const getFilePathElectron = async file => {
1237
+ return invoke$2('FileSystemHandle.getFilePathElectron', file);
1238
+ };
1239
+ const showContextMenu = async (x, y, id, ...args) => {
1240
+ return invoke$2('ContextMenu.show', x, y, id, ...args);
1241
+ };
1242
+ const getElectronVersion = async () => {
1243
+ return invoke$2('Process.getElectronVersion');
1244
+ };
1245
+ const applyBulkReplacement = async bulkEdits => {
1246
+ await invoke$2('BulkReplacement.applyBulkReplacement', bulkEdits);
1247
+ };
1248
+ const setColorTheme = async id => {
1249
+ // @ts-ignore
1250
+ return invoke$2(/* ColorTheme.setColorTheme */'ColorTheme.setColorTheme', /* colorThemeId */id);
1251
+ };
1252
+ const getNodeVersion = async () => {
1253
+ return invoke$2('Process.getNodeVersion');
1254
+ };
1255
+ const getChromeVersion = async () => {
1256
+ return invoke$2('Process.getChromeVersion');
1257
+ };
1258
+ const getV8Version = async () => {
1259
+ return invoke$2('Process.getV8Version');
1260
+ };
1261
+ const getFileHandles = async fileIds => {
1262
+ const files = await invoke$2('FileSystemHandle.getFileHandles', fileIds);
1263
+ return files;
1264
+ };
1265
+ const setWorkspacePath = async path => {
1266
+ await invoke$2('Workspace.setPath', path);
1267
+ };
1268
+ const registerWebViewInterceptor = async (id, port) => {
1269
+ await invokeAndTransfer('WebView.registerInterceptor', id, port);
1270
+ };
1271
+ const unregisterWebViewInterceptor = async id => {
1272
+ await invoke$2('WebView.unregisterInterceptor', id);
1273
+ };
1274
+ const sendMessagePortToEditorWorker = async (port, rpcId) => {
1275
+ const command = 'HandleMessagePort.handleMessagePort';
1276
+ // @ts-ignore
1277
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToEditorWorker', port, command, rpcId);
1278
+ };
1279
+ const sendMessagePortToErrorWorker = async (port, rpcId) => {
1280
+ const command = 'Errors.handleMessagePort';
1281
+ // @ts-ignore
1282
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToErrorWorker', port, command, rpcId);
1283
+ };
1284
+ const sendMessagePortToMarkdownWorker = async (port, rpcId) => {
1285
+ const command = 'Markdown.handleMessagePort';
1286
+ // @ts-ignore
1287
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToMarkdownWorker', port, command, rpcId);
1288
+ };
1289
+ const sendMessagePortToIconThemeWorker = async (port, rpcId) => {
1290
+ const command = 'IconTheme.handleMessagePort';
1291
+ // @ts-ignore
1292
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToIconThemeWorker', port, command, rpcId);
1293
+ };
1294
+ const sendMessagePortToFileSystemWorker = async (port, rpcId) => {
1295
+ const command = 'FileSystem.handleMessagePort';
1296
+ // @ts-ignore
1297
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToFileSystemWorker', port, command, rpcId);
1298
+ };
1299
+ const readFile = async uri => {
1300
+ return invoke$2('FileSystem.readFile', uri);
1301
+ };
1302
+ const getWebViewSecret = async key => {
1303
+ // @ts-ignore
1304
+ return invoke$2('WebView.getSecret', key);
1305
+ };
1306
+ const setWebViewPort = async (uid, port, origin, portType) => {
1307
+ return invokeAndTransfer('WebView.setPort', uid, port, origin, portType);
1308
+ };
1309
+ const setFocus = key => {
1310
+ return invoke$2('Focus.setFocus', key);
1311
+ };
1312
+ const getFileIcon = async options => {
1313
+ return invoke$2('IconTheme.getFileIcon', options);
1314
+ };
1315
+ const getColorThemeNames = async () => {
1316
+ return invoke$2('ColorTheme.getColorThemeNames');
1317
+ };
1318
+ const disableExtension = async id => {
1319
+ // @ts-ignore
1320
+ return invoke$2('ExtensionManagement.disable', id);
1321
+ };
1322
+ const enableExtension = async id => {
1323
+ // @ts-ignore
1324
+ return invoke$2('ExtensionManagement.enable', id);
1325
+ };
1326
+ const handleDebugChange = async params => {
1327
+ // @ts-ignore
1328
+ return invoke$2('Run And Debug.handleChange', params);
1329
+ };
1330
+ const getFolderIcon = async options => {
1331
+ return invoke$2('IconTheme.getFolderIcon', options);
1332
+ };
1333
+ const closeWidget = async widgetId => {
1334
+ return invoke$2('Viewlet.closeWidget', widgetId);
1335
+ };
1336
+ const sendMessagePortToExtensionHostWorker = async (port, rpcId = 0) => {
1337
+ const command = 'HandleMessagePort.handleMessagePort2';
1338
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToExtensionHostWorker', port, command, rpcId);
1339
+ };
1340
+ const sendMessagePortToSearchProcess = async port => {
1341
+ await invokeAndTransfer('SendMessagePortToElectron.sendMessagePortToElectron', port, 'HandleMessagePortForSearchProcess.handleMessagePortForSearchProcess');
1342
+ };
1343
+ const confirm = async (message, options) => {
1344
+ // @ts-ignore
1345
+ const result = await invoke$2('ConfirmPrompt.prompt', message, options);
1346
+ return result;
1347
+ };
1348
+ const getRecentlyOpened$1 = async () => {
1349
+ return invoke$2(/* RecentlyOpened.getRecentlyOpened */'RecentlyOpened.getRecentlyOpened');
1350
+ };
1351
+ const getKeyBindings$1 = async () => {
1352
+ return invoke$2('KeyBindingsInitial.getKeyBindings');
1353
+ };
1354
+ const writeClipBoardText = async text => {
1355
+ await invoke$2('ClipBoard.writeText', /* text */text);
1356
+ };
1357
+ const writeClipBoardImage = async blob => {
1358
+ // @ts-ignore
1359
+ await invoke$2('ClipBoard.writeImage', /* text */blob);
1360
+ };
1361
+ const searchFileMemory = async uri => {
1362
+ // @ts-ignore
1363
+ return invoke$2('ExtensionHost.searchFileWithMemory', uri);
1364
+ };
1365
+ const searchFileFetch = async uri => {
1366
+ return invoke$2('ExtensionHost.searchFileWithFetch', uri);
1367
+ };
1368
+ const showMessageBox = async options => {
1369
+ return invoke$2('ElectronDialog.showMessageBox', options);
1370
+ };
1371
+ const handleDebugResumed = async params => {
1372
+ await invoke$2('Run And Debug.handleResumed', params);
1373
+ };
1374
+ const openWidget = async name => {
1375
+ await invoke$2('Viewlet.openWidget', name);
1376
+ };
1377
+ const getIcons = async requests => {
1378
+ const icons = await invoke$2('IconTheme.getIcons', requests);
1379
+ return icons;
1380
+ };
1381
+ const activateByEvent = event => {
1382
+ return invoke$2('ExtensionHostManagement.activateByEvent', event);
1383
+ };
1384
+ const setAdditionalFocus = focusKey => {
1385
+ // @ts-ignore
1386
+ return invoke$2('Focus.setAdditionalFocus', focusKey);
1387
+ };
1388
+ const getActiveEditorId = () => {
1389
+ // @ts-ignore
1390
+ return invoke$2('GetActiveEditor.getActiveEditorId');
1391
+ };
1392
+ const getWorkspacePath = () => {
1393
+ return invoke$2('Workspace.getPath');
1394
+ };
1395
+ const sendMessagePortToRendererProcess = async port => {
1396
+ const command = 'HandleMessagePort.handleMessagePort';
1397
+ // @ts-ignore
1398
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToRendererProcess', port, command, DebugWorker);
1399
+ };
1400
+ const getPreference = async key => {
1401
+ return await invoke$2('Preferences.get', key);
1402
+ };
1403
+ const getAllExtensions = async () => {
1404
+ return invoke$2('ExtensionManagement.getAllExtensions');
1405
+ };
1406
+ const rerenderEditor = async key => {
1407
+ // @ts-ignore
1408
+ return invoke$2('Editor.rerender', key);
1409
+ };
1410
+ const handleDebugPaused = async params => {
1411
+ await invoke$2('Run And Debug.handlePaused', params);
1412
+ };
1413
+ const openUri = async (uri, focus, options) => {
1414
+ await invoke$2('Main.openUri', uri, focus, options);
1415
+ };
1416
+ const sendMessagePortToSyntaxHighlightingWorker = async port => {
1417
+ await invokeAndTransfer(
1418
+ // @ts-ignore
1419
+ 'SendMessagePortToSyntaxHighlightingWorker.sendMessagePortToSyntaxHighlightingWorker', port, 'HandleMessagePort.handleMessagePort2');
1420
+ };
1421
+ const handleDebugScriptParsed = async script => {
1422
+ await invoke$2('Run And Debug.handleScriptParsed', script);
1423
+ };
1424
+ const getWindowId = async () => {
1425
+ return invoke$2('GetWindowId.getWindowId');
1426
+ };
1427
+ const getBlob = async uri => {
1428
+ // @ts-ignore
1429
+ return invoke$2('FileSystem.getBlob', uri);
1430
+ };
1431
+ const getExtensionCommands = async () => {
1432
+ return invoke$2('ExtensionHost.getCommands');
1433
+ };
1434
+ const showErrorDialog = async errorInfo => {
1435
+ // @ts-ignore
1436
+ await invoke$2('ErrorHandling.showErrorDialog', errorInfo);
1437
+ };
1438
+ const getFolderSize = async uri => {
1439
+ // @ts-ignore
1440
+ return await invoke$2('FileSystem.getFolderSize', uri);
1441
+ };
1442
+ const getExtension = async id => {
1443
+ // @ts-ignore
1444
+ return invoke$2('ExtensionManagement.getExtension', id);
1445
+ };
1446
+ const getMarkdownDom = async html => {
1447
+ // @ts-ignore
1448
+ return invoke$2('Markdown.getVirtualDom', html);
1449
+ };
1450
+ const renderMarkdown = async (markdown, options) => {
1451
+ // @ts-ignore
1452
+ return invoke$2('Markdown.renderMarkdown', markdown, options);
1453
+ };
1454
+ const openNativeFolder = async uri => {
1455
+ // @ts-ignore
1456
+ await invoke$2('OpenNativeFolder.openNativeFolder', uri);
1457
+ };
1458
+ const uninstallExtension = async id => {
1459
+ return invoke$2('ExtensionManagement.uninstall', id);
1460
+ };
1461
+ const installExtension = async id => {
1462
+ // @ts-ignore
1463
+ return invoke$2('ExtensionManagement.install', id);
1464
+ };
1465
+ const openExtensionSearch = async () => {
1466
+ // @ts-ignore
1467
+ return invoke$2('SideBar.openViewlet', 'Extensions');
1468
+ };
1469
+ const setExtensionsSearchValue = async searchValue => {
1470
+ // @ts-ignore
1471
+ return invoke$2('Extensions.handleInput', searchValue);
1472
+ };
1473
+ const openExternal = async uri => {
1474
+ // @ts-ignore
1475
+ await invoke$2('Open.openExternal', uri);
1476
+ };
1477
+ const openUrl = async uri => {
1478
+ // @ts-ignore
1479
+ await invoke$2('Open.openUrl', uri);
1480
+ };
1481
+ const getAllPreferences = async () => {
1482
+ // @ts-ignore
1483
+ return invoke$2('Preferences.getAll');
1484
+ };
1485
+ const showSaveFilePicker = async () => {
1486
+ // @ts-ignore
1487
+ return invoke$2('FilePicker.showSaveFilePicker');
1488
+ };
1489
+ const getLogsDir = async () => {
1490
+ // @ts-ignore
1491
+ return invoke$2('PlatformPaths.getLogsDir');
1492
+ };
1493
+ const registerMockRpc = commandMap => {
1494
+ const mockRpc = createMockRpc({
1495
+ commandMap
1496
+ });
1497
+ set$2(mockRpc);
1498
+ return mockRpc;
1499
+ };
1500
+
1501
+ const RendererWorker = {
1502
+ __proto__: null,
1503
+ activateByEvent,
1504
+ applyBulkReplacement,
1505
+ closeWidget,
1506
+ confirm,
1507
+ disableExtension,
1508
+ dispose,
1509
+ enableExtension,
1510
+ getActiveEditorId,
1511
+ getAllExtensions,
1512
+ getAllPreferences,
1513
+ getBlob,
1514
+ getChromeVersion,
1515
+ getColorThemeNames,
1516
+ getElectronVersion,
1517
+ getExtension,
1518
+ getExtensionCommands,
1519
+ getFileHandles,
1520
+ getFileIcon,
1521
+ getFilePathElectron,
1522
+ getFolderIcon,
1523
+ getFolderSize,
1524
+ getIcons,
1525
+ getKeyBindings: getKeyBindings$1,
1526
+ getLogsDir,
1527
+ getMarkdownDom,
1528
+ getNodeVersion,
1529
+ getPreference,
1530
+ getRecentlyOpened: getRecentlyOpened$1,
1531
+ getV8Version,
1532
+ getWebViewSecret,
1533
+ getWindowId,
1534
+ getWorkspacePath,
1535
+ handleDebugChange,
1536
+ handleDebugPaused,
1537
+ handleDebugResumed,
1538
+ handleDebugScriptParsed,
1539
+ installExtension,
1540
+ invoke: invoke$2,
1541
+ invokeAndTransfer,
1542
+ openExtensionSearch,
1543
+ openExternal,
1544
+ openNativeFolder,
1545
+ openUri,
1546
+ openUrl,
1547
+ openWidget,
1548
+ readFile,
1549
+ registerMockRpc,
1550
+ registerWebViewInterceptor,
1551
+ renderMarkdown,
1552
+ rerenderEditor,
1553
+ searchFileFetch,
1554
+ searchFileHtml,
1555
+ searchFileMemory,
1556
+ sendMessagePortToEditorWorker,
1557
+ sendMessagePortToErrorWorker,
1558
+ sendMessagePortToExtensionHostWorker,
1559
+ sendMessagePortToFileSystemWorker,
1560
+ sendMessagePortToIconThemeWorker,
1561
+ sendMessagePortToMarkdownWorker,
1562
+ sendMessagePortToRendererProcess,
1563
+ sendMessagePortToSearchProcess,
1564
+ sendMessagePortToSyntaxHighlightingWorker,
1565
+ set: set$2,
1566
+ setAdditionalFocus,
1567
+ setColorTheme,
1568
+ setExtensionsSearchValue,
1569
+ setFocus,
1570
+ setWebViewPort,
1571
+ setWorkspacePath,
1572
+ showContextMenu,
1573
+ showErrorDialog,
1574
+ showMessageBox,
1575
+ showSaveFilePicker,
1576
+ uninstallExtension,
1577
+ unregisterWebViewInterceptor,
1578
+ writeClipBoardImage,
1579
+ writeClipBoardText
1580
+ };
1206
1581
 
1207
1582
  const matchesArgs = (a, b) => {
1208
1583
  if (!a && !b) {
@@ -1235,8 +1610,9 @@ const warn = (...args) => {
1235
1610
  console.warn(...args);
1236
1611
  };
1237
1612
 
1238
- const invoke = async (method, ...params) => {
1239
- // TODO
1613
+ const invoke$1 = async (method, ...params) => {
1614
+ // @ts-ignore
1615
+ await invoke$2('WebView.compatRendererProcessInvoke', method, ...params);
1240
1616
  };
1241
1617
 
1242
1618
  const state$1 = {
@@ -1548,7 +1924,7 @@ const MenuEntriesHelp = {
1548
1924
  };
1549
1925
 
1550
1926
  const getRecentlyOpened = () => {
1551
- return invoke$1(/* RecentlyOpened.getRecentlyOpened */'RecentlyOpened.getRecentlyOpened');
1927
+ return invoke$2(/* RecentlyOpened.getRecentlyOpened */'RecentlyOpened.getRecentlyOpened');
1552
1928
  };
1553
1929
 
1554
1930
  const File = 'file://';
@@ -1807,33 +2183,13 @@ const menus$1 = [MenuEntriesEdit, MenuEntriesFile, MenuEntriesGo, MenuEntriesHel
1807
2183
  const getMenus = () => {
1808
2184
  return menus$1;
1809
2185
  };
1810
- const getModule$1 = id => {
1811
- for (const module of menus$1) {
1812
- if (module.id === id) {
1813
- return module;
1814
- }
1815
- }
1816
- return undefined;
1817
- };
1818
2186
  const getMenuEntries$1 = async (id, ...args) => {
1819
- try {
1820
- const module = getModule$1(id);
1821
- // @ts-ignore
1822
- const inject = module.inject || [];
1823
- // @ts-ignore
1824
- return module.getMenuEntries(...args);
1825
- } catch (error) {
1826
- throw new VError(error, `Failed to load menu entries for id ${id}`);
1827
- }
2187
+ // @ts-ignore
2188
+ return invoke$2('Menu.getMenuEntries', id, ...args);
1828
2189
  };
1829
2190
  const getMenuEntries2 = async (uid, menuId, ...args) => {
1830
- try {
1831
- const module = await getModule$1(menuId);
1832
- // @ts-ignore
1833
- return module.getMenuEntries(uid, ...args);
1834
- } catch (error) {
1835
- throw new VError(error, `Failed to load menu entries for id ${menuId}`);
1836
- }
2191
+ // @ts-ignore
2192
+ return invoke$2('Menu.getMenuEntries2', uid, menuId, ...args);
1837
2193
  };
1838
2194
 
1839
2195
  const addKeyBindings = menuEntries => {
@@ -2160,6 +2516,10 @@ const getMenuWidth = () => {
2160
2516
  return CONTEXT_MENU_WIDTH$1;
2161
2517
  };
2162
2518
 
2519
+ // TODO difference between focusing with mouse or keyboard
2520
+ // with mouse -> open submenu
2521
+ // with keyboard -> don't open submenu, only focus
2522
+
2163
2523
  // TODO handle printable letter and focus item that starts with that letter
2164
2524
 
2165
2525
  // TODO pageup / pagedown keys
@@ -2220,7 +2580,7 @@ const show$2 = async (x, y, id, mouseBlocking = false, ...args) => {
2220
2580
  });
2221
2581
  const visible = getVisible(menu.items, -1, false, menu.level);
2222
2582
  const dom = getMenuVirtualDom(visible).slice(1);
2223
- await invoke(/* Menu.show */'Menu.showMenu', /* x */bounds.x, /* y */bounds.y, /* width */bounds.width, /* height */bounds.height, /* items */menu.items, /* level */menu.level, /* parentIndex */-1, /* dom */dom, /* mouseBlocking */mouseBlocking);
2583
+ await invoke$1(/* Menu.show */'Menu.showMenu', /* x */bounds.x, /* y */bounds.y, /* width */bounds.width, /* height */bounds.height, /* items */menu.items, /* level */menu.level, /* parentIndex */-1, /* dom */dom, /* mouseBlocking */mouseBlocking);
2224
2584
  };
2225
2585
  const show2$2 = async (uid, menuId, x, y, mouseBlocking = false, ...args) => {
2226
2586
  const items = await getMenuEntriesWithKeyBindings2(uid, menuId, ...args);
@@ -2235,19 +2595,19 @@ const show2$2 = async (uid, menuId, x, y, mouseBlocking = false, ...args) => {
2235
2595
  });
2236
2596
  const visible = getVisible(menu.items, -1, false, menu.level);
2237
2597
  const dom = getMenuVirtualDom(visible).slice(1);
2238
- await invoke(/* Menu.show */'Menu.showMenu', /* x */bounds.x, /* y */bounds.y, /* width */bounds.width, /* height */bounds.height, /* items */menu.items, /* level */menu.level, /* parentIndex */-1, /* dom */dom, /* mouseBlocking */mouseBlocking);
2598
+ await invoke$1(/* Menu.show */'Menu.showMenu', /* x */bounds.x, /* y */bounds.y, /* width */bounds.width, /* height */bounds.height, /* items */menu.items, /* level */menu.level, /* parentIndex */-1, /* dom */dom, /* mouseBlocking */mouseBlocking);
2239
2599
  };
2240
- const hide = async (restoreFocus = true) => {
2600
+ const hide$1 = async (restoreFocus = true) => {
2241
2601
  if (getCount() === 0) {
2242
2602
  return;
2243
2603
  }
2244
2604
  reset();
2245
- await invoke(/* Menu.hide */'Menu.hide', /* restoreFocus */restoreFocus);
2605
+ await invoke$1(/* Menu.hide */'Menu.hide', /* restoreFocus */restoreFocus);
2246
2606
  };
2247
2607
 
2248
2608
  const show$1 = async (x, y, id, ...args) => {
2249
2609
  // @ts-ignore
2250
- await hide();
2610
+ await hide$1();
2251
2611
  // TODO handle error
2252
2612
  // TODO race condition
2253
2613
  await show$2(x, y, id, /* mouseBlocking */true, ...args);
@@ -2256,7 +2616,7 @@ const show$1 = async (x, y, id, ...args) => {
2256
2616
  };
2257
2617
  const show2$1 = async (uid, menuId, x, y, ...args) => {
2258
2618
  // @ts-ignore
2259
- await hide();
2619
+ await hide$1();
2260
2620
  // TODO handle error
2261
2621
  // TODO race condition
2262
2622
  await show2$2(uid, menuId, x, y, /* mouseBlocking */true, ...args);
@@ -2401,6 +2761,22 @@ const focusFirst = async () => {
2401
2761
  await focusIndex(menu, indexToFocus);
2402
2762
  };
2403
2763
 
2764
+ const getIndexToFocusPreviousStartingAt$1 = (items, startIndex) => {
2765
+ for (let i = startIndex; i > startIndex - items.length; i--) {
2766
+ const index = (i + items.length) % items.length;
2767
+ const item = items[index];
2768
+ if (canBeFocused(item)) {
2769
+ return index;
2770
+ }
2771
+ }
2772
+ return -1;
2773
+ };
2774
+ const focusLast = async () => {
2775
+ const menu = getCurrentMenu();
2776
+ const indexToFocus = getIndexToFocusPreviousStartingAt$1(menu.items, menu.items.length - 1);
2777
+ await focusIndex(menu, indexToFocus);
2778
+ };
2779
+
2404
2780
  const getIndexToFocusNext = menu => {
2405
2781
  const startIndex = menu.focusedIndex + 1;
2406
2782
  return getIndexToFocusNextStartingAt(menu.items, startIndex);
@@ -2414,6 +2790,30 @@ const focusNext = async () => {
2414
2790
  await focusIndex(menu, indexToFocus);
2415
2791
  };
2416
2792
 
2793
+ // TODO this code seems a bit too complicated, maybe it can be simplified
2794
+ const getIndexToFocusPreviousStartingAt = (items, startIndex) => {
2795
+ for (let i = startIndex; i > startIndex - items.length; i--) {
2796
+ const index = (i + items.length) % items.length;
2797
+ const item = items[index];
2798
+ if (canBeFocused(item)) {
2799
+ return index;
2800
+ }
2801
+ }
2802
+ return -1;
2803
+ };
2804
+ const getIndexToFocusPrevious = menu => {
2805
+ const startIndex = menu.focusedIndex === -1 ? menu.items.length - 1 : menu.focusedIndex - 1;
2806
+ return getIndexToFocusPreviousStartingAt(menu.items, startIndex);
2807
+ };
2808
+ const focusPrevious = async () => {
2809
+ const menu = getCurrentMenu();
2810
+ if (menu.items.length === 0) {
2811
+ return;
2812
+ }
2813
+ const indexToFocus = getIndexToFocusPrevious(menu);
2814
+ await focusIndex(menu, indexToFocus);
2815
+ };
2816
+
2417
2817
  const commandsIds = ['closeMenu', 'handleClickAt', 'focus', 'focusFirst', 'focusIndex', 'focusLast', 'focusNext', 'focusPrevious', 'handleClick', 'handleFocus', 'handleFocusOut', 'handleKeyArrowDown', 'handleKeyArrowLeft', 'handleKeyArrowRight', 'handleKeyArrowUp', 'handleKeyEnd', 'handleKeyEnter', 'handleKeyEscape', 'handleKeyHome', 'handleKeySpace', 'handleMenuClick', 'handleMenuMouseOver', 'handleMouseOut', 'handleMouseOver', 'handlePointerOver', 'handlePointerOut', 'toggleIndex', 'toggleMenu'];
2418
2818
 
2419
2819
  const getCommandIds = () => {
@@ -2530,9 +2930,11 @@ const getMenuBounds = (x, y, items) => {
2530
2930
 
2531
2931
  if (x + menuWidth > windowWidth) {
2532
2932
  x -= menuWidth;
2933
+ x = Math.max(x, 0);
2533
2934
  }
2534
2935
  if (y + menuHeight > windowHeight) {
2535
2936
  y -= menuHeight;
2937
+ y = Math.max(y, 0);
2536
2938
  }
2537
2939
  return {
2538
2940
  x,
@@ -2764,6 +3166,75 @@ const saveState = uid => {
2764
3166
  };
2765
3167
  };
2766
3168
 
3169
+ const {
3170
+ invoke} = RendererWorker;
3171
+
3172
+ const executeMenuItemCommand = async item => {
3173
+ await invoke(item.command, ...(item.args || []));
3174
+ };
3175
+
3176
+ const hide = async (restoreFocus = true) => {
3177
+ const {
3178
+ commands
3179
+ } = await getMenuHideCommands(restoreFocus);
3180
+ if (commands.length > 0) {
3181
+ // @ts-ignore
3182
+ await invoke$2(...commands);
3183
+ }
3184
+ };
3185
+
3186
+ const selectIndexNone = async (menu, item) => {
3187
+ await Promise.all([hide(/* restoreFocus */false), executeMenuItemCommand(item)]);
3188
+ };
3189
+ const selectIndexRestoreFocus = async (menu, item) => {
3190
+ await Promise.all([hide(/* restoreFocus */true), executeMenuItemCommand(item)]);
3191
+ };
3192
+ const selectIndexSubMenu = async (menu, item, index) => {
3193
+ if (menu.focusedIndex === index) {
3194
+ return;
3195
+ }
3196
+ await showSubMenu(menu.level, menu.focusedIndex);
3197
+ };
3198
+ const selectIndexDefault = async () => {};
3199
+ const selectIndexIgnore = async (menu, item) => {
3200
+ await executeMenuItemCommand(item);
3201
+ };
3202
+ const getSelectIndexFunction = flags => {
3203
+ switch (flags) {
3204
+ case None$1:
3205
+ return selectIndexNone;
3206
+ case SubMenu$1:
3207
+ return selectIndexSubMenu;
3208
+ case RestoreFocus$1:
3209
+ return selectIndexRestoreFocus;
3210
+ case Ignore$1:
3211
+ return selectIndexIgnore;
3212
+ default:
3213
+ return selectIndexDefault;
3214
+ }
3215
+ };
3216
+ const selectIndex = async (level, index) => {
3217
+ const menu = get$1(level);
3218
+ // TODO avoid assignment
3219
+ menu.focusedIndex = index;
3220
+ const item = menu.items[menu.focusedIndex];
3221
+ const fn = getSelectIndexFunction(item.flags);
3222
+ await fn(menu, item, index);
3223
+ };
3224
+
3225
+ const selectItem = async text => {
3226
+ for (let level = 0; level < getCount(); level++) {
3227
+ const menu = get$1(level);
3228
+ for (let i = 0; i < menu.items.length; i++) {
3229
+ const item = menu.items[i];
3230
+ if (item.label === text) {
3231
+ return selectIndex(level, i);
3232
+ }
3233
+ }
3234
+ }
3235
+ throw new Error(`menu item not found: ${text}`);
3236
+ };
3237
+
2767
3238
  const wrapCommand = fn => {
2768
3239
  const wrapped = async (uid, ...args) => {
2769
3240
  const {
@@ -2781,7 +3252,10 @@ const wrapCommand = fn => {
2781
3252
 
2782
3253
  const commandMap = {
2783
3254
  'Menu.diff2': diff2,
3255
+ 'Menu.focusPrevious': focusPrevious,
2784
3256
  'Menu.focusFirst': focusFirst,
3257
+ 'Menu.focusIndex': focusIndex,
3258
+ 'Menu.focusLast': focusLast,
2785
3259
  'Menu.focusNext': focusNext,
2786
3260
  'Menu.getCommands': getCommandIds,
2787
3261
  'Menu.getHideCommands': getMenuHideCommands,
@@ -2798,6 +3272,7 @@ const commandMap = {
2798
3272
  'Menu.render2': render2,
2799
3273
  'Menu.renderEventListeners': renderEventListeners,
2800
3274
  'Menu.saveState': saveState,
3275
+ 'Menu.selectItem': selectItem,
2801
3276
  'Menu.show': show,
2802
3277
  'Menu.show2': show2,
2803
3278
  'Menu.showSubMenu': showSubMenu
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/menu-worker",
3
- "version": "1.7.0",
3
+ "version": "2.0.0",
4
4
  "description": "Menu Worker",
5
5
  "repository": {
6
6
  "type": "git",