@lvce-editor/main-area-worker 1.3.0 → 1.4.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.
@@ -90,12 +90,24 @@ const getType = value => {
90
90
  return Unknown;
91
91
  }
92
92
  };
93
+ const object = value => {
94
+ const type = getType(value);
95
+ if (type !== Object$1) {
96
+ throw new AssertionError('expected value to be of type object');
97
+ }
98
+ };
93
99
  const number = value => {
94
100
  const type = getType(value);
95
101
  if (type !== Number$1) {
96
102
  throw new AssertionError('expected value to be of type number');
97
103
  }
98
104
  };
105
+ const string = value => {
106
+ const type = getType(value);
107
+ if (type !== String) {
108
+ throw new AssertionError('expected value to be of type string');
109
+ }
110
+ };
99
111
 
100
112
  const isMessagePort = value => {
101
113
  return value && value instanceof MessagePort;
@@ -450,7 +462,7 @@ const getFirstEvent = (eventEmitter, eventMap) => {
450
462
  return promise;
451
463
  };
452
464
  const Message$1 = 3;
453
- const create$5 = async ({
465
+ const create$5$1 = async ({
454
466
  isMessagePortOpen,
455
467
  messagePort
456
468
  }) => {
@@ -501,7 +513,7 @@ const wrap$5 = messagePort => {
501
513
  };
502
514
  const IpcParentWithMessagePort$1 = {
503
515
  __proto__: null,
504
- create: create$5,
516
+ create: create$5$1,
505
517
  signal: signal$1,
506
518
  wrap: wrap$5
507
519
  };
@@ -720,7 +732,7 @@ const getErrorResponse = (id, error, preparePrettyError, logError) => {
720
732
  const errorProperty = getErrorProperty(error, prettyError);
721
733
  return create$1$2(id, errorProperty);
722
734
  };
723
- const create$3 = (message, result) => {
735
+ const create$5 = (message, result) => {
724
736
  return {
725
737
  jsonrpc: Two$1,
726
738
  id: message.id,
@@ -729,7 +741,7 @@ const create$3 = (message, result) => {
729
741
  };
730
742
  const getSuccessResponse = (message, result) => {
731
743
  const resultProperty = result ?? null;
732
- return create$3(message, resultProperty);
744
+ return create$5(message, resultProperty);
733
745
  };
734
746
  const getErrorResponseSimple = (id, error) => {
735
747
  return {
@@ -1053,7 +1065,7 @@ const remove = id => {
1053
1065
  };
1054
1066
 
1055
1067
  /* eslint-disable @typescript-eslint/explicit-function-return-type */
1056
- const create$2 = rpcId => {
1068
+ const create$3 = rpcId => {
1057
1069
  return {
1058
1070
  async dispose() {
1059
1071
  const rpc = get$1(rpcId);
@@ -1091,12 +1103,12 @@ const create$2 = rpcId => {
1091
1103
 
1092
1104
  const {
1093
1105
  set: set$2
1094
- } = create$2(ExtensionHostWorker);
1106
+ } = create$3(ExtensionHostWorker);
1095
1107
 
1096
1108
  const {
1097
1109
  invokeAndTransfer,
1098
1110
  set: set$1
1099
- } = create$2(RendererWorker);
1111
+ } = create$3(RendererWorker);
1100
1112
  const sendMessagePortToExtensionHostWorker$1 = async (port, rpcId = 0) => {
1101
1113
  const command = 'HandleMessagePort.handleMessagePort2';
1102
1114
  await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToExtensionHostWorker', port, command, rpcId);
@@ -1106,7 +1118,7 @@ const toCommandId = key => {
1106
1118
  const dotIndex = key.indexOf('.');
1107
1119
  return key.slice(dotIndex + 1);
1108
1120
  };
1109
- const create$1 = () => {
1121
+ const create$2 = () => {
1110
1122
  const states = Object.create(null);
1111
1123
  const commandMapRef = {};
1112
1124
  return {
@@ -1192,9 +1204,9 @@ const {
1192
1204
  getCommandIds,
1193
1205
  registerCommands,
1194
1206
  set,
1195
- wrapCommand} = create$1();
1207
+ wrapCommand} = create$2();
1196
1208
 
1197
- const create = (uid, uri, x, y, width, height, platform, assetDir) => {
1209
+ const create$1 = (uid, uri, x, y, width, height, platform, assetDir) => {
1198
1210
  const state = {
1199
1211
  assetDir,
1200
1212
  platform,
@@ -1294,6 +1306,266 @@ const loadContent = async state => {
1294
1306
  };
1295
1307
  };
1296
1308
 
1309
+ let idCounter = 0;
1310
+ const create = () => {
1311
+ idCounter++;
1312
+ return idCounter;
1313
+ };
1314
+
1315
+ const getTitle = (uri, homeDir) => {
1316
+ if (!uri) {
1317
+ return '';
1318
+ }
1319
+ return uri;
1320
+ };
1321
+ const getLabel = uri => {
1322
+ if (uri.startsWith('settings://')) {
1323
+ return 'Settings';
1324
+ }
1325
+ if (uri.startsWith('simple-browser://')) {
1326
+ return 'Simple Browser';
1327
+ }
1328
+ return uri;
1329
+ // return Workspace.pathBaseName(uri)
1330
+ };
1331
+
1332
+ /**
1333
+ *
1334
+ * @param {string} uri
1335
+ * @returns
1336
+ */
1337
+ const getFileIcon = uri => {
1338
+ if (uri === 'app://keybindings') {
1339
+ return `MaskIconRecordKey`;
1340
+ }
1341
+ if (uri.startsWith('extension-detail://')) {
1342
+ return `MaskIconExtensions`;
1343
+ }
1344
+ return '';
1345
+ };
1346
+
1347
+ const Preview = 1 << 4;
1348
+
1349
+ // @ts-nocheck
1350
+
1351
+ const focusIndex = async (state, index) => {
1352
+ const {
1353
+ activeGroupIndex,
1354
+ groups,
1355
+ tabHeight,
1356
+ uid
1357
+ } = state;
1358
+ const group = groups[activeGroupIndex];
1359
+ const {
1360
+ editors
1361
+ } = group;
1362
+ const oldActiveIndex = group.activeIndex;
1363
+ if (index === oldActiveIndex) {
1364
+ return {
1365
+ commands: [],
1366
+ newState: state
1367
+ };
1368
+ }
1369
+ const newGroup = {
1370
+ ...group,
1371
+ activeIndex: index
1372
+ };
1373
+ const newGroups = [...groups.slice(0, activeGroupIndex), newGroup, ...groups.slice(activeGroupIndex + 1)];
1374
+ const newState = {
1375
+ ...state,
1376
+ groups: newGroups
1377
+ };
1378
+ const editor = editors[index];
1379
+ const {
1380
+ x
1381
+ } = group;
1382
+ const y = group.y + tabHeight;
1383
+ const {
1384
+ width
1385
+ } = group;
1386
+ const contentHeight = group.height - tabHeight;
1387
+ const id = await ViewletMap.getModuleId(editor.uri);
1388
+ const oldEditor = editors[oldActiveIndex];
1389
+ const oldId = await ViewletMap.getModuleId(oldEditor.uri);
1390
+ // @ts-ignore
1391
+ ViewletStates.getInstance(oldId);
1392
+ const previousUid = oldEditor.uid;
1393
+ number(previousUid);
1394
+ const disposeCommands = Viewlet.disposeFunctional(previousUid);
1395
+ const maybeHiddenEditorInstance = ViewletStates.getInstance(editor.uid);
1396
+ if (maybeHiddenEditorInstance) {
1397
+ const commands = Viewlet.showFunctional(editor.uid);
1398
+ const allCommands = [...disposeCommands, ...commands];
1399
+ return {
1400
+ commands: allCommands,
1401
+ newState
1402
+ };
1403
+ }
1404
+ const instanceUid = create();
1405
+ const instance = ViewletManager.create(ViewletModule.load, id, uid, editor.uri, x, y, width, contentHeight);
1406
+ instance.show = false;
1407
+ instance.setBounds = false;
1408
+ instance.uid = instanceUid;
1409
+ editor.uid = instanceUid;
1410
+ const resizeCommands = ['Viewlet.setBounds', instanceUid, x, tabHeight, width, contentHeight];
1411
+
1412
+ // @ts-ignore
1413
+ const commands = await ViewletManager.load(instance);
1414
+ // @ts-ignore
1415
+ commands.unshift(...disposeCommands);
1416
+ // @ts-ignore
1417
+ commands.push(resizeCommands);
1418
+ // @ts-ignore
1419
+ commands.push(['Viewlet.append', uid, instanceUid]);
1420
+ return {
1421
+ commands,
1422
+ newState
1423
+ };
1424
+ };
1425
+
1426
+ // @ts-nocheck
1427
+ const openUri = async (state, uri, focus = true, {
1428
+ preview = false,
1429
+ ...context
1430
+ } = {}) => {
1431
+ object(state);
1432
+ string(uri);
1433
+ const {
1434
+ activeGroupIndex,
1435
+ groups,
1436
+ tabFontFamily,
1437
+ tabFontSize,
1438
+ tabFontWeight,
1439
+ tabHeight,
1440
+ tabLetterSpacing
1441
+ } = state;
1442
+ const {
1443
+ x
1444
+ } = state;
1445
+ const y = state.y + tabHeight;
1446
+ const {
1447
+ width
1448
+ } = state;
1449
+ const contentHeight = state.height - tabHeight;
1450
+ // @ts-ignore
1451
+ const moduleId = await ViewletMap.getModuleId(uri, context.opener);
1452
+ let activeGroup = groups[activeGroupIndex];
1453
+ activeGroup ||= {
1454
+ activeIndex: -1,
1455
+ editors: [],
1456
+ focusedIndex: -1,
1457
+ height: state.height,
1458
+ tabsUid: create(),
1459
+ uid: create(),
1460
+ width,
1461
+ x,
1462
+ y: 0
1463
+ };
1464
+ const {
1465
+ activeIndex,
1466
+ editors
1467
+ } = activeGroup;
1468
+ const previousEditor = editors[activeIndex];
1469
+ let disposeCommands;
1470
+ // @ts-ignore
1471
+ if (previousEditor && previousEditor.uri === uri && previousEditor.opener === context.opener) {
1472
+ return {
1473
+ commands: [],
1474
+ newState: state
1475
+ };
1476
+ }
1477
+ for (let i = 0; i < editors.length; i++) {
1478
+ const editor = editors[i];
1479
+ if (editor.uri === uri &&
1480
+ // @ts-ignore
1481
+ editor.opener === context.opener) {
1482
+ return focusIndex(state, i);
1483
+ }
1484
+ }
1485
+ // TODO editor needs to be disposed when closing
1486
+ // other tabs and closing all tabs
1487
+ if (previousEditor) {
1488
+ const previousUid = previousEditor.uid;
1489
+ disposeCommands = Viewlet.hideFunctional(previousUid);
1490
+ }
1491
+ const instanceUid = create();
1492
+ const instance = ViewletManager.create(ViewletModule.load, moduleId, state.uid, uri, activeGroup.x, y, activeGroup.width, contentHeight);
1493
+ instance.uid = instanceUid;
1494
+ // const oldActiveIndex = state.activeIndex
1495
+ const tabLabel = getLabel(uri);
1496
+ const tabWidth = MeasureTabWidth.measureTabWidth(tabLabel, tabFontWeight, tabFontSize, tabFontFamily, tabLetterSpacing);
1497
+ const tabTitle = getTitle(uri);
1498
+ const icon = getFileIcon(uri);
1499
+ const newEditor = {
1500
+ flags: Preview,
1501
+ icon,
1502
+ label: tabLabel,
1503
+ moduleId,
1504
+ tabWidth,
1505
+ title: tabTitle,
1506
+ uid: instanceUid,
1507
+ uri
1508
+ };
1509
+ const newEditors = [...activeGroup.editors, newEditor];
1510
+ const newActiveIndex = newEditors.length - 1;
1511
+ const newGroup = {
1512
+ ...activeGroup,
1513
+ activeIndex: newActiveIndex,
1514
+ editors: newEditors
1515
+ };
1516
+ const newGroups = [...groups.slice(0, activeGroupIndex), newGroup, ...groups.slice(activeGroupIndex + 1)];
1517
+ // @ts-ignore
1518
+ instance.show = false;
1519
+ instance.setBounds = false;
1520
+ ViewletStates.setState(state.uid, {
1521
+ ...state,
1522
+ activeGroupIndex: 0,
1523
+ groups: newGroups,
1524
+ pendingUid: instanceUid
1525
+ });
1526
+ if (context) {
1527
+ instance.args = [context];
1528
+ }
1529
+ // @ts-ignore
1530
+ const commands = await ViewletManager.load(instance, focus);
1531
+ commands.push(['Viewlet.setBounds', instanceUid, activeGroup.x, tabHeight, activeGroup.width, contentHeight]);
1532
+ let {
1533
+ tabsUid
1534
+ } = state;
1535
+ if (tabsUid === -1) {
1536
+ tabsUid = create();
1537
+ }
1538
+ if (disposeCommands) {
1539
+ commands.push(...disposeCommands);
1540
+ }
1541
+ commands.push(['Viewlet.append', state.uid, instanceUid]);
1542
+ if (focus) {
1543
+ commands.push(['Viewlet.focus', instanceUid]);
1544
+ }
1545
+ const latestState = ViewletStates.getState(state.uid);
1546
+ const latestPendingUid = latestState.pendingUid;
1547
+ if (latestPendingUid !== instanceUid) {
1548
+ return {
1549
+ commands: [],
1550
+ newState: state
1551
+ };
1552
+ }
1553
+ if (!ViewletStates.hasInstance(instanceUid)) {
1554
+ return {
1555
+ commands,
1556
+ newState: state
1557
+ };
1558
+ }
1559
+ return {
1560
+ commands,
1561
+ newState: {
1562
+ ...state,
1563
+ groups: newGroups,
1564
+ tabsUid
1565
+ }
1566
+ };
1567
+ };
1568
+
1297
1569
  const text = data => {
1298
1570
  return {
1299
1571
  childCount: 0,
@@ -1303,58 +1575,48 @@ const text = data => {
1303
1575
  };
1304
1576
 
1305
1577
  const CSS_CLASSES = {
1306
- CUSTOM_EDITOR: 'custom-editor',
1307
- EDITOR_CONTAINER: 'editor-container',
1308
- EDITOR_CONTENT: 'editor-content',
1309
- EDITOR_GROUP: 'editor-group',
1310
- EDITOR_GROUP_FOCUSED: 'focused',
1311
- EDITOR_GROUPS_CONTAINER: 'editor-groups-container',
1312
- MAIN_AREA: 'main-area',
1313
- TAB: 'tab',
1314
- TAB_ACTIVE: 'active',
1315
- TAB_BAR: 'tab-bar',
1316
- TAB_CLOSE: 'tab-close',
1317
- TAB_TITLE: 'tab-title',
1318
- TEXT_EDITOR: 'text-editor'
1319
- };
1578
+ EDITOR_GROUPS_CONTAINER: 'editor-groups-container'};
1320
1579
 
1321
1580
  const renderTab = (tab, isActive) => {
1322
1581
  return [{
1323
1582
  childCount: 2,
1324
- className: `${CSS_CLASSES.TAB} ${isActive ? CSS_CLASSES.TAB_ACTIVE : ''}`,
1583
+ className: 'MainTab',
1325
1584
  type: Div
1326
1585
  }, {
1327
1586
  childCount: 1,
1328
- className: CSS_CLASSES.TAB_TITLE,
1587
+ className: 'TabTitle',
1329
1588
  type: Span
1330
1589
  }, text(tab.isDirty ? `*${tab.title}` : tab.title), {
1331
1590
  childCount: 1,
1332
- className: CSS_CLASSES.TAB_CLOSE,
1591
+ className: 'TabCloseButton',
1333
1592
  type: Button
1334
1593
  }, text('×')];
1335
1594
  };
1336
1595
  const renderTabBar = group => {
1337
1596
  return [{
1338
1597
  childCount: group.tabs.length,
1339
- className: CSS_CLASSES.TAB_BAR,
1598
+ className: 'MainTabs',
1340
1599
  type: Div
1341
- }, ...group.tabs.flatMap(tab => renderTab(tab, tab.id === group.activeTabId))];
1600
+ }, ...group.tabs.flatMap(tab => renderTab(tab))];
1342
1601
  };
1343
1602
  const renderEditor = tab => {
1603
+ if (!tab) {
1604
+ return [text('Tab not found')];
1605
+ }
1344
1606
  if (tab.editorType === 'custom') {
1345
1607
  return [{
1346
1608
  childCount: 1,
1347
- className: CSS_CLASSES.CUSTOM_EDITOR,
1609
+ className: 'CustomEditor',
1348
1610
  type: Div
1349
1611
  }, text(`Custom Editor: ${tab.customEditorId}`)];
1350
1612
  }
1351
1613
  return [{
1352
1614
  childCount: 1,
1353
- className: CSS_CLASSES.TEXT_EDITOR,
1615
+ className: 'TextEditor',
1354
1616
  type: Div
1355
1617
  }, {
1356
1618
  childCount: 1,
1357
- className: CSS_CLASSES.EDITOR_CONTENT,
1619
+ className: 'EditorContent',
1358
1620
  type: Pre
1359
1621
  }, text(tab.content || '')];
1360
1622
  };
@@ -1362,18 +1624,18 @@ const renderEditorGroup = group => {
1362
1624
  const activeTab = group.tabs.find(tab => tab.id === group.activeTabId);
1363
1625
  return [{
1364
1626
  childCount: 2,
1365
- className: `${CSS_CLASSES.EDITOR_GROUP} ${group.focused ? CSS_CLASSES.EDITOR_GROUP_FOCUSED : ''}`,
1627
+ className: 'EditorGroup',
1366
1628
  type: Div
1367
1629
  }, ...renderTabBar(group), {
1368
1630
  childCount: activeTab ? 1 : 1,
1369
- className: CSS_CLASSES.EDITOR_CONTAINER,
1631
+ className: 'EditorContainer',
1370
1632
  type: Div
1371
1633
  }, ...renderEditor(activeTab)];
1372
1634
  };
1373
1635
  const getMainAreaVirtualDom = layout => {
1374
1636
  return [{
1375
1637
  childCount: 1,
1376
- className: CSS_CLASSES.MAIN_AREA,
1638
+ className: 'Main',
1377
1639
  type: Div
1378
1640
  }, {
1379
1641
  childCount: layout.groups.length,
@@ -1455,12 +1717,13 @@ const saveState = uid => {
1455
1717
  };
1456
1718
 
1457
1719
  const commandMap = {
1458
- 'MainArea.create': create,
1720
+ 'MainArea.create': create$1,
1459
1721
  'MainArea.diff2': diff2,
1460
1722
  'MainArea.getCommandIds': getCommandIds,
1461
1723
  'MainArea.handleClick': wrapCommand(handleClick),
1462
1724
  'MainArea.initialize': initialize,
1463
1725
  'MainArea.loadContent': wrapCommand(loadContent),
1726
+ 'MainArea.openUri': wrapCommand(openUri),
1464
1727
  'MainArea.render2': render2,
1465
1728
  'MainArea.renderEventListeners': renderEventListeners,
1466
1729
  'MainArea.resize': wrapCommand(resize),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/main-area-worker",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Main Area Worker",
5
5
  "repository": {
6
6
  "type": "git",