@ai-table/state 0.0.43 → 0.0.45

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.
@@ -1,6 +1,6 @@
1
1
  import * as Y from 'yjs';
2
2
  import { isArray } from 'ngx-tethys/util';
3
- import { AITableQueries, FieldModelMap, AITableFilterOperation, AITableFieldType, isSystemField, isEmpty, getDefaultFieldValue, Direction as Direction$1, idsCreator, idCreator, shortIdCreator, shortIdsCreator, AI_TABLE_GRID_FIELD_SERVICE_MAP, AITable, isMac, buildClipboardData, writeToClipboard, writeToAITable } from '@ai-table/grid';
3
+ import { AITableQueries, FieldModelMap, AITableFilterOperation, AITableFieldType, isSystemField, isEmpty, getDefaultFieldValue, Direction as Direction$1, idsCreator, idCreator, shortIdCreator, generateNewName, shortIdsCreator, AI_TABLE_GRID_FIELD_SERVICE_MAP, AITable, isMac, buildClipboardData, writeToClipboard, getI18nTextByKey, AITableGridI18nKey, writeToAITable } from '@ai-table/grid';
4
4
  import { createDraft, finishDraft } from 'immer';
5
5
  import * as _ from 'lodash';
6
6
  import ___default from 'lodash';
@@ -120,7 +120,7 @@ function toRecordSyncElement(record, fields) {
120
120
  element.insert(0, [systemFieldValues, customFieldValues]);
121
121
  return element;
122
122
  }
