@ai-table/state 0.0.14 → 0.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/action/general.d.ts.map +1 -1
- package/action/index.d.ts +2 -2
- package/action/position.d.ts +5 -7
- package/action/position.d.ts.map +1 -1
- package/action/record.d.ts +3 -1
- package/action/record.d.ts.map +1 -1
- package/constants/default.d.ts +2 -0
- package/constants/default.d.ts.map +1 -0
- package/constants/field-menu-item.d.ts +1 -7
- package/constants/field-menu-item.d.ts.map +1 -1
- package/constants/index.d.ts +1 -0
- package/constants/index.d.ts.map +1 -1
- package/esm2022/action/general.mjs +26 -13
- package/esm2022/action/index.mjs +3 -3
- package/esm2022/action/position.mjs +7 -15
- package/esm2022/action/record.mjs +18 -11
- package/esm2022/constants/default.mjs +2 -0
- package/esm2022/constants/field-menu-item.mjs +1 -9
- package/esm2022/constants/index.mjs +2 -1
- package/esm2022/shared/to-table/array-event.mjs +49 -27
- package/esm2022/shared/to-yjs/add-node.mjs +30 -15
- package/esm2022/shared/to-yjs/index.mjs +15 -14
- package/esm2022/shared/to-yjs/remove-node.mjs +2 -14
- package/esm2022/shared/to-yjs/set-node.mjs +2 -2
- package/esm2022/shared/to-yjs/update-field-value.mjs +2 -2
- package/esm2022/shared/utils/index.mjs +2 -3
- package/esm2022/shared/utils/initialize.mjs +51 -0
- package/esm2022/shared/utils/translate.mjs +75 -18
- package/esm2022/types/action.mjs +3 -3
- package/esm2022/types/shared.mjs +1 -1
- package/esm2022/types/view.mjs +3 -1
- package/esm2022/utils/field/add-fields.mjs +6 -4
- package/esm2022/utils/field/remove-field.mjs +14 -0
- package/esm2022/utils/index.mjs +3 -1
- package/esm2022/utils/record/add-records.mjs +3 -3
- package/esm2022/utils/record/update-field-value.mjs +9 -3
- package/esm2022/utils/record/update-system-field-value.mjs +8 -0
- package/esm2022/utils/view.mjs +4 -4
- package/fesm2022/ai-table-state.mjs +280 -176
- package/fesm2022/ai-table-state.mjs.map +1 -1
- package/package.json +1 -1
- package/shared/to-table/array-event.d.ts +5 -2
- package/shared/to-table/array-event.d.ts.map +1 -1
- package/shared/to-yjs/add-node.d.ts +3 -2
- package/shared/to-yjs/add-node.d.ts.map +1 -1
- package/shared/to-yjs/index.d.ts +2 -2
- package/shared/to-yjs/index.d.ts.map +1 -1
- package/shared/to-yjs/remove-node.d.ts +3 -2
- package/shared/to-yjs/remove-node.d.ts.map +1 -1
- package/shared/to-yjs/set-node.d.ts +2 -1
- package/shared/to-yjs/set-node.d.ts.map +1 -1
- package/shared/to-yjs/update-field-value.d.ts +2 -1
- package/shared/to-yjs/update-field-value.d.ts.map +1 -1
- package/shared/utils/index.d.ts +1 -2
- package/shared/utils/index.d.ts.map +1 -1
- package/shared/utils/initialize.d.ts +20 -0
- package/shared/utils/initialize.d.ts.map +1 -0
- package/shared/utils/translate.d.ts +18 -7
- package/shared/utils/translate.d.ts.map +1 -1
- package/types/action.d.ts +14 -13
- package/types/action.d.ts.map +1 -1
- package/types/shared.d.ts +9 -1
- package/types/shared.d.ts.map +1 -1
- package/types/view.d.ts +3 -0
- package/types/view.d.ts.map +1 -1
- package/utils/field/add-fields.d.ts +2 -2
- package/utils/field/add-fields.d.ts.map +1 -1
- package/utils/field/remove-field.d.ts +9 -0
- package/utils/field/remove-field.d.ts.map +1 -0
- package/utils/index.d.ts +2 -0
- package/utils/index.d.ts.map +1 -1
- package/utils/record/add-records.d.ts +2 -2
- package/utils/record/add-records.d.ts.map +1 -1
- package/utils/record/update-field-value.d.ts +2 -2
- package/utils/record/update-field-value.d.ts.map +1 -1
- package/utils/record/update-system-field-value.d.ts +4 -0
- package/utils/record/update-system-field-value.d.ts.map +1 -0
- package/esm2022/shared/utils/init-shared.mjs +0 -48
- package/esm2022/shared/utils/init-table.mjs +0 -13
- package/shared/utils/init-shared.d.ts +0 -16
- package/shared/utils/init-shared.d.ts.map +0 -1
- package/shared/utils/init-table.d.ts +0 -7
- package/shared/utils/init-table.d.ts.map +0 -1
@@ -1,7 +1,8 @@
|
|
1
1
|
import * as Y from 'yjs';
|
2
2
|
import { isArray, isEmpty as isEmpty$1, isObject, isUndefinedOrNull, TinyDate } from 'ngx-tethys/util';
|
3
|
-
import { AITableQueries,
|
4
|
-
import
|
3
|
+
import { AITableQueries, idCreator, AITableFieldType, getDefaultFieldValue, Direction as Direction$1, AI_TABLE_GRID_FIELD_SERVICE_MAP } from '@ai-table/grid';
|
4
|
+
import * as _ from 'lodash';
|
5
|
+
import ___default, { isEqual } from 'lodash';
|
5
6
|
import { fromUnixTime, subDays } from 'date-fns';
|
6
7
|
import { createDraft, finishDraft } from 'immer';
|
7
8
|
|
@@ -46,6 +47,8 @@ const YjsAITable = {
|
|
46
47
|
|
47
48
|
class Positions {
|
48
49
|
}
|
50
|
+
class RemovePositions {
|
51
|
+
}
|
49
52
|
var Direction;
|
50
53
|
(function (Direction) {
|
51
54
|
Direction[Direction["default"] = 0] = "default";
|
@@ -88,8 +91,8 @@ var ActionName;
|
|
88
91
|
ActionName["SetView"] = "set_view";
|
89
92
|
ActionName["AddView"] = "add_view";
|
90
93
|
ActionName["RemoveView"] = "remove_view";
|
91
|
-
ActionName["
|
92
|
-
ActionName["
|
94
|
+
ActionName["SetRecordPositions"] = "set_record_positions";
|
95
|
+
ActionName["UpdateSystemFieldValue"] = "update_system_field_value";
|
93
96
|
})(ActionName || (ActionName = {}));
|
94
97
|
var ExecuteType;
|
95
98
|
(function (ExecuteType) {
|
@@ -98,29 +101,9 @@ var ExecuteType;
|
|
98
101
|
ExecuteType[ExecuteType["Redo"] = 2] = "Redo";
|
99
102
|
})(ExecuteType || (ExecuteType = {}));
|
100
103
|
|
101
|
-
const
|
102
|
-
|
103
|
-
|
104
|
-
return sharedType;
|
105
|
-
};
|
106
|
-
const initSharedType = (doc, initializeValue) => {
|
107
|
-
const sharedType = doc.getMap('ai-table');
|
108
|
-
toSharedType(sharedType, initializeValue);
|
109
|
-
return sharedType;
|
110
|
-
};
|
111
|
-
function toSharedType(sharedType, data) {
|
112
|
-
sharedType.doc.transact(() => {
|
113
|
-
const fieldSharedType = new Y.Array();
|
114
|
-
fieldSharedType.insert(0, data.fields.map(toSyncElement));
|
115
|
-
sharedType.set('fields', fieldSharedType);
|
116
|
-
const recordSharedType = new Y.Array();
|
117
|
-
sharedType.set('records', recordSharedType);
|
118
|
-
recordSharedType.insert(0, data.records.map(toRecordSyncElement));
|
119
|
-
const viewsSharedType = new Y.Array();
|
120
|
-
sharedType.set('views', viewsSharedType);
|
121
|
-
viewsSharedType.insert(0, data.views.map(toSyncElement));
|
122
|
-
});
|
123
|
-
}
|
104
|
+
const POSITIONS_INDEX = 3;
|
105
|
+
const UPDATED_AT_INDEX = 4;
|
106
|
+
const UPDATED_BY_INDEX = 5;
|
124
107
|
function toSyncElement(node) {
|
125
108
|
const element = new Y.Map();
|
126
109
|
for (const key in node) {
|
@@ -128,41 +111,21 @@ function toSyncElement(node) {
|
|
128
111
|
}
|
129
112
|
return element;
|
130
113
|
}
|
131
|
-
function toRecordSyncElement(record) {
|
132
|
-
const
|
114
|
+
function toRecordSyncElement(record, fields) {
|
115
|
+
const systemFieldValues = new Y.Array();
|
133
116
|
// 临时方案:为了解决删除时协同操作无法精准获取删除的 id 的问题,将原来的[idValue] 改为[{'_id': idValue}]
|
134
|
-
|
135
|
-
|
136
|
-
const
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
editableArray.insert(0, [...editableFields, record['positions']]);
|
117
|
+
systemFieldValues.insert(0, getSystemFieldValues(record));
|
118
|
+
const customFieldValues = new Y.Array();
|
119
|
+
const valuesArray = [];
|
120
|
+
fields.forEach((field) => {
|
121
|
+
valuesArray.push(record['values'][field._id]);
|
122
|
+
});
|
123
|
+
customFieldValues.insert(0, valuesArray);
|
142
124
|
// To save memory, convert map to array.
|
143
125
|
const element = new Y.Array();
|
144
|
-
element.insert(0, [
|
126
|
+
element.insert(0, [systemFieldValues, customFieldValues]);
|
145
127
|
return element;
|
146
128
|
}
|
147
|
-
|
148
|
-
const translateToRecordValues = (arrayRecord, fields) => {
|
149
|
-
const fieldIds = fields.map((item) => item._id);
|
150
|
-
const recordValue = {};
|
151
|
-
fieldIds.forEach((item, index) => {
|
152
|
-
recordValue[item] = arrayRecord[index] || '';
|
153
|
-
});
|
154
|
-
return recordValue;
|
155
|
-
};
|
156
|
-
const translateToRecords = (arrayRecords, fields) => {
|
157
|
-
return arrayRecords.map((record) => {
|
158
|
-
const [nonEditableArray, editableArray] = record;
|
159
|
-
return {
|
160
|
-
_id: nonEditableArray[0]['_id'],
|
161
|
-
positions: editableArray[editableArray.length - 1],
|
162
|
-
values: translateToRecordValues(editableArray.slice(0, editableArray.length - 1), fields)
|
163
|
-
};
|
164
|
-
});
|
165
|
-
};
|
166
129
|
function translatePositionToPath(data, position, activeViewId) {
|
167
130
|
let index = data.findIndex((value, index) => {
|
168
131
|
if (index === 0) {
|
@@ -181,6 +144,9 @@ function getShareTypeNumberPath(path) {
|
|
181
144
|
function getSharedRecordId(records, recordIndex) {
|
182
145
|
return records && records.get(recordIndex).get(0).get(0)['_id'];
|
183
146
|
}
|
147
|
+
function getSharedRecord(records, recordIndex) {
|
148
|
+
return records && records.get(recordIndex);
|
149
|
+
}
|
184
150
|
function getSharedMapValueId(values, index) {
|
185
151
|
return values && values.get(index).get('_id');
|
186
152
|
}
|
@@ -206,11 +172,64 @@ function getSharedMapValueIndex(sharedNodes, id) {
|
|
206
172
|
}
|
207
173
|
return nodeIndex;
|
208
174
|
}
|
175
|
+
const getSystemFieldValues = (record) => {
|
176
|
+
return [{ _id: record['_id'] }, record.created_at, record.created_by, record['positions'], record.updated_at, record.updated_by];
|
177
|
+
};
|
178
|
+
const getCustomFieldValues = (record) => {
|
179
|
+
throw new Error('No implement');
|
180
|
+
};
|
181
|
+
const getValuesByCustomFieldValues = (customFieldValues, fields) => {
|
182
|
+
const fieldIds = fields.map((item) => item._id);
|
183
|
+
const recordValue = {};
|
184
|
+
fieldIds.forEach((item, index) => {
|
185
|
+
recordValue[item] = customFieldValues[index] || '';
|
186
|
+
});
|
187
|
+
return recordValue;
|
188
|
+
};
|
189
|
+
const getTrackableEntityBySystemFieldValues = (systemFieldValues) => {
|
190
|
+
return {
|
191
|
+
created_at: systemFieldValues[1],
|
192
|
+
created_by: systemFieldValues[2],
|
193
|
+
updated_at: systemFieldValues[4],
|
194
|
+
updated_by: systemFieldValues[5]
|
195
|
+
};
|
196
|
+
};
|
197
|
+
const getIdBySystemFieldValues = (systemFieldValues) => {
|
198
|
+
return systemFieldValues[0]['_id'];
|
199
|
+
};
|
200
|
+
const getIdBySystemFieldValuesType = (systemFieldValuesType) => {
|
201
|
+
return systemFieldValuesType.get(0)['_id'];
|
202
|
+
};
|
203
|
+
const getPositionsBySystemFieldValues = (systemFieldValues) => {
|
204
|
+
return systemFieldValues[POSITIONS_INDEX];
|
205
|
+
};
|
206
|
+
const getPositionsByRecordSyncElement = (recordSyncElement) => {
|
207
|
+
const systemFieldType = recordSyncElement.get(0);
|
208
|
+
const positions = systemFieldType.get(POSITIONS_INDEX);
|
209
|
+
return positions;
|
210
|
+
};
|
211
|
+
const setRecordPositions$1 = (recordSyncElement, newPositions) => {
|
212
|
+
const systemFieldType = recordSyncElement.get(0);
|
213
|
+
systemFieldType.delete(POSITIONS_INDEX);
|
214
|
+
systemFieldType.insert(POSITIONS_INDEX, [newPositions]);
|
215
|
+
};
|
216
|
+
const setRecordUpdatedInfo = (recordSyncElement, info) => {
|
217
|
+
const systemFieldType = recordSyncElement.get(0);
|
218
|
+
systemFieldType.delete(UPDATED_AT_INDEX, 2);
|
219
|
+
systemFieldType.insert(UPDATED_AT_INDEX, [info.updated_at, info.updated_by]);
|
220
|
+
};
|
209
221
|
|
210
|
-
const
|
222
|
+
const AI_TABLE_CONTENT_FIELD_NAME = 'content';
|
223
|
+
|
224
|
+
const createSharedType = (fieldName = AI_TABLE_CONTENT_FIELD_NAME) => {
|
225
|
+
const doc = new Y.Doc();
|
226
|
+
const sharedType = doc.getMap(fieldName);
|
227
|
+
return sharedType;
|
228
|
+
};
|
229
|
+
const getDataBySharedType = (sharedType) => {
|
211
230
|
const data = sharedType.toJSON();
|
212
231
|
const fields = data['fields'];
|
213
|
-
const records =
|
232
|
+
const records = getRecordsBySharedJson(data['records'], fields);
|
214
233
|
const views = data['views'];
|
215
234
|
return {
|
216
235
|
records,
|
@@ -218,6 +237,37 @@ const initTable = (sharedType) => {
|
|
218
237
|
views
|
219
238
|
};
|
220
239
|
};
|
240
|
+
const getSharedTypeByData = (doc, initializeValue) => {
|
241
|
+
const sharedType = doc.getMap(AI_TABLE_CONTENT_FIELD_NAME);
|
242
|
+
toSharedType(sharedType, initializeValue);
|
243
|
+
return sharedType;
|
244
|
+
};
|
245
|
+
function toSharedType(sharedType, data) {
|
246
|
+
sharedType.doc.transact(() => {
|
247
|
+
const fieldSharedType = new Y.Array();
|
248
|
+
fieldSharedType.insert(0, data.fields.map(toSyncElement));
|
249
|
+
sharedType.set('fields', fieldSharedType);
|
250
|
+
const recordSharedType = new Y.Array();
|
251
|
+
sharedType.set('records', recordSharedType);
|
252
|
+
recordSharedType.insert(0, data.records.map((record) => {
|
253
|
+
return toRecordSyncElement(record, data.fields);
|
254
|
+
}));
|
255
|
+
const viewsSharedType = new Y.Array();
|
256
|
+
sharedType.set('views', viewsSharedType);
|
257
|
+
viewsSharedType.insert(0, data.views.map(toSyncElement));
|
258
|
+
});
|
259
|
+
}
|
260
|
+
const getRecordsBySharedJson = (recordJsonArray, fields) => {
|
261
|
+
return recordJsonArray.map((record) => {
|
262
|
+
const [systemFieldValues, customFieldValues] = record;
|
263
|
+
return {
|
264
|
+
_id: getIdBySystemFieldValues(systemFieldValues),
|
265
|
+
...getTrackableEntityBySystemFieldValues(systemFieldValues),
|
266
|
+
positions: getPositionsBySystemFieldValues(systemFieldValues),
|
267
|
+
values: getValuesByCustomFieldValues(customFieldValues, fields)
|
268
|
+
};
|
269
|
+
});
|
270
|
+
};
|
221
271
|
|
222
272
|
function translateArrayEvent(aiTable, sharedType, event) {
|
223
273
|
let offset = 0;
|
@@ -262,15 +312,18 @@ function translateArrayEvent(aiTable, sharedType, event) {
|
|
262
312
|
if (isAddOrRemove(targetPath)) {
|
263
313
|
delta.insert?.map((item) => {
|
264
314
|
const data = item.toJSON();
|
265
|
-
const [
|
266
|
-
const
|
315
|
+
const [systemFieldValues, customFieldValues] = data;
|
316
|
+
const positions = getPositionsBySystemFieldValues(customFieldValues);
|
317
|
+
const position = positions[activeViewId];
|
267
318
|
const path = translatePositionToPath(aiTable.records(), position, activeViewId);
|
268
319
|
actions.push({
|
269
320
|
type: ActionName.AddRecord,
|
270
321
|
path: path,
|
271
322
|
record: {
|
272
|
-
_id:
|
273
|
-
|
323
|
+
_id: getIdBySystemFieldValues(systemFieldValues),
|
324
|
+
...getTrackableEntityBySystemFieldValues(systemFieldValues),
|
325
|
+
positions: getPositionsBySystemFieldValues(customFieldValues),
|
326
|
+
values: getValuesByCustomFieldValues(customFieldValues, aiTable.fields())
|
274
327
|
}
|
275
328
|
});
|
276
329
|
});
|
@@ -279,30 +332,34 @@ function translateArrayEvent(aiTable, sharedType, event) {
|
|
279
332
|
try {
|
280
333
|
const sharedRecords = sharedType.get('records');
|
281
334
|
const sharedFields = sharedType.get('fields');
|
335
|
+
let systemFieldOffset = 0;
|
282
336
|
delta.insert?.map((item) => {
|
283
337
|
const recordIndex = targetPath[0];
|
284
338
|
const fieldIndex = offset;
|
285
339
|
const record = aiTable.records()[recordIndex];
|
286
|
-
if (
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
path: [record._id],
|
292
|
-
position: {
|
293
|
-
[key]: item[key]
|
294
|
-
}
|
295
|
-
});
|
340
|
+
if (isSystemFieldOperation(targetPath)) {
|
341
|
+
if (isPositionsOperation(fieldIndex)) {
|
342
|
+
const newPositions = {};
|
343
|
+
for (const key in item) {
|
344
|
+
newPositions[key] = item[key];
|
296
345
|
}
|
346
|
+
actions.push({
|
347
|
+
type: ActionName.SetRecordPositions,
|
348
|
+
path: [record._id],
|
349
|
+
positions: newPositions
|
350
|
+
});
|
351
|
+
// 此处的循环会包含 updated_at 和 updated_by 各一次,这里只处理 updated_by 同时包含两个字段的修改
|
297
352
|
}
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
353
|
+
else if (isUpdatedByOperation(fieldIndex + systemFieldOffset)) {
|
354
|
+
const systemFieldValues = getSharedRecord(sharedRecords, recordIndex).get(0).toJSON();
|
355
|
+
const { updated_at, updated_by } = getTrackableEntityBySystemFieldValues(systemFieldValues);
|
356
|
+
actions.push({
|
357
|
+
type: ActionName.UpdateSystemFieldValue,
|
358
|
+
path: [record._id],
|
359
|
+
updatedInfo: { updated_at, updated_by }
|
360
|
+
});
|
305
361
|
}
|
362
|
+
systemFieldOffset++;
|
306
363
|
}
|
307
364
|
else {
|
308
365
|
const recordId = getSharedRecordId(sharedRecords, recordIndex);
|
@@ -353,8 +410,23 @@ function translateArrayEvent(aiTable, sharedType, event) {
|
|
353
410
|
function isAddOrRemove(targetPath) {
|
354
411
|
return targetPath.length === 0;
|
355
412
|
}
|
356
|
-
function
|
357
|
-
|
413
|
+
function isSystemFieldOperation(targetPath) {
|
414
|
+
if (targetPath.length === 2 && targetPath[1] === 0) {
|
415
|
+
return true;
|
416
|
+
}
|
417
|
+
return false;
|
418
|
+
}
|
419
|
+
function isCustomFieldOperation(targetPath) {
|
420
|
+
if (targetPath.length === 2 && targetPath[1] === 1) {
|
421
|
+
return true;
|
422
|
+
}
|
423
|
+
return false;
|
424
|
+
}
|
425
|
+
function isPositionsOperation(fieldIndex) {
|
426
|
+
return fieldIndex === POSITIONS_INDEX;
|
427
|
+
}
|
428
|
+
function isUpdatedByOperation(fieldIndex) {
|
429
|
+
return fieldIndex === UPDATED_BY_INDEX;
|
358
430
|
}
|
359
431
|
function getRemoveIds(event, type) {
|
360
432
|
const ids = [];
|
@@ -450,7 +522,7 @@ function applyYjsEvents(aiTable, sharedType, events) {
|
|
450
522
|
}
|
451
523
|
}
|
452
524
|
|
453
|
-
function updateFieldValue$2(sharedType, action) {
|
525
|
+
function updateFieldValue$2(aiTable, sharedType, action) {
|
454
526
|
const sharedRecords = sharedType.get('records');
|
455
527
|
const sharedFields = sharedType.get('fields');
|
456
528
|
if (sharedRecords && sharedFields) {
|
@@ -466,7 +538,7 @@ function updateFieldValue$2(sharedType, action) {
|
|
466
538
|
return sharedType;
|
467
539
|
}
|
468
540
|
|
469
|
-
function setNode(sharedType, action) {
|
541
|
+
function setNode(aiTable, sharedType, action) {
|
470
542
|
let sharedNodes;
|
471
543
|
if (action.type === ActionName.SetField) {
|
472
544
|
sharedNodes = sharedType.get('fields');
|
@@ -496,27 +568,41 @@ function setNode(sharedType, action) {
|
|
496
568
|
return sharedType;
|
497
569
|
}
|
498
570
|
|
499
|
-
function addNode(sharedType, action) {
|
571
|
+
function addNode(aiTable, sharedType, action) {
|
500
572
|
const records = sharedType.get('records');
|
501
573
|
const views = sharedType.get('views');
|
502
574
|
const fields = sharedType.get('fields');
|
503
575
|
switch (action.type) {
|
504
576
|
case ActionName.AddRecord:
|
505
|
-
records && records.push([toRecordSyncElement(action.record)]);
|
577
|
+
records && records.push([toRecordSyncElement(action.record, aiTable.fields())]);
|
506
578
|
break;
|
507
579
|
case ActionName.AddView:
|
508
580
|
views && views.push([toSyncElement(action.view)]);
|
509
581
|
break;
|
510
|
-
case ActionName.
|
582
|
+
case ActionName.SetRecordPositions:
|
511
583
|
if (records) {
|
512
584
|
const recordIndex = getSharedRecordIndex(records, action.path[0]);
|
513
585
|
const record = records.get(recordIndex);
|
514
|
-
const
|
515
|
-
const
|
516
|
-
const
|
517
|
-
|
518
|
-
|
519
|
-
|
586
|
+
const positions = getPositionsByRecordSyncElement(record);
|
587
|
+
const newPositions = { ...positions };
|
588
|
+
for (const key in action.positions) {
|
589
|
+
if (action.positions[key] === null || action.positions[key] === undefined) {
|
590
|
+
delete newPositions[key];
|
591
|
+
}
|
592
|
+
else {
|
593
|
+
newPositions[key] = action.positions[key];
|
594
|
+
}
|
595
|
+
}
|
596
|
+
setRecordPositions$1(record, newPositions);
|
597
|
+
}
|
598
|
+
break;
|
599
|
+
case ActionName.UpdateSystemFieldValue:
|
600
|
+
if (records) {
|
601
|
+
const recordIndex = getSharedRecordIndex(records, action.path[0]);
|
602
|
+
const record = records.get(recordIndex);
|
603
|
+
if (action.updatedInfo.updated_at && action.updatedInfo.updated_at) {
|
604
|
+
setRecordUpdatedInfo(record, action.updatedInfo);
|
605
|
+
}
|
520
606
|
}
|
521
607
|
break;
|
522
608
|
case ActionName.AddField:
|
@@ -524,9 +610,11 @@ function addNode(sharedType, action) {
|
|
524
610
|
fields.push([toSyncElement(action.field)]);
|
525
611
|
const path = action.path[0];
|
526
612
|
for (let value of records) {
|
527
|
-
const
|
528
|
-
const
|
529
|
-
|
613
|
+
const customFieldValues = value.get(1);
|
614
|
+
const systemFieldValues = value.get(0);
|
615
|
+
const recordEntity = aiTable.recordsMap()[getIdBySystemFieldValuesType(systemFieldValues)];
|
616
|
+
const newFieldValue = recordEntity.values[action.field._id];
|
617
|
+
customFieldValues.insert(path, [newFieldValue]);
|
530
618
|
}
|
531
619
|
}
|
532
620
|
break;
|
@@ -534,7 +622,7 @@ function addNode(sharedType, action) {
|
|
534
622
|
return sharedType;
|
535
623
|
}
|
536
624
|
|
537
|
-
function removeNode(sharedType, action) {
|
625
|
+
function removeNode(aiTable, sharedType, action) {
|
538
626
|
const fields = sharedType.get('fields');
|
539
627
|
const records = sharedType.get('records');
|
540
628
|
const views = sharedType.get('views');
|
@@ -555,18 +643,6 @@ function removeNode(sharedType, action) {
|
|
555
643
|
}
|
556
644
|
}
|
557
645
|
break;
|
558
|
-
case ActionName.RemoveRecordPosition:
|
559
|
-
if (records) {
|
560
|
-
const recordIndex = getSharedRecordIndex(records, action.path[1]);
|
561
|
-
const record = records.get(recordIndex);
|
562
|
-
const customField = record.get(1);
|
563
|
-
const positionsIndex = customField.length - 1;
|
564
|
-
const positions = customField.get(positionsIndex);
|
565
|
-
delete positions[action.path[0]];
|
566
|
-
customField.delete(positionsIndex);
|
567
|
-
customField.insert(positionsIndex, [positions]);
|
568
|
-
}
|
569
|
-
break;
|
570
646
|
case ActionName.RemoveField:
|
571
647
|
if (fields && records) {
|
572
648
|
const fieldIndex = getSharedMapValueIndex(fields, action.path[0]);
|
@@ -583,25 +659,25 @@ function removeNode(sharedType, action) {
|
|
583
659
|
}
|
584
660
|
|
585
661
|
const actionMappers = {
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
662
|
+
[ActionName.AddRecord]: addNode,
|
663
|
+
[ActionName.UpdateFieldValue]: updateFieldValue$2,
|
664
|
+
[ActionName.RemoveRecord]: removeNode,
|
665
|
+
[ActionName.AddField]: addNode,
|
666
|
+
[ActionName.SetField]: setNode,
|
667
|
+
[ActionName.RemoveField]: removeNode,
|
668
|
+
[ActionName.SetView]: setNode,
|
669
|
+
[ActionName.AddView]: addNode,
|
670
|
+
[ActionName.RemoveView]: removeNode,
|
671
|
+
[ActionName.SetRecordPositions]: addNode,
|
672
|
+
[ActionName.UpdateSystemFieldValue]: addNode
|
597
673
|
};
|
598
|
-
function applyActionOps(sharedType, actions
|
674
|
+
function applyActionOps(aiTable, sharedType, actions) {
|
599
675
|
if (actions.length > 0) {
|
600
676
|
sharedType.doc.transact(() => {
|
601
677
|
actions.forEach((action) => {
|
602
678
|
const apply = actionMappers[action.type];
|
603
679
|
if (apply) {
|
604
|
-
return apply(sharedType, action);
|
680
|
+
return apply(aiTable, sharedType, action);
|
605
681
|
}
|
606
682
|
return null;
|
607
683
|
});
|
@@ -1108,6 +1184,19 @@ const apply = (aiTable, records, fields, views, action) => {
|
|
1108
1184
|
}
|
1109
1185
|
break;
|
1110
1186
|
}
|
1187
|
+
case ActionName.UpdateSystemFieldValue: {
|
1188
|
+
const [recordId] = action.path;
|
1189
|
+
if (recordId && action.updatedInfo) {
|
1190
|
+
const recordIndex = aiTable.records().findIndex((item) => item._id === recordId);
|
1191
|
+
if (action.updatedInfo.updated_at) {
|
1192
|
+
records[recordIndex].updated_at = action.updatedInfo.updated_at;
|
1193
|
+
}
|
1194
|
+
if (action.updatedInfo.updated_by) {
|
1195
|
+
records[recordIndex].updated_by = action.updatedInfo.updated_by;
|
1196
|
+
}
|
1197
|
+
}
|
1198
|
+
break;
|
1199
|
+
}
|
1111
1200
|
case ActionName.AddRecord: {
|
1112
1201
|
const [recordIndex] = action.path;
|
1113
1202
|
if (recordIndex > -1) {
|
@@ -1237,23 +1326,23 @@ const apply = (aiTable, records, fields, views, action) => {
|
|
1237
1326
|
}
|
1238
1327
|
break;
|
1239
1328
|
}
|
1240
|
-
case ActionName.
|
1241
|
-
const {
|
1329
|
+
case ActionName.SetRecordPositions: {
|
1330
|
+
const { positions, path } = action;
|
1242
1331
|
const record = records.find((item) => item._id === path[0]);
|
1243
1332
|
if (record) {
|
1244
|
-
record.positions
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1333
|
+
const newPositions = { ...record.positions };
|
1334
|
+
for (const key in positions) {
|
1335
|
+
if (positions[key] === null || positions[key] === undefined) {
|
1336
|
+
delete newPositions[key];
|
1337
|
+
}
|
1338
|
+
else {
|
1339
|
+
newPositions[key] = positions[key];
|
1340
|
+
}
|
1341
|
+
}
|
1342
|
+
record.positions = newPositions;
|
1248
1343
|
}
|
1249
1344
|
break;
|
1250
1345
|
}
|
1251
|
-
case ActionName.RemoveRecordPosition: {
|
1252
|
-
const { path } = action;
|
1253
|
-
const record = records.find((item) => item._id === path[1]);
|
1254
|
-
delete record?.positions[path[0]];
|
1255
|
-
break;
|
1256
|
-
}
|
1257
1346
|
}
|
1258
1347
|
return {
|
1259
1348
|
records,
|
@@ -1273,37 +1362,35 @@ const GeneralActions = {
|
|
1273
1362
|
}
|
1274
1363
|
};
|
1275
1364
|
|
1276
|
-
function
|
1365
|
+
function setRecordPositions(aiTable, positions, path) {
|
1277
1366
|
const operation = {
|
1278
|
-
type: ActionName.
|
1279
|
-
|
1367
|
+
type: ActionName.SetRecordPositions,
|
1368
|
+
positions,
|
1280
1369
|
path
|
1281
1370
|
};
|
1282
1371
|
aiTable.apply(operation);
|
1283
1372
|
}
|
1284
|
-
|
1373
|
+
const PositionsActions = {
|
1374
|
+
setRecordPositions
|
1375
|
+
};
|
1376
|
+
|
1377
|
+
function updateFieldValue$1(aiTable, value, path) {
|
1378
|
+
const oldValue = AITableQueries.getFieldValue(aiTable, path);
|
1285
1379
|
const operation = {
|
1286
|
-
type: ActionName.
|
1380
|
+
type: ActionName.UpdateFieldValue,
|
1381
|
+
fieldValue: oldValue,
|
1382
|
+
newFieldValue: value,
|
1287
1383
|
path
|
1288
1384
|
};
|
1289
1385
|
aiTable.apply(operation);
|
1290
1386
|
}
|
1291
|
-
|
1292
|
-
|
1293
|
-
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1297
|
-
|
1298
|
-
if (oldValue !== value) {
|
1299
|
-
const operation = {
|
1300
|
-
type: ActionName.UpdateFieldValue,
|
1301
|
-
fieldValue: oldValue,
|
1302
|
-
newFieldValue: value,
|
1303
|
-
path
|
1304
|
-
};
|
1305
|
-
aiTable.apply(operation);
|
1306
|
-
}
|
1387
|
+
function updateSystemFieldValue(aiTable, path, updatedInfo) {
|
1388
|
+
const operation = {
|
1389
|
+
type: ActionName.UpdateSystemFieldValue,
|
1390
|
+
updatedInfo,
|
1391
|
+
path
|
1392
|
+
};
|
1393
|
+
aiTable.apply(operation);
|
1307
1394
|
}
|
1308
1395
|
function addRecord(aiTable, record, path) {
|
1309
1396
|
const operation = {
|
@@ -1332,7 +1419,8 @@ const RecordActions = {
|
|
1332
1419
|
addRecord,
|
1333
1420
|
updateFieldValue: updateFieldValue$1,
|
1334
1421
|
moveRecord,
|
1335
|
-
removeRecord
|
1422
|
+
removeRecord,
|
1423
|
+
updateSystemFieldValue
|
1336
1424
|
};
|
1337
1425
|
|
1338
1426
|
function setView(aiTable, value, path) {
|
@@ -1386,10 +1474,17 @@ const Actions = {
|
|
1386
1474
|
...RecordActions,
|
1387
1475
|
...FieldActions,
|
1388
1476
|
...ViewActions,
|
1389
|
-
...
|
1477
|
+
...PositionsActions
|
1390
1478
|
};
|
1391
1479
|
|
1392
|
-
function
|
1480
|
+
function updateRecordsUpdatedInfo(aiTable, updatedInfo) {
|
1481
|
+
const records = aiTable.records();
|
1482
|
+
records.forEach((item) => {
|
1483
|
+
Actions.updateSystemFieldValue(aiTable, [item._id], updatedInfo);
|
1484
|
+
});
|
1485
|
+
}
|
1486
|
+
|
1487
|
+
function addFields(aiTable, options, updatedInfo) {
|
1393
1488
|
const { originId, direction = Direction$1.after, defaultValue, isDuplicate, count = 1 } = options;
|
1394
1489
|
const activeView = aiTable.viewsMap()[aiTable.activeViewId()];
|
1395
1490
|
const fields = getSortFields(aiTable, aiTable.fields(), activeView);
|
@@ -1397,13 +1492,26 @@ function addFields(aiTable, options) {
|
|
1397
1492
|
if (direction === Direction$1.after) {
|
1398
1493
|
addIndex++;
|
1399
1494
|
}
|
1400
|
-
const
|
1401
|
-
|
1495
|
+
const newFieldIds = getNewIdsByCount(count);
|
1496
|
+
newFieldIds.forEach((id, index) => {
|
1402
1497
|
const newField = { _id: id, ...defaultValue };
|
1403
1498
|
Actions.addField(aiTable, newField, [addIndex + index]);
|
1404
1499
|
});
|
1500
|
+
updateRecordsUpdatedInfo(aiTable, updatedInfo);
|
1405
1501
|
}
|
1406
1502
|
|
1503
|
+
const buildRemoveFieldItem = (getUpdatedInfo) => {
|
1504
|
+
return {
|
1505
|
+
type: 'removeField',
|
1506
|
+
name: '删除列',
|
1507
|
+
icon: 'trash',
|
1508
|
+
exec: (aiTable, field) => {
|
1509
|
+
Actions.removeField(aiTable, [field()._id]);
|
1510
|
+
updateRecordsUpdatedInfo(aiTable, getUpdatedInfo());
|
1511
|
+
}
|
1512
|
+
};
|
1513
|
+
};
|
1514
|
+
|
1407
1515
|
function createDefaultPositions(views, activeId, data, index) {
|
1408
1516
|
const positions = {};
|
1409
1517
|
const position = getPosition(data, activeId, index);
|
@@ -1445,7 +1553,7 @@ function addView(aiTable, type) {
|
|
1445
1553
|
}
|
1446
1554
|
ViewActions.addView(aiTable, newView, [index]);
|
1447
1555
|
aiTable.records().forEach((record) => {
|
1448
|
-
|
1556
|
+
PositionsActions.setRecordPositions(aiTable, { [newId]: record.positions[originViewId] }, [record._id]);
|
1449
1557
|
});
|
1450
1558
|
aiTable.fields().forEach((field) => {
|
1451
1559
|
Actions.setField(aiTable, {
|
@@ -1459,7 +1567,7 @@ function addView(aiTable, type) {
|
|
1459
1567
|
}
|
1460
1568
|
function removeView(aiTable, records, fields, activeViewId) {
|
1461
1569
|
records.forEach((record) => {
|
1462
|
-
|
1570
|
+
PositionsActions.setRecordPositions(aiTable, { [activeViewId]: undefined }, [record._id]);
|
1463
1571
|
});
|
1464
1572
|
fields.forEach((field) => {
|
1465
1573
|
const positions = { ...field.positions };
|
@@ -1471,7 +1579,7 @@ function removeView(aiTable, records, fields, activeViewId) {
|
|
1471
1579
|
ViewActions.removeView(aiTable, [activeViewId]);
|
1472
1580
|
}
|
1473
1581
|
|
1474
|
-
function addRecords(aiTable, options) {
|
1582
|
+
function addRecords(aiTable, options, trackableEntity) {
|
1475
1583
|
const { originId, direction = Direction$1.after, isDuplicate, count = 1 } = options;
|
1476
1584
|
const activeView = aiTable.viewsMap()[aiTable.activeViewId()];
|
1477
1585
|
const records = getSortRecords(aiTable, aiTable.records(), activeView);
|
@@ -1490,7 +1598,7 @@ function addRecords(aiTable, options) {
|
|
1490
1598
|
});
|
1491
1599
|
}
|
1492
1600
|
newRecordIds.forEach((id, index) => {
|
1493
|
-
const newRecord = { _id: id, values: newRecordValues };
|
1601
|
+
const newRecord = { _id: id, values: newRecordValues, ...trackableEntity };
|
1494
1602
|
Actions.addRecord(aiTable, newRecord, [addIndex + index]);
|
1495
1603
|
});
|
1496
1604
|
}
|
@@ -1513,8 +1621,12 @@ function getDefaultRecordValues(aiTable, isDuplicate = false, recordId) {
|
|
1513
1621
|
return newRecordValues;
|
1514
1622
|
}
|
1515
1623
|
|
1516
|
-
function updateFieldValue(aiTable, options) {
|
1517
|
-
|
1624
|
+
function updateFieldValue(aiTable, options, updatedInfo) {
|
1625
|
+
const oldValue = AITableQueries.getFieldValue(aiTable, options.path);
|
1626
|
+
if (!_.isEqual(oldValue, options.value)) {
|
1627
|
+
Actions.updateFieldValue(aiTable, options.value, options.path);
|
1628
|
+
Actions.updateSystemFieldValue(aiTable, [options.path[0]], updatedInfo);
|
1629
|
+
}
|
1518
1630
|
}
|
1519
1631
|
|
1520
1632
|
const FLUSHING = new WeakMap();
|
@@ -1546,7 +1658,7 @@ const EditFieldPropertyItem = {
|
|
1546
1658
|
icon: 'edit',
|
1547
1659
|
exec: (aiTable, field, origin, position) => {
|
1548
1660
|
const fieldService = AI_TABLE_GRID_FIELD_SERVICE_MAP.get(aiTable);
|
1549
|
-
const copyField =
|
1661
|
+
const copyField = ___default.cloneDeep(field());
|
1550
1662
|
if (origin && position) {
|
1551
1663
|
const popoverRef = fieldService?.editFieldProperty(aiTable, {
|
1552
1664
|
field: copyField,
|
@@ -1564,14 +1676,6 @@ const EditFieldPropertyItem = {
|
|
1564
1676
|
return undefined;
|
1565
1677
|
}
|
1566
1678
|
};
|
1567
|
-
const RemoveFieldItem = {
|
1568
|
-
type: 'removeField',
|
1569
|
-
name: '删除列',
|
1570
|
-
icon: 'trash',
|
1571
|
-
exec: (aiTable, field) => {
|
1572
|
-
Actions.removeField(aiTable, [field()._id]);
|
1573
|
-
}
|
1574
|
-
};
|
1575
1679
|
|
1576
1680
|
const VIEW_ACTIONS = [ActionName.SetView, ActionName.AddView, ActionName.RemoveView];
|
1577
1681
|
|
@@ -1579,5 +1683,5 @@ const VIEW_ACTIONS = [ActionName.SetView, ActionName.AddView, ActionName.RemoveV
|
|
1579
1683
|
* Generated bundle index. Do not edit.
|
1580
1684
|
*/
|
1581
1685
|
|
1582
|
-
export { AITableFilterLogical, AITableFilterOperation, ActionName, Actions, Direction, DividerMenuItem, EditFieldPropertyItem, ExecuteType, FLUSHING, Positions,
|
1686
|
+
export { AITableFilterLogical, AITableFilterOperation, AI_TABLE_CONTENT_FIELD_NAME, ActionName, Actions, Direction, DividerMenuItem, EditFieldPropertyItem, ExecuteType, FLUSHING, POSITIONS_INDEX, Positions, RemovePositions, UPDATED_AT_INDEX, UPDATED_BY_INDEX, VIEW_ACTIONS, ViewOperationMap, YjsAITable, actionMappers, addFields, addRecords, addView, applyActionOps, applyEvents, applyYjsEvents, buildFieldsByView, buildRecordsByView, buildRemoveFieldItem, createDefaultPositions, createSharedType, doFilter, getCustomFieldValues, getDataBySharedType, getDefaultRecordDataByFilter, getDefaultRecordValues, getFilteredRecords, getIdBySystemFieldValues, getIdBySystemFieldValuesType, getNewIdsByCount, getPosition, getPositionsByRecordSyncElement, getPositionsBySystemFieldValues, getRecordsBySharedJson, getShareTypeNumberPath, getSharedMapValueId, getSharedMapValueIndex, getSharedRecord, getSharedRecordId, getSharedRecordIndex, getSharedTypeByData, getSortFields, getSortRecords, getSystemFieldValues, getTrackableEntityBySystemFieldValues, getValuesByCustomFieldValues, isEmpty, isPathEqual, removeView, setRecordPositions$1 as setRecordPositions, setRecordUpdatedInfo, sortByViewPosition, sortRecordsBySortInfo, toRecordSyncElement, toSharedType, toSyncElement, translatePositionToPath, translateYjsEvent, updateFieldValue, updateRecordsUpdatedInfo, withState };
|
1583
1687
|
//# sourceMappingURL=ai-table-state.mjs.map
|