@ai-table/state 0.2.5 → 0.3.1

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, buildClipboardData, writeToClipboard, getI18nTextByKey, AITableGridI18nKey, isMac, writeToAITable } 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) => {
@@ -2098,7 +2112,7 @@ function moveRecords(aiTable, options) {
2098
2112
  });
2099
2113
  }
2100
2114
 
2101
- function buildRecordsByView(aiTable, records, fields, activeView, sortKeysMap) {
2115
+ function buildRecordsByView(aiTable, records, fields, activeView) {
2102
2116
  const filteredRecords = getFilteredRecords(aiTable, records, fields, activeView);
2103
2117
  const sorts = buildSorts(activeView);
2104
2118
  const renderRecords = buildRecordsWithWillMoveRecords(filteredRecords, aiTable.recordsWillMove());
@@ -2155,111 +2169,6 @@ function mergeSorts(activeView) {
2155
2169
 
2156
2170
  const FLUSHING = new WeakMap();
2157
2171
 
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
2172
  function getFrozenFieldId(aiTable) {
2264
2173
  try {
2265
2174
  const viewTable = aiTable;
@@ -2346,7 +2255,9 @@ const freezeToThisColumn = (aiTable) => {
2346
2255
  exec: (aiTable, field) => {
2347
2256
  const currentField = field();
2348
2257
  const viewTable = aiTable;
2349
- setViewFrozenField(viewTable, currentField._id);
2258
+ ViewActions.setView(viewTable, {
2259
+ settings: { ...viewTable.viewsMap()[viewTable.activeViewId()].settings, frozen_field_id: currentField._id }
2260
+ }, [viewTable.activeViewId()]);
2350
2261
  },
2351
2262
  hidden: (aiTable, field) => {
2352
2263
  if (aiTable.context?.readonly?.()) {
@@ -2397,7 +2308,9 @@ const restoreDefaultFrozenColumn = (aiTable) => {
2397
2308
  icon: 'frozen',
2398
2309
  exec: (aiTable, field) => {
2399
2310
  const viewTable = aiTable;
2400
- setViewFrozenField(viewTable, undefined);
2311
+ ViewActions.setView(viewTable, {
2312
+ settings: { ...viewTable.viewsMap()[viewTable.activeViewId()].settings, frozen_field_id: undefined }
2313
+ }, [viewTable.activeViewId()]);
2401
2314
  },
2402
2315
  hidden: (aiTable, field) => {
2403
2316
  if (aiTable.context?.readonly?.()) {
@@ -2413,6 +2326,169 @@ const restoreDefaultFrozenColumn = (aiTable) => {
2413
2326
  };
2414
2327
  };
2415
2328
 
2329
+ function moveFields(aiTable, options) {
2330
+ const viewPositionOptions = {
2331
+ afterItemId: options.afterFieldId,
2332
+ beforeItemId: options.beforeFieldId,
2333
+ count: options.fieldIds.length
2334
+ };
2335
+ let positions = getCurrentViewPositions(aiTable, viewPositionOptions, aiTable.fields(), aiTable.fieldsMap());
2336
+ if (positions.length === 0) {
2337
+ PositionsActions.resetAllFieldsPositions(aiTable);
2338
+ positions = getCurrentViewPositions(aiTable, viewPositionOptions, aiTable.fields(), aiTable.fieldsMap());
2339
+ console.log('Reset all fields positions');
2340
+ }
2341
+ const activeViewId = aiTable.activeViewId();
2342
+ const activeView = aiTable.views().find((view) => view._id === activeViewId);
2343
+ const { fieldIds, afterFieldId, beforeFieldId } = options;
2344
+ const originalFields = aiTable.fields();
2345
+ const fieldsIndexMap = new Map(originalFields.map((field, index) => [field._id, index]));
2346
+ const sourceFields = [];
2347
+ fieldIds.forEach((id) => {
2348
+ const index = fieldsIndexMap.get(id);
2349
+ if (index === undefined) {
2350
+ return;
2351
+ }
2352
+ sourceFields.push(originalFields[index]);
2353
+ });
2354
+ const sortedSourceFields = sortByViewPosition(sourceFields, activeView);
2355
+ const actions = [];
2356
+ sortedSourceFields.forEach((field, index) => {
2357
+ const action = buildSetFieldAction(aiTable, { positions: { ...field.positions, [activeViewId]: positions[index] } }, [field._id]);
2358
+ if (action) {
2359
+ actions.push(action);
2360
+ }
2361
+ });
2362
+ const currentFrozenFieldId = getFrozenFieldId(aiTable);
2363
+ if (currentFrozenFieldId) {
2364
+ adjustFrozenFieldAfterMove(aiTable, currentFrozenFieldId, sortedSourceFields[0]._id, {
2365
+ afterFieldId,
2366
+ beforeFieldId
2367
+ });
2368
+ }
2369
+ aiTable.apply(actions);
2370
+ }
2371
+ function adjustFrozenFieldAfterMove(aiTable, currentFrozenFieldId, sourceFieldId, fieldOptions) {
2372
+ const fields = aiTable.gridData().fields;
2373
+ const fieldsIndexMap = aiTable.context?.visibleColumnsIndexMap();
2374
+ const currentFrozenFieldIndex = fieldsIndexMap.get(currentFrozenFieldId);
2375
+ if (currentFrozenFieldIndex === -1) {
2376
+ return;
2377
+ }
2378
+ const { afterFieldId, beforeFieldId } = fieldOptions;
2379
+ const sourceIndex = fieldsIndexMap.get(sourceFieldId);
2380
+ let targetIndex;
2381
+ if (beforeFieldId) {
2382
+ targetIndex = fieldsIndexMap.get(beforeFieldId);
2383
+ }
2384
+ else if (afterFieldId) {
2385
+ targetIndex = fieldsIndexMap.get(afterFieldId);
2386
+ }
2387
+ else {
2388
+ return;
2389
+ }
2390
+ const activeViewId = aiTable.activeViewId();
2391
+ let newFrozenFieldId = null;
2392
+ // 最后冻结列拖动到非冻结区或冻结区,冻结列向左移动
2393
+ if (sourceIndex === currentFrozenFieldIndex && targetIndex !== currentFrozenFieldIndex) {
2394
+ const newFrozenFieldIndex = Math.max(0, currentFrozenFieldIndex - 1);
2395
+ if (newFrozenFieldIndex < fields.length && newFrozenFieldIndex !== currentFrozenFieldIndex) {
2396
+ newFrozenFieldId = fields[newFrozenFieldIndex]._id;
2397
+ }
2398
+ else {
2399
+ // 如果没有前一个字段,恢复默认冻结
2400
+ newFrozenFieldId = undefined;
2401
+ }
2402
+ }
2403
+ // 冻结区拖动到最后冻结列后面,冻结列是被拖动列
2404
+ if (sourceIndex < currentFrozenFieldIndex && targetIndex === currentFrozenFieldIndex) {
2405
+ newFrozenFieldId = fields[sourceIndex]._id;
2406
+ }
2407
+ if (newFrozenFieldId !== null) {
2408
+ ViewActions.setView(aiTable, { settings: { ...aiTable.viewsMap()[activeViewId].settings, frozen_field_id: newFrozenFieldId } }, [
2409
+ activeViewId
2410
+ ]);
2411
+ }
2412
+ }
2413
+
2414
+ function getFieldPositionInView(viewId, fields, path, newPath) {
2415
+ const targetPosition = fields[newPath[0]].positions[viewId];
2416
+ let newPosition = 0;
2417
+ if (path[0] > newPath[0]) {
2418
+ const prevPath = newPath[0] - 1;
2419
+ if (prevPath >= 0) {
2420
+ const targetPrevField = fields[prevPath];
2421
+ const targetPrevPosition = targetPrevField.positions[viewId];
2422
+ newPosition = (targetPosition + targetPrevPosition) / 2;
2423
+ }
2424
+ else {
2425
+ const firstField = fields[0];
2426
+ newPosition = firstField.positions[viewId] - 0.1;
2427
+ }
2428
+ }
2429
+ else {
2430
+ const nextPath = newPath[0] + 1;
2431
+ if (fields.length > nextPath) {
2432
+ const targetNextField = fields[nextPath];
2433
+ const targetNextPosition = targetNextField.positions[viewId];
2434
+ newPosition = (targetPosition + targetNextPosition) / 2;
2435
+ }
2436
+ else {
2437
+ const lastField = fields[fields.length - 1];
2438
+ const lastPosition = lastField.positions[viewId] + 1;
2439
+ newPosition = (targetPosition + lastPosition) / 2;
2440
+ }
2441
+ }
2442
+ return newPosition;
2443
+ }
2444
+ function getFieldsSizeMap(fields, activeView) {
2445
+ const fieldsSizeMap = {};
2446
+ fields?.forEach((field) => {
2447
+ fieldsSizeMap[field._id] = field.widths?.[activeView._id];
2448
+ });
2449
+ return fieldsSizeMap;
2450
+ }
2451
+
2452
+ function addFields(aiTable, options) {
2453
+ const maxFieldsCount = aiTable.context?.maxFields();
2454
+ if (maxFieldsCount && aiTable.fields().length >= maxFieldsCount) {
2455
+ console.log(`Warning: ${getI18nTextByKey(aiTable, AITableGridI18nKey.pasteOverMaxRecords)}`);
2456
+ return;
2457
+ }
2458
+ const { defaultValue, isDuplicate, originId } = options;
2459
+ const fieldsMap = aiTable.fieldsMap();
2460
+ const newField = { ...defaultValue };
2461
+ const existNames = aiTable.fields().map((item) => item.name);
2462
+ if (existNames.includes(newField.name)) {
2463
+ newField.name = generateNewFieldName(aiTable, newField, newField.name);
2464
+ }
2465
+ if (fieldsMap[newField._id]) {
2466
+ newField._id = idCreator();
2467
+ }
2468
+ const viewPositionOptions = { count: options.count || 1 };
2469
+ if (isDuplicate) {
2470
+ viewPositionOptions.afterItemId = originId;
2471
+ }
2472
+ let positions = getNewItemsPosition(aiTable, viewPositionOptions, aiTable.fields(), aiTable.fieldsMap());
2473
+ if (positions.length === 0) {
2474
+ PositionsActions.resetAllFieldsPositions(aiTable);
2475
+ positions = getNewItemsPosition(aiTable, viewPositionOptions, aiTable.fields(), aiTable.fieldsMap());
2476
+ }
2477
+ newField.positions = positions[0];
2478
+ Actions.addField(aiTable, newField, originId, isDuplicate);
2479
+ }
2480
+
2481
+ const buildRemoveFieldItem = (aiTable, getUpdatedInfo) => {
2482
+ return {
2483
+ type: 'removeField',
2484
+ name: getStateI18nTextByKey(aiTable, AITableStateI18nKey.removeField),
2485
+ icon: 'trash',
2486
+ exec: (aiTable, field) => {
2487
+ Actions.removeField(aiTable, [field()._id]);
2488
+ }
2489
+ };
2490
+ };
2491
+
2416
2492
  const withState = (aiTable) => {
2417
2493
  const viewTable = aiTable;
2418
2494
  viewTable.actions = [];
@@ -2600,16 +2676,12 @@ const CopyCellsItem = (aiTable, actions) => {
2600
2676
  shortcutKey: isMac() ? `⌘ + C` : `Ctrl + C`,
2601
2677
  icon: 'copy',
2602
2678
  exec: (aiTable, targetName, position, notifyService) => {
2603
- const clipboardData = buildClipboardData(aiTable);
2604
- if (clipboardData) {
2605
- writeToClipboard(clipboardData).then(() => {
2606
- const copiedCellsCount = aiTable.selection().selectedCells.size;
2607
- const message = getI18nTextByKey(aiTable, AITableGridI18nKey.copiedCells).replace('{count}', copiedCellsCount.toString());
2608
- notifyService.success(message, undefined, {
2609
- placement: 'bottomLeft'
2610
- });
2611
- });
2612
- }
2679
+ document.dispatchEvent(new ClipboardEvent('copy', {
2680
+ clipboardData: new DataTransfer(),
2681
+ bubbles: true,
2682
+ cancelable: true,
2683
+ composed: true
2684
+ }));
2613
2685
  }
2614
2686
  };
2615
2687
  };
@@ -2620,16 +2692,12 @@ const PasteCellsItem = (aiTable, actions) => {
2620
2692
  shortcutKey: isMac() ? `⌘ + V` : `Ctrl + V`,
2621
2693
  icon: 'paste',
2622
2694
  exec: async (aiTable, targetName, position, notifyService) => {
2623
- writeToAITable(aiTable, actions).then((result) => {
2624
- if (result.isPasteOverMaxRecords || result.isPasteOverMaxFields) {
2625
- return;
2626
- }
2627
- if (!result.isPasteSuccess) {
2628
- notifyService.error(getStateI18nTextByKey(aiTable, AITableStateI18nKey.invalidPasteContent), undefined, {
2629
- placement: 'bottomLeft'
2630
- });
2631
- }
2632
- });
2695
+ document.dispatchEvent(new ClipboardEvent('paste', {
2696
+ clipboardData: window.dataTransfer,
2697
+ bubbles: true,
2698
+ cancelable: true,
2699
+ composed: true
2700
+ }));
2633
2701
  }
2634
2702
  };
2635
2703
  };
@@ -2640,5 +2708,5 @@ const VIEW_ACTIONS = [ActionName.SetView, ActionName.AddView, ActionName.RemoveV
2640
2708
  * Generated bundle index. Do not edit.
2641
2709
  */
2642
2710
 
2643
- 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 };
2711
+ 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, 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 };
2644
2712
  //# sourceMappingURL=ai-table-state.mjs.map