@ai-table/state 0.1.29 → 0.1.31-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,14 +638,101 @@ function getSortFields(aiTable, fields, activeView) {
638
638
  return sortByViewPosition(fields, activeView);
639
639
  }
640
640
 
641
+ function buildFieldStatType(data, activeView) {
642
+ return data.map((field, index) => {
643
+ const fieldStatType = field.fieldStatTypes?.[activeView._id];
644
+ if (fieldStatType) {
645
+ return {
646
+ ...field,
647
+ stat_type: fieldStatType
648
+ };
649
+ }
650
+ else {
651
+ return field;
652
+ }
653
+ });
654
+ }
655
+
641
656
  function buildRecordsByView(aiTable, records, fields, activeView, sortKeysMap) {
642
657
  const filteredRecords = getFilteredRecords(aiTable, records, fields, activeView);
643
658
  return getSortRecords(aiTable, filteredRecords, activeView, sortKeysMap);
644
659
  }
645
660
  function buildFieldsByView(aiTable, fields, activeView) {
646
- return getSortFields(aiTable, fields, activeView);
661
+ const sortFields = getSortFields(aiTable, fields, activeView);
662
+ return buildFieldStatType(sortFields, activeView);
647
663
  }
648
664
 
665
+ function setView(aiTable, value, path) {
666
+ const view = aiTable.views().find((item) => item._id === path[0]);
667
+ if (view) {
668
+ const properties = {};
669
+ const newProperties = {};
670
+ for (const key in value) {
671
+ const k = key;
672
+ if (JSON.stringify(view[k]) !== JSON.stringify(value[k])) {
673
+ if (view.hasOwnProperty(k)) {
674
+ properties[k] = view[k];
675
+ }
676
+ if (newProperties[k] !== null) {
677
+ newProperties[k] = value[k];
678
+ }
679
+ }
680
+ }
681
+ const operation = {
682
+ type: ActionName.SetView,
683
+ properties,
684
+ newProperties,
685
+ path
686
+ };
687
+ aiTable.apply(operation);
688
+ }
689
+ }
690
+ function addView$1(aiTable, originId, newView, isDuplicate) {
691
+ const views = sortViews(aiTable.views());
692
+ const currentIndex = views.findIndex((item) => item._id === originId);
693
+ if (isDuplicate) {
694
+ const nextIndex = currentIndex + 1;
695
+ newView.position = ((views[currentIndex]?.position ?? currentIndex) + (views[nextIndex]?.position ?? nextIndex)) / 2;
696
+ }
697
+ else {
698
+ newView.position = currentIndex + 1;
699
+ }
700
+ const operation = {
701
+ type: ActionName.AddView,
702
+ view: newView,
703
+ isDuplicate
704
+ };
705
+ aiTable.apply(operation);
706
+ }
707
+ function removeView$1(aiTable, path) {
708
+ const operation = {
709
+ type: ActionName.RemoveView,
710
+ path
711
+ };
712
+ aiTable.apply(operation);
713
+ }
714
+ const ViewActions = {
715
+ setView,
716
+ addView: addView$1,
717
+ removeView: removeView$1
718
+ };
719
+
720
+ function buildSetRecordPositionsActon(aiTable, positions, path) {
721
+ const action = {
722
+ type: ActionName.SetRecordPositions,
723
+ positions,
724
+ path
725
+ };
726
+ return action;
727
+ }
728
+ function setRecordPositions(aiTable, positions, path) {
729
+ const operation = buildSetRecordPositionsActon(aiTable, positions, path);
730
+ aiTable.apply(operation);
731
+ }
732
+ const PositionsActions = {
733
+ setRecordPositions
734
+ };
735
+
649
736
  function getFieldPositionInView(viewId, fields, path, newPath) {
650
737
  const targetPosition = fields[newPath[0]].positions[viewId];
651
738
  let newPosition = 0;
@@ -712,8 +799,11 @@ function moveField(aiTable, path, newPath) {
712
799
  }
713
800
  function setFieldWidth(aiTable, path, width) {
714
801
  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]);
802
+ setField(aiTable, { widths: { ...field.widths, [aiTable.activeViewId()]: width } }, [field._id]);
803
+ }
804
+ function setFieldStatType(aiTable, path, statType) {
805
+ const field = AITableQueries.getField(aiTable, path);
806
+ setField(aiTable, { fieldStatTypes: { ...field.fieldStatTypes, [aiTable.activeViewId()]: statType } }, [field._id]);
717
807
  }
