@ai-table/state 0.0.65 → 0.0.67

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.
Files changed (130) hide show
  1. package/action/field.d.ts +3 -4
  2. package/action/field.d.ts.map +1 -1
  3. package/action/general.d.ts +2 -1
  4. package/action/general.d.ts.map +1 -1
  5. package/action/index.d.ts +3 -4
  6. package/action/index.d.ts.map +1 -1
  7. package/action/position.d.ts +2 -2
  8. package/action/position.d.ts.map +1 -1
  9. package/action/record.d.ts +3 -5
  10. package/action/record.d.ts.map +1 -1
  11. package/action/view.d.ts +2 -1
  12. package/action/view.d.ts.map +1 -1
  13. package/constants/field-menu-item.d.ts +2 -1
  14. package/constants/field-menu-item.d.ts.map +1 -1
  15. package/constants/index.d.ts +0 -1
  16. package/constants/index.d.ts.map +1 -1
  17. package/constants/view.d.ts +1 -1
  18. package/constants/view.d.ts.map +1 -1
  19. package/esm2022/action/field.mjs +3 -4
  20. package/esm2022/action/general.mjs +12 -35
  21. package/esm2022/action/position.mjs +2 -2
  22. package/esm2022/action/record.mjs +4 -16
  23. package/esm2022/action/view.mjs +2 -2
  24. package/esm2022/constants/field-menu-item.mjs +2 -2
  25. package/esm2022/constants/index.mjs +1 -2
  26. package/esm2022/constants/view.mjs +2 -2
  27. package/esm2022/plugins/state.plugin.mjs +1 -1
  28. package/esm2022/shared/index.mjs +2 -1
  29. package/esm2022/shared/to-table/index.mjs +3 -3
  30. package/esm2022/shared/to-table/map-event.mjs +2 -3
  31. package/esm2022/shared/to-yjs/add-node.mjs +14 -8
  32. package/esm2022/shared/to-yjs/index.mjs +2 -2
  33. package/esm2022/shared/to-yjs/remove-node.mjs +3 -3
  34. package/esm2022/shared/to-yjs/set-node.mjs +3 -3
  35. package/esm2022/shared/to-yjs/update-field-value.mjs +10 -4
  36. package/esm2022/shared/undo-manager.service.mjs +60 -0
  37. package/esm2022/shared/utils/index.mjs +1 -2
  38. package/esm2022/shared/utils/initialize.mjs +5 -5
  39. package/esm2022/types/ai-table.mjs +1 -1
  40. package/esm2022/types/index.mjs +1 -5
  41. package/esm2022/utils/build.mjs +1 -1
  42. package/esm2022/utils/common.mjs +1 -1
  43. package/esm2022/utils/field/add-fields.mjs +21 -14
  44. package/esm2022/utils/field/move-fields.mjs +1 -1
  45. package/esm2022/utils/field/position-field.mjs +1 -1
  46. package/esm2022/utils/field/remove-field.mjs +1 -1
  47. package/esm2022/utils/field/sort-fields.mjs +2 -2
  48. package/esm2022/utils/record/add-records.mjs +4 -3
  49. package/esm2022/utils/record/filter.mjs +4 -3
  50. package/esm2022/utils/record/move-records.mjs +1 -1
  51. package/esm2022/utils/record/sort.mjs +2 -1
  52. package/esm2022/utils/record/update-field-value.mjs +1 -1
  53. package/esm2022/utils/record/update-system-field-value.mjs +1 -1
  54. package/esm2022/utils/view.mjs +1 -1
  55. package/fesm2022/ai-table-state.mjs +185 -507
  56. package/fesm2022/ai-table-state.mjs.map +1 -1
  57. package/package.json +2 -4
  58. package/plugins/state.plugin.d.ts.map +1 -1
  59. package/shared/index.d.ts +1 -0
  60. package/shared/index.d.ts.map +1 -1
  61. package/shared/to-table/index.d.ts +2 -1
  62. package/shared/to-table/index.d.ts.map +1 -1
  63. package/shared/to-table/map-event.d.ts +2 -1
  64. package/shared/to-table/map-event.d.ts.map +1 -1
  65. package/shared/to-yjs/add-node.d.ts +2 -1
  66. package/shared/to-yjs/add-node.d.ts.map +1 -1
  67. package/shared/to-yjs/index.d.ts +1 -1
  68. package/shared/to-yjs/index.d.ts.map +1 -1
  69. package/shared/to-yjs/remove-node.d.ts +2 -1
  70. package/shared/to-yjs/remove-node.d.ts.map +1 -1
  71. package/shared/to-yjs/set-node.d.ts +2 -1
  72. package/shared/to-yjs/set-node.d.ts.map +1 -1
  73. package/shared/to-yjs/update-field-value.d.ts +2 -1
  74. package/shared/to-yjs/update-field-value.d.ts.map +1 -1
  75. package/shared/undo-manager.service.d.ts +21 -0
  76. package/shared/undo-manager.service.d.ts.map +1 -0
  77. package/shared/utils/index.d.ts +0 -1
  78. package/shared/utils/index.d.ts.map +1 -1
  79. package/shared/utils/initialize.d.ts +1 -1
  80. package/shared/utils/initialize.d.ts.map +1 -1
  81. package/types/ai-table.d.ts +3 -4
  82. package/types/ai-table.d.ts.map +1 -1
  83. package/types/index.d.ts +0 -4
  84. package/types/index.d.ts.map +1 -1
  85. package/utils/build.d.ts +3 -3
  86. package/utils/build.d.ts.map +1 -1
  87. package/utils/common.d.ts +2 -3
  88. package/utils/common.d.ts.map +1 -1
  89. package/utils/field/add-fields.d.ts +1 -1
  90. package/utils/field/add-fields.d.ts.map +1 -1
  91. package/utils/field/move-fields.d.ts +1 -1
  92. package/utils/field/move-fields.d.ts.map +1 -1
  93. package/utils/field/position-field.d.ts +1 -2
  94. package/utils/field/position-field.d.ts.map +1 -1
  95. package/utils/field/remove-field.d.ts +2 -1
  96. package/utils/field/remove-field.d.ts.map +1 -1
  97. package/utils/field/sort-fields.d.ts +2 -2
  98. package/utils/field/sort-fields.d.ts.map +1 -1
  99. package/utils/record/add-records.d.ts +1 -1
  100. package/utils/record/add-records.d.ts.map +1 -1
  101. package/utils/record/filter.d.ts +3 -2
  102. package/utils/record/filter.d.ts.map +1 -1
  103. package/utils/record/move-records.d.ts +1 -1
  104. package/utils/record/move-records.d.ts.map +1 -1
  105. package/utils/record/sort.d.ts +4 -4
  106. package/utils/record/sort.d.ts.map +1 -1
  107. package/utils/record/update-field-value.d.ts +1 -1
  108. package/utils/record/update-field-value.d.ts.map +1 -1
  109. package/utils/record/update-system-field-value.d.ts +1 -1
  110. package/utils/record/update-system-field-value.d.ts.map +1 -1
  111. package/utils/view.d.ts +2 -1
  112. package/utils/view.d.ts.map +1 -1
  113. package/constants/default.d.ts +0 -2
  114. package/constants/default.d.ts.map +0 -1
  115. package/esm2022/constants/default.mjs +0 -2
  116. package/esm2022/shared/to-table/array-event.mjs +0 -189
  117. package/esm2022/shared/utils/translate.mjs +0 -142
  118. package/esm2022/types/action.mjs +0 -23
  119. package/esm2022/types/shared.mjs +0 -2
  120. package/esm2022/types/view.mjs +0 -16
  121. package/shared/to-table/array-event.d.ts +0 -10
  122. package/shared/to-table/array-event.d.ts.map +0 -1
  123. package/shared/utils/translate.d.ts +0 -33
  124. package/shared/utils/translate.d.ts.map +0 -1
  125. package/types/action.d.ts +0 -88
  126. package/types/action.d.ts.map +0 -1
  127. package/types/shared.d.ts +0 -13
  128. package/types/shared.d.ts.map +0 -1
  129. package/types/view.d.ts +0 -50
  130. package/types/view.d.ts.map +0 -1
