@ai-table/state 0.1.29 → 0.1.30

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.
package/action/field.d.ts CHANGED
@@ -1,8 +1,9 @@
1
- import { SetFieldAction, AITableViewField, AITableField, IdPath, NumberPath } from '@ai-table/utils';
1
+ import { SetFieldAction, AITableViewField, AITableField, IdPath, NumberPath, AITableFieldStatType } from '@ai-table/utils';
2
2
  import { AIViewTable } from '../types/ai-table';
3
3
  export declare function addField(aiTable: AIViewTable, field: AITableField, originId?: string, isDuplicate?: boolean): void;
4
4
  export declare function moveField(aiTable: AIViewTable, path: NumberPath, newPath: NumberPath): void;
5
5
  export declare function setFieldWidth(aiTable: AIViewTable, path: IdPath, width: number): void;
6
+ export declare function setFieldStatType(aiTable: AIViewTable, path: IdPath, statType: AITableFieldStatType): void;
6
7
  export declare function removeField(aiTable: AIViewTable, path: IdPath): void;
7
8
  export declare function buildSetFieldAction<T extends AITableViewField = AITableViewField>(aiTable: AIViewTable, value: Partial<T>, path: IdPath): SetFieldAction | null;
8
9
  export declare function setField<T extends AITableViewField = AITableViewField>(aiTable: AIViewTable, value: Partial<T>, path: IdPath): void;
@@ -12,5 +13,6 @@ export declare const FieldActions: {
12
13
  removeField: typeof removeField;
13
14
  setField: typeof setField;
14
15
  setFieldWidth: typeof setFieldWidth;
16
+ setFieldStatType: typeof setFieldStatType;
15
17
  };
16
18
  //# sourceMappingURL=field.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"field.d.ts","sourceRoot":"","sources":["../../../packages/state/src/action/field.ts"],"names":[],"mappings":"AACA,OAAO,EAIH,cAAc,EACd,gBAAgB,EAChB,YAAY,EACZ,MAAM,EACN,UAAU,EACb,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,wBAAgB,QAAQ,CAAC,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,YAAY,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,OAAO,QAe3G;AAED,wBAAgB,SAAS,CAAC,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,UAAU,QASpF;AAED,wBAAgB,aAAa,CAAC,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,QAI9E;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,QAM7D;AAED,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,gBAAgB,GAAG,gBAAgB,EAC7E,OAAO,EAAE,WAAW,EACpB,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,EACjB,IAAI,EAAE,MAAM,GACb,cAAc,GAAG,IAAI,CAyBvB;AAED,wBAAgB,QAAQ,CAAC,CAAC,SAAS,gBAAgB,GAAG,gBAAgB,EAAE,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,QAK5H;AAED,eAAO,MAAM,YAAY;;;;;;CAMxB,CAAC"}
