@ai-table/state 0.3.0 → 0.3.2

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.
@@ -3,7 +3,7 @@ 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, sortByViewPosition, idCreator, SortDirection, AI_TABLE_DEFAULT_MIN_UNFROZEN_WIDTH, AI_TABLE_MIN_FROZEN_COLUMN_COUNT } 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, shortIdCreator, generateNewName, AI_TABLE_GROUP_MAX_LEVEL, idsCreator, shortIdsCreator, closeExpendCell, setSelection, buildNormalLinearRows, AITable, getColumnIndicesSizeMap, AI_TABLE_GRID_FIELD_SERVICE_MAP, clearSelection, isMac } from '@ai-table/grid';
6
+ import { FieldModelMap, AITableQueries, isSystemField, AITableRowType, shortIdCreator, generateNewName, AI_TABLE_GROUP_MAX_LEVEL, idsCreator, shortIdsCreator, closeExpendCell, setSelection, buildNormalLinearRows, AITable, getColumnIndicesSizeMap, getI18nTextByKey, AITableGridI18nKey, generateNewFieldName, AI_TABLE_GRID_FIELD_SERVICE_MAP, clearSelection, isMac } from '@ai-table/grid';
7
7
  import { nanoid } from 'nanoid';
8
8
  import * as _ from 'lodash';
9
9
  import ___default, { map, unionBy } from 'lodash';
@@ -858,75 +858,6 @@ function getGroupRecordLength(aiTable, recordId, linearRows) {
858
858
  return 0;
859
859
  }
860
860
 