718
808
  function removeField(aiTable, path) {
719
809
  const operation = {
@@ -759,9 +849,134 @@ const FieldActions = {
759
849
  moveField,
760
850
  removeField,
761
851
  setField,
762
- setFieldWidth
852
+ setFieldWidth,
853
+ setFieldStatType
763
854
  };
764
855
 
856
+ function createPositions(views, activeId, data, index) {
857
+ return createMultiplePositions(views, activeId, data, index)[0];
858
+ }
859
+ function createMultiplePositions(views, activeId, data, targetIndex = data.length - 1, count = 1, isInsertUpward = false) {
860
+ const positionsOfItems = getPositions(activeId, data, targetIndex, count, isInsertUpward);
861
+ const viewsMaxPosition = {};
862
+ views.forEach((view) => {
863
+ viewsMaxPosition[view._id] = getMaxPosition(data, view._id);
864
+ });
865
+ const positions = positionsOfItems.map((itemPositions) => {
866
+ const viewPositions = {};
867
+ views.forEach((view) => {
868
+ if (view._id === activeId) {
869
+ viewPositions[view._id] = itemPositions;
870
+ }
871
+ else {
872
+ viewsMaxPosition[view._id] += 1;
873
+ viewPositions[view._id] = viewsMaxPosition[view._id];
874
+ }
875
+ });
876
+ return viewPositions;
877
+ });
878
+ return positions;
879
+ }
880
+ function getPositions(activeId, data, targetIndex = data.length - 1, count = 1, isInsertBefore = false) {
881
+ let startPosition = data.length - 1;
882
+ let endPosition = startPosition + count + 1;
883
+ if (data[targetIndex]) {
884
+ if (isInsertBefore) {
885
+ endPosition = data[targetIndex].positions[activeId];
886
+ startPosition = targetIndex - 1 >= 0 ? data[targetIndex - 1].positions[activeId] : endPosition - count - 1;
887
+ }
888
+ else {
889
+ startPosition = data[targetIndex].positions[activeId];
890
+ endPosition =
891
+ targetIndex + 1 < data.length
892
+ ? data[targetIndex + 1].positions[activeId]
893
+ : data[targetIndex].positions[activeId] + count + 1;
894
+ }
895
+ }
896
+ const step = (endPosition - startPosition) / (count + 1);
897
+ const positions = ___default.range(startPosition + step, endPosition, step);
898
+ return positions;
899
+ }
900
+ function getPosition(data, activeViewId, index) {
901
+ return getPositions(activeViewId, data, index)[0];
902
+ }
903
+ function getMaxPosition(data, activeViewId) {
904
+ return data.reduce((maxPosition, item) => {
905
+ if (item.positions[activeViewId] > maxPosition) {
906
+ maxPosition = item.positions[activeViewId];
907
+ }
908
+ return maxPosition;
909
+ }, 0);
910
+ }
911
+ function addView(aiTable, type, viewId) {
912
+ const newId = idCreator();
913
+ const shortId = shortIdCreator();
914
+ const views = aiTable.views();
915
+ const allViewNames = views.map((item) => item.name);
916
+ const count = views.length || 0;
917
+ const newViewName = generateNewName(allViewNames, count, getStateI18nTextByKey(aiTable, AITableStateI18nKey.tableView));
918
+ let newView = {
919
+ _id: newId,
920
+ short_id: shortId,
921
+ name: newViewName
922
+ };
923
+ let originViewId = views[views.length - 1]._id;
924
+ if (type === 'duplicate') {
925
+ originViewId = viewId ?? aiTable.activeViewId();
926
+ const copyView = views.find((item) => item._id === originViewId);
927
+ const copyName = copyView.name;
928
+ const copyViewName = generateCopyName(aiTable, allViewNames, copyName);
929
+ newView = {
930
+ ...copyView,
931
+ _id: newId,
932
+ name: copyViewName
933
+ };
934
+ }
935
+ ViewActions.addView(aiTable, originViewId, newView, type === 'duplicate');
936
+ const actions = [];
937
+ aiTable.records().forEach((record, index) => {
938
+ const action = buildSetRecordPositionsActon(aiTable, { [newId]: record.positions[originViewId] }, [index]);
939
+ actions.push(action);
940
+ });
941
+ aiTable.fields().forEach((field) => {
942
+ const action = buildSetFieldAction(aiTable, {
943
+ positions: {
944
+ ...field.positions,
945
+ [newId]: field.positions[originViewId]
946
+ }
947
+ }, [field._id]);
948
+ if (action) {
949
+ actions.push(action);
950
+ }
951
+ });
952
+ aiTable.apply(actions);
953
+ return newView;
954
+ }
955
+ function removeView(aiTable, records, fields, activeViewId) {
956
+ ViewActions.removeView(aiTable, [activeViewId]);
957
+ const actions = [];
958
+ records.forEach((record, index) => {
959
+ const action = buildSetRecordPositionsActon(aiTable, { [activeViewId]: undefined }, [index]);
960
+ actions.push(action);
961
+ });
962
+ fields.forEach((field) => {
963
+ const positions = { ...field.positions };
964
+ delete positions[activeViewId];
965
+ const action = buildSetFieldAction(aiTable, {
966
+ positions
967
+ }, [field._id]);
968
+ if (action) {
969
+ actions.push(action);
970
+ }
971
+ });
972
+ aiTable.apply(actions);
973
+ }
974
+ function sortViews(data) {
975
+ return [...data].sort((a, b) => (a.position ?? data.indexOf(a)) - (b.position ?? data.indexOf(b)));
976
+ }
977
+
978
+ const FLUSHING = new WeakMap();
979
+
765
980
  const apply = (aiTable, records, fields, views, action) => {
766
981
  switch (action.type) {
767
982
  case ActionName.UpdateFieldValue: {
@@ -920,22 +1135,6 @@ const GeneralActions = {
920
1135
  }
921
1136
  };
922
1137
 
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
1138
  function updateFieldValues$1(aiTable, options) {
940
1139
  let operations = [];
941
1140
  (options || []).forEach((option) => {
@@ -1017,61 +1216,6 @@ const RecordActions = {
1017
1216
  updateSystemFieldValues
1018
1217
  };
1019
1218
 
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
1219
  const Actions = {
1076
1220
  ...GeneralActions,
1077
1221
  ...RecordActions,
@@ -1080,126 +1224,52 @@ const Actions = {
1080
1224
  ...PositionsActions
1081
1225
  };
1082
1226
 
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;
1227
+ function moveFields(aiTable, options) {
1228
+ const { path, newPath } = options;
1229
+ Actions.moveField(aiTable, path, newPath);
1106
1230
  }
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
- }
1231
+
1232
+ function moveRecords(aiTable, options, updatedInfo) {
1233
+ const gridRecords = aiTable.gridData().records;
1234
+ const activeViewId = aiTable.activeViewId();
1235
+ const activeView = aiTable.views().find((view) => view._id === activeViewId);
1236
+ const { recordIds, newPath } = options;
1237
+ let targetPosition = 0;
1238
+ let prevPosition = 0;
1239
+ if (newPath[0] === 0) {
1240
+ targetPosition = gridRecords[0].positions[activeViewId];
1241
+ prevPosition = targetPosition - 1;
1122
1242
  }
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
- };
1243
+ else if (newPath[0] >= gridRecords.length) {
1244
+ targetPosition = getMaxPosition(gridRecords, activeViewId) + 1;
1245
+ prevPosition = gridRecords[gridRecords.length - 1].positions[activeViewId];
1161
1246
  }
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);
1247
+ else {
1248
+ targetPosition = gridRecords[newPath[0]].positions[activeViewId];
1249
+ prevPosition = gridRecords[newPath[0] - 1].positions[activeViewId];
1250
+ }
1251
+ const records = aiTable.records();
1252
+ const recordsIndexMap = new Map(records.map((row, index) => [row._id, index]));
1253
+ const sourceRecords = [];
1254
+ recordIds.forEach((idPath) => {
1255
+ const index = recordsIndexMap.get(idPath[0]);
1256
+ if (index === undefined) {
1257
+ throw new Error(`Record with id ${idPath[0]} not found`);
1177
1258
  }
1259
+ sourceRecords.push(records[index]);
1178
1260
  });
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);
1261
+ // 勾选多行顺序可能不一致,需要排序
1262
+ const sortedSourceRecords = sortByViewPosition(sourceRecords, activeView);
1263
+ let nextPosition = (prevPosition + targetPosition) / 2;
1264
+ sortedSourceRecords.forEach((record) => {
1265
+ const sourceIndex = recordsIndexMap.get(record._id);
1266
+ if (sourceIndex === undefined) {
1267
+ throw new Error(`Record with id ${record._id} not found`);
1197
1268
  }
1269
+ Actions.setRecordPositions(aiTable, { [activeViewId]: nextPosition }, [sourceIndex]);
1270
+ prevPosition = nextPosition;
1271
+ nextPosition = (prevPosition + targetPosition) / 2;
1198
1272
  });
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
1273
  }
1204
1274
 
1205
1275
  function addFields(aiTable, options) {
@@ -1326,56 +1396,6 @@ function updateFieldValues(aiTable, options, updatedInfo) {
1326
1396
  }
1327
1397
  }
1328
1398
 
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
1399
  const withState = (aiTable) => {
1380
1400
  const viewTable = aiTable;
1381
1401
  viewTable.actions = [];