123
- function translatePositionToPath(data, position, activeViewId) {
123
+ function translatePositionToPath(data, position, activeViewId, indexOffset = 0) {
124
124
  let index = data.findIndex((value, index) => {
125
125
  if (index === 0) {
126
126
  return position < value.positions[activeViewId];
@@ -130,7 +130,7 @@ function translatePositionToPath(data, position, activeViewId) {
130
130
  if (index === -1) {
131
131
  index = data.length;
132
132
  }
133
- return [index];
133
+ return [index + indexOffset];
134
134
  }
135
135
  function getShareTypeNumberPath(path) {
136
136
  return path.filter((node) => typeof node === 'number');
@@ -330,7 +330,7 @@ function translateArrayEvent(aiTable, sharedType, event) {
330
330
  short_id: getShortIdBySystemFieldValues(systemFieldValues),
331
331
  ...getTrackableEntityBySystemFieldValues(systemFieldValues),
332
332
  positions: getPositionsBySystemFieldValues(systemFieldValues),
333
- values: getValuesByCustomFieldValues(customFieldValues, aiTable.fields())
333
+ values: getValuesByCustomFieldValues(customFieldValues, aiTable.gridData().fields)
334
334
  }
335
335
  });
336
336
  });
@@ -340,9 +340,9 @@ function translateArrayEvent(aiTable, sharedType, event) {
340
340
  const sharedRecords = sharedType.get('records');
341
341
  const sharedFields = sharedType.get('fields');
342
342
  let systemFieldOffset = 0;
343
- delta.insert?.map((item) => {
343
+ delta.insert?.map((item, index) => {
344
344
  const recordIndex = targetPath[0];
345
- const fieldIndex = offset;
345
+ const fieldIndex = offset + index;
346
346
  const record = aiTable.records()[recordIndex];
347
347
  if (isSystemFieldOperation(targetPath)) {
348
348
  if (isPositionsOperation(fieldIndex)) {
@@ -389,9 +389,9 @@ function translateArrayEvent(aiTable, sharedType, event) {
389
389
  }
390
390
  }
391
391
  if (isFieldsTranslate) {
392
- delta.insert?.map((item) => {
392
+ delta.insert?.map((item, index) => {
393
393
  const data = item.toJSON();
394
- const path = translatePositionToPath(aiTable.fields(), data['positions'][activeViewId], activeViewId);
394
+ const path = translatePositionToPath(aiTable.gridData().fields, data['positions'][activeViewId], activeViewId, index);
395
395
  actions.push({
396
396
  type: ActionName.AddField,
397
397
  path,
@@ -468,7 +468,7 @@ function translateMapEvent(aiTable, sharedType, event) {
468
468
  if (isFieldsTranslate) {
469
469
  const field = sharedType.get('fields')?.get(targetPath);
470
470
  const fieldId = field && field.get('_id');
471
- targetElement = fieldId && aiTable.fields().find((item) => item._id === field.get('_id'));
471
+ targetElement = fieldId && aiTable.gridData().fields.find((item) => item._id === field.get('_id'));
472
472
  }
473
473
  if (isViewTranslate) {
474
474
  targetElement = aiTable.views()[targetPath];
@@ -695,6 +695,43 @@ function applyActionOps(aiTable, sharedType, actions) {
695
695
  return sharedType;
696
696
  }
697
697
 
698
+ var AITableStateI18nKey;
699
+ (function (AITableStateI18nKey) {
700
+ AITableStateI18nKey["copyField"] = "copyField";
701
+ AITableStateI18nKey["removeRecords"] = "removeRecords";
702
+ AITableStateI18nKey["copy"] = "copy";
703
+ AITableStateI18nKey["copySuffix"] = "copySuffix";
704
+ AITableStateI18nKey["paste"] = "paste";
705
+ AITableStateI18nKey["invalidPasteContent"] = "invalidPasteContent";
706
+ AITableStateI18nKey["tableView"] = "tableView";
707
+ AITableStateI18nKey["editField"] = "editField";
708
+ AITableStateI18nKey["removeField"] = "removeField";
709
+ })(AITableStateI18nKey || (AITableStateI18nKey = {}));
710
+ const AITableI18nText = {
711
+ [AITableStateI18nKey.copyField]: '复制列',
712
+ [AITableStateI18nKey.removeRecords]: '删除行',
713
+ [AITableStateI18nKey.copy]: '复制',
714
+ [AITableStateI18nKey.copySuffix]: '副本',
715
+ [AITableStateI18nKey.paste]: '粘贴',
716
+ [AITableStateI18nKey.invalidPasteContent]: '粘贴内容不符合当前类型',
717
+ [AITableStateI18nKey.tableView]: '表格视图',
718
+ [AITableStateI18nKey.editField]: '编辑列',
719
+ [AITableStateI18nKey.removeField]: '删除列'
720
+ };
721
+ const getDefaultI18nTextByKey = (key) => {
722
+ return AITableI18nText[key] || key;
723
+ };
724
+ const getStateI18nTextByKey = (aiTable, key) => {
725
+ if (aiTable.getI18nTextByKey) {
726
+ const customText = aiTable.getI18nTextByKey(key);
727
+ if (customText) {
728
+ return customText;
729
+ }
730
+ }
731
+ const defaultText = getDefaultI18nTextByKey(key);
732
+ return defaultText;
733
+ };
734
+
698
735
  function isPathEqual(path, another) {
699
736
  return path.length === another.length && path.every((n, i) => n === another[i]);
700
737
  }
@@ -705,6 +742,16 @@ function sortByViewPosition(data, activeView) {
705
742
  }
706
743
  return data;
707
744
  }
745
+ function generateCopyName(aiTable, existNames, name) {
746
+ const copyText = getStateI18nTextByKey(aiTable, AITableStateI18nKey.copySuffix);
747
+ let newName = `${name} ${copyText}`;
748
+ let index = 2;
749
+ while (existNames.includes(newName)) {
750
+ newName = `${name} ${copyText} ${index}`;
751
+ index++;
752
+ }
753
+ return newName;
754
+ }
708
755
 
709
756
  function getSortRecords(aiTable, records, activeView, sortKeysMap) {
710
757
  if (!activeView?.settings || !activeView.settings.sorts?.length) {
@@ -956,7 +1003,9 @@ const apply = (aiTable, records, fields, views, action) => {
956
1003
  const [fieldIndex] = action.path;
957
1004
  if (fieldIndex > -1) {
958
1005
  const newField = action.field;
959
- newField.positions = createDefaultPositions(aiTable.views(), aiTable.activeViewId(), aiTable.fields(), action.path[0]);
1006
+ if (!newField.positions) {
1007
+ newField.positions = createDefaultPositions(aiTable.views(), aiTable.activeViewId(), aiTable.gridData().fields, action.path[0]);
1008
+ }
960
1009
  fields.splice(fieldIndex, 0, newField);
961
1010
  records.forEach((item) => {
962
1011
  item.values[newField._id] =
@@ -1226,10 +1275,10 @@ function addFields(aiTable, options, updatedInfo) {
1226
1275
  updateRecordsUpdatedInfo(aiTable, updatedInfo);
1227
1276
  }
1228
1277
 
1229
- const buildRemoveFieldItem = (getUpdatedInfo) => {
1278
+ const buildRemoveFieldItem = (aiTable, getUpdatedInfo) => {
1230
1279
  return {
1231
1280
  type: 'removeField',
1232
- name: '删除列',
1281
+ name: getStateI18nTextByKey(aiTable, AITableStateI18nKey.removeField),
1233
1282
  icon: 'trash',
1234
1283
  exec: (aiTable, field) => {
1235
1284
  Actions.removeField(aiTable, [field()._id]);
@@ -1242,8 +1291,12 @@ function createDefaultPositions(views, activeId, data, index) {
1242
1291
  const positions = {};
1243
1292
  const position = getPosition(data, activeId, index);
1244
1293
  views.forEach((element) => {
1245
- const lastPosition = data.length ? data[data.length - 1].positions[element._id] : -1;
1246
- positions[element._id] = element._id === activeId ? position : lastPosition + 1;
1294
+ if (element._id === activeId) {
1295
+ positions[element._id] = position;
1296
+ }
1297
+ else {
1298
+ positions[element._id] = getMaxPosition(data, element._id) + 1;
1299
+ }
1247
1300
  });
1248
1301
  return positions;
1249
1302
  }
@@ -1255,28 +1308,41 @@ function getPosition(data, activeViewId, index) {
1255
1308
  position = (previousViewPosition + nextViewPosition) / 2;
1256
1309
  }
1257
1310
  else {
1258
- const lastPosition = data[data.length - 1].positions?.[activeViewId];
1259
- position = lastPosition ? lastPosition + 1 : index;
1311
+ const maxPosition = getMaxPosition(data, activeViewId);
1312
+ position = maxPosition + 1;
1260
1313
  }
1261
1314
  return position;
1262
1315
  }
1316
+ function getMaxPosition(data, activeViewId) {
1317
+ return data.reduce((maxPosition, item) => {
1318
+ if (item.positions[activeViewId] > maxPosition) {
1319
+ maxPosition = item.positions[activeViewId];
1320
+ }
1321
+ return maxPosition;
1322
+ }, 0);
1323
+ }
1263
1324
  function addView(aiTable, type, viewId) {
1264
1325
  let index = aiTable.views().length;
1265
1326
  const newId = idCreator();
1266
1327
  const shortId = shortIdCreator();
1328
+ const allViewNames = aiTable.views().map((item) => item.name);
1329
+ const count = aiTable.views().length || 0;
1330
+ const newViewName = generateNewName(allViewNames, count, getStateI18nTextByKey(aiTable, AITableStateI18nKey.tableView));
1267
1331
  let newView = {
1268
1332
  _id: newId,
1269
1333
  short_id: shortId,
1270
- name: '表格视图 ' + index
1334
+ name: newViewName
1271
1335
  };
1272
1336
  let originViewId = aiTable.views()[aiTable.views().length - 1]._id;
1273
1337
  if (type === 'copy') {
1274
1338
  originViewId = viewId ?? aiTable.activeViewId();
1275
1339
  const copyView = aiTable.views().find((item) => item._id === originViewId);
1340
+ const copyName = copyView.name;
1341
+ const copyViewName = generateCopyName(aiTable, allViewNames, copyName);
1276
1342
  newView = {
1277
1343
  ...copyView,
1278
1344
  _id: newId,
1279
- name: copyView.name + '-副本'
1345
+ name: copyViewName
1280
1346
  };
1281
1347
  index = aiTable.views().indexOf(copyView) + 1;
1282
1348
  }
@@ -1417,9 +1483,9 @@ const withState = (aiTable) => {
1417
1483
  const DividerMenuItem = {
1418
1484
  type: 'divider'
1419
1485
  };
1420
- const EditFieldPropertyItem = {
1486
+ const EditFieldPropertyItem = (aiTable) => ({
1421
1487
  type: 'editFieldProperty',
1422
- name: '编辑列',
1488
+ name: getStateI18nTextByKey(aiTable, AITableStateI18nKey.editField),
1423
1489
  icon: 'edit',
1424
1490
  exec: (aiTable, field, origin, position) => {
1425
1491
  const fieldService = AI_TABLE_GRID_FIELD_SERVICE_MAP.get(aiTable);
@@ -1440,20 +1506,17 @@ const EditFieldPropertyItem = {
1440
1506
  }
1441
1507
  return undefined;
1442
1508
  }
1443
- };
1444
- const CopyFieldPropertyItem = (addFieldFn) => {
1509
+ });
1510
+ const CopyFieldPropertyItem = (aiTable, addFieldFn) => {
1511
+ const name = getStateI18nTextByKey(aiTable, AITableStateI18nKey.copyField);
1445
1512
  return {
1446
1513
  type: 'copyFieldProperty',
1447
- name: '复制列',
1514
+ name,
1448
1515
  icon: 'copy',
1449
1516
  exec: (aiTable, field) => {
1450
1517
  const allFieldNames = (aiTable.fields() || []).map((item) => item.name);
1451
- let newFieldName = `${field().name} 副本`;
1452
- let index = 2;
1453
- while (allFieldNames.includes(newFieldName)) {
1454
- newFieldName = `${field().name} 副本 ${index}`;
1455
- index++;
1456
- }
1518
+ const copyName = field().name;
1519
+ let newFieldName = generateCopyName(aiTable, allFieldNames, copyName);
1457
1520
  const fieldOptions = {
1458
1521
  originId: field()._id,
1459
1522
  isCopy: true,
@@ -1468,45 +1531,50 @@ const CopyFieldPropertyItem = (addFieldFn) => {
1468
1531
  };
1469
1532
  };
1470
1533
 
1471
- const RemoveRecordsItem = {
1472
- type: 'removeRecords',
1473
- name: '删除行',
1474
- icon: 'trash',
1475
- exec: (aiTable, targetName, position, aiTableGridSelectionService) => {
1476
- let selectedRecordIds = AITable.getActiveRecordIds(aiTable);
1477
- selectedRecordIds.forEach((id) => {
1478
- Actions.removeRecord(aiTable, [id]);
1479
- });
1480
- aiTableGridSelectionService.clearSelection();
1481
- }
1534
+ const RemoveRecordsItem = (aiTable, actions) => {
1535
+ return {
1536
+ type: 'removeRecords',
1537
+ name: getStateI18nTextByKey(aiTable, AITableStateI18nKey.removeRecords),
1538
+ icon: 'trash',
1539
+ exec: (aiTable, targetName, position, aiTableGridSelectionService) => {
1540
+ let selectedRecordIds = AITable.getActiveRecordIds(aiTable);
1541
+ selectedRecordIds.forEach((id) => {
1542
+ Actions.removeRecord(aiTable, [id]);
1543
+ });
1544
+ aiTableGridSelectionService.clearSelection();
1545
+ }
1546
+ };
1482
1547
  };
1483
- const CopyCellsItem = {
1484
- type: 'copyCells',
1485
- name: '复制',
1486
- shortcutKey: isMac() ? `⌘ + C` : `Ctrl + C`,
1487
- icon: 'copy',
1488
- exec: (aiTable, targetName, position, aiTableGridSelectionService, notifyService) => {
1489
- const clipboardData = buildClipboardData(aiTable);
1490
- if (clipboardData) {
1491
- writeToClipboard(clipboardData).then(() => {
1492
- const copiedCellsCount = aiTable.selection().selectedCells.size;
1493
- notifyService.success(`已复制 ${copiedCellsCount} 个单元格`, undefined, {
1494
- placement: 'bottomLeft'
1548
+ const CopyCellsItem = (aiTable, actions) => {
1549
+ return {
1550
+ type: 'copyCells',
1551
+ name: getStateI18nTextByKey(aiTable, AITableStateI18nKey.copy),
1552
+ shortcutKey: isMac() ? `⌘ + C` : `Ctrl + C`,
1553
+ icon: 'copy',
1554
+ exec: (aiTable, targetName, position, aiTableGridSelectionService, notifyService) => {
1555
+ const clipboardData = buildClipboardData(aiTable);
1556
+ if (clipboardData) {
1557
+ writeToClipboard(clipboardData).then(() => {
1558
+ const copiedCellsCount = aiTable.selection().selectedCells.size;
1559
+ const message = getI18nTextByKey(aiTable, AITableGridI18nKey.copiedCells).replace('{count}', copiedCellsCount.toString());
1560
+ notifyService.success(message, undefined, {
1561
+ placement: 'bottomLeft'
1562
+ });
1495
1563
  });
1496
- });
1564
+ }
1497
1565
  }
1498
- }
1566
+ };
1499
1567
  };
1500
- const PasteCellsItem = (actions) => {
1568
+ const PasteCellsItem = (aiTable, actions) => {
1501
1569
  return {
1502
1570
  type: 'pasteCells',
1503
- name: '粘贴',
1571
+ name: getStateI18nTextByKey(aiTable, AITableStateI18nKey.paste),
1504
1572
  shortcutKey: isMac() ? `⌘ + V` : `Ctrl + V`,
1505
1573
  icon: 'paste',
1506
1574
  exec: async (aiTable, targetName, position, aiTableGridSelectionService, notifyService) => {
1507
1575
  writeToAITable(aiTable, actions).then((isPasteSuccess) => {
1508
1576
  if (!isPasteSuccess) {
1509
- notifyService.error('粘贴内容不符合当前类型', undefined, {
1577
+ notifyService.error(getStateI18nTextByKey(aiTable, AITableStateI18nKey.invalidPasteContent), undefined, {
1510
1578
  placement: 'bottomLeft'
1511
1579
  });
1512
1580
  }
@@ -1521,5 +1589,5 @@ const VIEW_ACTIONS = [ActionName.SetView, ActionName.AddView, ActionName.RemoveV
1521
1589
  * Generated bundle index. Do not edit.
1522
1590
  */
1523
1591
 
1524
- export { AITableFilterLogical, AI_TABLE_CONTENT_FIELD_NAME, ActionName, Actions, CopyCellsItem, CopyFieldPropertyItem, Direction, DividerMenuItem, EditFieldPropertyItem, ExecuteType, FLUSHING, PasteCellsItem, Positions, RemovePositions, RemoveRecordsItem, SystemFieldIndex, VIEW_ACTIONS, YjsAITable, actionMappers, addFields, addRecords, addView, applyActionOps, applyEvents, applyYjsEvents, buildFieldsByView, buildRecordsByView, buildRemoveFieldItem, createDefaultPositions, createSharedType, doFilter, getCustomFieldValues, getDataBySharedType, getDefaultRecordDataByFilter, getDefaultRecordValues, getFieldPositionInView, getFilteredRecords, getIdBySystemFieldValues, getIdBySystemFieldValuesType, getPosition, getPositionsByRecordSyncElement, getPositionsBySystemFieldValues, getRecordsBySharedJson, getShareTypeNumberPath, getSharedMapValueId, getSharedMapValueIndex, getSharedRecord, getSharedRecordId, getSharedRecordIndex, getSharedTypeByData, getShortIdBySystemFieldValues, getSortFields, getSortRecords, getSystemFieldValues, getTrackableEntityBySystemFieldValues, getValuesByCustomFieldValues, isPathEqual, moveFields, removeView, setRecordPositions$1 as setRecordPositions, setRecordUpdatedInfo, sortByViewPosition, sortRecordsBySortInfo, toRecordSyncElement, toSharedType, toSyncElement, translatePositionToPath, translateYjsEvent, updateFieldValue, updateRecordsUpdatedInfo, withState };
1592
+ export { AITableFilterLogical, AITableStateI18nKey, AI_TABLE_CONTENT_FIELD_NAME, ActionName, Actions, CopyCellsItem, CopyFieldPropertyItem, Direction, DividerMenuItem, EditFieldPropertyItem, ExecuteType, FLUSHING, PasteCellsItem, Positions, RemovePositions, RemoveRecordsItem, SystemFieldIndex, VIEW_ACTIONS, YjsAITable, actionMappers, addFields, addRecords, addView, applyActionOps, applyEvents, applyYjsEvents, buildFieldsByView, buildRecordsByView, buildRemoveFieldItem, createDefaultPositions, createSharedType, doFilter, generateCopyName, getCustomFieldValues, getDataBySharedType, getDefaultI18nTextByKey, getDefaultRecordDataByFilter, getDefaultRecordValues, getFieldPositionInView, getFilteredRecords, getIdBySystemFieldValues, getIdBySystemFieldValuesType, getMaxPosition, getPosition, getPositionsByRecordSyncElement, getPositionsBySystemFieldValues, getRecordsBySharedJson, getShareTypeNumberPath, getSharedMapValueId, getSharedMapValueIndex, getSharedRecord, getSharedRecordId, getSharedRecordIndex, getSharedTypeByData, getShortIdBySystemFieldValues, getSortFields, getSortRecords, getStateI18nTextByKey, getSystemFieldValues, getTrackableEntityBySystemFieldValues, getValuesByCustomFieldValues, isPathEqual, moveFields, removeView, setRecordPositions$1 as setRecordPositions, setRecordUpdatedInfo, sortByViewPosition, sortRecordsBySortInfo, toRecordSyncElement, toSharedType, toSyncElement, translatePositionToPath, translateYjsEvent, updateFieldValue, updateRecordsUpdatedInfo, withState };
1525
1593
  //# sourceMappingURL=ai-table-state.mjs.map