@@ -1,9 +1,12 @@
1
1
  import * as Y from 'yjs';
2
- import { isArray } from 'ngx-tethys/util';
3
- import { AITableQueries, FieldModelMap, AITableFilterOperation, AITableFieldType, isSystemField, isEmpty, getDefaultFieldValue, Direction as Direction$1, idsCreator, idCreator, shortIdCreator, generateNewName, shortIdsCreator, AI_TABLE_GRID_FIELD_SERVICE_MAP, AITable, isMac, buildClipboardData, writeToClipboard, getI18nTextByKey, AITableGridI18nKey, writeToAITable } from '@ai-table/grid';
4
- import { createDraft, finishDraft } from 'immer';
2
+ import { UndoManager } from 'yjs';
3
+ import { getShareTypeNumberPath, ActionName, translateArrayEvent, getSharedRecordIndex, getSharedMapValueIndex, toMapSyncElement, getIdBySystemFieldValuesType, setRecordUpdatedInfo, getPositionsByRecordSyncElement, setRecordPositions as setRecordPositions$1, toRecordSyncElement, AI_TABLE_CONTENT_FIELD_NAME, getIdBySystemFieldValues, getShortIdBySystemFieldValues, getTrackableEntityBySystemFieldValues, getPositionsBySystemFieldValues, getValuesByCustomFieldValues, AITableFilterLogical, AITableFilterOperation, AITableFieldType, Direction } from '@ai-table/utils';
4
+ import * as i0 from '@angular/core';
5
+ import { signal, Injectable } from '@angular/core';
6
+ import { FieldModelMap, AITableQueries, isSystemField, getDefaultFieldValue, idCreator, shortIdCreator, generateNewName, idsCreator, shortIdsCreator, AI_TABLE_GRID_FIELD_SERVICE_MAP, AITable, isMac, buildClipboardData, writeToClipboard, getI18nTextByKey, AITableGridI18nKey, writeToAITable } from '@ai-table/grid';
5
7
  import * as _ from 'lodash';
6
- import ___default from 'lodash';
8
+ import ___default, { isEmpty } from 'lodash';
9
+ import { createDraft, finishDraft } from 'immer';
7
10
 
8
11
  const IS_LOCAL = new WeakSet();
9
12
  const IS_REMOTE = new WeakSet();
@@ -44,420 +47,6 @@ const YjsAITable = {
44
47
  }
45
48
  };
46
49
 
