@ai-table/state 0.0.43 → 0.0.44

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.
@@ -120,7 +120,7 @@ function toRecordSyncElement(record, fields) {
120
120
  element.insert(0, [systemFieldValues, customFieldValues]);
121
121
  return element;
122
122
  }
123
- function translatePositionToPath(data, position, activeViewId) {
123
+ function translatePositionToPath(data, position, activeViewId, indexOffset = 0) {
124
124
  let index = data.findIndex((value, index) => {
125
125
  if (index === 0) {
126
126
  return position < value.positions[activeViewId];
@@ -130,7 +130,7 @@ function translatePositionToPath(data, position, activeViewId) {
130
130
  if (index === -1) {
131
131
  index = data.length;
132
132
  }
133
- return [index];
133
+ return [index + indexOffset];
134
134
  }
135
135
  function getShareTypeNumberPath(path) {
136
136
  return path.filter((node) => typeof node === 'number');
@@ -330,7 +330,7 @@ function translateArrayEvent(aiTable, sharedType, event) {
330
330
  short_id: getShortIdBySystemFieldValues(systemFieldValues),
331
331
  ...getTrackableEntityBySystemFieldValues(systemFieldValues),
332
332
  positions: getPositionsBySystemFieldValues(systemFieldValues),
333
- values: getValuesByCustomFieldValues(customFieldValues, aiTable.fields())
333
+ values: getValuesByCustomFieldValues(customFieldValues, aiTable.gridData().fields)
334
334
  }
335
335
  });
336
336
  });
@@ -340,9 +340,9 @@ function translateArrayEvent(aiTable, sharedType, event) {
340
340
  const sharedRecords = sharedType.get('records');
341
341
  const sharedFields = sharedType.get('fields');
342
342
  let systemFieldOffset = 0;
343
- delta.insert?.map((item) => {
343
+ delta.insert?.map((item, index) => {
344
344
  const recordIndex = targetPath[0];
345
- const fieldIndex = offset;
345
+ const fieldIndex = offset + index;
346
346
  const record = aiTable.records()[recordIndex];
347
347
  if (isSystemFieldOperation(targetPath)) {
348
348
  if (isPositionsOperation(fieldIndex)) {
@@ -389,9 +389,9 @@ function translateArrayEvent(aiTable, sharedType, event) {
389
389
  }
390
390
  }
391
391
  if (isFieldsTranslate) {
392
- delta.insert?.map((item) => {
392
+ delta.insert?.map((item, index) => {
393
393
  const data = item.toJSON();
394
- const path = translatePositionToPath(aiTable.fields(), data['positions'][activeViewId], activeViewId);
394
+ const path = translatePositionToPath(aiTable.gridData().fields, data['positions'][activeViewId], activeViewId, index);
395
395
  actions.push({
396
396
  type: ActionName.AddField,
397
397
  path,
@@ -468,7 +468,7 @@ function translateMapEvent(aiTable, sharedType, event) {
468
468
  if (isFieldsTranslate) {
469
469
  const field = sharedType.get('fields')?.get(targetPath);
470
470
  const fieldId = field && field.get('_id');
471
- targetElement = fieldId && aiTable.fields().find((item) => item._id === field.get('_id'));
471
+ targetElement = fieldId && aiTable.gridData().fields.find((item) => item._id === field.get('_id'));
472
472
  }
473
473
  if (isViewTranslate) {
474
474
  targetElement = aiTable.views()[targetPath];
@@ -705,6 +705,15 @@ function sortByViewPosition(data, activeView) {
705
705
  }
706
706
  return data;
707
707
  }
708
+ function generateCopyName(exsitNames, name) {
709
+ let newName = `${name} 副本`;
710
+ let index = 2;
711
+ while (exsitNames.includes(newName)) {
712
+ newName = `${name} 副本 ${index}`;
713
+ index++;
714
+ }
715
+ return newName;
716
+ }
708
717
 
709
718
  function getSortRecords(aiTable, records, activeView, sortKeysMap) {
710
719
  if (!activeView?.settings || !activeView.settings.sorts?.length) {
@@ -956,7 +965,7 @@ const apply = (aiTable, records, fields, views, action) => {
956
965
  const [fieldIndex] = action.path;
957
966
  if (fieldIndex > -1) {
958
967
  const newField = action.field;
959
- newField.positions = createDefaultPositions(aiTable.views(), aiTable.activeViewId(), aiTable.fields(), action.path[0]);
968
+ newField.positions = createDefaultPositions(aiTable.views(), aiTable.activeViewId(), aiTable.gridData().fields, action.path[0]);
960
969
  fields.splice(fieldIndex, 0, newField);
961
970
  records.forEach((item) => {
962
971
  item.values[newField._id] =
@@ -1242,8 +1251,12 @@ function createDefaultPositions(views, activeId, data, index) {
1242
1251
  const positions = {};
1243
1252
  const position = getPosition(data, activeId, index);
1244
1253
  views.forEach((element) => {
1245
- const lastPosition = data.length ? data[data.length - 1].positions[element._id] : -1;
1246
- positions[element._id] = element._id === activeId ? position : lastPosition + 1;
1254
+ if (element._id === activeId) {
1255
+ positions[element._id] = position;
1256
+ }
1257
+ else {
1258
+ positions[element._id] = getMaxPosition(data, element._id) + 1;
1259
+ }
1247
1260
  });
1248
1261
  return positions;
1249
1262
  }
@@ -1255,11 +1268,19 @@ function getPosition(data, activeViewId, index) {
1255
1268
  position = (previousViewPosition + nextViewPosition) / 2;
1256
1269
  }
1257
1270
  else {
1258
- const lastPosition = data[data.length - 1].positions?.[activeViewId];
1259
- position = lastPosition ? lastPosition + 1 : index;
1271
+ const maxPosition = getMaxPosition(data, activeViewId);
1272
+ position = maxPosition + 1;
1260
1273
  }
1261
1274
  return position;
1262
1275
  }
1276
+ function getMaxPosition(data, activeViewId) {
1277
+ return data.reduce((maxPosition, item) => {
1278
+ if (item.positions[activeViewId] > maxPosition) {
1279
+ maxPosition = item.positions[activeViewId];
1280
+ }
1281
+ return maxPosition;
1282
+ }, 0);
1283
+ }
1263
1284
  function addView(aiTable, type, viewId) {
1264
1285
  let index = aiTable.views().length;
1265
1286
  const newId = idCreator();
@@ -1273,10 +1294,13 @@ function addView(aiTable, type, viewId) {
1273
1294
  if (type === 'copy') {
1274
1295
  originViewId = viewId ?? aiTable.activeViewId();
1275
1296
  const copyView = aiTable.views().find((item) => item._id === originViewId);
1297
+ const allViewNames = aiTable.views().map((item) => item.name);
1298
+ const copyName = copyView.name;
1299
+ const newViewName = generateCopyName(allViewNames, copyName);
1276
1300
  newView = {
1277
1301
  ...copyView,
1278
1302
  _id: newId,
1279
- name: copyView.name + '-副本'
1303
+ name: newViewName
1280
1304
  };
1281
1305
  index = aiTable.views().indexOf(copyView) + 1;
1282
1306
  }
@@ -1448,12 +1472,8 @@ const CopyFieldPropertyItem = (addFieldFn) => {
1448
1472
  icon: 'copy',
1449
1473
  exec: (aiTable, field) => {
1450
1474
  const allFieldNames = (aiTable.fields() || []).map((item) => item.name);
1451
- let newFieldName = `${field().name} 副本`;
1452
- let index = 2;
1453
- while (allFieldNames.includes(newFieldName)) {
1454
- newFieldName = `${field().name} 副本 ${index}`;
1455
- index++;
1456
- }
1475
+ const copyName = field().name;
1476
+ let newFieldName = generateCopyName(allFieldNames, copyName);
1457
1477
  const fieldOptions = {
1458
1478
  originId: field()._id,
1459
1479
  isCopy: true,
@@ -1521,5 +1541,5 @@ const VIEW_ACTIONS = [ActionName.SetView, ActionName.AddView, ActionName.RemoveV
1521
1541
  * Generated bundle index. Do not edit.
1522
1542
  */
1523
1543
 
1524
- export { AITableFilterLogical, AI_TABLE_CONTENT_FIELD_NAME, ActionName, Actions, CopyCellsItem, CopyFieldPropertyItem, Direction, DividerMenuItem, EditFieldPropertyItem, ExecuteType, FLUSHING, PasteCellsItem, Positions, RemovePositions, RemoveRecordsItem, SystemFieldIndex, VIEW_ACTIONS, YjsAITable, actionMappers, addFields, addRecords, addView, applyActionOps, applyEvents, applyYjsEvents, buildFieldsByView, buildRecordsByView, buildRemoveFieldItem, createDefaultPositions, createSharedType, doFilter, getCustomFieldValues, getDataBySharedType, getDefaultRecordDataByFilter, getDefaultRecordValues, getFieldPositionInView, getFilteredRecords, getIdBySystemFieldValues, getIdBySystemFieldValuesType, getPosition, getPositionsByRecordSyncElement, getPositionsBySystemFieldValues, getRecordsBySharedJson, getShareTypeNumberPath, getSharedMapValueId, getSharedMapValueIndex, getSharedRecord, getSharedRecordId, getSharedRecordIndex, getSharedTypeByData, getShortIdBySystemFieldValues, getSortFields, getSortRecords, getSystemFieldValues, getTrackableEntityBySystemFieldValues, getValuesByCustomFieldValues, isPathEqual, moveFields, removeView, setRecordPositions$1 as setRecordPositions, setRecordUpdatedInfo, sortByViewPosition, sortRecordsBySortInfo, toRecordSyncElement, toSharedType, toSyncElement, translatePositionToPath, translateYjsEvent, updateFieldValue, updateRecordsUpdatedInfo, withState };
1544
+ export { AITableFilterLogical, AI_TABLE_CONTENT_FIELD_NAME, ActionName, Actions, CopyCellsItem, CopyFieldPropertyItem, Direction, DividerMenuItem, EditFieldPropertyItem, ExecuteType, FLUSHING, PasteCellsItem, Positions, RemovePositions, RemoveRecordsItem, SystemFieldIndex, VIEW_ACTIONS, YjsAITable, actionMappers, addFields, addRecords, addView, applyActionOps, applyEvents, applyYjsEvents, buildFieldsByView, buildRecordsByView, buildRemoveFieldItem, createDefaultPositions, createSharedType, doFilter, generateCopyName, getCustomFieldValues, getDataBySharedType, getDefaultRecordDataByFilter, getDefaultRecordValues, getFieldPositionInView, getFilteredRecords, getIdBySystemFieldValues, getIdBySystemFieldValuesType, getMaxPosition, getPosition, getPositionsByRecordSyncElement, getPositionsBySystemFieldValues, getRecordsBySharedJson, getShareTypeNumberPath, getSharedMapValueId, getSharedMapValueIndex, getSharedRecord, getSharedRecordId, getSharedRecordIndex, getSharedTypeByData, getShortIdBySystemFieldValues, getSortFields, getSortRecords, getSystemFieldValues, getTrackableEntityBySystemFieldValues, getValuesByCustomFieldValues, isPathEqual, moveFields, removeView, setRecordPositions$1 as setRecordPositions, setRecordUpdatedInfo, sortByViewPosition, sortRecordsBySortInfo, toRecordSyncElement, toSharedType, toSyncElement, translatePositionToPath, translateYjsEvent, updateFieldValue, updateRecordsUpdatedInfo, withState };
1525
1545
  //# sourceMappingURL=ai-table-state.mjs.map