1
+ {"version":3,"file":"field.d.ts","sourceRoot":"","sources":["../../../packages/state/src/action/field.ts"],"names":[],"mappings":"AACA,OAAO,EAIH,cAAc,EACd,gBAAgB,EAChB,YAAY,EACZ,MAAM,EACN,UAAU,EACV,oBAAoB,EACvB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,wBAAgB,QAAQ,CAAC,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,YAAY,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,OAAO,QAe3G;AAED,wBAAgB,SAAS,CAAC,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,UAAU,QASpF;AAED,wBAAgB,aAAa,CAAC,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,QAG9E;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,oBAAoB,QAGlG;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,QAM7D;AAED,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,gBAAgB,GAAG,gBAAgB,EAC7E,OAAO,EAAE,WAAW,EACpB,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,EACjB,IAAI,EAAE,MAAM,GACb,cAAc,GAAG,IAAI,CAyBvB;AAED,wBAAgB,QAAQ,CAAC,CAAC,SAAS,gBAAgB,GAAG,gBAAgB,EAAE,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,QAK5H;AAED,eAAO,MAAM,YAAY;;;;;;;CAOxB,CAAC"}
package/action/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export declare const Actions: {
8
8
  removeField: typeof import("./field").removeField;
9
9
  setField: typeof import("./field").setField;
10
10
  setFieldWidth: typeof import("./field").setFieldWidth;
11
+ setFieldStatType: typeof import("./field").setFieldStatType;
11
12
  addRecord: typeof import("./record").addRecord;
12
13
  addRecords: typeof import("./record").addRecords;
13
14
  removeRecord: typeof import("./record").removeRecord;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../packages/state/src/action/index.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,OAAO;;;;;;;;;;;;;;;;CAMnB,CAAC;AAEF,OAAO,EAAE,4BAA4B,EAAE,MAAM,YAAY,CAAC;AAE1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../packages/state/src/action/index.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,OAAO;;;;;;;;;;;;;;;;;CAMnB,CAAC;AAEF,OAAO,EAAE,4BAA4B,EAAE,MAAM,YAAY,CAAC;AAE1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC"}
@@ -3,10 +3,10 @@ import { UndoManager } from 'yjs';
3
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, 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, getDefaultFieldValue, shortIdCreator, generateNewName, idsCreator, shortIdsCreator, AITableSelectAllState, AI_TABLE_GRID_FIELD_SERVICE_MAP, AITable, buildClipboardData, writeToClipboard, getI18nTextByKey, AITableGridI18nKey, isMac, writeToAITable } from '@ai-table/grid';
7
- import { createDraft, finishDraft } from 'immer';
6
+ import { FieldModelMap, AITableQueries, isSystemField, shortIdCreator, generateNewName, getDefaultFieldValue, idsCreator, shortIdsCreator, AITableSelectAllState, AI_TABLE_GRID_FIELD_SERVICE_MAP, AITable, buildClipboardData, writeToClipboard, getI18nTextByKey, AITableGridI18nKey, isMac, writeToAITable } from '@ai-table/grid';
8
7
  import * as _ from 'lodash';
9
8
  import ___default from 'lodash';
9
+ import { createDraft, finishDraft } from 'immer';
10
10
 
11
11
  const IS_LOCAL = new WeakSet();
12
12
  const IS_REMOTE = new WeakSet();
@@ -638,13 +638,98 @@ function getSortFields(aiTable, fields, activeView) {
638
638
  return sortByViewPosition(fields, activeView);
639
639
  }
640
640
 
641
+ function buildFieldStatType(data, activeView) {
642
+ data.forEach((field, index) => {
643
+ const fieldStatType = field.fieldStatTypes?.[activeView._id];
644
+ if (fieldStatType) {
645
+ data[index] = {
646
+ ...field,
647
+ stat_type: fieldStatType
648
+ };
649
+ }
650
+ });
651
+ }
652
+
641
653
  function buildRecordsByView(aiTable, records, fields, activeView, sortKeysMap) {
642
654
  const filteredRecords = getFilteredRecords(aiTable, records, fields, activeView);
643
655
  return getSortRecords(aiTable, filteredRecords, activeView, sortKeysMap);
644
656
  }
645
657
  function buildFieldsByView(aiTable, fields, activeView) {
646
- return getSortFields(aiTable, fields, activeView);
658
+ const sortFields = getSortFields(aiTable, fields, activeView);
659
+ buildFieldStatType(sortFields, activeView);
660
+ return sortFields;
661
+ }
662
+
663
+ function setView(aiTable, value, path) {
664
+ const view = aiTable.views().find((item) => item._id === path[0]);
665
+ if (view) {
666
+ const properties = {};
667
+ const newProperties = {};
668
+ for (const key in value) {
669
+ const k = key;
670
+ if (JSON.stringify(view[k]) !== JSON.stringify(value[k])) {
671
+ if (view.hasOwnProperty(k)) {
672
+ properties[k] = view[k];
673
+ }
674
+ if (newProperties[k] !== null) {
675
+ newProperties[k] = value[k];
676
+ }
677
+ }
678
+ }
679
+ const operation = {
680
+ type: ActionName.SetView,
681
+ properties,
682
+ newProperties,
683
+ path
684
+ };
685
+ aiTable.apply(operation);
686
+ }
687
+ }
688
+ function addView$1(aiTable, originId, newView, isDuplicate) {
689
+ const views = sortViews(aiTable.views());
690
+ const currentIndex = views.findIndex((item) => item._id === originId);
691
+ if (isDuplicate) {
692
+ const nextIndex = currentIndex + 1;
693
+ newView.position = ((views[currentIndex]?.position ?? currentIndex) + (views[nextIndex]?.position ?? nextIndex)) / 2;
694
+ }
695
+ else {
696
+ newView.position = currentIndex + 1;
697
+ }
698
+ const operation = {
699
+ type: ActionName.AddView,
700
+ view: newView,
701
+ isDuplicate
702
+ };
703
+ aiTable.apply(operation);
704
+ }
705
+ function removeView$1(aiTable, path) {
706
+ const operation = {
707
+ type: ActionName.RemoveView,
708
+ path
709
+ };
710
+ aiTable.apply(operation);
711
+ }
712
+ const ViewActions = {
713
+ setView,
714
+ addView: addView$1,
715
+ removeView: removeView$1
716
+ };
717
+
718
+ function buildSetRecordPositionsActon(aiTable, positions, path) {
719
+ const action = {
720
+ type: ActionName.SetRecordPositions,
721
+ positions,
722
+ path
723
+ };
724
+ return action;
725
+ }
726
+ function setRecordPositions(aiTable, positions, path) {
727
+ const operation = buildSetRecordPositionsActon(aiTable, positions, path);
728
+ aiTable.apply(operation);
647
729
  }
