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