861
- function setView(aiTable, value, path) {
862
- const view = aiTable.views().find((item) => item._id === path[0]);
863
- if (view) {
864
- const properties = {};
865
- const newProperties = {};
866
- for (const key in value) {
867
- const k = key;
868
- if (JSON.stringify(view[k]) !== JSON.stringify(value[k])) {
869
- if (view.hasOwnProperty(k)) {
870
- properties[k] = view[k];
871
- }
872
- if (newProperties[k] !== null) {
873
- newProperties[k] = value[k];
874
- }
875
- }
876
- }
877
- const operation = {
878
- type: ActionName.SetView,
879
- properties,
880
- newProperties,
881
- path
882
- };
883
- aiTable.apply(operation);
884
- }
885
- }
886
- function addView$1(aiTable, originId, newView, isDuplicate) {
887
- const views = sortViews(aiTable.views());
888
- const currentIndex = views.findIndex((item) => item._id === originId);
889
- if (isDuplicate) {
890
- const nextIndex = currentIndex + 1;
891
- newView.position = ((views[currentIndex]?.position ?? currentIndex) + (views[nextIndex]?.position ?? nextIndex)) / 2;
892
- }
893
- else {
894
- newView.position = currentIndex + 1;
895
- }
896
- const operation = {
897
- type: ActionName.AddView,
898
- view: newView,
899
- isDuplicate
900
- };
901
- aiTable.apply(operation);
902
- }
903
- function removeView$1(aiTable, path) {
904
- const operation = {
905
- type: ActionName.RemoveView,
906
- path
907
- };
908
- aiTable.apply(operation);
909
- }
910
- function setViewFrozenField(aiTable, frozenFieldId) {
911
- const activeViewId = aiTable.activeViewId();
912
- const activeView = aiTable.viewsMap()[activeViewId];
913
- const currentSettings = activeView.settings || {};
914
- const newSettings = {
915
- ...currentSettings,
916
- frozen_field_id: frozenFieldId
917
- };
918
- if (frozenFieldId === undefined) {
919
- delete newSettings.frozen_field_id;
920
- }
921
- setView(aiTable, { settings: newSettings }, [activeViewId]);
922
- }
923
- const ViewActions = {
924
- setView,
925
- addView: addView$1,
926
- removeView: removeView$1,
927
- setViewFrozenField
928
- };
929
-
930
861
  function addField(aiTable, field, originId, isDuplicate) {
931
862
  const operation = {
932
863
  type: ActionName.AddField,
@@ -943,35 +874,6 @@ function addField(aiTable, field, originId, isDuplicate) {
943
874
  }
944
875
  aiTable.apply(operation);
945
876
  }
946
- function adjustFrozenFieldAfterMove(aiTable, sourceIndex, targetIndex, currentFrozenFieldId) {
947
- if (!currentFrozenFieldId) {
948
- return;
949
- }
950
- const fields = aiTable.gridData().fields;
951
- const currentFrozenFieldIndex = fields.findIndex((field) => field._id === currentFrozenFieldId);
952
- if (currentFrozenFieldIndex === -1) {
953
- return;
954
- }
955
- // 最后冻结列拖动到非冻结区或冻结区,冻结列向左移动
956
- if (sourceIndex === currentFrozenFieldIndex && targetIndex !== currentFrozenFieldIndex) {
957
- const newFrozenFieldIndex = Math.max(0, currentFrozenFieldIndex - 1);
958
- if (newFrozenFieldIndex < fields.length && newFrozenFieldIndex !== currentFrozenFieldIndex) {
959
- const newFrozenField = fields[newFrozenFieldIndex];
960
- setViewFrozenField(aiTable, newFrozenField._id);
961
- }
962
- else {
963
- // 如果没有前一个字段,恢复默认冻结
964
- setViewFrozenField(aiTable, undefined);
965
- }
966
- return;
967
- }
968
- // 冻结区拖动到最后冻结列后面,冻结列是被拖动列
969
- if (sourceIndex < currentFrozenFieldIndex && targetIndex === currentFrozenFieldIndex) {
970
- const newFrozenField = fields[sourceIndex];
971
- setViewFrozenField(aiTable, newFrozenField._id);
972
- return;
973
- }
974
- }
975
877
  function setFieldWidth(aiTable, path, width) {
976
878
  const field = AITableQueries.getField(aiTable, path);
977
879
  setField(aiTable, { widths: { ...field.widths, [aiTable.activeViewId()]: width } }, [field._id]);
@@ -1185,7 +1087,96 @@ const GeneralActions = {
1185
1087
  }
1186
1088
  };
1187
1089
 
1188
- function buildSetRecordPositionsActon(aiTable, positions, path) {
1090
+ function buildSetViewAction(aiTable, value, path) {
1091
+ const view = aiTable.views().find((item) => item._id === path[0]);
1092
+ if (view) {
1093
+ const properties = {};
1094
+ const newProperties = {};
1095
+ for (const key in value) {
1096
+ const k = key;
1097
+ if (JSON.stringify(view[k]) !== JSON.stringify(value[k])) {
1098
+ if (view.hasOwnProperty(k)) {
1099
+ properties[k] = view[k];
1100
+ }
1101
+ if (newProperties[k] !== null) {
1102
+ newProperties[k] = value[k];
1103
+ }
1104
+ }
1105
+ }
1106
+ const operation = {
1107
+ type: ActionName.SetView,
1108
+ properties,
1109
+ newProperties,
1110
+ path
1111
+ };
1112
+ return operation;
1113
+ }
1114
+ return null;
1115
+ }
1116
+ function setView(aiTable, value, path) {
1117
+ const action = buildSetViewAction(aiTable, value, path);
1118
+ if (action) {
1119
+ aiTable.apply(action);
1120
+ }
1121
+ }
1122
+ function addView$1(aiTable, originId, newView, isDuplicate) {
1123
+ const views = sortViews(aiTable.views());
1124
+ const currentIndex = views.findIndex((item) => item._id === originId);
1125
+ if (isDuplicate) {
1126
+ const prev = currentIndex >= 0 ? (views[currentIndex].position ?? currentIndex) : null;
1127
+ const next = currentIndex + 1 < views.length ? (views[currentIndex + 1].position ?? currentIndex + 1) : null;
1128
+ let newPos;
1129
+ if (prev !== null && next === null) {
1130
+ // 复制最后一个插入最后位置
1131
+ newPos = insertAtEnd(prev, 1)[0];
1132
+ }
1133
+ else if (prev !== null && next !== null) {
1134
+ // 插入中间
1135
+ const result = insertBetween(prev, next, 1);
1136
+ if (result.positions.length) {
1137
+ newPos = result.positions[0];
1138
+ }
1139
+ else {
1140
+ PositionsActions.resetAllViewsPositions(aiTable);
1141
+ const reSort = sortViews(aiTable.views());
1142
+ const idx = reSort.findIndex((item) => item._id === originId);
1143
+ const prev2 = idx >= 0 ? (reSort[idx].position ?? idx) : 0;
1144
+ const next2 = idx + 1 < reSort.length ? (reSort[idx + 1].position ?? idx + 1) : null;
1145
+ newPos = next2 === null ? insertAtEnd(prev2, 1)[0] : insertBetween(prev2, next2, 1).positions[0];
1146
+ }
1147
+ }
1148
+ else {
1149
+ const maxPosition = getMaxPosition(views);
1150
+ newPos = insertAtEnd(maxPosition, 1)[0];
1151
+ }
1152
+ newView.position = newPos;
1153
+ }
1154
+ else {
1155
+ const maxPosition = getMaxPosition(views);
1156
+ newView.position = insertAtEnd(maxPosition, 1)[0];
1157
+ }
1158
+ const operation = {
1159
+ type: ActionName.AddView,
1160
+ view: newView,
1161
+ isDuplicate
1162
+ };
1163
+ aiTable.apply(operation);
1164
+ }
1165
+ function removeView$1(aiTable, path) {
1166
+ const operation = {
1167
+ type: ActionName.RemoveView,
1168
+ path
1169
+ };
1170
+ aiTable.apply(operation);
1171
+ }
1172
+ const ViewActions = {
1173
+ setView,
1174
+ addView: addView$1,
1175
+ removeView: removeView$1,
1176
+ buildSetViewAction
1177
+ };
1178
+
1179
+ function buildSetRecordPositionsAction(aiTable, positions, path) {
1189
1180
  const action = {
1190
1181
  type: ActionName.SetRecordPositions,
1191
1182
  positions,
@@ -1202,7 +1193,7 @@ function resetAllRecordsPositions(aiTable) {
1202
1193
  const actions = [];
1203
1194
  const positions = insertAtEnd(0, sortedRecords.length);
1204
1195
  sortedRecords.forEach((record, index) => {
1205
- const action = buildSetRecordPositionsActon(aiTable, { [activeView._id]: positions[index] }, [recordsIndexMap.get(record._id)]);
1196
+ const action = buildSetRecordPositionsAction(aiTable, { [activeView._id]: positions[index] }, [recordsIndexMap.get(record._id)]);
1206
1197
  actions.push(action);
1207
1198
  });
1208
1199
  aiTable.apply(actions);
@@ -1222,14 +1213,27 @@ function resetAllFieldsPositions(aiTable) {
1222
1213
  });
1223
1214
  aiTable.apply(actions);
1224
1215
  }
1216
+ function resetAllViewsPositions(aiTable) {
1217
+ const views = aiTable.views();
1218
+ const positions = insertAtEnd(0, views.length);
1219
+ const actions = [];
1220
+ views.forEach((v, i) => {
1221
+ const action = buildSetViewAction(aiTable, { position: positions[i] }, [v._id]);
1222
+ if (action) {
1223
+ actions.push(action);
1224
+ }
1225
+ });
1226
+ aiTable.apply(actions);
1227
+ }
1225
1228
  function setRecordPositions(aiTable, positions, path) {
1226
- const operation = buildSetRecordPositionsActon(aiTable, positions, path);
1229
+ const operation = buildSetRecordPositionsAction(aiTable, positions, path);
1227
1230
  aiTable.apply(operation);
1228
1231
  }
1229
1232
  const PositionsActions = {
1230
1233
  setRecordPositions,
1231
1234
  resetAllRecordsPositions,
1232
- resetAllFieldsPositions
1235
+ resetAllFieldsPositions,
1236
+ resetAllViewsPositions
1233
1237
  };
1234
1238
 
1235
1239
  const DEFAULT_INITIAL_GAP = 65536;
@@ -1400,9 +1404,19 @@ function getPosition(data, activeViewId, index) {
1400
1404
  return getPositions(activeViewId, data, index)[0];
1401
1405
  }
1402
1406
  function getMaxPosition(data, activeViewId) {
1403
- return data.reduce((maxPosition, item) => {
1404
- if (item.positions[activeViewId] > maxPosition) {
1405
- maxPosition = item.positions[activeViewId];
1407
+ const first = data[0];
1408
+ if (first && typeof first === 'object' && 'positions' in first && activeViewId) {
1409
+ return data.reduce((maxPosition, item) => {
1410
+ if (item.positions[activeViewId] > maxPosition) {
1411
+ maxPosition = item.positions[activeViewId];
1412
+ }
1413
+ return maxPosition;
1414
+ }, Number.MIN_SAFE_INTEGER);
1415
+ }
1416
+ return data.reduce((maxPosition, item, index) => {
1417
+ const pos = typeof item.position === 'number' ? item.position : index;
1418
+ if (pos > maxPosition) {
1419
+ maxPosition = pos;
1406
1420
  }
1407
1421
  return maxPosition;
1408
1422
  }, Number.MIN_SAFE_INTEGER);
@@ -1434,7 +1448,7 @@ function addView(aiTable, type, viewId) {
1434
1448
  ViewActions.addView(aiTable, originViewId, newView, type === 'duplicate');
1435
1449
  const actions = [];
1436
1450
  aiTable.records().forEach((record, index) => {
1437
- const action = buildSetRecordPositionsActon(aiTable, { [newId]: record.positions[originViewId] }, [index]);
1451
+ const action = buildSetRecordPositionsAction(aiTable, { [newId]: record.positions[originViewId] }, [index]);
1438
1452
  actions.push(action);
1439
1453
  });
1440
1454
  aiTable.fields().forEach((field) => {
@@ -1455,7 +1469,7 @@ function removeView(aiTable, records, fields, activeViewId) {
1455
1469
  ViewActions.removeView(aiTable, [activeViewId]);
1456
1470
  const actions = [];
1457
1471
  records.forEach((record, index) => {
1458
- const action = buildSetRecordPositionsActon(aiTable, { [activeViewId]: undefined }, [index]);
1472
+ const action = buildSetRecordPositionsAction(aiTable, { [activeViewId]: undefined }, [index]);
1459
1473
  actions.push(action);
1460
1474
  });
1461
1475
  fields.forEach((field) => {
@@ -1548,9 +1562,13 @@ function getPreviousAndNextPosition(aiTable, options, items, itemsMap) {
1548
1562
  };
1549
1563
  }
1550
1564
  function getCurrentViewPositions(aiTable, options, items, itemsMap) {
1551
- const { previousPosition, nextPosition } = getPreviousAndNextPosition(aiTable, options, items, itemsMap);
1552
1565
  const count = options.count || 1;
1553
1566
  let positions = [];
1567
+ if (items.length === 0) {
1568
+ positions = insertAtEnd(0, count);
1569
+ return positions;
1570
+ }
1571
+ const { previousPosition, nextPosition } = getPreviousAndNextPosition(aiTable, options, items, itemsMap);
1554
1572
  if (options.beforeItemId && previousPosition === null && nextPosition !== null) {
1555
1573
  positions = insertAtStart(nextPosition, count);
1556
1574
  }
@@ -2098,7 +2116,7 @@ function moveRecords(aiTable, options) {
2098
2116
  });
2099
2117
  }
2100
2118
 
2101
- function buildRecordsByView(aiTable, records, fields, activeView, sortKeysMap) {
2119
+ function buildRecordsByView(aiTable, records, fields, activeView) {
2102
2120
  const filteredRecords = getFilteredRecords(aiTable, records, fields, activeView);
2103
2121
  const sorts = buildSorts(activeView);
2104
2122
  const renderRecords = buildRecordsWithWillMoveRecords(filteredRecords, aiTable.recordsWillMove());
@@ -2155,111 +2173,6 @@ function mergeSorts(activeView) {
2155
2173
 
2156
2174
  const FLUSHING = new WeakMap();
2157
2175
 
2158
- function moveFields(aiTable, options) {
2159
- const viewPositionOptions = {
2160
- afterItemId: options.afterFieldId,
2161
- beforeItemId: options.beforeFieldId,
2162
- count: options.fieldIds.length
2163
- };
2164
- let positions = getCurrentViewPositions(aiTable, viewPositionOptions, aiTable.fields(), aiTable.fieldsMap());
2165
- if (positions.length === 0) {
2166
- PositionsActions.resetAllFieldsPositions(aiTable);
2167
- positions = getCurrentViewPositions(aiTable, viewPositionOptions, aiTable.fields(), aiTable.fieldsMap());
2168
- console.log('Reset all fields positions');
2169
- }
2170
- const activeViewId = aiTable.activeViewId();
2171
- const activeView = aiTable.views().find((view) => view._id === activeViewId);
2172
- const { fieldIds, afterFieldId, beforeFieldId } = options;
2173
- const originalFields = aiTable.fields();
2174
- const fieldsIndexMap = new Map(originalFields.map((field, index) => [field._id, index]));
2175
- const sourceFields = [];
2176
- fieldIds.forEach((id) => {
2177
- const index = fieldsIndexMap.get(id);
2178
- if (index === undefined) {
2179
- return;
2180
- }
2181
- sourceFields.push(originalFields[index]);
2182
- });
2183
- const sortedSourceFields = sortByViewPosition(sourceFields, activeView);
2184
- const actions = [];
2185
- sortedSourceFields.forEach((field, index) => {
2186
- const action = buildSetFieldAction(aiTable, { positions: { ...field.positions, [activeViewId]: positions[index] } }, [field._id]);
2187
- if (action) {
2188
- actions.push(action);
2189
- }
2190
- });
2191
- aiTable.apply(actions);
2192
- }
2193
-
2194
- function getFieldPositionInView(viewId, fields, path, newPath) {
2195
- const targetPosition = fields[newPath[0]].positions[viewId];
2196
- let newPosition = 0;
2197
- if (path[0] > newPath[0]) {
2198
- const prevPath = newPath[0] - 1;
2199
- if (prevPath >= 0) {
2200
- const targetPrevField = fields[prevPath];
2201
- const targetPrevPosition = targetPrevField.positions[viewId];
2202
- newPosition = (targetPosition + targetPrevPosition) / 2;
2203
- }
2204
- else {
2205
- const firstField = fields[0];
2206
- newPosition = firstField.positions[viewId] - 0.1;
2207
- }
2208
- }
2209
- else {
2210
- const nextPath = newPath[0] + 1;
2211
- if (fields.length > nextPath) {
2212
- const targetNextField = fields[nextPath];
2213
- const targetNextPosition = targetNextField.positions[viewId];
2214
- newPosition = (targetPosition + targetNextPosition) / 2;
2215
- }
2216
- else {
2217
- const lastField = fields[fields.length - 1];
2218
- const lastPosition = lastField.positions[viewId] + 1;
2219
- newPosition = (targetPosition + lastPosition) / 2;
2220
- }
2221
- }
2222
- return newPosition;
2223
- }
2224
- function getFieldsSizeMap(fields, activeView) {
2225
- const fieldsSizeMap = {};
2226
- fields?.forEach((field) => {
2227
- fieldsSizeMap[field._id] = field.widths?.[activeView._id];
2228
- });
2229
- return fieldsSizeMap;
2230
- }
2231
-
2232
- function addFields(aiTable, options) {
2233
- const { defaultValue, isDuplicate, originId } = options;
2234
- const fieldsMap = aiTable.fieldsMap();
2235
- const newField = { ...defaultValue };
2236
- if (fieldsMap[newField._id]) {
2237
- newField._id = idCreator();
2238
- }
2239
- const viewPositionOptions = { count: options.count || 1 };
2240
- if (isDuplicate) {
2241
- viewPositionOptions.afterItemId = originId;
2242
- }
2243
- let positions = getNewItemsPosition(aiTable, viewPositionOptions, aiTable.fields(), aiTable.fieldsMap());
2244
- if (positions.length === 0) {
2245
- PositionsActions.resetAllFieldsPositions(aiTable);
2246
- positions = getNewItemsPosition(aiTable, viewPositionOptions, aiTable.fields(), aiTable.fieldsMap());
2247
- }
2248
- newField.positions = positions[0];
2249
- Actions.addField(aiTable, newField, originId, isDuplicate);
2250
- }
2251
-
2252
- const buildRemoveFieldItem = (aiTable, getUpdatedInfo) => {
2253
- return {
2254
- type: 'removeField',
2255
- name: getStateI18nTextByKey(aiTable, AITableStateI18nKey.removeField),
2256
- icon: 'trash',
2257
- exec: (aiTable, field) => {
2258
- Actions.removeField(aiTable, [field()._id]);
2259
- }
2260
- };
2261
- };
2262
-
2263
2176
  function getFrozenFieldId(aiTable) {
2264
2177
  try {
2265
2178
  const viewTable = aiTable;
@@ -2346,7 +2259,9 @@ const freezeToThisColumn = (aiTable) => {
2346
2259
  exec: (aiTable, field) => {
2347
2260
  const currentField = field();
2348
2261
  const viewTable = aiTable;
2349
- setViewFrozenField(viewTable, currentField._id);
2262
+ ViewActions.setView(viewTable, {
2263
+ settings: { ...viewTable.viewsMap()[viewTable.activeViewId()].settings, frozen_field_id: currentField._id }
2264
+ }, [viewTable.activeViewId()]);
2350
2265
  },
2351
2266
  hidden: (aiTable, field) => {
2352
2267
  if (aiTable.context?.readonly?.()) {
@@ -2397,7 +2312,9 @@ const restoreDefaultFrozenColumn = (aiTable) => {
2397
2312
  icon: 'frozen',
2398
2313
  exec: (aiTable, field) => {
2399
2314
  const viewTable = aiTable;
2400
- setViewFrozenField(viewTable, undefined);
2315
+ ViewActions.setView(viewTable, {
2316
+ settings: { ...viewTable.viewsMap()[viewTable.activeViewId()].settings, frozen_field_id: undefined }
2317
+ }, [viewTable.activeViewId()]);
2401
2318
  },
2402
2319
  hidden: (aiTable, field) => {
2403
2320
  if (aiTable.context?.readonly?.()) {
@@ -2413,6 +2330,169 @@ const restoreDefaultFrozenColumn = (aiTable) => {
2413
2330
  };
2414
2331
  };
2415
2332
 
2333
+ function moveFields(aiTable, options) {
2334
+ const viewPositionOptions = {
2335
+ afterItemId: options.afterFieldId,
2336
+ beforeItemId: options.beforeFieldId,
2337
+ count: options.fieldIds.length
2338
+ };
2339
+ let positions = getCurrentViewPositions(aiTable, viewPositionOptions, aiTable.fields(), aiTable.fieldsMap());
2340
+ if (positions.length === 0) {
2341
+ PositionsActions.resetAllFieldsPositions(aiTable);
2342
+ positions = getCurrentViewPositions(aiTable, viewPositionOptions, aiTable.fields(), aiTable.fieldsMap());
2343
+ console.log('Reset all fields positions');
2344
+ }
2345
+ const activeViewId = aiTable.activeViewId();
2346
+ const activeView = aiTable.views().find((view) => view._id === activeViewId);
2347
+ const { fieldIds, afterFieldId, beforeFieldId } = options;
2348
+ const originalFields = aiTable.fields();
2349
+ const fieldsIndexMap = new Map(originalFields.map((field, index) => [field._id, index]));
2350
+ const sourceFields = [];
2351
+ fieldIds.forEach((id) => {
2352
+ const index = fieldsIndexMap.get(id);
2353
+ if (index === undefined) {
2354
+ return;
2355
+ }
2356
+ sourceFields.push(originalFields[index]);
2357
+ });
2358
+ const sortedSourceFields = sortByViewPosition(sourceFields, activeView);
2359
+ const actions = [];
2360
+ sortedSourceFields.forEach((field, index) => {
2361
+ const action = buildSetFieldAction(aiTable, { positions: { ...field.positions, [activeViewId]: positions[index] } }, [field._id]);
2362
+ if (action) {
2363
+ actions.push(action);
2364
+ }
2365
+ });
2366
+ const currentFrozenFieldId = getFrozenFieldId(aiTable);
2367
+ if (currentFrozenFieldId) {
2368
+ adjustFrozenFieldAfterMove(aiTable, currentFrozenFieldId, sortedSourceFields[0]._id, {
2369
+ afterFieldId,
2370
+ beforeFieldId
2371
+ });
2372
+ }
2373
+ aiTable.apply(actions);
2374
+ }
2375
+ function adjustFrozenFieldAfterMove(aiTable, currentFrozenFieldId, sourceFieldId, fieldOptions) {
2376
+ const fields = aiTable.gridData().fields;
2377
+ const fieldsIndexMap = aiTable.context?.visibleColumnsIndexMap();
2378
+ const currentFrozenFieldIndex = fieldsIndexMap.get(currentFrozenFieldId);
2379
+ if (currentFrozenFieldIndex === -1) {
2380
+ return;
2381
+ }
2382
+ const { afterFieldId, beforeFieldId } = fieldOptions;
2383
+ const sourceIndex = fieldsIndexMap.get(sourceFieldId);
2384
+ let targetIndex;
2385
+ if (beforeFieldId) {
2386
+ targetIndex = fieldsIndexMap.get(beforeFieldId);
2387
+ }
2388
+ else if (afterFieldId) {
2389
+ targetIndex = fieldsIndexMap.get(afterFieldId);
2390
+ }
2391
+ else {
2392
+ return;
2393
+ }
2394
+ const activeViewId = aiTable.activeViewId();
2395
+ let newFrozenFieldId = null;
2396
+ // 最后冻结列拖动到非冻结区或冻结区,冻结列向左移动
2397
+ if (sourceIndex === currentFrozenFieldIndex && targetIndex !== currentFrozenFieldIndex) {
2398
+ const newFrozenFieldIndex = Math.max(0, currentFrozenFieldIndex - 1);
2399
+ if (newFrozenFieldIndex < fields.length && newFrozenFieldIndex !== currentFrozenFieldIndex) {
2400
+ newFrozenFieldId = fields[newFrozenFieldIndex]._id;
2401
+ }
2402
+ else {
2403
+ // 如果没有前一个字段,恢复默认冻结
2404
+ newFrozenFieldId = undefined;
2405
+ }
2406
+ }
2407
+ // 冻结区拖动到最后冻结列后面,冻结列是被拖动列
2408
+ if (sourceIndex < currentFrozenFieldIndex && targetIndex === currentFrozenFieldIndex) {
2409
+ newFrozenFieldId = fields[sourceIndex]._id;
2410
+ }
2411
+ if (newFrozenFieldId !== null) {
2412
+ ViewActions.setView(aiTable, { settings: { ...aiTable.viewsMap()[activeViewId].settings, frozen_field_id: newFrozenFieldId } }, [
2413
+ activeViewId
2414
+ ]);
2415
+ }
2416
+ }
2417
+
2418
+ function getFieldPositionInView(viewId, fields, path, newPath) {
2419
+ const targetPosition = fields[newPath[0]].positions[viewId];
2420
+ let newPosition = 0;
2421
+ if (path[0] > newPath[0]) {
2422
+ const prevPath = newPath[0] - 1;
2423
+ if (prevPath >= 0) {
2424
+ const targetPrevField = fields[prevPath];
2425
+ const targetPrevPosition = targetPrevField.positions[viewId];
2426
+ newPosition = (targetPosition + targetPrevPosition) / 2;
2427
+ }
2428
+ else {
2429
+ const firstField = fields[0];
2430
+ newPosition = firstField.positions[viewId] - 0.1;
2431
+ }
2432
+ }
2433
+ else {
2434
+ const nextPath = newPath[0] + 1;
2435
+ if (fields.length > nextPath) {
2436
+ const targetNextField = fields[nextPath];
2437
+ const targetNextPosition = targetNextField.positions[viewId];
2438
+ newPosition = (targetPosition + targetNextPosition) / 2;
2439
+ }
2440
+ else {
2441
+ const lastField = fields[fields.length - 1];
2442
+ const lastPosition = lastField.positions[viewId] + 1;
2443
+ newPosition = (targetPosition + lastPosition) / 2;
2444
+ }
2445
+ }
2446
+ return newPosition;
2447
+ }
2448
+ function getFieldsSizeMap(fields, activeView) {
2449
+ const fieldsSizeMap = {};
2450
+ fields?.forEach((field) => {
2451
+ fieldsSizeMap[field._id] = field.widths?.[activeView._id];
2452
+ });
2453
+ return fieldsSizeMap;
2454
+ }
2455
+
2456
+ function addFields(aiTable, options) {
2457
+ const maxFieldsCount = aiTable.context?.maxFields();
2458
+ if (maxFieldsCount && aiTable.fields().length >= maxFieldsCount) {
2459
+ console.log(`Warning: ${getI18nTextByKey(aiTable, AITableGridI18nKey.pasteOverMaxRecords)}`);
2460
+ return;
2461
+ }
2462
+ const { defaultValue, isDuplicate, originId } = options;
2463
+ const fieldsMap = aiTable.fieldsMap();
2464
+ const newField = { ...defaultValue };
2465
+ const existNames = aiTable.fields().map((item) => item.name);
2466
+ if (existNames.includes(newField.name)) {
2467
+ newField.name = generateNewFieldName(aiTable, newField, newField.name);
2468
+ }
2469
+ if (fieldsMap[newField._id]) {
2470
+ newField._id = idCreator();
2471
+ }
2472
+ const viewPositionOptions = { count: options.count || 1 };
2473
+ if (isDuplicate) {
2474
+ viewPositionOptions.afterItemId = originId;
2475
+ }
2476
+ let positions = getNewItemsPosition(aiTable, viewPositionOptions, aiTable.fields(), aiTable.fieldsMap());
2477
+ if (positions.length === 0) {
2478
+ PositionsActions.resetAllFieldsPositions(aiTable);
2479
+ positions = getNewItemsPosition(aiTable, viewPositionOptions, aiTable.fields(), aiTable.fieldsMap());
2480
+ }
2481
+ newField.positions = positions[0];
2482
+ Actions.addField(aiTable, newField, originId, isDuplicate);
2483
+ }
2484
+
2485
+ const buildRemoveFieldItem = (aiTable, getUpdatedInfo) => {
2486
+ return {
2487
+ type: 'removeField',
2488
+ name: getStateI18nTextByKey(aiTable, AITableStateI18nKey.removeField),
2489
+ icon: 'trash',
2490
+ exec: (aiTable, field) => {
2491
+ Actions.removeField(aiTable, [field()._id]);
2492
+ }
2493
+ };
2494
+ };
2495
+
2416
2496
  const withState = (aiTable) => {
2417
2497
  const viewTable = aiTable;
2418
2498
  viewTable.actions = [];
@@ -2632,5 +2712,5 @@ const VIEW_ACTIONS = [ActionName.SetView, ActionName.AddView, ActionName.RemoveV
2632
2712
  * Generated bundle index. Do not edit.
2633
2713
  */
2634
2714
 
2635
- 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, freezeToThisColumn, generateCopyName, getDataBySharedType, getDefaultI18nTextByKey, getDefaultRecordDataByFilter, getDefaultRecordValues, getFieldPositionInView, getFieldsSizeMap, getFilteredRecords, getFrozenFieldId, getGroupRecordLength, getMaxPosition, getParentGroupValuesByGroupId, getParentLinearRowGroups, getPosition, getPositions, getPrevRecordIdByAddGroupId, getRecordsBySharedJson, getSharedTypeByData, getSortFields, getStateI18nTextByKey, insertAtEnd, insertAtStart, insertBetween, isPathEqual, isSameParentGroup, mergeSorts, moveFields, moveRecords, removeView, restoreDefaultFrozenColumn, sortRecordsByConditions, sortViews, toSharedType, translateYjsEvent, updateFieldAndValues, updateFieldValues, withState };
2715
+ export { AITableStateI18nKey, AITableStateI18nText, Actions, CopyCellsItem, CopyFieldPropertyItem, DEFAULT_INITIAL_GAP, DEFAULT_PRECISION_THRESHOLD, 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, buildSetRecordPositionsAction, buildSorts, calculateAdaptiveFrozenColumnCount, checkConditions, createMultiplePositions, createPositions, createSharedType, doFilter, freezeToThisColumn, generateCopyName, getDataBySharedType, getDefaultI18nTextByKey, getDefaultRecordDataByFilter, getDefaultRecordValues, getFieldPositionInView, getFieldsSizeMap, getFilteredRecords, getFrozenFieldId, getGroupRecordLength, getMaxPosition, getParentGroupValuesByGroupId, getParentLinearRowGroups, getPosition, getPositions, getPrevRecordIdByAddGroupId, getRecordsBySharedJson, getSharedTypeByData, getSortFields, getStateI18nTextByKey, insertAtEnd, insertAtStart, insertBetween, isPathEqual, isSameParentGroup, mergeSorts, moveFields, moveRecords, removeView, restoreDefaultFrozenColumn, sortRecordsByConditions, sortViews, toSharedType, translateYjsEvent, updateFieldAndValues, updateFieldValues, withState };
2636
2716
  //# sourceMappingURL=ai-table-state.mjs.map