730
+ const PositionsActions = {
731
+ setRecordPositions
732
+ };
648
733
 
649
734
  function getFieldPositionInView(viewId, fields, path, newPath) {
650
735
  const targetPosition = fields[newPath[0]].positions[viewId];
@@ -712,8 +797,11 @@ function moveField(aiTable, path, newPath) {
712
797
  }
713
798
  function setFieldWidth(aiTable, path, width) {
714
799
  const field = AITableQueries.getField(aiTable, path);
715
- const activeView = aiTable.views().find((item) => item._id === aiTable.activeViewId());
716
- setField(aiTable, { widths: { ...field.widths, [activeView._id]: width } }, [field._id]);
800
+ setField(aiTable, { widths: { ...field.widths, [aiTable.activeViewId()]: width } }, [field._id]);
801
+ }
802
+ function setFieldStatType(aiTable, path, statType) {
803
+ const field = AITableQueries.getField(aiTable, path);
804
+ setField(aiTable, { fieldStatTypes: { ...field.fieldStatTypes, [aiTable.activeViewId()]: statType } }, [field._id]);
717
805
  }
718
806
  function removeField(aiTable, path) {
719
807
  const operation = {
@@ -759,9 +847,134 @@ const FieldActions = {
759
847
  moveField,
760
848
  removeField,
761
849
  setField,
762
- setFieldWidth
850
+ setFieldWidth,
851
+ setFieldStatType
763
852
  };
764
853
 
854
+ function createPositions(views, activeId, data, index) {
855
+ return createMultiplePositions(views, activeId, data, index)[0];
856
+ }
857
+ function createMultiplePositions(views, activeId, data, targetIndex = data.length - 1, count = 1, isInsertUpward = false) {
858
+ const positionsOfItems = getPositions(activeId, data, targetIndex, count, isInsertUpward);
859
+ const viewsMaxPosition = {};
860
+ views.forEach((view) => {
861
+ viewsMaxPosition[view._id] = getMaxPosition(data, view._id);
862
+ });
863
+ const positions = positionsOfItems.map((itemPositions) => {
864
+ const viewPositions = {};
865
+ views.forEach((view) => {
866
+ if (view._id === activeId) {
867
+ viewPositions[view._id] = itemPositions;
868
+ }
869
+ else {
870
+ viewsMaxPosition[view._id] += 1;
871
+ viewPositions[view._id] = viewsMaxPosition[view._id];
872
+ }
873
+ });
874
+ return viewPositions;
875
+ });
876
+ return positions;
877
+ }
878
+ function getPositions(activeId, data, targetIndex = data.length - 1, count = 1, isInsertBefore = false) {
879
+ let startPosition = data.length - 1;
880
+ let endPosition = startPosition + count + 1;
881
+ if (data[targetIndex]) {
882
+ if (isInsertBefore) {
883
+ endPosition = data[targetIndex].positions[activeId];
884
+ startPosition = targetIndex - 1 >= 0 ? data[targetIndex - 1].positions[activeId] : endPosition - count - 1;
885
+ }
886
+ else {
887
+ startPosition = data[targetIndex].positions[activeId];
888
+ endPosition =
889
+ targetIndex + 1 < data.length
890
+ ? data[targetIndex + 1].positions[activeId]
891
+ : data[targetIndex].positions[activeId] + count + 1;
892
+ }
893
+ }
894
+ const step = (endPosition - startPosition) / (count + 1);
895
+ const positions = ___default.range(startPosition + step, endPosition, step);
896
+ return positions;
897
+ }
898
+ function getPosition(data, activeViewId, index) {
899
+ return getPositions(activeViewId, data, index)[0];
900
+ }
901
+ function getMaxPosition(data, activeViewId) {
902
+ return data.reduce((maxPosition, item) => {
903
+ if (item.positions[activeViewId] > maxPosition) {
904
+ maxPosition = item.positions[activeViewId];
905
+ }
906
+ return maxPosition;
907
+ }, 0);
908
+ }
909
+ function addView(aiTable, type, viewId) {
910
+ const newId = idCreator();
911
+ const shortId = shortIdCreator();
912
+ const views = aiTable.views();
913
+ const allViewNames = views.map((item) => item.name);
914
+ const count = views.length || 0;
915
+ const newViewName = generateNewName(allViewNames, count, getStateI18nTextByKey(aiTable, AITableStateI18nKey.tableView));
916
+ let newView = {
917
+ _id: newId,
918
+ short_id: shortId,
919
+ name: newViewName
920
+ };
921
+ let originViewId = views[views.length - 1]._id;
922
+ if (type === 'duplicate') {
923
+ originViewId = viewId ?? aiTable.activeViewId();
924
+ const copyView = views.find((item) => item._id === originViewId);
925
+ const copyName = copyView.name;
926
+ const copyViewName = generateCopyName(aiTable, allViewNames, copyName);
927
+ newView = {
928
+ ...copyView,
929
+ _id: newId,
930
+ name: copyViewName
931
+ };
932
+ }
933
+ ViewActions.addView(aiTable, originViewId, newView, type === 'duplicate');
934
+ const actions = [];
935
+ aiTable.records().forEach((record, index) => {
936
+ const action = buildSetRecordPositionsActon(aiTable, { [newId]: record.positions[originViewId] }, [index]);
937
+ actions.push(action);
938
+ });
939
+ aiTable.fields().forEach((field) => {
940
+ const action = buildSetFieldAction(aiTable, {
941
+ positions: {
942
+ ...field.positions,
943
+ [newId]: field.positions[originViewId]
944
+ }
945
+ }, [field._id]);
946
+ if (action) {
947
+ actions.push(action);
948
+ }
949
+ });
950
+ aiTable.apply(actions);
951
+ return newView;
952
+ }
953
+ function removeView(aiTable, records, fields, activeViewId) {
954
+ ViewActions.removeView(aiTable, [activeViewId]);
955
+ const actions = [];
956
+ records.forEach((record, index) => {
957
+ const action = buildSetRecordPositionsActon(aiTable, { [activeViewId]: undefined }, [index]);
958
+ actions.push(action);
959
+ });
960
+ fields.forEach((field) => {
961
+ const positions = { ...field.positions };
962
+ delete positions[activeViewId];
963
+ const action = buildSetFieldAction(aiTable, {
964
+ positions
965
+ }, [field._id]);
966
+ if (action) {
967
+ actions.push(action);
968
+ }
969
+ });
970
+ aiTable.apply(actions);
971
+ }
972
+ function sortViews(data) {
973
+ return [...data].sort((a, b) => (a.position ?? data.indexOf(a)) - (b.position ?? data.indexOf(b)));
974
+ }
975
+
976
+ const FLUSHING = new WeakMap();
977
+
765
978
  const apply = (aiTable, records, fields, views, action) => {
766
979
  switch (action.type) {
767
980
  case ActionName.UpdateFieldValue: {
@@ -920,22 +1133,6 @@ const GeneralActions = {
920
1133
  }
921
1134
  };
922
1135
 
923
- function buildSetRecordPositionsActon(aiTable, positions, path) {
924
- const action = {
925
- type: ActionName.SetRecordPositions,
926
- positions,
927
- path
928
- };
929
- return action;
930
- }
931
- function setRecordPositions(aiTable, positions, path) {
932
- const operation = buildSetRecordPositionsActon(aiTable, positions, path);
933
- aiTable.apply(operation);
934
- }
935
- const PositionsActions = {
936
- setRecordPositions
937
- };
938
-
939
1136
  function updateFieldValues$1(aiTable, options) {
940
1137
  let operations = [];
941
1138
  (options || []).forEach((option) => {
@@ -1017,61 +1214,6 @@ const RecordActions = {
1017
1214
  updateSystemFieldValues
1018
1215
  };
1019
1216
 
1020
- function setView(aiTable, value, path) {
1021
- const view = aiTable.views().find((item) => item._id === path[0]);
1022
- if (view) {
1023
- const properties = {};
1024
- const newProperties = {};
1025
- for (const key in value) {
1026
- const k = key;
1027
- if (JSON.stringify(view[k]) !== JSON.stringify(value[k])) {
1028
- if (view.hasOwnProperty(k)) {
1029
- properties[k] = view[k];
1030
- }
1031
- if (newProperties[k] !== null) {
1032
- newProperties[k] = value[k];
1033
- }
1034
- }
1035
- }
1036
- const operation = {
1037
- type: ActionName.SetView,
1038
- properties,
1039
- newProperties,
1040
- path
1041
- };
1042
- aiTable.apply(operation);
1043
- }
1044
- }
1045
- function addView$1(aiTable, originId, newView, isDuplicate) {
1046
- const views = sortViews(aiTable.views());
1047
- const currentIndex = views.findIndex((item) => item._id === originId);
1048
- if (isDuplicate) {
1049
- const nextIndex = currentIndex + 1;
1050
- newView.position = ((views[currentIndex]?.position ?? currentIndex) + (views[nextIndex]?.position ?? nextIndex)) / 2;
1051
- }
1052
- else {
1053
- newView.position = currentIndex + 1;
1054
- }
1055
- const operation = {
1056
- type: ActionName.AddView,
1057
- view: newView,
1058
- isDuplicate
1059
- };
1060
- aiTable.apply(operation);
1061
- }
1062
- function removeView$1(aiTable, path) {
1063
- const operation = {
1064
- type: ActionName.RemoveView,
1065
- path
1066
- };
1067
- aiTable.apply(operation);
1068
- }
1069
- const ViewActions = {
1070
- setView,
1071
- addView: addView$1,
1072
- removeView: removeView$1
1073
- };
1074
-
1075
1217
  const Actions = {
1076
1218
  ...GeneralActions,
1077
1219
  ...RecordActions,
@@ -1080,126 +1222,52 @@ const Actions = {
1080
1222
  ...PositionsActions
1081
1223
  };
1082
1224
 
1083
- function createPositions(views, activeId, data, index) {
1084
- return createMultiplePositions(views, activeId, data, index)[0];
1085
- }
1086
- function createMultiplePositions(views, activeId, data, targetIndex = data.length - 1, count = 1, isInsertUpward = false) {
1087
- const positionsOfItems = getPositions(activeId, data, targetIndex, count, isInsertUpward);
1088
- const viewsMaxPosition = {};
1089
- views.forEach((view) => {
1090
- viewsMaxPosition[view._id] = getMaxPosition(data, view._id);
1091
- });
1092
- const positions = positionsOfItems.map((itemPositions) => {
1093
- const viewPositions = {};
1094
- views.forEach((view) => {
1095
- if (view._id === activeId) {
1096
- viewPositions[view._id] = itemPositions;
1097
- }
1098
- else {
1099
- viewsMaxPosition[view._id] += 1;
1100
- viewPositions[view._id] = viewsMaxPosition[view._id];
1101
- }
1102
- });
1103
- return viewPositions;
1104
- });
1105
- return positions;
1225
+ function moveFields(aiTable, options) {
1226
+ const { path, newPath } = options;
1227
+ Actions.moveField(aiTable, path, newPath);
1106
1228
  }
1107
- function getPositions(activeId, data, targetIndex = data.length - 1, count = 1, isInsertBefore = false) {
1108
- let startPosition = data.length - 1;
1109
- let endPosition = startPosition + count + 1;
1110
- if (data[targetIndex]) {
1111
- if (isInsertBefore) {
1112
- endPosition = data[targetIndex].positions[activeId];
1113
- startPosition = targetIndex - 1 >= 0 ? data[targetIndex - 1].positions[activeId] : endPosition - count - 1;
1114
- }
1115
- else {
1116
- startPosition = data[targetIndex].positions[activeId];
1117
- endPosition =
1118
- targetIndex + 1 < data.length
1119
- ? data[targetIndex + 1].positions[activeId]
1120
- : data[targetIndex].positions[activeId] + count + 1;
1121
- }
1229
+
1230
+ function moveRecords(aiTable, options, updatedInfo) {
1231
+ const gridRecords = aiTable.gridData().records;
1232
+ const activeViewId = aiTable.activeViewId();
1233
+ const activeView = aiTable.views().find((view) => view._id === activeViewId);
1234
+ const { recordIds, newPath } = options;
1235
+ let targetPosition = 0;
1236
+ let prevPosition = 0;
1237
+ if (newPath[0] === 0) {
1238
+ targetPosition = gridRecords[0].positions[activeViewId];
1239
+ prevPosition = targetPosition - 1;
1122
1240
  }
1123
- const step = (endPosition - startPosition) / (count + 1);
1124
- const positions = ___default.range(startPosition + step, endPosition, step);
1125
- return positions;
1126
- }
1127
- function getPosition(data, activeViewId, index) {
1128
- return getPositions(activeViewId, data, index)[0];
1129
- }
1130
- function getMaxPosition(data, activeViewId) {
1131
- return data.reduce((maxPosition, item) => {
1132
- if (item.positions[activeViewId] > maxPosition) {
1133
- maxPosition = item.positions[activeViewId];
1134
- }
1135
- return maxPosition;
1136
- }, 0);
1137
- }
1138
- function addView(aiTable, type, viewId) {
1139
- const newId = idCreator();
1140
- const shortId = shortIdCreator();
1141
- const views = aiTable.views();
1142
- const allViewNames = views.map((item) => item.name);
1143
- const count = views.length || 0;
1144
- const newViewName = generateNewName(allViewNames, count, getStateI18nTextByKey(aiTable, AITableStateI18nKey.tableView));
1145
- let newView = {
1146
- _id: newId,
1147
- short_id: shortId,
1148
- name: newViewName
1149
- };
1150
- let originViewId = views[views.length - 1]._id;
1151
- if (type === 'duplicate') {
1152
- originViewId = viewId ?? aiTable.activeViewId();
1153
- const copyView = views.find((item) => item._id === originViewId);
1154
- const copyName = copyView.name;
1155
- const copyViewName = generateCopyName(aiTable, allViewNames, copyName);
1156
- newView = {
1157
- ...copyView,
1158
- _id: newId,
1159
- name: copyViewName
1160
- };
1241
+ else if (newPath[0] >= gridRecords.length) {
1242
+ targetPosition = getMaxPosition(gridRecords, activeViewId) + 1;
1243
+ prevPosition = gridRecords[gridRecords.length - 1].positions[activeViewId];
1161
1244
  }
1162
- ViewActions.addView(aiTable, originViewId, newView, type === 'duplicate');
1163
- const actions = [];
1164
- aiTable.records().forEach((record, index) => {
1165
- const action = buildSetRecordPositionsActon(aiTable, { [newId]: record.positions[originViewId] }, [index]);
1166
- actions.push(action);
1167
- });
1168
- aiTable.fields().forEach((field) => {
1169
- const action = buildSetFieldAction(aiTable, {
1170
- positions: {
1171
- ...field.positions,
1172
- [newId]: field.positions[originViewId]
1173
- }
1174
- }, [field._id]);
1175
- if (action) {
1176
- actions.push(action);
1245
+ else {
1246
+ targetPosition = gridRecords[newPath[0]].positions[activeViewId];
1247
+ prevPosition = gridRecords[newPath[0] - 1].positions[activeViewId];
1248
+ }
1249
+ const records = aiTable.records();
1250
+ const recordsIndexMap = new Map(records.map((row, index) => [row._id, index]));
1251
+ const sourceRecords = [];
1252
+ recordIds.forEach((idPath) => {
1253
+ const index = recordsIndexMap.get(idPath[0]);
1254
+ if (index === undefined) {
1255
+ throw new Error(`Record with id ${idPath[0]} not found`);
1177
1256
  }
1257
+ sourceRecords.push(records[index]);
1178
1258
  });
1179
- aiTable.apply(actions);
1180
- return newView;
1181
- }
1182
- function removeView(aiTable, records, fields, activeViewId) {
1183
- ViewActions.removeView(aiTable, [activeViewId]);
1184
- const actions = [];
1185
- records.forEach((record, index) => {
1186
- const action = buildSetRecordPositionsActon(aiTable, { [activeViewId]: undefined }, [index]);
1187
- actions.push(action);
1188
- });
1189
- fields.forEach((field) => {
1190
- const positions = { ...field.positions };
1191
- delete positions[activeViewId];
1192
- const action = buildSetFieldAction(aiTable, {
1193
- positions
1194
- }, [field._id]);
1195
- if (action) {
1196
- actions.push(action);
1259
+ // 勾选多行顺序可能不一致,需要排序
1260
+ const sortedSourceRecords = sortByViewPosition(sourceRecords, activeView);
1261
+ let nextPosition = (prevPosition + targetPosition) / 2;
1262
+ sortedSourceRecords.forEach((record) => {
1263
+ const sourceIndex = recordsIndexMap.get(record._id);
1264
+ if (sourceIndex === undefined) {
1265
+ throw new Error(`Record with id ${record._id} not found`);
1197
1266
  }
1267
+ Actions.setRecordPositions(aiTable, { [activeViewId]: nextPosition }, [sourceIndex]);
1268
+ prevPosition = nextPosition;
1269
+ nextPosition = (prevPosition + targetPosition) / 2;
1198
1270
  });
1199
- aiTable.apply(actions);
1200
- }
1201
- function sortViews(data) {
1202
- return [...data].sort((a, b) => (a.position ?? data.indexOf(a)) - (b.position ?? data.indexOf(b)));
1203
1271
  }
1204
1272
 
1205
1273
  function addFields(aiTable, options) {
@@ -1326,56 +1394,6 @@ function updateFieldValues(aiTable, options, updatedInfo) {
1326
1394
  }
1327
1395
  }
1328
1396
 
1329
- const FLUSHING = new WeakMap();
1330
-
1331
- function moveFields(aiTable, options) {
1332
- const { path, newPath } = options;
1333
- Actions.moveField(aiTable, path, newPath);
1334
- }
1335
-
1336
- function moveRecords(aiTable, options, updatedInfo) {
1337
- const gridRecords = aiTable.gridData().records;
1338
- const activeViewId = aiTable.activeViewId();
1339
- const activeView = aiTable.views().find((view) => view._id === activeViewId);
1340
- const { recordIds, newPath } = options;
1341
- let targetPosition = 0;
1342
- let prevPosition = 0;
1343
- if (newPath[0] === 0) {
1344
- targetPosition = gridRecords[0].positions[activeViewId];
1345
- prevPosition = targetPosition - 1;
1346
- }
1347
- else if (newPath[0] >= gridRecords.length) {
1348
- targetPosition = getMaxPosition(gridRecords, activeViewId) + 1;
1349
- prevPosition = gridRecords[gridRecords.length - 1].positions[activeViewId];
1350
- }
1351
- else {
1352
- targetPosition = gridRecords[newPath[0]].positions[activeViewId];
1353
- prevPosition = gridRecords[newPath[0] - 1].positions[activeViewId];
1354
- }
1355
- const records = aiTable.records();
1356
- const recordsIndexMap = new Map(records.map((row, index) => [row._id, index]));
1357
- const sourceRecords = [];
1358
- recordIds.forEach((idPath) => {
1359
- const index = recordsIndexMap.get(idPath[0]);
1360
- if (index === undefined) {
1361
- throw new Error(`Record with id ${idPath[0]} not found`);
1362
- }
1363
- sourceRecords.push(records[index]);
1364
- });
1365
- // 勾选多行顺序可能不一致,需要排序
1366
- const sortedSourceRecords = sortByViewPosition(sourceRecords, activeView);
1367
- let nextPosition = (prevPosition + targetPosition) / 2;
1368
- sortedSourceRecords.forEach((record) => {
1369
- const sourceIndex = recordsIndexMap.get(record._id);
1370
- if (sourceIndex === undefined) {
1371
- throw new Error(`Record with id ${record._id} not found`);
1372
- }
1373
- Actions.setRecordPositions(aiTable, { [activeViewId]: nextPosition }, [sourceIndex]);
1374
- prevPosition = nextPosition;
1375
- nextPosition = (prevPosition + targetPosition) / 2;
1376
- });
1377
- }
1378
-
1379
1397
  const withState = (aiTable) => {
1380
1398
  const viewTable = aiTable;
1381
1399
  viewTable.actions = [];