@ai-table/state 0.2.1 → 0.2.3

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,9 +1,9 @@
1
1
  import * as Y from 'yjs';
2
2
  import { UndoManager } from 'yjs';
3
- import { getShareTypeNumberPath, ActionName, translateArrayEvent, getSharedRecordIndex, getSharedMapValueIndex, toMapSyncElement, getIdBySystemFieldValuesType, setRecordUpdatedInfo, getPositionsByRecordSyncElement, setRecordPositions as setRecordPositions$1, toRecordSyncElement, AI_TABLE_CONTENT_FIELD_NAME, getValuesByCustomFieldValues, getPositionsBySystemFieldValues, getTrackableEntityBySystemFieldValues, getShortIdBySystemFieldValues, getIdBySystemFieldValues, AITableFilterLogical, AITableFilterOperation, AITableFieldType, isEmpty, sortByViewPosition, AI_TABLE_DEFAULT_MIN_UNFROZEN_WIDTH, AI_TABLE_MIN_FROZEN_COLUMN_COUNT, idCreator, SortDirection } from '@ai-table/utils';
3
+ import { getShareTypeNumberPath, ActionName, translateArrayEvent, getSharedRecordIndex, getSharedMapValueIndex, toMapSyncElement, getIdBySystemFieldValuesType, setRecordUpdatedInfo, getPositionsByRecordSyncElement, setRecordPositions as setRecordPositions$1, toRecordSyncElement, AI_TABLE_CONTENT_FIELD_NAME, getValuesByCustomFieldValues, getPositionsBySystemFieldValues, getTrackableEntityBySystemFieldValues, getShortIdBySystemFieldValues, getIdBySystemFieldValues, AITableFilterLogical, AITableFilterOperation, AITableFieldType, isEmpty, sortByViewPosition, AI_TABLE_DEFAULT_MIN_UNFROZEN_WIDTH, AI_TABLE_MIN_FROZEN_COLUMN_COUNT, SortDirection, idCreator } from '@ai-table/utils';
4
4
  import * as i0 from '@angular/core';
5
5
  import { signal, Injectable } from '@angular/core';
6
- import { FieldModelMap, AITableQueries, isSystemField, AITableRowType, AITable, getColumnIndicesSizeMap, shortIdCreator, generateNewName, getDefaultFieldValue, AI_TABLE_GROUP_MAX_LEVEL, idsCreator, shortIdsCreator, closeExpendCell, setSelection, AI_TABLE_GRID_FIELD_SERVICE_MAP, clearSelection, buildClipboardData, writeToClipboard, getI18nTextByKey, AITableGridI18nKey, isMac, writeToAITable } from '@ai-table/grid';
6
+ import { FieldModelMap, AITableQueries, isSystemField, AITableRowType, AITable, getColumnIndicesSizeMap, AI_TABLE_GROUP_MAX_LEVEL, shortIdCreator, generateNewName, idsCreator, shortIdsCreator, closeExpendCell, setSelection, buildNormalLinearRows, AI_TABLE_GRID_FIELD_SERVICE_MAP, clearSelection, buildClipboardData, writeToClipboard, getI18nTextByKey, AITableGridI18nKey, isMac, writeToAITable } from '@ai-table/grid';
7
7
  import { nanoid } from 'nanoid';
8
8
  import * as _ from 'lodash';
9
9
  import ___default, { map, unionBy } from 'lodash';
@@ -409,7 +409,8 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.10", ngImpo
409
409
  type: Injectable
410
410
  }] });
411
411
 
