@lvce-editor/main-area-worker 1.3.0 → 1.5.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,14 +1204,17 @@ 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,
1212
+ layout: {
1213
+ activeGroupId: '',
1214
+ direction: 'horizontal',
1215
+ groups: []
1216
+ },
1200
1217
  platform,
1201
- statusBarItemsLeft: [],
1202
- statusBarItemsRight: [],
1203
1218
  uid
1204
1219
  };
1205
1220
  set(uid, state, state);
@@ -1241,6 +1256,11 @@ const handleClick = async (state, name) => {
1241
1256
  return state;
1242
1257
  };
1243
1258
 
1259
+ const handleClickCloseTab = (state, groupId, tabId) => {
1260
+ // TODO
1261
+ return state;
1262
+ };
1263
+
1244
1264
  const id = 7201;
1245
1265
  const sendMessagePortToExtensionHostWorker = async port => {
1246
1266
  await sendMessagePortToExtensionHostWorker$1(port, id);
@@ -1294,6 +1314,266 @@ const loadContent = async state => {
1294
1314
  };
1295
1315
  };
1296
1316
 
1317
+ let idCounter = 0;
1318
+ const create = () => {
1319
+ idCounter++;
1320
+ return idCounter;
1321
+ };
1322
+
1323
+ const getTitle = (uri, homeDir) => {
1324
+ if (!uri) {
1325
+ return '';
1326
+ }
1327
+ return uri;
1328
+ };
1329
+ const getLabel = uri => {
1330
+ if (uri.startsWith('settings://')) {
1331
+ return 'Settings';
1332
+ }
1333
+ if (uri.startsWith('simple-browser://')) {
1334
+ return 'Simple Browser';
1335
+ }
1336
+ return uri;
1337
+ // return Workspace.pathBaseName(uri)
1338
+ };
1339
+
1340
+ /**
1341
+ *
1342
+ * @param {string} uri
1343
+ * @returns
1344
+ */
1345
+ const getFileIcon = uri => {
1346
+ if (uri === 'app://keybindings') {
1347
+ return `MaskIconRecordKey`;
1348
+ }
1349
+ if (uri.startsWith('extension-detail://')) {
1350
+ return `MaskIconExtensions`;
1351
+ }
1352
+ return '';
1353
+ };
1354
+
1355
+ const Preview = 1 << 4;
1356
+
1357
+ // @ts-nocheck
1358
+
1359
+ const focusIndex = async (state, index) => {
1360
+ const {
1361
+ activeGroupIndex,
1362
+ groups,
1363
+ tabHeight,
1364
+ uid
1365
+ } = state;
1366
+ const group = groups[activeGroupIndex];
1367
+ const {
1368
+ editors
1369
+ } = group;
1370
+ const oldActiveIndex = group.activeIndex;
1371
+ if (index === oldActiveIndex) {
1372
+ return {
1373
+ commands: [],
1374
+ newState: state
1375
+ };
1376
+ }
1377
+ const newGroup = {
1378
+ ...group,
1379
+ activeIndex: index
1380
+ };
1381
+ const newGroups = [...groups.slice(0, activeGroupIndex), newGroup, ...groups.slice(activeGroupIndex + 1)];
1382
+ const newState = {
1383
+ ...state,
1384
+ groups: newGroups
1385
+ };
1386
+ const editor = editors[index];
1387
+ const {
1388
+ x
1389
+ } = group;
1390
+ const y = group.y + tabHeight;
1391
+ const {
1392
+ width
1393
+ } = group;
1394
+ const contentHeight = group.height - tabHeight;
1395
+ const id = await ViewletMap.getModuleId(editor.uri);
1396
+ const oldEditor = editors[oldActiveIndex];
1397
+ const oldId = await ViewletMap.getModuleId(oldEditor.uri);
1398
+ // @ts-ignore
1399
+ ViewletStates.getInstance(oldId);
1400
+ const previousUid = oldEditor.uid;
1401
+ number(previousUid);
1402
+ const disposeCommands = Viewlet.disposeFunctional(previousUid);
1403
+ const maybeHiddenEditorInstance = ViewletStates.getInstance(editor.uid);
1404
+ if (maybeHiddenEditorInstance) {
1405
+ const commands = Viewlet.showFunctional(editor.uid);
1406
+ const allCommands = [...disposeCommands, ...commands];
1407
+ return {
1408
+ commands: allCommands,
1409
+ newState
1410
+ };
1411
+ }
1412
+ const instanceUid = create();
1413
+ const instance = ViewletManager.create(ViewletModule.load, id, uid, editor.uri, x, y, width, contentHeight);
1414
+ instance.show = false;
1415
+ instance.setBounds = false;
1416
+ instance.uid = instanceUid;
1417
+ editor.uid = instanceUid;
1418
+ const resizeCommands = ['Viewlet.setBounds', instanceUid, x, tabHeight, width, contentHeight];
1419
+
1420
+ // @ts-ignore
1421
+ const commands = await ViewletManager.load(instance);
1422
+ // @ts-ignore
1423
+ commands.unshift(...disposeCommands);
1424
+ // @ts-ignore
1425
+ commands.push(resizeCommands);
1426
+ // @ts-ignore
1427
+ commands.push(['Viewlet.append', uid, instanceUid]);
1428
+ return {
1429
+ commands,
1430
+ newState
1431
+ };
1432
+ };
1433
+
1434
+ // @ts-nocheck
1435
+ const openUri = async (state, uri, focus = true, {
1436
+ preview = false,
1437
+ ...context
1438
+ } = {}) => {
1439
+ object(state);
1440
+ string(uri);
1441
+ const {
1442
+ activeGroupIndex,
1443
+ groups,
1444
+ tabFontFamily,
1445
+ tabFontSize,
1446
+ tabFontWeight,
1447
+ tabHeight,
1448
+ tabLetterSpacing
1449
+ } = state;
1450
+ const {
1451
+ x
1452
+ } = state;
1453
+ const y = state.y + tabHeight;
1454
+ const {
1455
+ width
1456
+ } = state;
1457
+ const contentHeight = state.height - tabHeight;
1458
+ // @ts-ignore
1459
+ const moduleId = await ViewletMap.getModuleId(uri, context.opener);
1460
+ let activeGroup = groups[activeGroupIndex];
1461
+ activeGroup ||= {
1462
+ activeIndex: -1,
1463
+ editors: [],
1464
+ focusedIndex: -1,
1465
+ height: state.height,
1466
+ tabsUid: create(),
1467
+ uid: create(),
1468
+ width,
1469
+ x,
1470
+ y: 0
1471
+ };
1472
+ const {
1473
+ activeIndex,
1474
+ editors
1475
+ } = activeGroup;
1476
+ const previousEditor = editors[activeIndex];
1477
+ let disposeCommands;
1478
+ // @ts-ignore
1479
+ if (previousEditor && previousEditor.uri === uri && previousEditor.opener === context.opener) {
1480
+ return {
1481
+ commands: [],
1482
+ newState: state
1483
+ };
1484
+ }
1485
+ for (let i = 0; i < editors.length; i++) {
1486
+ const editor = editors[i];
1487
+ if (editor.uri === uri &&
1488
+ // @ts-ignore
1489
+ editor.opener === context.opener) {
1490
+ return focusIndex(state, i);
1491
+ }
1492
+ }
1493
+ // TODO editor needs to be disposed when closing
1494
+ // other tabs and closing all tabs
1495
+ if (previousEditor) {
1496
+ const previousUid = previousEditor.uid;
1497
+ disposeCommands = Viewlet.hideFunctional(previousUid);
1498
+ }
1499
+ const instanceUid = create();
1500
+ const instance = ViewletManager.create(ViewletModule.load, moduleId, state.uid, uri, activeGroup.x, y, activeGroup.width, contentHeight);
1501
+ instance.uid = instanceUid;
1502
+ // const oldActiveIndex = state.activeIndex
1503
+ const tabLabel = getLabel(uri);
1504
+ const tabWidth = MeasureTabWidth.measureTabWidth(tabLabel, tabFontWeight, tabFontSize, tabFontFamily, tabLetterSpacing);
1505
+ const tabTitle = getTitle(uri);
1506
+ const icon = getFileIcon(uri);
1507
+ const newEditor = {
1508
+ flags: Preview,
1509
+ icon,
1510
+ label: tabLabel,
1511
+ moduleId,
1512
+ tabWidth,
1513
+ title: tabTitle,
1514
+ uid: instanceUid,
1515
+ uri
1516
+ };
1517
+ const newEditors = [...activeGroup.editors, newEditor];
1518
+ const newActiveIndex = newEditors.length - 1;
1519
+ const newGroup = {
1520
+ ...activeGroup,
1521
+ activeIndex: newActiveIndex,
1522
+ editors: newEditors
1523
+ };
1524
+ const newGroups = [...groups.slice(0, activeGroupIndex), newGroup, ...groups.slice(activeGroupIndex + 1)];
1525
+ // @ts-ignore
1526
+ instance.show = false;
1527
+ instance.setBounds = false;
1528
+ ViewletStates.setState(state.uid, {
1529
+ ...state,
1530
+ activeGroupIndex: 0,
1531
+ groups: newGroups,
1532
+ pendingUid: instanceUid
1533
+ });
1534
+ if (context) {
1535
+ instance.args = [context];
1536
+ }
1537
+ // @ts-ignore
1538
+ const commands = await ViewletManager.load(instance, focus);
1539
+ commands.push(['Viewlet.setBounds', instanceUid, activeGroup.x, tabHeight, activeGroup.width, contentHeight]);
1540
+ let {
1541
+ tabsUid
1542
+ } = state;
1543
+ if (tabsUid === -1) {
1544
+ tabsUid = create();
1545
+ }
1546
+ if (disposeCommands) {
1547
+ commands.push(...disposeCommands);
1548
+ }
1549
+ commands.push(['Viewlet.append', state.uid, instanceUid]);
1550
+ if (focus) {
1551
+ commands.push(['Viewlet.focus', instanceUid]);
1552
+ }
1553
+ const latestState = ViewletStates.getState(state.uid);
1554
+ const latestPendingUid = latestState.pendingUid;
1555
+ if (latestPendingUid !== instanceUid) {
1556
+ return {
1557
+ commands: [],
1558
+ newState: state
1559
+ };
1560
+ }
1561
+ if (!ViewletStates.hasInstance(instanceUid)) {
1562
+ return {
1563
+ commands,
1564
+ newState: state
1565
+ };
1566
+ }
1567
+ return {
1568
+ commands,
1569
+ newState: {
1570
+ ...state,
1571
+ groups: newGroups,
1572
+ tabsUid
1573
+ }
1574
+ };
1575
+ };
1576
+
1297
1577
  const text = data => {
1298
1578
  return {
1299
1579
  childCount: 0,
@@ -1303,77 +1583,71 @@ const text = data => {
1303
1583
  };
1304
1584
 
1305
1585
  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'
1586
+ EDITOR_GROUPS_CONTAINER: 'editor-groups-container'};
1587
+
1588
+ const renderEditor = tab => {
1589
+ if (!tab) {
1590
+ return [text('Tab not found')];
1591
+ }
1592
+ if (tab.editorType === 'custom') {
1593
+ return [{
1594
+ childCount: 1,
1595
+ className: 'CustomEditor',
1596
+ type: Div
1597
+ }, text(`Custom Editor: ${tab.customEditorId}`)];
1598
+ }
1599
+ return [{
1600
+ childCount: 1,
1601
+ className: 'TextEditor',
1602
+ type: Div
1603
+ }, {
1604
+ childCount: 1,
1605
+ className: 'EditorContent',
1606
+ type: Pre
1607
+ }, text(tab.content || '')];
1319
1608
  };
1320
1609
 
1321
1610
  const renderTab = (tab, isActive) => {
1322
1611
  return [{
1323
1612
  childCount: 2,
1324
- className: `${CSS_CLASSES.TAB} ${isActive ? CSS_CLASSES.TAB_ACTIVE : ''}`,
1613
+ className: isActive ? 'MainTab MainTabActive' : 'MainTab',
1325
1614
  type: Div
1326
1615
  }, {
1327
1616
  childCount: 1,
1328
- className: CSS_CLASSES.TAB_TITLE,
1617
+ className: 'TabTitle',
1329
1618
  type: Span
1330
1619
  }, text(tab.isDirty ? `*${tab.title}` : tab.title), {
1331
1620
  childCount: 1,
1332
- className: CSS_CLASSES.TAB_CLOSE,
1621
+ className: 'EditorTabCloseButton',
1333
1622
  type: Button
1334
1623
  }, text('×')];
1335
1624
  };
1625
+
1336
1626
  const renderTabBar = group => {
1337
1627
  return [{
1338
1628
  childCount: group.tabs.length,
1339
- className: CSS_CLASSES.TAB_BAR,
1629
+ className: 'MainTabs',
1340
1630
  type: Div
1341
1631
  }, ...group.tabs.flatMap(tab => renderTab(tab, tab.id === group.activeTabId))];
1342
1632
  };
1343
- const renderEditor = tab => {
1344
- if (tab.editorType === 'custom') {
1345
- return [{
1346
- childCount: 1,
1347
- className: CSS_CLASSES.CUSTOM_EDITOR,
1348
- type: Div
1349
- }, text(`Custom Editor: ${tab.customEditorId}`)];
1350
- }
1351
- return [{
1352
- childCount: 1,
1353
- className: CSS_CLASSES.TEXT_EDITOR,
1354
- type: Div
1355
- }, {
1356
- childCount: 1,
1357
- className: CSS_CLASSES.EDITOR_CONTENT,
1358
- type: Pre
1359
- }, text(tab.content || '')];
1360
- };
1633
+
1361
1634
  const renderEditorGroup = group => {
1362
1635
  const activeTab = group.tabs.find(tab => tab.id === group.activeTabId);
1363
1636
  return [{
1364
1637
  childCount: 2,
1365
- className: `${CSS_CLASSES.EDITOR_GROUP} ${group.focused ? CSS_CLASSES.EDITOR_GROUP_FOCUSED : ''}`,
1638
+ className: 'EditorGroup',
1366
1639
  type: Div
1367
1640
  }, ...renderTabBar(group), {
1368
1641
  childCount: activeTab ? 1 : 1,
1369
- className: CSS_CLASSES.EDITOR_CONTAINER,
1642
+ className: 'EditorContainer',
1370
1643
  type: Div
1371
1644
  }, ...renderEditor(activeTab)];
1372
1645
  };
1646
+
1373
1647
  const getMainAreaVirtualDom = layout => {
1374
1648
  return [{
1375
1649
  childCount: 1,
1376
- className: CSS_CLASSES.MAIN_AREA,
1650
+ className: 'Main',
1377
1651
  type: Div
1378
1652
  }, {
1379
1653
  childCount: layout.groups.length,
@@ -1423,11 +1697,15 @@ const render2 = (uid, diffResult) => {
1423
1697
  };
1424
1698
 
1425
1699
  const HandleClick = 11;
1700
+ const HandleClickClose = 12;
1426
1701
 
1427
1702
  const renderEventListeners = () => {
1428
1703
  return [{
1429
1704
  name: HandleClick,
1430
1705
  params: ['handleClick', TargetName]
1706
+ }, {
1707
+ name: HandleClickClose,
1708
+ params: ['handleClickClose', 'event.target.dataset.groupIndex', 'event.target.dataset.index']
1431
1709
  }];
1432
1710
  };
1433
1711
 
@@ -1455,12 +1733,14 @@ const saveState = uid => {
1455
1733
  };
1456
1734
 
1457
1735
  const commandMap = {
1458
- 'MainArea.create': create,
1736
+ 'MainArea.create': create$1,
1459
1737
  'MainArea.diff2': diff2,
1460
1738
  'MainArea.getCommandIds': getCommandIds,
1461
1739
  'MainArea.handleClick': wrapCommand(handleClick),
1740
+ 'MainArea.handleClickCloseTab': wrapCommand(handleClickCloseTab),
1462
1741
  'MainArea.initialize': initialize,
1463
1742
  'MainArea.loadContent': wrapCommand(loadContent),
1743
+ 'MainArea.openUri': wrapCommand(openUri),
1464
1744
  'MainArea.render2': render2,
1465
1745
  'MainArea.renderEventListeners': renderEventListeners,
1466
1746
  '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.5.0",
4
4
  "description": "Main Area Worker",
5
5
  "repository": {
6
6
  "type": "git",