47
- class Positions {
48
- }
49
- class RemovePositions {
50
- }
51
- var Direction;
52
- (function (Direction) {
53
- Direction[Direction["default"] = 0] = "default";
54
- Direction[Direction["ascending"] = 1] = "ascending";
55
- Direction[Direction["descending"] = -1] = "descending";
56
- })(Direction || (Direction = {}));
57
- var AITableFilterLogical;
58
- (function (AITableFilterLogical) {
59
- AITableFilterLogical["and"] = "and";
60
- AITableFilterLogical["or"] = "or";
61
- })(AITableFilterLogical || (AITableFilterLogical = {}));
62
-
63
- var ActionName;
64
- (function (ActionName) {
65
- ActionName["UpdateFieldValue"] = "update_field_value";
66
- ActionName["AddRecord"] = "add_record";
67
- ActionName["AddField"] = "add_field";
68
- ActionName["MoveField"] = "move_field";
69
- ActionName["MoveRecord"] = "move_record";
70
- ActionName["RemoveField"] = "remove_field";
71
- ActionName["RemoveRecord"] = "remove_record";
72
- ActionName["SetField"] = "set_field";
73
- ActionName["SetView"] = "set_view";
74
- ActionName["AddView"] = "add_view";
75
- ActionName["RemoveView"] = "remove_view";
76
- ActionName["SetRecordPositions"] = "set_record_positions";
77
- ActionName["UpdateSystemFieldValue"] = "update_system_field_value";
78
- })(ActionName || (ActionName = {}));
79
- var ExecuteType;
80
- (function (ExecuteType) {
81
- ExecuteType[ExecuteType["Execute"] = 0] = "Execute";
82
- ExecuteType[ExecuteType["Undo"] = 1] = "Undo";
83
- ExecuteType[ExecuteType["Redo"] = 2] = "Redo";
84
- })(ExecuteType || (ExecuteType = {}));
85
-
86
- var SystemFieldIndex;
87
- (function (SystemFieldIndex) {
88
- SystemFieldIndex[SystemFieldIndex["Id"] = 0] = "Id";
89
- SystemFieldIndex[SystemFieldIndex["ShortId"] = 1] = "ShortId";
90
- SystemFieldIndex[SystemFieldIndex["CreatedAt"] = 2] = "CreatedAt";
91
- SystemFieldIndex[SystemFieldIndex["CreatedBy"] = 3] = "CreatedBy";
92
- SystemFieldIndex[SystemFieldIndex["Positions"] = 4] = "Positions";
93
- SystemFieldIndex[SystemFieldIndex["UpdatedAt"] = 5] = "UpdatedAt";
94
- SystemFieldIndex[SystemFieldIndex["UpdatedBy"] = 6] = "UpdatedBy";
95
- })(SystemFieldIndex || (SystemFieldIndex = {}));
96
- function toSyncElement(node) {
97
- const element = new Y.Map();
98
- for (const key in node) {
99
- element.set(key, node[key]);
100
- }
101
- return element;
102
- }
103
- function toRecordSyncElement(record, fields) {
104
- const systemFieldValues = new Y.Array();
105
- // 临时方案:为了解决删除时协同操作无法精准获取删除的 id 的问题,将原来的[idValue] 改为[{'_id': idValue}]
106
- systemFieldValues.insert(0, getSystemFieldValues(record));
107
- const customFieldValues = new Y.Array();
108
- const valuesArray = [];
109
- fields.forEach((field) => {
110
- let value = record['values'][field._id];
111
- // yjs will throw an error if the value is undefined.
112
- if (value === undefined) {
113
- value = null;
114
- }
115
- valuesArray.push(value);
116
- });
117
- customFieldValues.insert(0, valuesArray);
118
- // To save memory, convert map to array.
119
- const element = new Y.Array();
120
- element.insert(0, [systemFieldValues, customFieldValues]);
121
- return element;
122
- }
123
- function translatePositionToPath(data, position, activeViewId, indexOffset = 0) {
124
- let index = data.findIndex((value, index) => {
125
- if (index === 0) {
126
- return position < value.positions[activeViewId];
127
- }
128
- return position > data[index - 1].positions[activeViewId] && position < value.positions[activeViewId];
129
- });
130
- if (index === -1) {
131
- index = data.length;
132
- }
133
- return [index + indexOffset];
134
- }
135
- function getShareTypeNumberPath(path) {
136
- return path.filter((node) => typeof node === 'number');
137
- }
138
- function getSharedRecordId(records, recordIndex) {
139
- return records && records.get(recordIndex).get(0).get(0)['_id'];
140
- }
141
- function getSharedRecord(records, recordIndex) {
142
- return records && records.get(recordIndex);
143
- }
144
- function getSharedMapValueId(values, index) {
145
- return values && values.get(index).get('_id');
146
- }
147
- function getSharedRecordIndex(sharedRecords, recordId) {
148
- let recordIndex = -1;
149
- for (let index = 0; index < sharedRecords.length; index++) {
150
- const sharedRecordId = getSharedRecordId(sharedRecords, index);
151
- if (sharedRecordId === recordId) {
152
- recordIndex = index;
153
- break;
154
- }
155
- }
156
- return recordIndex;
157
- }
158
- function getSharedMapValueIndex(sharedNodes, id) {
159
- let nodeIndex = -1;
160
- for (let index = 0; index < sharedNodes.length; index++) {
161
- const sharedId = getSharedMapValueId(sharedNodes, index);
162
- if (sharedId === id) {
163
- nodeIndex = index;
164
- break;
165
- }
166
- }
167
- return nodeIndex;
168
- }
169
- const getSystemFieldValues = (record) => {
170
- return [
171
- { _id: record['_id'] },
172
- record.short_id,
173
- record.created_at,
174
- record.created_by,
175
- record['positions'],
176
- record.updated_at,
177
- record.updated_by
178
- ];
179
- };
180
- const getCustomFieldValues = (record) => {
181
- throw new Error('No implement');
182
- };
183
- const getValuesByCustomFieldValues = (customFieldValues, fields) => {
184
- const fieldIds = fields.map((item) => item._id);
185
- const recordValue = {};
186
- fieldIds.forEach((item, index) => {
187
- recordValue[item] = customFieldValues[index];
188
- });
189
- return recordValue;
190
- };
191
- const getTrackableEntityBySystemFieldValues = (systemFieldValues) => {
192
- return {
193
- created_at: systemFieldValues[SystemFieldIndex.CreatedAt],
194
- created_by: systemFieldValues[SystemFieldIndex.CreatedBy],
195
- updated_at: systemFieldValues[SystemFieldIndex.UpdatedAt],
196
- updated_by: systemFieldValues[SystemFieldIndex.UpdatedBy]
197
- };
198
- };
199
- const getIdBySystemFieldValues = (systemFieldValues) => {
200
- return systemFieldValues[SystemFieldIndex.Id]['_id'];
201
- };
202
- const getShortIdBySystemFieldValues = (systemFieldValues) => {
203
- return systemFieldValues[SystemFieldIndex.ShortId];
204
- };
205
- const getIdBySystemFieldValuesType = (systemFieldValuesType) => {
206
- return systemFieldValuesType.get(SystemFieldIndex.Id)['_id'];
207
- };
208
- const getPositionsBySystemFieldValues = (systemFieldValues) => {
209
- return systemFieldValues[SystemFieldIndex.Positions];
210
- };
211
- const getPositionsByRecordSyncElement = (recordSyncElement) => {
212
- const systemFieldType = recordSyncElement.get(0);
213
- const positions = systemFieldType.get(SystemFieldIndex.Positions);
214
- return positions;
215
- };
216
- const setRecordPositions$1 = (recordSyncElement, newPositions) => {
217
- const systemFieldType = recordSyncElement.get(0);
218
- systemFieldType.delete(SystemFieldIndex.Positions);
219
- systemFieldType.insert(SystemFieldIndex.Positions, [newPositions]);
220
- };
221
- const setRecordUpdatedInfo = (recordSyncElement, info) => {
222
- const systemFieldType = recordSyncElement.get(0);
223
- systemFieldType.delete(SystemFieldIndex.UpdatedAt, 2);
224
- systemFieldType.insert(SystemFieldIndex.UpdatedAt, [info.updated_at, info.updated_by]);
225
- };
226
-
227
- const AI_TABLE_CONTENT_FIELD_NAME = 'content';
228
-
229
- const createSharedType = (fieldName = AI_TABLE_CONTENT_FIELD_NAME) => {
230
- const doc = new Y.Doc();
231
- const sharedType = doc.getMap(fieldName);
232
- return sharedType;
233
- };
234
- const getDataBySharedType = (sharedType) => {
235
- const data = sharedType.toJSON();
236
- const fields = data['fields'];
237
- const records = getRecordsBySharedJson(data['records'], fields);
238
- const views = data['views'];
239
- return {
240
- records,
241
- fields,
242
- views
243
- };
244
- };
245
- const getSharedTypeByData = (doc, initializeValue) => {
246
- const sharedType = doc.getMap(AI_TABLE_CONTENT_FIELD_NAME);
247
- toSharedType(sharedType, initializeValue);
248
- return sharedType;
249
- };
250
- function toSharedType(sharedType, data) {
251
- sharedType.doc.transact(() => {
252
- const fieldSharedType = new Y.Array();
253
- fieldSharedType.insert(0, data.fields.map(toSyncElement));
254
- sharedType.set('fields', fieldSharedType);
255
- const recordSharedType = new Y.Array();
256
- sharedType.set('records', recordSharedType);
257
- recordSharedType.insert(0, data.records.map((record) => {
258
- return toRecordSyncElement(record, data.fields);
259
- }));
260
- const viewsSharedType = new Y.Array();
261
- sharedType.set('views', viewsSharedType);
262
- viewsSharedType.insert(0, data.views.map(toSyncElement));
263
- });
264
- }
265
- const getRecordsBySharedJson = (recordJsonArray, fields) => {
266
- return recordJsonArray.map((record) => {
267
- const [systemFieldValues, customFieldValues] = record;
268
- return {
269
- _id: getIdBySystemFieldValues(systemFieldValues),
270
- short_id: getShortIdBySystemFieldValues(systemFieldValues),
271
- ...getTrackableEntityBySystemFieldValues(systemFieldValues),
272
- positions: getPositionsBySystemFieldValues(systemFieldValues),
273
- values: getValuesByCustomFieldValues(customFieldValues, fields)
274
- };
275
- });
276
- };
277
-
278
- function translateArrayEvent(aiTable, sharedType, event) {
279
- let offset = 0;
280
- let targetPath = getShareTypeNumberPath(event.path);
281
- const isRecordsTranslate = event.path.includes('records');
282
- const isFieldsTranslate = event.path.includes('fields');
283
- const isViewsTranslate = event.path.includes('views');
284
- const actions = [];
285
- const activeViewId = aiTable.activeViewId();
286
- event.changes.delta.forEach((delta) => {
287
- if ('retain' in delta) {
288
- offset += delta.retain ?? 0;
289
- }
290
- if ('delete' in delta) {
291
- if (isAddOrRemove(targetPath)) {
292
- if (isViewsTranslate) {
293
- const removeView = aiTable.views()[offset];
294
- if (removeView) {
295
- actions.push({
296
- type: ActionName.RemoveView,
297
- path: [removeView._id]
298
- });
299
- }
300
- }
301
- else {
302
- const type = isRecordsTranslate ? ActionName.RemoveRecord : ActionName.RemoveField;
303
- const removeIds = getRemoveIds(event, type);
304
- if (removeIds.length) {
305
- removeIds.forEach((path) => {
306
- actions.push({
307
- type,
308
- path
309
- });
310
- });
311
- }
312
- }
313
- }
314
- }
315
- if ('insert' in delta) {
316
- if (isArray(delta.insert)) {
317
- if (isRecordsTranslate) {
318
- if (isAddOrRemove(targetPath)) {
319
- delta.insert?.map((item) => {
320
- const data = item.toJSON();
321
- const [systemFieldValues, customFieldValues] = data;
322
- const positions = getPositionsBySystemFieldValues(systemFieldValues);
323
- const position = positions[activeViewId];
324
- const path = translatePositionToPath(aiTable.records(), position, activeViewId);
325
- actions.push({
326
- type: ActionName.AddRecord,
327
- path: path,
328
- record: {
329
- _id: getIdBySystemFieldValues(systemFieldValues),
330
- short_id: getShortIdBySystemFieldValues(systemFieldValues),
331
- ...getTrackableEntityBySystemFieldValues(systemFieldValues),
332
- positions: getPositionsBySystemFieldValues(systemFieldValues),
333
- values: getValuesByCustomFieldValues(customFieldValues, aiTable.gridData().fields)
334
- }
335
- });
336
- });
337
- }
338
- else {
339
- try {
340
- const sharedRecords = sharedType.get('records');
341
- const sharedFields = sharedType.get('fields');
342
- let systemFieldOffset = 0;
343
- delta.insert?.map((item, index) => {
344
- const recordIndex = targetPath[0];
345
- const fieldIndex = offset + index;
346
- const record = aiTable.records()[recordIndex];
347
- if (isSystemFieldOperation(targetPath)) {
348
- if (isPositionsOperation(fieldIndex)) {
349
- const newPositions = {};
350
- for (const key in item) {
351
- newPositions[key] = item[key];
352
- }
353
- actions.push({
354
- type: ActionName.SetRecordPositions,
355
- path: [record._id],
356
- positions: newPositions
357
- });
358
- // 此处的循环会包含 updated_at 和 updated_by 各一次,这里只处理 updated_by 同时包含两个字段的修改
359
- }
360
- else if (isUpdatedByOperation(fieldIndex + systemFieldOffset)) {
361
- const systemFieldValues = getSharedRecord(sharedRecords, recordIndex).get(0).toJSON();
362
- const { updated_at, updated_by } = getTrackableEntityBySystemFieldValues(systemFieldValues);
363
- actions.push({
364
- type: ActionName.UpdateSystemFieldValue,
365
- path: [record._id],
366
- updatedInfo: { updated_at, updated_by }
367
- });
368
- }
369
- systemFieldOffset++;
370
- }
371
- else {
372
- const recordId = getSharedRecordId(sharedRecords, recordIndex);
373
- const fieldId = getSharedMapValueId(sharedFields, fieldIndex);
374
- const path = [recordId, fieldId];
375
- const fieldValue = AITableQueries.getFieldValue(aiTable, path);
376
- // To exclude insert triggered by field inserts.
377
- if (fieldValue !== item) {
378
- actions.push({
379
- type: ActionName.UpdateFieldValue,
380
- path,
381
- fieldValue,
382
- newFieldValue: item
383
- });
384
- }
385
- }
386
- });
387
- }
388
- catch (error) { }
389
- }
390
- }
391
- if (isFieldsTranslate) {
392
- delta.insert?.map((item, index) => {
393
- const data = item.toJSON();
394
- const path = translatePositionToPath(aiTable.gridData().fields, data['positions'][activeViewId], activeViewId, index);
395
- actions.push({
396
- type: ActionName.AddField,
397
- path,
398
- field: data
399
- });
400
- });
401
- }
402
- if (isViewsTranslate) {
403
- delta.insert?.map((item, index) => {
404
- const data = item.toJSON();
405
- actions.push({
406
- type: ActionName.AddView,
407
- path: [offset + index],
408
- view: data
409
- });
410
- });
411
- }
412
- }
413
- }
414
- });
415
- return actions;
416
- }
417
- function isAddOrRemove(targetPath) {
418
- return targetPath.length === 0;
419
- }
420
- function isSystemFieldOperation(targetPath) {
421
- if (targetPath.length === 2 && targetPath[1] === 0) {
422
- return true;
423
- }
424
- return false;
425
- }
426
- function isCustomFieldOperation(targetPath) {
427
- if (targetPath.length === 2 && targetPath[1] === 1) {
428
- return true;
429
- }
430
- return false;
431
- }
432
- function isPositionsOperation(fieldIndex) {
433
- return fieldIndex === SystemFieldIndex.Positions;
434
- }
435
- function isUpdatedByOperation(fieldIndex) {
436
- return fieldIndex === SystemFieldIndex.UpdatedBy;
437
- }
438
- function getRemoveIds(event, type) {
439
- const ids = [];
440
- if (!type) {
441
- return ids;
442
- }
443
- Y.iterateDeletedStructs(event.transaction, event.transaction.deleteSet,
444
- // @param {Item|GC} item
445
- (item) => {
446
- if (item instanceof Y.Item && item.deleted) {
447
- if (type === ActionName.RemoveField && item.parentSub === '_id') {
448
- ids.push(item.content.getContent());
449
- }
450
- if (type === ActionName.RemoveRecord) {
451
- const content = item.content.getContent();
452
- if (content[0] && content[0]['_id']) {
453
- ids.push([content[0]['_id']]);
454
- }
455
- }
456
- }
457
- });
458
- return ids;
459
- }
460
-
461
50
  function translateMapEvent(aiTable, sharedType, event) {
462
51
  const isFieldsTranslate = event.path.includes('fields');
463
52
  const isViewTranslate = event.path.includes('views');
@@ -506,7 +95,7 @@ function translateMapEvent(aiTable, sharedType, event) {
506
95
 
507
96
  function translateYjsEvent(aiTable, sharedType, event) {
508
97
  if (event instanceof Y.YArrayEvent) {
509
- return translateArrayEvent(aiTable, sharedType, event);
98
+ return translateArrayEvent(sharedType, event);
510
99
  }
511
100
  if (event instanceof Y.YMapEvent) {
512
101
  return translateMapEvent(aiTable, sharedType, event);
@@ -538,8 +127,14 @@ function updateFieldValue$2(aiTable, sharedType, action) {
538
127
  if (recordIndex > -1 && fieldIndex > -1) {
539
128
  const record = sharedRecords?.get(recordIndex);
540
129
  const customField = record.get(1);
541
- customField.delete(fieldIndex);
542
- customField.insert(fieldIndex, [action.newFieldValue]);
130
+ if (fieldIndex < customField.length) {
131
+ customField.delete(fieldIndex);
132
+ customField.insert(fieldIndex, [action.newFieldValue]);
133
+ }
134
+ else {
135
+ // 幽灵单元格,协同的后端会对这样的数据进行纠正,但是在纠正同步到位之前,前端可能会出现幽灵单元格
136
+ console.error('Field index out of bounds, cannot update field value');
137
+ }
543
138
  }
544
139
  }
545
140
  return sharedType;
@@ -584,7 +179,7 @@ function addNode(aiTable, sharedType, action) {
584
179
  records && records.push([toRecordSyncElement(action.record, aiTable.fields())]);
585
180
  break;
586
181
  case ActionName.AddView:
587
- views && views.push([toSyncElement(action.view)]);
182
+ views && views.push([toMapSyncElement(action.view)]);
588
183
  break;
589
184
  case ActionName.SetRecordPositions:
590
185
  if (records) {
@@ -614,16 +209,22 @@ function addNode(aiTable, sharedType, action) {
614
209
  break;
615
210
  case ActionName.AddField:
616
211
  if (fields && records) {
617
- const { field, path } = action;
618
- const [insertIndex] = path;
619
- const fieldSyncElement = toSyncElement(field);
212
+ const { field } = action;
213
+ const insertIndex = fields.length;
214
+ const fieldSyncElement = toMapSyncElement(field);
620
215
  fields.insert(insertIndex, [fieldSyncElement]);
621
216
  for (let value of records) {
622
217
  const customFieldValues = value.get(1);
623
218
  const systemFieldValues = value.get(0);
624
219
  const recordEntity = aiTable.recordsMap()[getIdBySystemFieldValuesType(systemFieldValues)];
625
220
  const newFieldValue = recordEntity.values[action.field._id];
626
- customFieldValues.insert(insertIndex, [newFieldValue]);
221
+ // 幽灵单元格,暂不处理交给后端统一处理
222
+ if (insertIndex <= customFieldValues.length) {
223
+ customFieldValues.insert(insertIndex, [newFieldValue]);
224
+ }
225
+ else {
226
+ console.error('Field index out of bounds, cannot initialize record value for new field');
227
+ }
627
228
  }
628
229
  }
629
230
  break;
@@ -695,6 +296,111 @@ function applyActionOps(aiTable, sharedType, actions) {
695
296
  return sharedType;
696
297
  }
697
298
 
299
+ const createSharedType = (fieldName = AI_TABLE_CONTENT_FIELD_NAME) => {
300
+ const doc = new Y.Doc();
301
+ const sharedType = doc.getMap(fieldName);
302
+ return sharedType;
303
+ };
304
+ const getDataBySharedType = (sharedType) => {
305
+ const data = sharedType.toJSON();
306
+ const fields = data['fields'];
307
+ const records = getRecordsBySharedJson(data['records'], fields);
308
+ const views = data['views'];
309
+ return {
310
+ records,
311
+ fields,
312
+ views
313
+ };
314
+ };
315
+ const getSharedTypeByData = (doc, initializeValue) => {
316
+ const sharedType = doc.getMap(AI_TABLE_CONTENT_FIELD_NAME);
317
+ toSharedType(sharedType, initializeValue);
318
+ return sharedType;
319
+ };
320
+ function toSharedType(sharedType, data) {
321
+ sharedType.doc.transact(() => {
322
+ const fieldSharedType = new Y.Array();
323
+ fieldSharedType.insert(0, data.fields.map(toMapSyncElement));
324
+ sharedType.set('fields', fieldSharedType);
325
+ const recordSharedType = new Y.Array();
326
+ sharedType.set('records', recordSharedType);
327
+ recordSharedType.insert(0, data.records.map((record) => {
328
+ return toRecordSyncElement(record, data.fields);
329
+ }));
330
+ const viewsSharedType = new Y.Array();
331
+ sharedType.set('views', viewsSharedType);
332
+ viewsSharedType.insert(0, data.views.map(toMapSyncElement));
333
+ });
334
+ }
335
+ const getRecordsBySharedJson = (recordJsonArray, fields) => {
336
+ return recordJsonArray.map((record) => {
337
+ const [systemFieldValues, customFieldValues] = record;
338
+ return {
339
+ _id: getIdBySystemFieldValues(systemFieldValues),
340
+ short_id: getShortIdBySystemFieldValues(systemFieldValues),
341
+ ...getTrackableEntityBySystemFieldValues(systemFieldValues),
342
+ positions: getPositionsBySystemFieldValues(systemFieldValues),
343
+ values: getValuesByCustomFieldValues(customFieldValues, fields)
344
+ };
345
+ });
346
+ };
347
+
348
+ class UndoManagerService {
349
+ constructor() {
350
+ this._undoManager = null;
351
+ this._canUndoCount = signal(0);
352
+ this._canRedoCount = signal(0);
353
+ this._aiTable = null;
354
+ }
355
+ get undoManager() {
356
+ return this._undoManager;
357
+ }
358
+ get canUndoCount() {
359
+ return this._canUndoCount;
360
+ }
361
+ get canRedoCount() {
362
+ return this._canRedoCount;
363
+ }
364
+ initialize(sharedType, aiTable) {
365
+ if (!sharedType || !aiTable) {
366
+ return;
367
+ }
368
+ this._aiTable = aiTable;
369
+ this._undoManager = new UndoManager(sharedType, {
370
+ trackedOrigins: new Set([aiTable]),
371
+ captureTimeout: 300
372
+ });
373
+ this._undoManager.on('stack-item-added', () => {
374
+ this._canUndoCount.set(this._undoManager.undoStack.length);
375
+ this._canRedoCount.set(this._undoManager.redoStack.length);
376
+ });
377
+ this._undoManager.on('stack-item-popped', () => {
378
+ this._canUndoCount.set(this._undoManager.undoStack.length);
379
+ this._canRedoCount.set(this._undoManager.redoStack.length);
380
+ });
381
+ }
382
+ undo() {
383
+ if (this._undoManager?.canUndo()) {
384
+ YjsAITable.asUndo(this._aiTable, () => this._undoManager.undo());
385
+ }
386
+ }
387
+ redo() {
388
+ if (this._undoManager?.canRedo()) {
389
+ YjsAITable.asUndo(this._aiTable, () => this._undoManager.redo());
390
+ }
391
+ }
392
+ destroy() {
393
+ this._undoManager = null;
394
+ this._canUndoCount.set(0);
395
+ this._canRedoCount.set(0);
396
+ }
397
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: UndoManagerService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
398
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: UndoManagerService }); }
399
+ }
400
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: UndoManagerService, decorators: [{
401
+ type: Injectable
402
+ }] });
403
+
698
404
  var AITableStateI18nKey;
699
405
  (function (AITableStateI18nKey) {
700
406
  AITableStateI18nKey["copyField"] = "copyField";
@@ -753,6 +459,7 @@ function generateCopyName(aiTable, existNames, name) {
753
459
  return newName;
754
460
  }
755
461
 
462
+ // import { FieldModelMap } from '../../../../grid/src/utils/field/field';
756
463
  function getSortRecords(aiTable, records, activeView, sortKeysMap) {
757
464
  if (!activeView?.settings || !activeView.settings.sorts?.length) {
758
465
  return sortByViewPosition(records, activeView);
@@ -958,11 +665,10 @@ function getFieldsSizeMap(fields, activeView) {
958
665
  return fieldsSizeMap;
959
666
  }
960
667
 
961
- function addField(aiTable, field, path, originId, isCopy) {
668
+ function addField(aiTable, field, originId, isCopy) {
962
669
  const operation = {
963
670
  type: ActionName.AddField,
964
671
  field,
965
- path,
966
672
  originId,
967
673
  isCopy
968
674
  };
@@ -1054,42 +760,19 @@ const apply = (aiTable, records, fields, views, action) => {
1054
760
  break;
1055
761
  }
1056
762
  case ActionName.AddRecord: {
1057
- const [recordIndex] = action.path;
1058
- if (recordIndex > -1) {
1059
- if (!action.record.positions) {
1060
- const activeView = aiTable.views().find((item) => item._id === aiTable.activeViewId());
1061
- let index = recordIndex;
1062
- if (activeView?.settings?.conditions) {
1063
- index = records.length;
1064
- }
1065
- action.record.positions = createDefaultPositions(aiTable.views(), aiTable.activeViewId(), aiTable.records(), index);
1066
- }
1067
- records.splice(recordIndex, 0, action.record);
763
+ if (!action.record.positions) {
764
+ action.record.positions = createDefaultPositions(aiTable.views(), aiTable.activeViewId(), aiTable.records(), records.length);
1068
765
  }
766
+ records.push(action.record);
1069
767
  break;
1070
768
  }
1071
769
  case ActionName.AddField: {
1072
- const [fieldIndex] = action.path;
1073
- if (fieldIndex > -1) {
1074
- const newField = action.field;
1075
- if (!newField.positions) {
1076
- newField.positions = createDefaultPositions(aiTable.views(), aiTable.activeViewId(), aiTable.gridData().fields, action.path[0]);
1077
- }
1078
- fields.splice(fieldIndex, 0, newField);
1079
- records.forEach((item) => {
1080
- item.values[newField._id] =
1081
- action.isCopy && action.originId ? item.values[action.originId] : getDefaultFieldValue(action.field);
1082
- });
1083
- }
1084
- break;
1085
- }
1086
- case ActionName.MoveRecord: {
1087
- if (isPathEqual(action.path, action.newPath)) {
1088
- return;
1089
- }
1090
- const record = records[action.path[0]];
1091
- records.splice(action.path[0], 1);
1092
- records.splice(action.newPath[0], 0, record);
770
+ const newField = action.field;
771
+ fields.push(newField);
772
+ records.forEach((item) => {
773
+ item.values[newField._id] =
774
+ action.isCopy && action.originId ? item.values[action.originId] : getDefaultFieldValue(action.field);
775
+ });
1093
776
  break;
1094
777
  }
1095
778
  case ActionName.RemoveField: {
@@ -1228,13 +911,11 @@ const PositionsActions = {
1228
911
  };
1229
912
 
1230
913
  function updateFieldValue$1(aiTable, value, path) {
1231
- const oldValue = AITableQueries.getFieldValue(aiTable, path);
1232
914
  const field = AITableQueries.getField(aiTable, [path[1]]);
1233
915
  const fieldModel = field && FieldModelMap[field.type];
1234
916
  if (fieldModel && fieldModel.isValid(value)) {
1235
917
  const operation = {
1236
918
  type: ActionName.UpdateFieldValue,
1237
- fieldValue: oldValue,
1238
919
  newFieldValue: value,
1239
920
  path
1240
921
  };
@@ -1252,7 +933,7 @@ function updateSystemFieldValue(aiTable, path, updatedInfo) {
1252
933
  };
1253
934
  aiTable.apply(operation);
1254
935
  }
1255
- function addRecord(aiTable, record, path) {
936
+ function addRecord(aiTable, record) {
1256
937
  const invalidFieldValues = [];
1257
938
  const isValid = Object.entries(record.values).every(([fieldId, value]) => {
1258
939
  const field = AITableQueries.getField(aiTable, [fieldId]);
@@ -1266,8 +947,7 @@ function addRecord(aiTable, record, path) {
1266
947
  if (isValid) {
1267
948
  const operation = {
1268
949
  type: ActionName.AddRecord,
1269
- record,
1270
- path
950
+ record
1271
951
  };
1272
952
  aiTable.apply(operation);
1273
953
  }
@@ -1275,14 +955,6 @@ function addRecord(aiTable, record, path) {
1275
955
  console.error(`Invalid field values at add record. invalidFieldValues: ${invalidFieldValues}`);
1276
956
  }
1277
957
  }
1278
- function moveRecord(aiTable, path, newPath) {
1279
- const operation = {
1280
- type: ActionName.MoveRecord,
1281
- path,
1282
- newPath
1283
- };
1284
- aiTable.apply(operation);
1285
- }
1286
958
  function removeRecord(aiTable, path) {
1287
959
  const operation = {
1288
960
  type: ActionName.RemoveRecord,
@@ -1293,7 +965,6 @@ function removeRecord(aiTable, path) {
1293
965
  const RecordActions = {
1294
966
  addRecord,
1295
967
  updateFieldValue: updateFieldValue$1,
1296
- moveRecord,
1297
968
  removeRecord,
1298
969
  updateSystemFieldValue
1299
970
  };
@@ -1359,34 +1030,6 @@ function updateRecordsUpdatedInfo(aiTable, updatedInfo) {
1359
1030
  });
1360
1031
  }
1361
1032
 
1362
- function addFields(aiTable, options, updatedInfo) {
1363
- const { originId, direction = Direction$1.after, defaultValue, isDuplicate, count = 1 } = options;
1364
- const activeView = aiTable.viewsMap()[aiTable.activeViewId()];
1365
- const fields = getSortFields(aiTable, aiTable.gridData().fields, activeView);
1366
- let addIndex = fields.findIndex((item) => item._id === originId);
1367
- if (direction === Direction$1.after) {
1368
- addIndex++;
1369
- }
1370
- const newFieldIds = idsCreator(count);
1371
- newFieldIds.forEach((id, index) => {
1372
- const newField = { _id: id, ...defaultValue, positions: null };
1373
- Actions.addField(aiTable, newField, [addIndex + index], options.originId, options.isCopy);
1374
- });
1375
- updateRecordsUpdatedInfo(aiTable, updatedInfo);
1376
- }
1377
-
1378
- const buildRemoveFieldItem = (aiTable, getUpdatedInfo) => {
1379
- return {
1380
- type: 'removeField',
1381
- name: getStateI18nTextByKey(aiTable, AITableStateI18nKey.removeField),
1382
- icon: 'trash',
1383
- exec: (aiTable, field) => {
1384
- Actions.removeField(aiTable, [field()._id]);
1385
- updateRecordsUpdatedInfo(aiTable, getUpdatedInfo());
1386
- }
1387
- };
1388
- };
1389
-
1390
1033
  function createDefaultPositions(views, activeId, data, index) {
1391
1034
  const positions = {};
1392
1035
  const position = getPosition(data, activeId, index);
@@ -1474,12 +1117,47 @@ function removeView(aiTable, records, fields, activeViewId) {
1474
1117
  ViewActions.removeView(aiTable, [activeViewId]);
1475
1118
  }
1476
1119
 
1120
+ function addFields(aiTable, options, updatedInfo) {
1121
+ const { defaultValue, isDuplicate, isCopy } = options;
1122
+ const fields = aiTable.gridData().fields;
1123
+ const fieldsMap = aiTable.fieldsMap();
1124
+ const activeViewId = aiTable.activeViewId();
1125
+ const newField = { ...defaultValue };
1126
+ if (fieldsMap[newField._id]) {
1127
+ newField._id = idCreator();
1128
+ }
1129
+ if (isDuplicate) {
1130
+ const currentFieldIndex = fields.findIndex((item) => item._id === options.originId);
1131
+ newField.positions = {
1132
+ ...newField.positions,
1133
+ [activeViewId]: getPosition(fields, activeViewId, currentFieldIndex + 1)
1134
+ };
1135
+ }
1136
+ else {
1137
+ newField.positions = createDefaultPositions(aiTable.views(), aiTable.activeViewId(), aiTable.gridData().fields, fields.length);
1138
+ }
1139
+ Actions.addField(aiTable, newField, options.originId, isCopy || isDuplicate);
1140
+ updateRecordsUpdatedInfo(aiTable, updatedInfo);
1141
+ }
1142
+
1143
+ const buildRemoveFieldItem = (aiTable, getUpdatedInfo) => {
1144
+ return {
1145
+ type: 'removeField',
1146
+ name: getStateI18nTextByKey(aiTable, AITableStateI18nKey.removeField),
1147
+ icon: 'trash',
1148
+ exec: (aiTable, field) => {
1149
+ Actions.removeField(aiTable, [field()._id]);
1150
+ updateRecordsUpdatedInfo(aiTable, getUpdatedInfo());
1151
+ }
1152
+ };
1153
+ };
1154
+
1477
1155
  function addRecords(aiTable, options, trackableEntity) {
1478
- const { originId, direction = Direction$1.after, isDuplicate, count = 1 } = options;
1156
+ const { originId, direction = Direction.after, isDuplicate, count = 1 } = options;
1479
1157
  const activeView = aiTable.viewsMap()[aiTable.activeViewId()];
1480
1158
  const records = getSortRecords(aiTable, aiTable.gridData().records, activeView);
1481
1159
  let addIndex = records.findIndex((item) => item._id === originId);
1482
- if (direction === Direction$1.after) {
1160
+ if (direction === Direction.after) {
1483
1161
  addIndex++;
1484
1162
  }
1485
1163
  const newRecordIds = idsCreator(count);
@@ -1495,7 +1173,7 @@ function addRecords(aiTable, options, trackableEntity) {
1495
1173
  }
1496
1174
  newRecordIds.forEach((id, index) => {
1497
1175
  const newRecord = { _id: id, short_id: newRecordShortIds[index], values: newRecordValues, ...trackableEntity };
1498
- Actions.addRecord(aiTable, newRecord, [addIndex + index]);
1176
+ Actions.addRecord(aiTable, newRecord);
1499
1177
  });
1500
1178
  }
1501
1179
  function getDefaultRecordValues(aiTable, isDuplicate = false, recordId) {
@@ -1619,7 +1297,7 @@ const CopyFieldPropertyItem = (aiTable, addFieldFn) => {
1619
1297
  let newFieldName = generateCopyName(aiTable, allFieldNames, copyName);
1620
1298
  const fieldOptions = {
1621
1299
  originId: field()._id,
1622
- isCopy: true,
1300
+ isDuplicate: true,
1623
1301
  defaultValue: {
1624
1302
  ...field(),
1625
1303
  _id: idCreator(),
@@ -1689,5 +1367,5 @@ const VIEW_ACTIONS = [ActionName.SetView, ActionName.AddView, ActionName.RemoveV
1689
1367
  * Generated bundle index. Do not edit.
1690
1368
  */
1691
1369
 
1692
- export { AITableFilterLogical, AITableStateI18nKey, 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, getDefaultI18nTextByKey, getDefaultRecordDataByFilter, getDefaultRecordValues, getFieldPositionInView, getFieldsSizeMap, getFilteredRecords, getIdBySystemFieldValues, getIdBySystemFieldValuesType, getMaxPosition, getPosition, getPositionsByRecordSyncElement, getPositionsBySystemFieldValues, getRecordsBySharedJson, getShareTypeNumberPath, getSharedMapValueId, getSharedMapValueIndex, getSharedRecord, getSharedRecordId, getSharedRecordIndex, getSharedTypeByData, getShortIdBySystemFieldValues, getSortFields, getSortRecords, getStateI18nTextByKey, getSystemFieldValues, getTrackableEntityBySystemFieldValues, getValuesByCustomFieldValues, isPathEqual, moveFields, moveRecords, removeView, setRecordPositions$1 as setRecordPositions, setRecordUpdatedInfo, sortByViewPosition, sortRecordsBySortInfo, toRecordSyncElement, toSharedType, toSyncElement, translatePositionToPath, translateYjsEvent, updateFieldValue, updateRecordsUpdatedInfo, withState };
1370
+ export { AITableStateI18nKey, Actions, CopyCellsItem, CopyFieldPropertyItem, DividerMenuItem, EditFieldPropertyItem, FLUSHING, PasteCellsItem, RemoveRecordsItem, UndoManagerService, VIEW_ACTIONS, YjsAITable, actionMappers, addFields, addRecords, addView, applyActionOps, applyEvents, applyYjsEvents, buildFieldsByView, buildRecordsByView, buildRemoveFieldItem, createDefaultPositions, createSharedType, doFilter, generateCopyName, getDataBySharedType, getDefaultI18nTextByKey, getDefaultRecordDataByFilter, getDefaultRecordValues, getFieldPositionInView, getFieldsSizeMap, getFilteredRecords, getMaxPosition, getPosition, getRecordsBySharedJson, getSharedTypeByData, getSortFields, getSortRecords, getStateI18nTextByKey, isPathEqual, moveFields, moveRecords, removeView, sortByViewPosition, sortRecordsBySortInfo, toSharedType, translateYjsEvent, updateFieldValue, updateRecordsUpdatedInfo, withState };
1693
1371
  //# sourceMappingURL=ai-table-state.mjs.map