@lvce-editor/main-area-worker 1.8.0 → 1.10.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.
@@ -96,12 +96,6 @@ const object = value => {
96
96
  throw new AssertionError('expected value to be of type object');
97
97
  }
98
98
  };
99
- const number = value => {
100
- const type = getType(value);
101
- if (type !== Number$1) {
102
- throw new AssertionError('expected value to be of type number');
103
- }
104
- };
105
99
 
106
100
  const isMessagePort = value => {
107
101
  return value && value instanceof MessagePort;
@@ -456,7 +450,7 @@ const getFirstEvent = (eventEmitter, eventMap) => {
456
450
  return promise;
457
451
  };
458
452
  const Message$1 = 3;
459
- const create$5 = async ({
453
+ const create$5$1 = async ({
460
454
  isMessagePortOpen,
461
455
  messagePort
462
456
  }) => {
@@ -507,7 +501,7 @@ const wrap$5 = messagePort => {
507
501
  };
508
502
  const IpcParentWithMessagePort$1 = {
509
503
  __proto__: null,
510
- create: create$5,
504
+ create: create$5$1,
511
505
  signal: signal$1,
512
506
  wrap: wrap$5
513
507
  };
@@ -726,7 +720,7 @@ const getErrorResponse = (id, error, preparePrettyError, logError) => {
726
720
  const errorProperty = getErrorProperty(error, prettyError);
727
721
  return create$1$2(id, errorProperty);
728
722
  };
729
- const create$3 = (message, result) => {
723
+ const create$5 = (message, result) => {
730
724
  return {
731
725
  jsonrpc: Two$1,
732
726
  id: message.id,
@@ -735,7 +729,7 @@ const create$3 = (message, result) => {
735
729
  };
736
730
  const getSuccessResponse = (message, result) => {
737
731
  const resultProperty = result ?? null;
738
- return create$3(message, resultProperty);
732
+ return create$5(message, resultProperty);
739
733
  };
740
734
  const getErrorResponseSimple = (id, error) => {
741
735
  return {
@@ -1059,7 +1053,7 @@ const remove = id => {
1059
1053
  };
1060
1054
 
1061
1055
  /* eslint-disable @typescript-eslint/explicit-function-return-type */
1062
- const create$2 = rpcId => {
1056
+ const create$3 = rpcId => {
1063
1057
  return {
1064
1058
  async dispose() {
1065
1059
  const rpc = get$1(rpcId);
@@ -1097,12 +1091,12 @@ const create$2 = rpcId => {
1097
1091
 
1098
1092
  const {
1099
1093
  set: set$2
1100
- } = create$2(ExtensionHostWorker);
1094
+ } = create$3(ExtensionHostWorker);
1101
1095
 
1102
1096
  const {
1103
1097
  invokeAndTransfer,
1104
1098
  set: set$1
1105
- } = create$2(RendererWorker);
1099
+ } = create$3(RendererWorker);
1106
1100
  const sendMessagePortToExtensionHostWorker$1 = async (port, rpcId = 0) => {
1107
1101
  const command = 'HandleMessagePort.handleMessagePort2';
1108
1102
  await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToExtensionHostWorker', port, command, rpcId);
@@ -1112,7 +1106,7 @@ const toCommandId = key => {
1112
1106
  const dotIndex = key.indexOf('.');
1113
1107
  return key.slice(dotIndex + 1);
1114
1108
  };
1115
- const create$1 = () => {
1109
+ const create$2 = () => {
1116
1110
  const states = Object.create(null);
1117
1111
  const commandMapRef = {};
1118
1112
  return {
@@ -1198,13 +1192,13 @@ const {
1198
1192
  getCommandIds,
1199
1193
  registerCommands,
1200
1194
  set,
1201
- wrapCommand} = create$1();
1195
+ wrapCommand} = create$2();
1202
1196
 
1203
- const create = (uid, uri, x, y, width, height, platform, assetDir) => {
1197
+ const create$1 = (uid, uri, x, y, width, height, platform, assetDir) => {
1204
1198
  const state = {
1205
1199
  assetDir,
1206
1200
  layout: {
1207
- activeGroupId: '',
1201
+ activeGroupId: undefined,
1208
1202
  direction: 'horizontal',
1209
1203
  groups: []
1210
1204
  },
@@ -1250,9 +1244,87 @@ const handleClick = async (state, name) => {
1250
1244
  return state;
1251
1245
  };
1252
1246
 
1253
- const handleClickCloseTab = (state, groupId, tabId) => {
1254
- // TODO
1255
- return state;
1247
+ const closeTab = (state, groupId, tabId) => {
1248
+ const {
1249
+ layout
1250
+ } = state;
1251
+ const {
1252
+ activeGroupId,
1253
+ groups
1254
+ } = layout;
1255
+ const newGroups = groups.map(group => {
1256
+ if (group.id === groupId) {
1257
+ const newTabs = group.tabs.filter(tab => tab.id !== tabId);
1258
+ let newActiveTabId = group.activeTabId;
1259
+ if (group.activeTabId === tabId) {
1260
+ const tabIndex = group.tabs.findIndex(tab => tab.id === tabId);
1261
+ if (newTabs.length > 0) {
1262
+ newActiveTabId = newTabs[Math.min(tabIndex, newTabs.length - 1)].id;
1263
+ } else {
1264
+ newActiveTabId = undefined;
1265
+ }
1266
+ }
1267
+ return {
1268
+ ...group,
1269
+ activeTabId: newActiveTabId,
1270
+ tabs: newTabs
1271
+ };
1272
+ }
1273
+ return group;
1274
+ });
1275
+
1276
+ // If the group has no tabs left and there are other groups, remove the group
1277
+ const groupWithNoTabs = newGroups.find(group => group.id === groupId && group.tabs.length === 0);
1278
+ if (groupWithNoTabs && newGroups.length > 1) {
1279
+ const remainingGroups = newGroups.filter(group => group.id !== groupId);
1280
+ const redistributedGroups = remainingGroups.map(group => ({
1281
+ ...group,
1282
+ size: Math.round(100 / remainingGroups.length)
1283
+ }));
1284
+ const newActiveGroupId = activeGroupId === groupId ? remainingGroups[0]?.id : activeGroupId;
1285
+ return {
1286
+ ...state,
1287
+ layout: {
1288
+ ...layout,
1289
+ activeGroupId: newActiveGroupId,
1290
+ groups: redistributedGroups
1291
+ }
1292
+ };
1293
+ }
1294
+ return {
1295
+ ...state,
1296
+ layout: {
1297
+ ...layout,
1298
+ groups: newGroups
1299
+ }
1300
+ };
1301
+ };
1302
+
1303
+ const handleClickCloseTab = (state, rawGroupIndex, rawIndex) => {
1304
+ if (!rawGroupIndex || !rawIndex) {
1305
+ return state;
1306
+ }
1307
+ const groupIndex = Number.parseInt(rawGroupIndex);
1308
+ const index = Number.parseInt(rawIndex);
1309
+ const {
1310
+ layout
1311
+ } = state;
1312
+ const {
1313
+ groups
1314
+ } = layout;
1315
+
1316
+ // Validate indexes
1317
+ if (groupIndex < 0 || groupIndex >= groups.length) {
1318
+ return state;
1319
+ }
1320
+ const group = groups[groupIndex];
1321
+ if (index < 0 || index >= group.tabs.length) {
1322
+ return state;
1323
+ }
1324
+ const tab = group.tabs[index];
1325
+ const groupId = group.id;
1326
+ const tabId = tab.id;
1327
+ return closeTab(state, groupId, tabId);
1256
1328
  };
1257
1329
 
1258
1330
  const selectTab = async (state, groupIndex, index) => {
@@ -1327,30 +1399,59 @@ const initialize = async () => {
1327
1399
  set$2(rpc);
1328
1400
  };
1329
1401
 
1330
- const loadContent = async state => {
1402
+ const getTabs = async () => {
1331
1403
  const tabs = [{
1332
1404
  content: '',
1333
1405
  editorType: 'text',
1334
- id: '1',
1406
+ id: 1,
1335
1407
  isDirty: false,
1336
1408
  title: 'tab 1'
1337
1409
  }, {
1338
1410
  content: '',
1339
1411
  editorType: 'text',
1340
- id: '2',
1412
+ id: 2,
1341
1413
  isDirty: false,
1342
1414
  title: 'tab 2'
1415
+ }, {
1416
+ content: '',
1417
+ editorType: 'text',
1418
+ id: 3,
1419
+ isDirty: false,
1420
+ title: 'tab 3'
1421
+ }, {
1422
+ content: '',
1423
+ editorType: 'text',
1424
+ id: 4,
1425
+ isDirty: false,
1426
+ title: 'tab 4'
1427
+ }, {
1428
+ content: '',
1429
+ editorType: 'text',
1430
+ id: 5,
1431
+ isDirty: false,
1432
+ title: 'tab 5'
1433
+ }, {
1434
+ content: '',
1435
+ editorType: 'text',
1436
+ id: 6,
1437
+ isDirty: false,
1438
+ title: 'tab 6'
1343
1439
  }];
1440
+ return tabs;
1441
+ };
1442
+
1443
+ const loadContent = async state => {
1444
+ const tabs = await getTabs();
1344
1445
  return {
1345
1446
  ...state,
1346
1447
  layout: {
1347
- activeGroupId: '0',
1448
+ activeGroupId: 0,
1348
1449
  direction: 'horizontal',
1349
1450
  groups: [{
1350
1451
  activeTabId: tabs.length > 0 ? tabs[0].id : undefined,
1351
1452
  direction: 'horizontal',
1352
1453
  focused: false,
1353
- id: '0',
1454
+ id: 0,
1354
1455
  size: 300,
1355
1456
  tabs
1356
1457
  }]
@@ -1358,14 +1459,185 @@ const loadContent = async state => {
1358
1459
  };
1359
1460
  };
1360
1461
 
1462
+ const findTabByUri = (state, uri) => {
1463
+ const {
1464
+ layout
1465
+ } = state;
1466
+ const {
1467
+ groups
1468
+ } = layout;
1469
+ for (const group of groups) {
1470
+ const tab = group.tabs.find(t => t.path === uri);
1471
+ if (tab) {
1472
+ return {
1473
+ groupId: group.id,
1474
+ tab
1475
+ };
1476
+ }
1477
+ }
1478
+ return undefined;
1479
+ };
1480
+
1481
+ const focusEditorGroup = (state, groupId) => {
1482
+ const {
1483
+ layout
1484
+ } = state;
1485
+ const {
1486
+ groups
1487
+ } = layout;
1488
+ const updatedGroups = groups.map(group => ({
1489
+ ...group,
1490
+ focused: group.id === groupId
1491
+ }));
1492
+ return {
1493
+ ...state,
1494
+ layout: {
1495
+ ...layout,
1496
+ activeGroupId: groupId,
1497
+ groups: updatedGroups
1498
+ }
1499
+ };
1500
+ };
1501
+
1502
+ let idCounter = 0;
1503
+ const create = () => {
1504
+ idCounter++;
1505
+ return idCounter;
1506
+ };
1507
+
1508
+ const openTab = (state, groupId, tab) => {
1509
+ const newTab = {
1510
+ ...tab,
1511
+ id: create()
1512
+ };
1513
+ const {
1514
+ layout
1515
+ } = state;
1516
+ const {
1517
+ groups
1518
+ } = layout;
1519
+ const updatedGroups = groups.map(group => {
1520
+ if (group.id === groupId) {
1521
+ return {
1522
+ ...group,
1523
+ activeTabId: newTab.id,
1524
+ tabs: [...group.tabs, newTab]
1525
+ };
1526
+ }
1527
+ return group;
1528
+ });
1529
+ return {
1530
+ ...state,
1531
+ layout: {
1532
+ ...layout,
1533
+ groups: updatedGroups
1534
+ }
1535
+ };
1536
+ };
1537
+
1538
+ const getLabel = uri => {
1539
+ if (uri.startsWith('settings://')) {
1540
+ return 'Settings';
1541
+ }
1542
+ if (uri.startsWith('simple-browser://')) {
1543
+ return 'Simple Browser';
1544
+ }
1545
+ return uri;
1546
+ // return Workspace.pathBaseName(uri)
1547
+ };
1548
+
1549
+ const switchTab = (state, groupId, tabId) => {
1550
+ const {
1551
+ layout
1552
+ } = state;
1553
+ const {
1554
+ groups
1555
+ } = layout;
1556
+ const updatedGroups = groups.map(group => {
1557
+ if (group.id === groupId) {
1558
+ const tabExists = group.tabs.some(tab => tab.id === tabId);
1559
+ if (tabExists) {
1560
+ return {
1561
+ ...group,
1562
+ activeTabId: tabId
1563
+ };
1564
+ }
1565
+ }
1566
+ return group;
1567
+ });
1568
+ return {
1569
+ ...state,
1570
+ layout: {
1571
+ ...layout,
1572
+ groups: updatedGroups
1573
+ }
1574
+ };
1575
+ };
1576
+
1361
1577
  const openUri = async (state, options) => {
1362
1578
  object(state);
1363
1579
  object(options);
1580
+ const {
1581
+ uri
1582
+ } = options;
1364
1583
 
1365
- // TODO
1366
- return {
1367
- ...state
1584
+ // Check if a tab with this URI already exists
1585
+ const existingTab = findTabByUri(state, uri);
1586
+ if (existingTab) {
1587
+ // Tab exists, switch to it and focus its group
1588
+ const focusedState = focusEditorGroup(state, existingTab.groupId);
1589
+ return switchTab(focusedState, existingTab.groupId, existingTab.tab.id);
1590
+ }
1591
+
1592
+ // Find the active group (by activeGroupId or focused flag)
1593
+ const {
1594
+ layout
1595
+ } = state;
1596
+ const {
1597
+ activeGroupId,
1598
+ groups
1599
+ } = layout;
1600
+ const activeGroup = activeGroupId === undefined ? groups.find(group => group.focused) : groups.find(group => group.id === activeGroupId);
1601
+
1602
+ // If no active group exists, create one
1603
+ if (!activeGroup) {
1604
+ const groupId = create();
1605
+ const title = getLabel(uri);
1606
+ const newTab = {
1607
+ content: '',
1608
+ editorType: 'text',
1609
+ id: create(),
1610
+ isDirty: false,
1611
+ path: uri,
1612
+ title
1613
+ };
1614
+ const newGroup = {
1615
+ activeTabId: newTab.id,
1616
+ focused: true,
1617
+ id: groupId,
1618
+ size: 100,
1619
+ tabs: [newTab]
1620
+ };
1621
+ return {
1622
+ ...state,
1623
+ layout: {
1624
+ ...layout,
1625
+ activeGroupId: groupId,
1626
+ groups: [...groups, newGroup]
1627
+ }
1628
+ };
1629
+ }
1630
+
1631
+ // Create a new tab with the URI in the active group
1632
+ const title = getLabel(uri);
1633
+ const newTab = {
1634
+ content: '',
1635
+ editorType: 'text',
1636
+ isDirty: false,
1637
+ path: uri,
1638
+ title
1368
1639
  };
1640
+ return openTab(state, activeGroup.id, newTab);
1369
1641
  };
1370
1642
 
1371
1643
  const text = data => {
@@ -1381,7 +1653,15 @@ const CSS_CLASSES = {
1381
1653
 
1382
1654
  const renderEditor = tab => {
1383
1655
  if (!tab) {
1384
- return [text('Tab not found')];
1656
+ return [{
1657
+ childCount: 1,
1658
+ className: 'TextEditor',
1659
+ type: Div
1660
+ }, {
1661
+ childCount: 1,
1662
+ className: 'EditorContent',
1663
+ type: Pre
1664
+ }, text('')];
1385
1665
  }
1386
1666
  if (tab.editorType === 'custom') {
1387
1667
  return [{
@@ -1421,6 +1701,8 @@ const renderTab = (tab, isActive, tabIndex, groupIndex) => {
1421
1701
  }, text(tab.isDirty ? `*${tab.title}` : tab.title), {
1422
1702
  childCount: 1,
1423
1703
  className: 'EditorTabCloseButton',
1704
+ 'data-groupIndex': groupIndex,
1705
+ 'data-index': tabIndex,
1424
1706
  onClick: HandleClickClose,
1425
1707
  type: Button
1426
1708
  }, text('×')];
@@ -1519,24 +1801,17 @@ const resize = (state, dimensions) => {
1519
1801
  };
1520
1802
  };
1521
1803
 
1522
- const saveState = uid => {
1523
- number(uid);
1524
- const value = get(uid);
1804
+ const saveState = state => {
1525
1805
  const {
1526
- newState
1527
- } = value;
1528
- const {
1529
- statusBarItemsLeft,
1530
- statusBarItemsRight
1531
- } = newState;
1806
+ layout
1807
+ } = state;
1532
1808
  return {
1533
- itemsLeft: statusBarItemsLeft,
1534
- itemsRight: statusBarItemsRight
1809
+ layout
1535
1810
  };
1536
1811
  };
1537
1812
 
1538
1813
  const commandMap = {
1539
- 'MainArea.create': create,
1814
+ 'MainArea.create': create$1,
1540
1815
  'MainArea.diff2': diff2,
1541
1816
  'MainArea.getCommandIds': getCommandIds,
1542
1817
  'MainArea.handleClick': wrapCommand(handleClick),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/main-area-worker",
3
- "version": "1.8.0",
3
+ "version": "1.10.0",
4
4
  "description": "Main Area Worker",
5
5
  "repository": {
6
6
  "type": "git",
@@ -11,6 +11,6 @@
11
11
  "type": "module",
12
12
  "main": "dist/mainAreaWorkerMain.js",
13
13
  "dependencies": {
14
- "@lvce-editor/virtual-dom-worker": "^5.0.0"
14
+ "@lvce-editor/virtual-dom-worker": "^5.1.0"
15
15
  }
16
16
  }