412
- function sortRecordsByConditions(aiTable, records, activeView, sorts, sortKeysMap) {
412
+ function sortRecordsByConditions(aiTable, records, activeView, sorts) {
413
+ const sortKeysMap = aiTable.sortKeysMap;
413
414
  const shallowRecords = [...records];
414
415
  return shallowRecords.sort((record1, record2) => {
415
416
  if (sorts.length) {
@@ -429,8 +430,8 @@ function compareBySorts(record1, record2, sorts, aiTable, sortKeysMap) {
429
430
  }
430
431
  const fieldMethod = FieldModelMap[field.type];
431
432
  const sortKey = sortKeysMap?.[field.type];
432
- const cellValue1 = AITableQueries.getFieldValue(aiTable, [record1._id, field._id]);
433
- const cellValue2 = AITableQueries.getFieldValue(aiTable, [record2._id, field._id]);
433
+ const cellValue1 = AITableQueries.getFieldValue(aiTable, [record1._id, field._id], record1);
434
+ const cellValue2 = AITableQueries.getFieldValue(aiTable, [record2._id, field._id], record2);
434
435
  const references = aiTable.context.references();
435
436
  const res = fieldMethod.compare(cellValue1, cellValue2, references, sortKey, {
436
437
  aiTable,
@@ -634,8 +635,8 @@ class GroupCalculator {
634
635
  const field = this.fieldsMap[groupField.field_id];
635
636
  if (!field)
636
637
  return;
637
- const prevValue = AITableQueries.getFieldValue(this.aiTable, [previousRecord._id, field._id]);
638
- const currValue = AITableQueries.getFieldValue(this.aiTable, [record._id, field._id]);
638
+ const prevValue = AITableQueries.getFieldValue(this.aiTable, [previousRecord._id, field._id], previousRecord);
639
+ const currValue = AITableQueries.getFieldValue(this.aiTable, [record._id, field._id], record);
639
640
  const fieldModel = FieldModelMap[field.type];
640
641
  if (!fieldModel)
641
642
  return;
@@ -762,7 +763,7 @@ class GroupCalculator {
762
763
  const groupId = this.generateGroupId(groupField.field_id, depth, breakpointIndex);
763
764
  const isParentCollapsed = this.isParentGroupCollapsed(depth, recordIndex);
764
765
  if (!isParentCollapsed) {
765
- const groupValue = AITableQueries.getFieldValue(this.aiTable, [record._id, field._id]);
766
+ const groupValue = AITableQueries.getFieldValue(this.aiTable, [record._id, field._id], record);
766
767
  const recordRange = this.calculateGroupRecordRange(groupField.field_id, breakpointIndex, totalRecords);
767
768
  groupTabRows.push({
768
769
  type: AITableRowType.group,
@@ -822,8 +823,8 @@ class GroupCalculator {
822
823
  }
823
824
  }
824
825
 
825
- function getParentLinearRowGroups(aiTable, targetId) {
826
- const linearRows = aiTable.context.linearRows();
826
+ function getParentLinearRowGroups(aiTable, targetId, linearRows) {
827
+ linearRows = linearRows || aiTable.context.linearRows();
827
828
  const targetIndex = aiTable.context.visibleRowsIndexMap().get(targetId);
828
829
  if (targetIndex === -1) {
829
830
  return [];
@@ -841,57 +842,113 @@ function getParentLinearRowGroups(aiTable, targetId) {
841
842
  }
842
843
  }
843
844
  }
844
- return parentGroups;
845
+ return parentGroups.reverse();
845
846
  }
846
-
847
- function buildRecordsByView(aiTable, records, fields, activeView, sortKeysMap) {
848
- const filteredRecords = getFilteredRecords(aiTable, records, fields, activeView);
849
- const sorts = buildSorts(activeView);
850
- return sortRecordsByConditions(aiTable, filteredRecords, activeView, sorts, sortKeysMap);
847
+ function isSameParentGroup(aiTable, recordId, originGroupLinearRows, tmpNewGroupLinearRows) {
848
+ const originalParentLinearRowGroups = getParentLinearRowGroups(aiTable, recordId, originGroupLinearRows);
849
+ const newParentLinearRowGroups = getParentLinearRowGroups(aiTable, recordId, tmpNewGroupLinearRows);
850
+ return ___default.join(___default.map(originalParentLinearRowGroups, 'groupValue'), ':') !== ___default.join(___default.map(newParentLinearRowGroups, 'groupValue'), ':');
851
851
  }
852
- function buildFieldsByView(aiTable, fields, activeView) {
853
- const sortFields = getSortFields(aiTable, fields, activeView);
854
- return buildFieldStatType(sortFields, activeView);
852
+ function getGroupRecordLength(aiTable, recordId, linearRows) {
853
+ const parentLinearRowGroups = getParentLinearRowGroups(aiTable, recordId, linearRows);
854
+ const parentGroup = parentLinearRowGroups[parentLinearRowGroups.length - 1];
855
+ if (parentGroup && parentGroup.range) {
856
+ return parentGroup.range[1] - parentGroup.range[0] + 1;
857
+ }
858
+ return 0;
855
859
  }
856
- function buildGroupLinearRows(aiTable, activeView, records) {
857
- if (aiTable && activeView?.settings?.groups?.length) {
858
- try {
859
- const groups = activeView.settings?.groups;
860
- const collapsedGroupIds = activeView.settings?.collapsed_group_ids;
861
- const calculator = new GroupCalculator(aiTable, groups, collapsedGroupIds);
862
- return calculator.calculateLinearRows(records);
860
+
861
+ function getFieldPositionInView(viewId, fields, path, newPath) {
862
+ const targetPosition = fields[newPath[0]].positions[viewId];
863
+ let newPosition = 0;
864
+ if (path[0] > newPath[0]) {
865
+ const prevPath = newPath[0] - 1;
866
+ if (prevPath >= 0) {
867
+ const targetPrevField = fields[prevPath];
868
+ const targetPrevPosition = targetPrevField.positions[viewId];
869
+ newPosition = (targetPosition + targetPrevPosition) / 2;
863
870
  }
864
- catch (error) {
865
- console.warn('Grouped build failed, using the default build method:', error);
871
+ else {
872
+ const firstField = fields[0];
873
+ newPosition = firstField.positions[viewId] - 0.1;
866
874
  }
867
875
  }
868
- return null;
869
- }
870
- function buildSorts(activeView) {
871
- const groups = activeView.settings?.groups || [];
872
- const isKeepSort = activeView.settings?.is_keep_sort;
873
- if (groups.length > 0 && !isKeepSort) {
874
- return groups.map((group) => ({
875
- sort_by: group.field_id,
876
- direction: group.direction
877
- }));
878
- }
879
- else if (isKeepSort) {
880
- return mergeSorts(activeView);
876
+ else {
877
+ const nextPath = newPath[0] + 1;
878
+ if (fields.length > nextPath) {
879
+ const targetNextField = fields[nextPath];
880
+ const targetNextPosition = targetNextField.positions[viewId];
881
+ newPosition = (targetPosition + targetNextPosition) / 2;
882
+ }
883
+ else {
884
+ const lastField = fields[fields.length - 1];
885
+ const lastPosition = lastField.positions[viewId] + 1;
886
+ newPosition = (targetPosition + lastPosition) / 2;
887
+ }
881
888
  }
882
- return [];
889
+ return newPosition;
883
890
  }
884
- function mergeSorts(activeView) {
885
- const groups = activeView.settings?.groups || [];
886
- const sorts = activeView.settings?.sorts || [];
887
- const groupsAsSorts = map(groups, (group) => ({
888
- sort_by: group.field_id,
889
- direction: group.direction
890
- }));
891
- const mergedSorts = unionBy(groupsAsSorts, sorts, 'sort_by');
892
- return mergedSorts;
891
+ function getFieldsSizeMap(fields, activeView) {
892
+ const fieldsSizeMap = {};
893
+ fields?.forEach((field) => {
894
+ fieldsSizeMap[field._id] = field.widths?.[activeView._id];
895
+ });
896
+ return fieldsSizeMap;
893
897
  }
894
898
 
899
+ var AITableStateI18nKey;
900
+ (function (AITableStateI18nKey) {
901
+ AITableStateI18nKey["copyField"] = "copyField";
902
+ AITableStateI18nKey["removeRecords"] = "removeRecords";
903
+ AITableStateI18nKey["copy"] = "copy";
904
+ AITableStateI18nKey["copySuffix"] = "copySuffix";
905
+ AITableStateI18nKey["paste"] = "paste";
906
+ AITableStateI18nKey["invalidPasteContent"] = "invalidPasteContent";
907
+ AITableStateI18nKey["tableView"] = "tableView";
908
+ AITableStateI18nKey["editField"] = "editField";
909
+ AITableStateI18nKey["removeField"] = "removeField";
910
+ AITableStateI18nKey["pasteOverMaxRecords"] = "pasteOverMaxRecords";
911
+ AITableStateI18nKey["pasteOverMaxFields"] = "pasteOverMaxFields";
912
+ AITableStateI18nKey["insertUpward"] = "insertUpward";
913
+ AITableStateI18nKey["insertDownward"] = "insertDownward";
914
+ AITableStateI18nKey["upward"] = "upward";
915
+ AITableStateI18nKey["downward"] = "downward";
916
+ AITableStateI18nKey["freezeToThisColumn"] = "freezeToThisColumn";
917
+ AITableStateI18nKey["restoreDefaultFrozenColumn"] = "restoreDefaultFrozenColumn";
918
+ })(AITableStateI18nKey || (AITableStateI18nKey = {}));
919
+ const AITableStateI18nText = {
920
+ [AITableStateI18nKey.copyField]: '复制列',
921
+ [AITableStateI18nKey.removeRecords]: '删除行',
922
+ [AITableStateI18nKey.copy]: '复制',
923
+ [AITableStateI18nKey.copySuffix]: '副本',
924
+ [AITableStateI18nKey.paste]: '粘贴',
925
+ [AITableStateI18nKey.invalidPasteContent]: '粘贴内容不符合当前类型',
926
+ [AITableStateI18nKey.tableView]: '表格视图',
927
+ [AITableStateI18nKey.editField]: '编辑列',
928
+ [AITableStateI18nKey.removeField]: '删除列',
929
+ [AITableStateI18nKey.pasteOverMaxRecords]: '粘贴数据超过最大行数',
930
+ [AITableStateI18nKey.pasteOverMaxFields]: '粘贴数据超过最大列数',
931
+ [AITableStateI18nKey.insertUpward]: '向上插入',
932
+ [AITableStateI18nKey.insertDownward]: '向下插入',
933
+ [AITableStateI18nKey.upward]: '行',
934
+ [AITableStateI18nKey.downward]: '行',
935
+ [AITableStateI18nKey.freezeToThisColumn]: '冻结至此列',
936
+ [AITableStateI18nKey.restoreDefaultFrozenColumn]: '恢复默认冻结列'
937
+ };
938
+ const getDefaultI18nTextByKey = (key) => {
939
+ return AITableStateI18nText[key] || key;
940
+ };
941
+ const getStateI18nTextByKey = (aiTable, key) => {
942
+ if (aiTable.getI18nTextByKey) {
943
+ const customText = aiTable.getI18nTextByKey(key);
944
+ if (customText) {
945
+ return customText;
946
+ }
947
+ }
948
+ const defaultText = getDefaultI18nTextByKey(key);
949
+ return defaultText;
950
+ };
951
+
895
952
  function setView(aiTable, value, path) {
896
953
  const view = aiTable.views().find((item) => item._id === path[0]);
897
954
  if (view) {
@@ -961,127 +1018,6 @@ const ViewActions = {
961
1018
  setViewFrozenField
962
1019
  };
963
1020
 
964
- function buildSetRecordPositionsActon(aiTable, positions, path) {
965
- const action = {
966
- type: ActionName.SetRecordPositions,
967
- positions,
968
- path
969
- };
970
- return action;
971
- }
972
- function setRecordPositions(aiTable, positions, path) {
973
- const operation = buildSetRecordPositionsActon(aiTable, positions, path);
974
- aiTable.apply(operation);
975
- }
976
- const PositionsActions = {
977
- setRecordPositions
978
- };
979
-
980
- var AITableStateI18nKey;
981
- (function (AITableStateI18nKey) {
982
- AITableStateI18nKey["copyField"] = "copyField";
983
- AITableStateI18nKey["removeRecords"] = "removeRecords";
984
- AITableStateI18nKey["copy"] = "copy";
985
- AITableStateI18nKey["copySuffix"] = "copySuffix";
986
- AITableStateI18nKey["paste"] = "paste";
987
- AITableStateI18nKey["invalidPasteContent"] = "invalidPasteContent";
988
- AITableStateI18nKey["tableView"] = "tableView";
989
- AITableStateI18nKey["editField"] = "editField";
990
- AITableStateI18nKey["removeField"] = "removeField";
991
- AITableStateI18nKey["pasteOverMaxRecords"] = "pasteOverMaxRecords";
992
- AITableStateI18nKey["pasteOverMaxFields"] = "pasteOverMaxFields";
993
- AITableStateI18nKey["insertUpward"] = "insertUpward";
994
- AITableStateI18nKey["insertDownward"] = "insertDownward";
995
- AITableStateI18nKey["upward"] = "upward";
996
- AITableStateI18nKey["downward"] = "downward";
997
- AITableStateI18nKey["freezeToThisColumn"] = "freezeToThisColumn";
998
- AITableStateI18nKey["restoreDefaultFrozenColumn"] = "restoreDefaultFrozenColumn";
999
- })(AITableStateI18nKey || (AITableStateI18nKey = {}));
1000
- const AITableStateI18nText = {
1001
- [AITableStateI18nKey.copyField]: '复制列',
1002
- [AITableStateI18nKey.removeRecords]: '删除行',
1003
- [AITableStateI18nKey.copy]: '复制',
1004
- [AITableStateI18nKey.copySuffix]: '副本',
1005
- [AITableStateI18nKey.paste]: '粘贴',
1006
- [AITableStateI18nKey.invalidPasteContent]: '粘贴内容不符合当前类型',
1007
- [AITableStateI18nKey.tableView]: '表格视图',
1008
- [AITableStateI18nKey.editField]: '编辑列',
1009
- [AITableStateI18nKey.removeField]: '删除列',
1010
- [AITableStateI18nKey.pasteOverMaxRecords]: '粘贴数据超过最大行数',
1011
- [AITableStateI18nKey.pasteOverMaxFields]: '粘贴数据超过最大列数',
1012
- [AITableStateI18nKey.insertUpward]: '向上插入',
1013
- [AITableStateI18nKey.insertDownward]: '向下插入',
1014
- [AITableStateI18nKey.upward]: '行',
1015
- [AITableStateI18nKey.downward]: '行',
1016
- [AITableStateI18nKey.freezeToThisColumn]: '冻结至此列',
1017
- [AITableStateI18nKey.restoreDefaultFrozenColumn]: '恢复默认冻结列'
1018
- };
1019
- const getDefaultI18nTextByKey = (key) => {
1020
- return AITableStateI18nText[key] || key;
1021
- };
1022
- const getStateI18nTextByKey = (aiTable, key) => {
1023
- if (aiTable.getI18nTextByKey) {
1024
- const customText = aiTable.getI18nTextByKey(key);
1025
- if (customText) {
1026
- return customText;
1027
- }
1028
- }
1029
- const defaultText = getDefaultI18nTextByKey(key);
1030
- return defaultText;
1031
- };
1032
-
1033
- function isPathEqual(path, another) {
1034
- return path.length === another.length && path.every((n, i) => n === another[i]);
1035
- }
1036
- function generateCopyName(aiTable, existNames, name) {
1037
- const copyText = getStateI18nTextByKey(aiTable, AITableStateI18nKey.copySuffix);
1038
- let newName = `${name} ${copyText}`;
1039
- let index = 2;
1040
- while (existNames.includes(newName)) {
1041
- newName = `${name} ${copyText} ${index}`;
1042
- index++;
1043
- }
1044
- return newName;
1045
- }
1046
-
1047
- function getFieldPositionInView(viewId, fields, path, newPath) {
1048
- const targetPosition = fields[newPath[0]].positions[viewId];
1049
- let newPosition = 0;
1050
- if (path[0] > newPath[0]) {
1051
- const prevPath = newPath[0] - 1;
1052
- if (prevPath >= 0) {
1053
- const targetPrevField = fields[prevPath];
1054
- const targetPrevPosition = targetPrevField.positions[viewId];
1055
- newPosition = (targetPosition + targetPrevPosition) / 2;
1056
- }
1057
- else {
1058
- const firstField = fields[0];
1059
- newPosition = firstField.positions[viewId] - 0.1;
1060
- }
1061
- }
1062
- else {
1063
- const nextPath = newPath[0] + 1;
1064
- if (fields.length > nextPath) {
1065
- const targetNextField = fields[nextPath];
1066
- const targetNextPosition = targetNextField.positions[viewId];
1067
- newPosition = (targetPosition + targetNextPosition) / 2;
1068
- }
1069
- else {
1070
- const lastField = fields[fields.length - 1];
1071
- const lastPosition = lastField.positions[viewId] + 1;
1072
- newPosition = (targetPosition + lastPosition) / 2;
1073
- }
1074
- }
1075
- return newPosition;
1076
- }
1077
- function getFieldsSizeMap(fields, activeView) {
1078
- const fieldsSizeMap = {};
1079
- fields?.forEach((field) => {
1080
- fieldsSizeMap[field._id] = field.widths?.[activeView._id];
1081
- });
1082
- return fieldsSizeMap;
1083
- }
1084
-
1085
1021
  function getFrozenFieldId(aiTable) {
1086
1022
  try {
1087
1023
  const viewTable = aiTable;
@@ -1348,130 +1284,6 @@ const FieldActions = {
1348
1284
  setFieldStatType
1349
1285
  };
1350
1286
 
1351
- function createPositions(views, activeId, data, index) {
1352
- return createMultiplePositions(views, activeId, data, index)[0];
1353
- }
1354
- function createMultiplePositions(views, activeId, data, targetIndex = data.length - 1, count = 1, isInsertUpward = false) {
1355
- const positionsOfItems = getPositions(activeId, data, targetIndex, count, isInsertUpward);
1356
- const viewsMaxPosition = {};
1357
- views.forEach((view) => {
1358
- viewsMaxPosition[view._id] = getMaxPosition(data, view._id);
1359
- });
1360
- const positions = positionsOfItems.map((itemPositions) => {
1361
- const viewPositions = {};
1362
- views.forEach((view) => {
1363
- if (view._id === activeId) {
1364
- viewPositions[view._id] = itemPositions;
1365
- }
1366
- else {
1367
- viewsMaxPosition[view._id] += 1;
1368
- viewPositions[view._id] = viewsMaxPosition[view._id];
1369
- }
1370
- });
1371
- return viewPositions;
1372
- });
1373
- return positions;
1374
- }
1375
- function getPositions(activeId, data, targetIndex = data.length - 1, count = 1, isInsertBefore = false) {
1376
- let startPosition = data[data.length - 1].positions[activeId];
1377
- let endPosition = startPosition + count + 1;
1378
- if (data[targetIndex]) {
1379
- if (isInsertBefore) {
1380
- endPosition = data[targetIndex].positions[activeId];
1381
- startPosition = targetIndex - 1 >= 0 ? data[targetIndex - 1].positions[activeId] : endPosition - count - 1;
1382
- }
1383
- else {
1384
- startPosition = data[targetIndex].positions[activeId];
1385
- endPosition =
1386
- targetIndex + 1 < data.length
1387
- ? data[targetIndex + 1].positions[activeId]
1388
- : data[targetIndex].positions[activeId] + count + 1;
1389
- }
1390
- }
1391
- const step = (endPosition - startPosition) / (count + 1);
1392
- const positions = ___default.range(startPosition + step, endPosition, step);
1393
- return positions;
1394
- }
1395
- function getPosition(data, activeViewId, index) {
1396
- return getPositions(activeViewId, data, index)[0];
1397
- }
1398
- function getMaxPosition(data, activeViewId) {
1399
- return data.reduce((maxPosition, item) => {
1400
- if (item.positions[activeViewId] > maxPosition) {
1401
- maxPosition = item.positions[activeViewId];
1402
- }
1403
- return maxPosition;
1404
- }, 0);
1405
- }
1406
- function addView(aiTable, type, viewId) {
1407
- const newId = idCreator();
1408
- const shortId = shortIdCreator();
1409
- const views = aiTable.views();
1410
- const allViewNames = views.map((item) => item.name);
1411
- const count = views.length || 0;
1412
- const newViewName = generateNewName(allViewNames, count, getStateI18nTextByKey(aiTable, AITableStateI18nKey.tableView));
1413
- let newView = {
1414
- _id: newId,
1415
- short_id: shortId,
1416
- name: newViewName
1417
- };
1418
- let originViewId = views[views.length - 1]._id;
1419
- if (type === 'duplicate') {
1420
- originViewId = viewId ?? aiTable.activeViewId();
1421
- const copyView = views.find((item) => item._id === originViewId);
1422
- const copyName = copyView.name;
1423
- const copyViewName = generateCopyName(aiTable, allViewNames, copyName);
1424
- newView = {
1425
- ...copyView,
1426
- _id: newId,
1427
- name: copyViewName
1428
- };
1429
- }
1430
- ViewActions.addView(aiTable, originViewId, newView, type === 'duplicate');
1431
- const actions = [];
1432
- aiTable.records().forEach((record, index) => {
1433
- const action = buildSetRecordPositionsActon(aiTable, { [newId]: record.positions[originViewId] }, [index]);
1434
- actions.push(action);
1435
- });
1436
- aiTable.fields().forEach((field) => {
1437
- const action = buildSetFieldAction(aiTable, {
1438
- positions: {
1439
- ...field.positions,
1440
- [newId]: field.positions[originViewId]
1441
- }
1442
- }, [field._id]);
1443
- if (action) {
1444
- actions.push(action);
1445
- }
1446
- });
1447
- aiTable.apply(actions);
1448
- return newView;
1449
- }
1450
- function removeView(aiTable, records, fields, activeViewId) {
1451
- ViewActions.removeView(aiTable, [activeViewId]);
1452
- const actions = [];
1453
- records.forEach((record, index) => {
1454
- const action = buildSetRecordPositionsActon(aiTable, { [activeViewId]: undefined }, [index]);
1455
- actions.push(action);
1456
- });
1457
- fields.forEach((field) => {
1458
- const positions = { ...field.positions };
1459
- delete positions[activeViewId];
1460
- const action = buildSetFieldAction(aiTable, {
1461
- positions
1462
- }, [field._id]);
1463
- if (action) {
1464
- actions.push(action);
1465
- }
1466
- });
1467
- aiTable.apply(actions);
1468
- }
1469
- function sortViews(data) {
1470
- return [...data].sort((a, b) => (a.position ?? data.indexOf(a)) - (b.position ?? data.indexOf(b)));
1471
- }
1472
-
1473
- const FLUSHING = new WeakMap();
1474
-
1475
1287
  const apply = (aiTable, records, fields, views, action) => {
1476
1288
  switch (action.type) {
1477
1289
  case ActionName.UpdateFieldValue: {
@@ -1502,9 +1314,9 @@ const apply = (aiTable, records, fields, views, action) => {
1502
1314
  case ActionName.AddField: {
1503
1315
  const newField = action.field;
1504
1316
  fields.push(newField);
1317
+ const defaultValue = FieldModelMap[newField.type].getDefaultValue();
1505
1318
  records.forEach((item) => {
1506
- item.values[newField._id] =
1507
- action.isDuplicate && action.originId ? item.values[action.originId] : getDefaultFieldValue(action.field);
1319
+ item.values[newField._id] = action.isDuplicate && action.originId ? item.values[action.originId] : defaultValue;
1508
1320
  });
1509
1321
  break;
1510
1322
  }
@@ -1630,6 +1442,22 @@ const GeneralActions = {
1630
1442
  }
1631
1443
  };
1632
1444
 
1445
+ function buildSetRecordPositionsActon(aiTable, positions, path) {
1446
+ const action = {
1447
+ type: ActionName.SetRecordPositions,
1448
+ positions,
1449
+ path
1450
+ };
1451
+ return action;
1452
+ }
1453
+ function setRecordPositions(aiTable, positions, path) {
1454
+ const operation = buildSetRecordPositionsActon(aiTable, positions, path);
1455
+ aiTable.apply(operation);
1456
+ }
1457
+ const PositionsActions = {
1458
+ setRecordPositions
1459
+ };
1460
+
1633
1461
  function updateFieldValues$1(aiTable, options) {
1634
1462
  let operations = [];
1635
1463
  (options || []).forEach((option) => {
@@ -1825,9 +1653,140 @@ const Actions = {
1825
1653
  ...GroupActions
1826
1654
  };
1827
1655
 
1828
- function moveFields(aiTable, options) {
1829
- const { path, newPath } = options;
1830
- Actions.moveField(aiTable, path, newPath);
1656
+ function isPathEqual(path, another) {
1657
+ return path.length === another.length && path.every((n, i) => n === another[i]);
1658
+ }
1659
+ function generateCopyName(aiTable, existNames, name) {
1660
+ const copyText = getStateI18nTextByKey(aiTable, AITableStateI18nKey.copySuffix);
1661
+ let newName = `${name} ${copyText}`;
1662
+ let index = 2;
1663
+ while (existNames.includes(newName)) {
1664
+ newName = `${name} ${copyText} ${index}`;
1665
+ index++;
1666
+ }
1667
+ return newName;
1668
+ }
1669
+
1670
+ function createPositions(views, activeId, data, index) {
1671
+ return createMultiplePositions(views, activeId, data, index)[0];
1672
+ }
1673
+ function createMultiplePositions(views, activeId, data, targetIndex = data.length - 1, count = 1, isInsertUpward = false) {
1674
+ const positionsOfItems = getPositions(activeId, data, targetIndex, count, isInsertUpward);
1675
+ const viewsMaxPosition = {};
1676
+ views.forEach((view) => {
1677
+ viewsMaxPosition[view._id] = getMaxPosition(data, view._id);
1678
+ });
1679
+ const positions = positionsOfItems.map((itemPositions) => {
1680
+ const viewPositions = {};
1681
+ views.forEach((view) => {
1682
+ if (view._id === activeId) {
1683
+ viewPositions[view._id] = itemPositions;
1684
+ }
1685
+ else {
1686
+ viewsMaxPosition[view._id] += 1;
1687
+ viewPositions[view._id] = viewsMaxPosition[view._id];
1688
+ }
1689
+ });
1690
+ return viewPositions;
1691
+ });
1692
+ return positions;
1693
+ }
1694
+ function getPositions(activeId, data, targetIndex = data.length - 1, count = 1, isInsertBefore = false) {
1695
+ let startPosition = data[data.length - 1].positions[activeId];
1696
+ let endPosition = startPosition + count + 1;
1697
+ if (data[targetIndex]) {
1698
+ if (isInsertBefore) {
1699
+ endPosition = data[targetIndex].positions[activeId];
1700
+ startPosition = targetIndex - 1 >= 0 ? data[targetIndex - 1].positions[activeId] : endPosition - count - 1;
1701
+ }
1702
+ else {
1703
+ startPosition = data[targetIndex].positions[activeId];
1704
+ endPosition =
1705
+ targetIndex + 1 < data.length
1706
+ ? data[targetIndex + 1].positions[activeId]
1707
+ : data[targetIndex].positions[activeId] + count + 1;
1708
+ }
1709
+ }
1710
+ const step = (endPosition - startPosition) / (count + 1);
1711
+ const positions = ___default.range(startPosition + step, endPosition, step);
1712
+ return positions;
1713
+ }
1714
+ function getPosition(data, activeViewId, index) {
1715
+ return getPositions(activeViewId, data, index)[0];
1716
+ }
1717
+ function getMaxPosition(data, activeViewId) {
1718
+ return data.reduce((maxPosition, item) => {
1719
+ if (item.positions[activeViewId] > maxPosition) {
1720
+ maxPosition = item.positions[activeViewId];
1721
+ }
1722
+ return maxPosition;
1723
+ }, 0);
1724
+ }
1725
+ function addView(aiTable, type, viewId) {
1726
+ const newId = idCreator();
1727
+ const shortId = shortIdCreator();
1728
+ const views = aiTable.views();
1729
+ const allViewNames = views.map((item) => item.name);
1730
+ const count = views.length || 0;
1731
+ const newViewName = generateNewName(allViewNames, count, getStateI18nTextByKey(aiTable, AITableStateI18nKey.tableView));
1732
+ let newView = {
1733
+ _id: newId,
1734
+ short_id: shortId,
1735
+ name: newViewName
1736
+ };
1737
+ let originViewId = views[views.length - 1]._id;
1738
+ if (type === 'duplicate') {
1739
+ originViewId = viewId ?? aiTable.activeViewId();
1740
+ const copyView = views.find((item) => item._id === originViewId);
1741
+ const copyName = copyView.name;
1742
+ const copyViewName = generateCopyName(aiTable, allViewNames, copyName);
1743
+ newView = {
1744
+ ...copyView,
1745
+ _id: newId,
1746
+ name: copyViewName
1747
+ };
1748
+ }
1749
+ ViewActions.addView(aiTable, originViewId, newView, type === 'duplicate');
1750
+ const actions = [];
1751
+ aiTable.records().forEach((record, index) => {
1752
+ const action = buildSetRecordPositionsActon(aiTable, { [newId]: record.positions[originViewId] }, [index]);
1753
+ actions.push(action);
1754
+ });
1755
+ aiTable.fields().forEach((field) => {
1756
+ const action = buildSetFieldAction(aiTable, {
1757
+ positions: {
1758
+ ...field.positions,
1759
+ [newId]: field.positions[originViewId]
1760
+ }
1761
+ }, [field._id]);
1762
+ if (action) {
1763
+ actions.push(action);
1764
+ }
1765
+ });
1766
+ aiTable.apply(actions);
1767
+ return newView;
1768
+ }
1769
+ function removeView(aiTable, records, fields, activeViewId) {
1770
+ ViewActions.removeView(aiTable, [activeViewId]);
1771
+ const actions = [];
1772
+ records.forEach((record, index) => {
1773
+ const action = buildSetRecordPositionsActon(aiTable, { [activeViewId]: undefined }, [index]);
1774
+ actions.push(action);
1775
+ });
1776
+ fields.forEach((field) => {
1777
+ const positions = { ...field.positions };
1778
+ delete positions[activeViewId];
1779
+ const action = buildSetFieldAction(aiTable, {
1780
+ positions
1781
+ }, [field._id]);
1782
+ if (action) {
1783
+ actions.push(action);
1784
+ }
1785
+ });
1786
+ aiTable.apply(actions);
1787
+ }
1788
+ function sortViews(data) {
1789
+ return [...data].sort((a, b) => (a.position ?? data.indexOf(a)) - (b.position ?? data.indexOf(b)));
1831
1790
  }
1832
1791
 
1833
1792
  function findNextRecordForTargetInOriginalRecords(aiTable, targetRecordId) {
@@ -1964,87 +1923,12 @@ function getPrevRecordIdByAddGroupId(aiTable, groupId) {
1964
1923
  }
1965
1924
  return null;
1966
1925
  }
1967
-
1968
- function moveRecords(aiTable, options, updatedInfo) {
1969
- const activeViewId = aiTable.activeViewId();
1970
- const activeView = aiTable.views().find((view) => view._id === activeViewId);
1971
- const { recordIds, afterRecordId, beforeRecordId } = options;
1972
- const originalRecords = aiTable.records();
1973
- const recordsIndexMap = new Map(originalRecords.map((row, index) => [row._id, index]));
1974
- const sourceRecords = [];
1975
- recordIds.forEach((id) => {
1976
- const index = recordsIndexMap.get(id);
1977
- if (index === undefined) {
1978
- throw new Error(`Record with id ${id} not found`);
1979
- }
1980
- sourceRecords.push(originalRecords[index]);
1981
- });
1982
- let { targetPosition, prevPosition } = getPositionByAfterOrBeforeRecordId(aiTable, {
1983
- afterRecordId,
1984
- beforeRecordId
1926
+ function buildRecordsWithWillMoveRecords(records, willMoveRecordsMap) {
1927
+ return records.map((record) => {
1928
+ return (willMoveRecordsMap.get(record._id) || record);
1985
1929
  });
1986
- const groups = activeView.settings?.groups;
1987
- let needCopyGroupValuesMap = null;
1988
- if (groups?.length && (afterRecordId || beforeRecordId)) {
1989
- needCopyGroupValuesMap = getParentGroupValuesByGroupId(aiTable, (afterRecordId || beforeRecordId));
1990
- }
1991
- // 勾选多行顺序可能不一致,需要排序
1992
- const sortedSourceRecords = sortByViewPosition(sourceRecords, activeView);
1993
- let nextPosition = (prevPosition + targetPosition) / 2;
1994
- sortedSourceRecords.forEach((record) => {
1995
- const sourceIndex = recordsIndexMap.get(record._id);
1996
- if (sourceIndex === undefined) {
1997
- throw new Error(`Record with id ${record._id} not found`);
1998
- }
1999
- if (groups?.length && needCopyGroupValuesMap) {
2000
- const updateFieldValues = [];
2001
- groups.forEach((group) => {
2002
- updateFieldValues.push({
2003
- path: [record._id, group.field_id],
2004
- value: needCopyGroupValuesMap[group.field_id]
2005
- });
2006
- });
2007
- Actions.updateFieldValues(aiTable, updateFieldValues);
2008
- }
2009
- Actions.setRecordPositions(aiTable, { [activeViewId]: nextPosition }, [sourceIndex]);
2010
- prevPosition = nextPosition;
2011
- nextPosition = (prevPosition + targetPosition) / 2;
2012
- });
2013
- }
2014
-
2015
- function addFields(aiTable, options) {
2016
- const { defaultValue, isDuplicate, originId } = options;
2017
- const fields = aiTable.gridData().fields;
2018
- const fieldsMap = aiTable.fieldsMap();
2019
- const activeViewId = aiTable.activeViewId();
2020
- const newField = { ...defaultValue };
2021
- if (fieldsMap[newField._id]) {
2022
- newField._id = idCreator();
2023
- }
2024
- if (isDuplicate) {
2025
- const currentFieldIndex = fields.findIndex((item) => item._id === originId);
2026
- newField.positions = {
2027
- ...newField.positions,
2028
- [activeViewId]: getPosition(fields, activeViewId, currentFieldIndex)
2029
- };
2030
- }
2031
- else {
2032
- newField.positions = createPositions(aiTable.views(), aiTable.activeViewId(), aiTable.gridData().fields);
2033
- }
2034
- Actions.addField(aiTable, newField, originId, isDuplicate);
2035
1930
  }
2036
1931
 
2037
- const buildRemoveFieldItem = (aiTable, getUpdatedInfo) => {
2038
- return {
2039
- type: 'removeField',
2040
- name: getStateI18nTextByKey(aiTable, AITableStateI18nKey.removeField),
2041
- icon: 'trash',
2042
- exec: (aiTable, field) => {
2043
- Actions.removeField(aiTable, [field()._id]);
2044
- }
2045
- };
2046
- };
2047
-
2048
1932
  function addRecords(aiTable, trackableEntity, options) {
2049
1933
  options = options || {};
2050
1934
  const newRecords = [];
@@ -2117,13 +2001,8 @@ function getDefaultRecordValues(aiTable, isDuplicate = false, recordId) {
2117
2001
  const activeView = aiTable.viewsMap()[aiTable.activeViewId()];
2118
2002
  const fields = getSortFields(aiTable, aiTable.fields(), activeView);
2119
2003
  fields.map((field) => {
2120
- const customGetDefaultFieldValue = aiTable.context?.aiFieldConfig()?.customFields?.[field.type]?.getDefaultFieldValue;
2121
- if (customGetDefaultFieldValue) {
2122
- newRecordValues[field._id] = customGetDefaultFieldValue(field);
2123
- }
2124
- else {
2125
- newRecordValues[field._id] = getDefaultFieldValue(field);
2126
- }
2004
+ const defaultValue = FieldModelMap[field.type].getDefaultValue();
2005
+ newRecordValues[field._id] = defaultValue;
2127
2006
  });
2128
2007
  const { conditions, condition_logical } = activeView.settings || {};
2129
2008
  if (conditions && conditions.length) {
@@ -2160,12 +2039,98 @@ function updateWillHiddenRecordIds(aiTable, needUpdateOptions, updatedInfo) {
2160
2039
  return value;
2161
2040
  });
2162
2041
  }
2042
+ function updateWillMoveRecords(aiTable, needUpdateOptions, updatedInfo) {
2043
+ const activeView = aiTable.viewsMap()[aiTable.activeViewId()];
2044
+ const groups = activeView.settings?.groups ?? [];
2045
+ const sorts = activeView.settings?.sorts ?? [];
2046
+ const sortFieldSet = new Set();
2047
+ const isKeepSort = activeView.settings?.is_keep_sort;
2048
+ if (isKeepSort) {
2049
+ sorts.forEach((sort) => {
2050
+ sortFieldSet.add(sort.sort_by);
2051
+ });
2052
+ }
2053
+ groups.forEach((group) => {
2054
+ sortFieldSet.add(group.field_id);
2055
+ });
2056
+ const updateRecordsMap = new Map();
2057
+ needUpdateOptions.forEach((option) => {
2058
+ const [recordId, fieldId] = option.path;
2059
+ if (sortFieldSet.has(fieldId)) {
2060
+ let record = updateRecordsMap.get(recordId) || _.cloneDeep(aiTable.recordsMap()[recordId]);
2061
+ record.values[fieldId] = option.value;
2062
+ if (updatedInfo) {
2063
+ record = {
2064
+ ...record,
2065
+ ...updatedInfo
2066
+ };
2067
+ }
2068
+ updateRecordsMap.set(recordId, record);
2069
+ }
2070
+ });
2071
+ if (updateRecordsMap.size === 0) {
2072
+ return;
2073
+ }
2074
+ const visibleRowsIndexMap = aiTable.context.visibleRowsIndexMap();
2075
+ const tmpNewRecords = aiTable.records().map((record) => {
2076
+ const newRecord = updateRecordsMap.get(record._id);
2077
+ if (newRecord) {
2078
+ return newRecord;
2079
+ }
2080
+ return record;
2081
+ });
2082
+ const willMoveRecordMap = new Map();
2083
+ const allSorts = buildSorts(activeView);
2084
+ const tmpNewSortedRecords = sortRecordsByConditions(aiTable, tmpNewRecords, activeView, allSorts);
2085
+ let originGroupLinearRows = aiTable.context.linearRows();
2086
+ const tmpNewGroupLinearRows = buildLinearRows(aiTable, activeView, tmpNewSortedRecords) ?? [];
2087
+ updateRecordsMap.forEach((updateSortFieldRecord, recordId) => {
2088
+ const originalRecordIndex = visibleRowsIndexMap.get(recordId);
2089
+ const newRecordOfIndex = tmpNewGroupLinearRows[originalRecordIndex];
2090
+ if (recordId !== newRecordOfIndex?._id) {
2091
+ const originalRecord = aiTable.recordsWillMove().get(recordId) || aiTable.recordsMap()[recordId];
2092
+ willMoveRecordMap.set(recordId, originalRecord);
2093
+ }
2094
+ else {
2095
+ /**
2096
+ * 处理以下场景
2097
+ * group1 - true
2098
+ * group2 - 1
2099
+ * record1 true 1
2100
+ * record2 true 1
2101
+ *
2102
+ * record1 true --> false 变更后的实际分组为
2103
+ *
2104
+ * group3 - false
2105
+ * group4 - 1
2106
+ * record1 false 1
2107
+ * group1 - true
2108
+ * group2 - 1
2109
+ * record2 true 1
2110
+ *
2111
+ * 或者由下变为上面的数据时同理( record1 false --> true)
2112
+ */
2113
+ const originParentGroupRecordsCount = getGroupRecordLength(aiTable, recordId, originGroupLinearRows);
2114
+ const tmpNewParentGroupRecordsCount = getGroupRecordLength(aiTable, recordId, tmpNewGroupLinearRows);
2115
+ if ((originParentGroupRecordsCount > 1 || tmpNewParentGroupRecordsCount > 1) &&
2116
+ isSameParentGroup(aiTable, recordId, originGroupLinearRows, tmpNewGroupLinearRows)) {
2117
+ const originalRecord = aiTable.recordsWillMove().get(recordId) || aiTable.recordsMap()[recordId];
2118
+ willMoveRecordMap.set(recordId, originalRecord);
2119
+ }
2120
+ else {
2121
+ willMoveRecordMap.delete(recordId);
2122
+ }
2123
+ }
2124
+ });
2125
+ aiTable.recordsWillMove.set(willMoveRecordMap);
2126
+ }
2163
2127
  function updateFieldValues(aiTable, options, updatedInfo) {
2164
2128
  const needUpdateOptions = options.filter((option) => {
2165
2129
  const oldValue = AITableQueries.getFieldValue(aiTable, option.path);
2166
2130
  return !_.isEqual(oldValue, option.value);
2167
2131
  });
2168
2132
  updateWillHiddenRecordIds(aiTable, needUpdateOptions, updatedInfo);
2133
+ updateWillMoveRecords(aiTable, needUpdateOptions, updatedInfo);
2169
2134
  Actions.updateFieldValues(aiTable, needUpdateOptions);
2170
2135
  if (updatedInfo) {
2171
2136
  const needUpdateSystemOptions = needUpdateOptions.map((option) => {
@@ -2178,6 +2143,148 @@ function updateFieldValues(aiTable, options, updatedInfo) {
2178
2143
  }
2179
2144
  }
2180
2145
 
2146
+ function moveRecords(aiTable, options, updatedInfo) {
2147
+ const activeViewId = aiTable.activeViewId();
2148
+ const activeView = aiTable.views().find((view) => view._id === activeViewId);
2149
+ const { recordIds, afterRecordId, beforeRecordId } = options;
2150
+ const originalRecords = aiTable.records();
2151
+ const recordsIndexMap = new Map(originalRecords.map((row, index) => [row._id, index]));
2152
+ const sourceRecords = [];
2153
+ recordIds.forEach((id) => {
2154
+ const index = recordsIndexMap.get(id);
2155
+ if (index === undefined) {
2156
+ throw new Error(`Record with id ${id} not found`);
2157
+ }
2158
+ sourceRecords.push(originalRecords[index]);
2159
+ });
2160
+ let { targetPosition, prevPosition } = getPositionByAfterOrBeforeRecordId(aiTable, {
2161
+ afterRecordId,
2162
+ beforeRecordId
2163
+ });
2164
+ const groups = activeView.settings?.groups;
2165
+ let needCopyGroupValuesMap = null;
2166
+ if (groups?.length && (afterRecordId || beforeRecordId)) {
2167
+ needCopyGroupValuesMap = getParentGroupValuesByGroupId(aiTable, (afterRecordId || beforeRecordId));
2168
+ }
2169
+ // 勾选多行顺序可能不一致,需要排序
2170
+ const sortedSourceRecords = sortByViewPosition(sourceRecords, activeView);
2171
+ let nextPosition = (prevPosition + targetPosition) / 2;
2172
+ sortedSourceRecords.forEach((record) => {
2173
+ const sourceIndex = recordsIndexMap.get(record._id);
2174
+ if (sourceIndex === undefined) {
2175
+ throw new Error(`Record with id ${record._id} not found`);
2176
+ }
2177
+ if (groups?.length && needCopyGroupValuesMap) {
2178
+ const updateFieldValues = [];
2179
+ groups.forEach((group) => {
2180
+ updateFieldValues.push({
2181
+ path: [record._id, group.field_id],
2182
+ value: needCopyGroupValuesMap[group.field_id]
2183
+ });
2184
+ });
2185
+ Actions.updateFieldValues(aiTable, updateFieldValues);
2186
+ }
2187
+ Actions.setRecordPositions(aiTable, { [activeViewId]: nextPosition }, [sourceIndex]);
2188
+ prevPosition = nextPosition;
2189
+ nextPosition = (prevPosition + targetPosition) / 2;
2190
+ });
2191
+ }
2192
+
2193
+ function buildRecordsByView(aiTable, records, fields, activeView, sortKeysMap) {
2194
+ const filteredRecords = getFilteredRecords(aiTable, records, fields, activeView);
2195
+ const sorts = buildSorts(activeView);
2196
+ const renderRecords = buildRecordsWithWillMoveRecords(filteredRecords, aiTable.recordsWillMove());
2197
+ return sortRecordsByConditions(aiTable, renderRecords, activeView, sorts);
2198
+ }
2199
+ function buildFieldsByView(aiTable, fields, activeView) {
2200
+ const sortFields = getSortFields(aiTable, fields, activeView);
2201
+ return buildFieldStatType(sortFields, activeView);
2202
+ }
2203
+ function buildLinearRows(aiTable, activeView, records) {
2204
+ if (aiTable && activeView?.settings?.groups?.length) {
2205
+ try {
2206
+ const groups = activeView.settings?.groups;
2207
+ let collapsedGroupIds = [];
2208
+ if (!aiTable.context.groupCollapseDisabled()) {
2209
+ collapsedGroupIds = activeView.settings?.collapsed_group_ids || [];
2210
+ }
2211
+ const calculator = new GroupCalculator(aiTable, groups, collapsedGroupIds);
2212
+ return calculator.calculateLinearRows(records);
2213
+ }
2214
+ catch (error) {
2215
+ console.warn('Grouped build failed, using the default build method:', error);
2216
+ return null;
2217
+ }
2218
+ }
2219
+ else {
2220
+ return buildNormalLinearRows(records);
2221
+ }
2222
+ }
2223
+ function buildSorts(activeView) {
2224
+ const groups = activeView.settings?.groups || [];
2225
+ const isKeepSort = activeView.settings?.is_keep_sort;
2226
+ if (groups.length > 0 && !isKeepSort) {
2227
+ return groups.map((group) => ({
2228
+ sort_by: group.field_id,
2229
+ direction: group.direction
2230
+ }));
2231
+ }
2232
+ else if (isKeepSort) {
2233
+ return mergeSorts(activeView);
2234
+ }
2235
+ return [];
2236
+ }
2237
+ function mergeSorts(activeView) {
2238
+ const groups = activeView.settings?.groups || [];
2239
+ const sorts = activeView.settings?.sorts || [];
2240
+ const groupsAsSorts = map(groups, (group) => ({
2241
+ sort_by: group.field_id,
2242
+ direction: group.direction
2243
+ }));
2244
+ const mergedSorts = unionBy(groupsAsSorts, sorts, 'sort_by');
2245
+ return mergedSorts;
2246
+ }
2247
+
2248
+ const FLUSHING = new WeakMap();
2249
+
2250
+ function moveFields(aiTable, options) {
2251
+ const { path, newPath } = options;
2252
+ Actions.moveField(aiTable, path, newPath);
2253
+ }
2254
+
2255
+ function addFields(aiTable, options) {
2256
+ const { defaultValue, isDuplicate, originId } = options;
2257
+ const fields = aiTable.gridData().fields;
2258
+ const fieldsMap = aiTable.fieldsMap();
2259
+ const activeViewId = aiTable.activeViewId();
2260
+ const newField = { ...defaultValue };
2261
+ if (fieldsMap[newField._id]) {
2262
+ newField._id = idCreator();
2263
+ }
2264
+ if (isDuplicate) {
2265
+ const currentFieldIndex = fields.findIndex((item) => item._id === originId);
2266
+ newField.positions = {
2267
+ ...newField.positions,
2268
+ [activeViewId]: getPosition(fields, activeViewId, currentFieldIndex)
2269
+ };
2270
+ }
2271
+ else {
2272
+ newField.positions = createPositions(aiTable.views(), aiTable.activeViewId(), aiTable.gridData().fields);
2273
+ }
2274
+ Actions.addField(aiTable, newField, originId, isDuplicate);
2275
+ }
2276
+
2277
+ const buildRemoveFieldItem = (aiTable, getUpdatedInfo) => {
2278
+ return {
2279
+ type: 'removeField',
2280
+ name: getStateI18nTextByKey(aiTable, AITableStateI18nKey.removeField),
2281
+ icon: 'trash',
2282
+ exec: (aiTable, field) => {
2283
+ Actions.removeField(aiTable, [field()._id]);
2284
+ }
2285
+ };
2286
+ };
2287
+
2181
2288
  const withState = (aiTable) => {
2182
2289
  const viewTable = aiTable;
2183
2290
  viewTable.actions = [];
@@ -2405,5 +2512,5 @@ const VIEW_ACTIONS = [ActionName.SetView, ActionName.AddView, ActionName.RemoveV
2405
2512
  * Generated bundle index. Do not edit.
2406
2513
  */
2407
2514
 
2408
- export { AITableStateI18nKey, AITableStateI18nText, Actions, CopyCellsItem, CopyFieldPropertyItem, DividerMenuItem, EditFieldPropertyItem, FLUSHING, GroupCalculator, InsertDownwardRecords, InsertUpwardRecords, PasteCellsItem, RemoveRecordsItem, UndoManagerService, VIEW_ACTIONS, YjsAITable, actionMappers, addFields, addRecords, addView, applyActionOps, applyActions, applyEvents, applyYjsEvents, buildFieldsByView, buildGroupLinearRows, buildRecordsByView, buildRemoveFieldItem, buildSetFieldAction, buildSetRecordPositionsActon, buildSorts, calculateAdaptiveFrozenColumnCount, checkConditions, createMultiplePositions, createPositions, createSharedType, doFilter, findNextRecordForTargetInOriginalRecords, findPrevRecordForTargetInOriginalRecords, freezeToThisColumn, generateCopyName, getDataBySharedType, getDefaultI18nTextByKey, getDefaultRecordDataByFilter, getDefaultRecordValues, getFieldPositionInView, getFieldsSizeMap, getFilteredRecords, getFrozenFieldId, getMaxPosition, getNewRecordsPosition, getParentGroupValuesByGroupId, getParentLinearRowGroups, getPosition, getPositionByAfterOrBeforeRecordId, getPositions, getPrevRecordIdByAddGroupId, getRecordsBySharedJson, getSharedTypeByData, getSortFields, getStateI18nTextByKey, isPathEqual, mergeSorts, moveFields, moveRecords, removeView, restoreDefaultFrozenColumn, sortRecordsByConditions, sortViews, toSharedType, translateYjsEvent, updateFieldAndValues, updateFieldValues, withState };
2515
+ export { AITableStateI18nKey, AITableStateI18nText, Actions, CopyCellsItem, CopyFieldPropertyItem, DividerMenuItem, EditFieldPropertyItem, FLUSHING, GroupCalculator, InsertDownwardRecords, InsertUpwardRecords, PasteCellsItem, RemoveRecordsItem, UndoManagerService, VIEW_ACTIONS, YjsAITable, actionMappers, addFields, addRecords, addView, applyActionOps, applyActions, applyEvents, applyYjsEvents, buildFieldsByView, buildLinearRows, buildRecordsByView, buildRecordsWithWillMoveRecords, buildRemoveFieldItem, buildSetFieldAction, buildSetRecordPositionsActon, buildSorts, calculateAdaptiveFrozenColumnCount, checkConditions, createMultiplePositions, createPositions, createSharedType, doFilter, findNextRecordForTargetInOriginalRecords, findPrevRecordForTargetInOriginalRecords, freezeToThisColumn, generateCopyName, getDataBySharedType, getDefaultI18nTextByKey, getDefaultRecordDataByFilter, getDefaultRecordValues, getFieldPositionInView, getFieldsSizeMap, getFilteredRecords, getFrozenFieldId, getGroupRecordLength, getMaxPosition, getNewRecordsPosition, getParentGroupValuesByGroupId, getParentLinearRowGroups, getPosition, getPositionByAfterOrBeforeRecordId, getPositions, getPrevRecordIdByAddGroupId, getRecordsBySharedJson, getSharedTypeByData, getSortFields, getStateI18nTextByKey, isPathEqual, isSameParentGroup, mergeSorts, moveFields, moveRecords, removeView, restoreDefaultFrozenColumn, sortRecordsByConditions, sortViews, toSharedType, translateYjsEvent, updateFieldAndValues, updateFieldValues, withState };
2409
2516
  //# sourceMappingURL=ai-table-state.mjs.map