@ai-table/state 0.0.39 → 0.0.41

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, ViewOperationMap, isSystemField, AITableFieldType, isEmpty, getDefaultFieldValue, Direction as Direction$1, idsCreator, idCreator, shortIdCreator, shortIdsCreator, AI_TABLE_GRID_FIELD_SERVICE_MAP, AITable } from '@ai-table/grid';
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';
4
4
  import { createDraft, finishDraft } from 'immer';
5
5
  import * as _ from 'lodash';
6
6
  import ___default from 'lodash';
@@ -725,7 +725,7 @@ function sortRecordsBySortInfo(aiTable, records, activeView, sortKeysMap) {
725
725
  if (!field || acc !== 0) {
726
726
  return acc;
727
727
  }
728
- const fieldMethod = ViewOperationMap[field.type];
728
+ const fieldMethod = FieldModelMap[field.type];
729
729
  const sortKey = sortKeysMap?.[field.type];
730
730
  const cellValue1 = AITableQueries.getFieldValue(aiTable, [prev._id, field._id]);
731
731
  const cellValue2 = AITableQueries.getFieldValue(aiTable, [current._id, field._id]);
@@ -782,14 +782,41 @@ function doFilterOperations(fields, record, condition) {
782
782
  }
783
783
  }
784
784
  function doFilter(condition, field, cellValue) {
785
- return ViewOperationMap[field.type].isMeetFilter(condition, cellValue);
785
+ return FieldModelMap[field.type].isMeetFilter(condition, cellValue);
786
786
  }
787
- function getDefaultRecordDataByFilter(recordValues, conditions, conditionLogical) {
788
- if (conditions.length === 1) {
789
- // recordValues[conditions[0].field_id] = conditions[0].value;
790
- }
791
- else {
792
- //...
787
+ function getDefaultRecordDataByFilter(recordValues, conditions, fields, conditionLogical) {
788
+ const fieldMap = new Map(fields.map((field) => [field._id, field]));
789
+ const conditionFieldCountMap = new Map();
790
+ conditions.forEach((condition) => {
791
+ const fieldId = condition.field_id.toString();
792
+ const tmpFieldCount = conditionFieldCountMap.get(fieldId) || 0;
793
+ conditionFieldCountMap.set(fieldId, tmpFieldCount + 1);
794
+ });
795
+ if (conditionLogical === AITableFilterLogical.and) {
796
+ conditions.forEach((condition) => {
797
+ if (conditionFieldCountMap.get(condition.field_id.toString()) === 1) {
798
+ const field = fieldMap.get(condition.field_id.toString());
799
+ const canMultipleOperationCondition = (!field.settings?.is_multiple && condition.operation === AITableFilterOperation.eq) ||
800
+ field?.settings?.is_multiple;
801
+ if ([AITableFilterOperation.eq, AITableFilterOperation.in].includes(condition.operation) &&
802
+ [AITableFieldType.select, AITableFieldType.member].includes(field?.type) &&
803
+ canMultipleOperationCondition) {
804
+ recordValues[condition.field_id] = condition.value;
805
+ }
806
+ if (field?.type === AITableFieldType.date && condition.operation === AITableFilterOperation.eq) {
807
+ recordValues[condition.field_id] = {
808
+ timestamp: condition.value
809
+ };
810
+ }
811
+ if (condition.operation === AITableFilterOperation.contain && AITableFieldType.text === field?.type) {
812
+ recordValues[condition.field_id] = condition.value;
813
+ }
814
+ if (condition.operation === AITableFilterOperation.eq &&
815
+ [AITableFieldType.rate, AITableFieldType.number, AITableFieldType.progress].includes(field?.type)) {
816
+ recordValues[condition.field_id] = condition.value;
817
+ }
818
+ }
819
+ });
793
820
  }
794
821
  return recordValues;
795
822
  }
@@ -885,6 +912,26 @@ const FieldActions = {
885
912
  setField
886
913
  };
887
914
 
915
+ function moveFields(aiTable, options, updatedInfo) {
916
+ const { path, newPath } = options;
917
+ Actions.moveField(aiTable, path, newPath);
918
+ }
919
+ function updateFieldPositionInView(viewId, fields, sourceField, targetField, path, newPath) {
920
+ const targetPosition = targetField.positions[viewId];
921
+ let calculatePosition = 0;
922
+ if (path[0] > newPath[0]) {
923
+ const targetPrevField = fields[Math.max(0, newPath[0] - 1)];
924
+ const targetPrevPosition = targetPrevField.positions[viewId];
925
+ calculatePosition = (targetPosition + targetPrevPosition) / 2;
926
+ }
927
+ else {
928
+ const targetNextField = fields[Math.min(fields.length - 1, newPath[0] + 1)];
929
+ const targetNextPosition = targetNextField.positions[viewId];
930
+ calculatePosition = (targetPosition + targetNextPosition) / 2;
931
+ }
932
+ sourceField.positions[viewId] = calculatePosition;
933
+ }
934
+
888
935
  const apply = (aiTable, records, fields, views, action) => {
889
936
  switch (action.type) {
890
937
  case ActionName.UpdateFieldValue: {
@@ -949,9 +996,10 @@ const apply = (aiTable, records, fields, views, action) => {
949
996
  if (isPathEqual(action.path, action.newPath)) {
950
997
  return;
951
998
  }
952
- const field = fields[action.path[0]];
953
- fields.splice(action.path[0], 1);
954
- fields.splice(action.newPath[0], 0, field);
999
+ const activeView = aiTable.views().find((item) => item._id === aiTable.activeViewId());
1000
+ const sourceField = fields[action.path[0]];
1001
+ const targetField = fields[action.newPath[0]];
1002
+ updateFieldPositionInView(activeView._id, fields, sourceField, targetField, action.path, action.newPath);
955
1003
  break;
956
1004
  }
957
1005
  case ActionName.RemoveField: {
@@ -1059,7 +1107,7 @@ const apply = (aiTable, records, fields, views, action) => {
1059
1107
  const GeneralActions = {
1060
1108
  transform(aiTable, action) {
1061
1109
  const records = createDraft(aiTable.records());
1062
- const fields = createDraft(aiTable.fields());
1110
+ const fields = createDraft(aiTable.gridData().fields);
1063
1111
  const views = createDraft(aiTable.views());
1064
1112
  apply(aiTable, records, fields, views, action);
1065
1113
  aiTable.fields.set(finishDraft(fields));
@@ -1324,7 +1372,7 @@ function getDefaultRecordValues(aiTable, isDuplicate = false, recordId) {
1324
1372
  });
1325
1373
  const { conditions, condition_logical } = activeView.settings || {};
1326
1374
  if (conditions && conditions.length) {
1327
- newRecordValues = getDefaultRecordDataByFilter(newRecordValues, conditions, condition_logical);
1375
+ newRecordValues = getDefaultRecordDataByFilter(newRecordValues, conditions, fields, condition_logical);
1328
1376
  }
1329
1377
  }
1330
1378
  return newRecordValues;
@@ -1424,6 +1472,40 @@ const RemoveRecordsItem = {
1424
1472
  aiTableGridSelectionService.clearSelection();
1425
1473
  }
1426
1474
  };
1475
+ const CopyCellsItem = {
1476
+ type: 'copyCells',
1477
+ name: '复制',
1478
+ shortcutKey: isMac() ? `⌘ + C` : `Ctrl + C`,
1479
+ icon: 'copy',
1480
+ exec: (aiTable, targetName, position, aiTableGridSelectionService, notifyService) => {
1481
+ const clipboardData = buildClipboardData(aiTable);
1482
+ if (clipboardData) {
1483
+ writeToClipboard(clipboardData).then(() => {
1484
+ const copiedCellsCount = aiTable.selection().selectedCells.size;
1485
+ notifyService.success(`已复制 ${copiedCellsCount} 个单元格`, undefined, {
1486
+ placement: 'bottomLeft'
1487
+ });
1488
+ });
1489
+ }
1490
+ }
1491
+ };
1492
+ const PasteCellsItem = (actions) => {
1493
+ return {
1494
+ type: 'pasteCells',
1495
+ name: '粘贴',
1496
+ shortcutKey: isMac() ? `⌘ + V` : `Ctrl + V`,
1497
+ icon: 'paste',
1498
+ exec: async (aiTable, targetName, position, aiTableGridSelectionService, notifyService) => {
1499
+ writeToAITable(aiTable, actions).then((isPasteSuccess) => {
1500
+ if (!isPasteSuccess) {
1501
+ notifyService.error('粘贴内容不符合当前类型', undefined, {
1502
+ placement: 'bottomLeft'
1503
+ });
1504
+ }
1505
+ });
1506
+ }
1507
+ };
1508
+ };
1427
1509
 
1428
1510
  const VIEW_ACTIONS = [ActionName.SetView, ActionName.AddView, ActionName.RemoveView];
1429
1511
 
@@ -1431,5 +1513,5 @@ const VIEW_ACTIONS = [ActionName.SetView, ActionName.AddView, ActionName.RemoveV
1431
1513
  * Generated bundle index. Do not edit.
1432
1514
  */
1433
1515
 
1434
- export { AITableFilterLogical, AI_TABLE_CONTENT_FIELD_NAME, ActionName, Actions, CopyFieldPropertyItem, Direction, DividerMenuItem, EditFieldPropertyItem, ExecuteType, FLUSHING, Positions, RemovePositions, RemoveRecordsItem, SystemFieldIndex, VIEW_ACTIONS, YjsAITable, actionMappers, addFields, addRecords, addView, applyActionOps, applyEvents, applyYjsEvents, buildFieldsByView, buildRecordsByView, buildRemoveFieldItem, createDefaultPositions, createSharedType, doFilter, getCustomFieldValues, getDataBySharedType, getDefaultRecordDataByFilter, getDefaultRecordValues, getFilteredRecords, getIdBySystemFieldValues, getIdBySystemFieldValuesType, getPosition, getPositionsByRecordSyncElement, getPositionsBySystemFieldValues, getRecordsBySharedJson, getShareTypeNumberPath, getSharedMapValueId, getSharedMapValueIndex, getSharedRecord, getSharedRecordId, getSharedRecordIndex, getSharedTypeByData, getShortIdBySystemFieldValues, getSortFields, getSortRecords, getSystemFieldValues, getTrackableEntityBySystemFieldValues, getValuesByCustomFieldValues, isPathEqual, removeView, setRecordPositions$1 as setRecordPositions, setRecordUpdatedInfo, sortByViewPosition, sortRecordsBySortInfo, toRecordSyncElement, toSharedType, toSyncElement, translatePositionToPath, translateYjsEvent, updateFieldValue, updateRecordsUpdatedInfo, withState };
1516
+ 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, 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, updateFieldPositionInView, updateFieldValue, updateRecordsUpdatedInfo, withState };
1435
1517
  //# sourceMappingURL=ai-table-state.mjs.map