@ai-table/state 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- package/ai-table-state.d.ts.map +1 -0
- package/esm2022/ai-table-state.mjs +5 -0
- package/esm2022/index.mjs +2 -0
- package/esm2022/public-api.mjs +6 -0
- package/esm2022/shared/index.mjs +5 -0
- package/esm2022/shared/to-table/array-event.mjs +136 -0
- package/esm2022/shared/to-table/index.mjs +29 -0
- package/esm2022/shared/to-table/map-event.mjs +50 -0
- package/esm2022/shared/to-yjs/add-node.mjs +30 -0
- package/esm2022/shared/to-yjs/index.mjs +30 -0
- package/esm2022/shared/to-yjs/remove-node.mjs +40 -0
- package/esm2022/shared/to-yjs/set-node.mjs +33 -0
- package/esm2022/shared/to-yjs/update-field-value.mjs +17 -0
- package/esm2022/shared/utils/index.mjs +4 -0
- package/esm2022/shared/utils/init-shared.mjs +48 -0
- package/esm2022/shared/utils/init-table.mjs +13 -0
- package/esm2022/shared/utils/translate.mjs +62 -0
- package/esm2022/shared/yjs-table.mjs +39 -0
- package/esm2022/types/index.mjs +3 -0
- package/esm2022/types/shared.mjs +2 -0
- package/esm2022/types/view.mjs +39 -0
- package/esm2022/view/action/general.mjs +53 -0
- package/esm2022/view/action/index.mjs +3 -0
- package/esm2022/view/action/view.mjs +47 -0
- package/esm2022/view/constants/view.mjs +3 -0
- package/esm2022/view/plugins/view.plugin.mjs +25 -0
- package/fesm2022/ai-table-state.mjs +668 -0
- package/fesm2022/ai-table-state.mjs.map +1 -0
- package/index.d.ts +2 -0
- package/index.d.ts.map +1 -0
- package/package.json +24 -0
- package/public-api.d.ts +6 -0
- package/public-api.d.ts.map +1 -0
- package/shared/index.d.ts +5 -0
- package/shared/index.d.ts.map +1 -0
- package/shared/to-table/array-event.d.ts +7 -0
- package/shared/to-table/array-event.d.ts.map +1 -0
- package/shared/to-table/index.d.ts +6 -0
- package/shared/to-table/index.d.ts.map +1 -0
- package/shared/to-table/map-event.d.ts +4 -0
- package/shared/to-table/map-event.d.ts.map +1 -0
- package/shared/to-yjs/add-node.d.ts +4 -0
- package/shared/to-yjs/add-node.d.ts.map +1 -0
- package/shared/to-yjs/index.d.ts +11 -0
- package/shared/to-yjs/index.d.ts.map +1 -0
- package/shared/to-yjs/remove-node.d.ts +4 -0
- package/shared/to-yjs/remove-node.d.ts.map +1 -0
- package/shared/to-yjs/set-node.d.ts +4 -0
- package/shared/to-yjs/set-node.d.ts.map +1 -0
- package/shared/to-yjs/update-field-value.d.ts +4 -0
- package/shared/to-yjs/update-field-value.d.ts.map +1 -0
- package/shared/utils/index.d.ts +4 -0
- package/shared/utils/index.d.ts.map +1 -0
- package/shared/utils/init-shared.d.ts +16 -0
- package/shared/utils/init-shared.d.ts.map +1 -0
- package/shared/utils/init-table.d.ts +7 -0
- package/shared/utils/init-table.d.ts.map +1 -0
- package/shared/utils/translate.d.ts +15 -0
- package/shared/utils/translate.d.ts.map +1 -0
- package/shared/yjs-table.d.ts +14 -0
- package/shared/yjs-table.d.ts.map +1 -0
- package/types/index.d.ts +3 -0
- package/types/index.d.ts.map +1 -0
- package/types/shared.d.ts +5 -0
- package/types/shared.d.ts.map +1 -0
- package/types/view.d.ts +87 -0
- package/types/view.d.ts.map +1 -0
- package/view/action/general.d.ts +6 -0
- package/view/action/general.d.ts.map +1 -0
- package/view/action/index.d.ts +3 -0
- package/view/action/index.d.ts.map +1 -0
- package/view/action/view.d.ts +10 -0
- package/view/action/view.d.ts.map +1 -0
- package/view/constants/view.d.ts +3 -0
- package/view/constants/view.d.ts.map +1 -0
- package/view/plugins/view.plugin.d.ts +3 -0
- package/view/plugins/view.plugin.d.ts.map +1 -0
@@ -0,0 +1,668 @@
|
|
1
|
+
import * as Y from 'yjs';
|
2
|
+
import { ActionName, AITableQueries, getDefaultFieldValue, Actions, FLUSHING } from '@ai-table/grid';
|
3
|
+
import { isArray } from 'ngx-tethys/util';
|
4
|
+
import { createDraft, finishDraft } from 'immer';
|
5
|
+
|
6
|
+
class Positions {
|
7
|
+
}
|
8
|
+
var Direction;
|
9
|
+
(function (Direction) {
|
10
|
+
Direction[Direction["default"] = 0] = "default";
|
11
|
+
Direction[Direction["ascending"] = 1] = "ascending";
|
12
|
+
Direction[Direction["descending"] = -1] = "descending";
|
13
|
+
})(Direction || (Direction = {}));
|
14
|
+
var LogicalOperator;
|
15
|
+
(function (LogicalOperator) {
|
16
|
+
LogicalOperator["and"] = "and";
|
17
|
+
LogicalOperator["or"] = "or";
|
18
|
+
})(LogicalOperator || (LogicalOperator = {}));
|
19
|
+
var FilterOperationSymbol;
|
20
|
+
(function (FilterOperationSymbol) {
|
21
|
+
FilterOperationSymbol["eq"] = "eq";
|
22
|
+
FilterOperationSymbol["gte"] = "gte";
|
23
|
+
FilterOperationSymbol["lte"] = "lte";
|
24
|
+
FilterOperationSymbol["gt"] = "gt";
|
25
|
+
FilterOperationSymbol["lt"] = "lt";
|
26
|
+
FilterOperationSymbol["in"] = "in";
|
27
|
+
FilterOperationSymbol["contain"] = "contain";
|
28
|
+
FilterOperationSymbol["ne"] = "ne";
|
29
|
+
FilterOperationSymbol["nin"] = "nin";
|
30
|
+
FilterOperationSymbol["between"] = "between";
|
31
|
+
FilterOperationSymbol["besides"] = "besides";
|
32
|
+
FilterOperationSymbol["empty"] = "empty";
|
33
|
+
FilterOperationSymbol["exists"] = "exists";
|
34
|
+
FilterOperationSymbol["notContain"] = "not_contain";
|
35
|
+
})(FilterOperationSymbol || (FilterOperationSymbol = {}));
|
36
|
+
class GridSettings {
|
37
|
+
}
|
38
|
+
var ViewActionName;
|
39
|
+
(function (ViewActionName) {
|
40
|
+
ViewActionName["SetView"] = "set_view";
|
41
|
+
ViewActionName["AddView"] = "add_view";
|
42
|
+
ViewActionName["RemoveView"] = "remove_view";
|
43
|
+
})(ViewActionName || (ViewActionName = {}));
|
44
|
+
|
45
|
+
const createSharedType = () => {
|
46
|
+
const doc = new Y.Doc();
|
47
|
+
const sharedType = doc.getMap('ai-table');
|
48
|
+
return sharedType;
|
49
|
+
};
|
50
|
+
const initSharedType = (doc, initializeValue) => {
|
51
|
+
const sharedType = doc.getMap('ai-table');
|
52
|
+
toSharedType(sharedType, initializeValue);
|
53
|
+
return sharedType;
|
54
|
+
};
|
55
|
+
function toSharedType(sharedType, data) {
|
56
|
+
sharedType.doc.transact(() => {
|
57
|
+
const fieldSharedType = new Y.Array();
|
58
|
+
fieldSharedType.insert(0, data.fields.map(toSyncElement));
|
59
|
+
sharedType.set('fields', fieldSharedType);
|
60
|
+
const recordSharedType = new Y.Array();
|
61
|
+
sharedType.set('records', recordSharedType);
|
62
|
+
recordSharedType.insert(0, data.records.map(toRecordSyncElement));
|
63
|
+
const viewsSharedType = new Y.Array();
|
64
|
+
sharedType.set('views', viewsSharedType);
|
65
|
+
viewsSharedType.insert(0, data.views.map(toSyncElement));
|
66
|
+
});
|
67
|
+
}
|
68
|
+
function toSyncElement(node) {
|
69
|
+
const element = new Y.Map();
|
70
|
+
for (const key in node) {
|
71
|
+
element.set(key, node[key]);
|
72
|
+
}
|
73
|
+
return element;
|
74
|
+
}
|
75
|
+
function toRecordSyncElement(record) {
|
76
|
+
const nonEditableArray = new Y.Array();
|
77
|
+
// 临时方案:为了解决删除时协同操作无法精准获取删除的 id 的问题,将原来的[idValue] 改为[{'_id': idValue}]
|
78
|
+
// 后续可能改为 YMap 或者通过在 views 中存储 positions 解决
|
79
|
+
nonEditableArray.insert(0, [{ _id: record['_id'] }]);
|
80
|
+
const editableArray = new Y.Array();
|
81
|
+
const editableFields = [];
|
82
|
+
for (const fieldId in record['values']) {
|
83
|
+
editableFields.push(record['values'][fieldId]);
|
84
|
+
}
|
85
|
+
editableArray.insert(0, [...editableFields, record['positions']]);
|
86
|
+
// To save memory, convert map to array.
|
87
|
+
const element = new Y.Array();
|
88
|
+
element.insert(0, [nonEditableArray, editableArray]);
|
89
|
+
return element;
|
90
|
+
}
|
91
|
+
|
92
|
+
const translateToRecordValues = (arrayRecord, fields) => {
|
93
|
+
const fieldIds = fields.map((item) => item._id);
|
94
|
+
const recordValue = {};
|
95
|
+
fieldIds.forEach((item, index) => {
|
96
|
+
recordValue[item] = arrayRecord[index] || '';
|
97
|
+
});
|
98
|
+
return recordValue;
|
99
|
+
};
|
100
|
+
const translateToRecords = (arrayRecords, fields) => {
|
101
|
+
return arrayRecords.map((record) => {
|
102
|
+
const [nonEditableArray, editableArray] = record;
|
103
|
+
return {
|
104
|
+
_id: nonEditableArray[0]['_id'],
|
105
|
+
positions: editableArray[editableArray.length - 1],
|
106
|
+
values: translateToRecordValues(editableArray.slice(0, editableArray.length - 1), fields)
|
107
|
+
};
|
108
|
+
});
|
109
|
+
};
|
110
|
+
function translatePositionToPath(data, position, activeViewId) {
|
111
|
+
let index = data.findIndex((value, index) => {
|
112
|
+
if (index === 0) {
|
113
|
+
return position < value.positions[activeViewId];
|
114
|
+
}
|
115
|
+
return position > data[index - 1].positions[activeViewId] && position < value.positions[activeViewId];
|
116
|
+
});
|
117
|
+
if (index === -1) {
|
118
|
+
index = data.length;
|
119
|
+
}
|
120
|
+
return [index];
|
121
|
+
}
|
122
|
+
function getShareTypeNumberPath(path) {
|
123
|
+
return path.filter((node) => typeof node === 'number');
|
124
|
+
}
|
125
|
+
function getSharedRecordId(records, recordIndex) {
|
126
|
+
return records && records.get(recordIndex).get(0).get(0)['_id'];
|
127
|
+
}
|
128
|
+
function getSharedMapValueId(values, index) {
|
129
|
+
return values && values.get(index).get('_id');
|
130
|
+
}
|
131
|
+
function getSharedRecordIndex(sharedRecords, recordId) {
|
132
|
+
let recordIndex = -1;
|
133
|
+
for (let index = 0; index < sharedRecords.length; index++) {
|
134
|
+
const sharedRecordId = getSharedRecordId(sharedRecords, index);
|
135
|
+
if (sharedRecordId === recordId) {
|
136
|
+
recordIndex = index;
|
137
|
+
break;
|
138
|
+
}
|
139
|
+
}
|
140
|
+
return recordIndex;
|
141
|
+
}
|
142
|
+
function getSharedMapValueIndex(sharedNodes, id) {
|
143
|
+
let nodeIndex = -1;
|
144
|
+
for (let index = 0; index < sharedNodes.length; index++) {
|
145
|
+
const sharedId = getSharedMapValueId(sharedNodes, index);
|
146
|
+
if (sharedId === id) {
|
147
|
+
nodeIndex = index;
|
148
|
+
break;
|
149
|
+
}
|
150
|
+
}
|
151
|
+
return nodeIndex;
|
152
|
+
}
|
153
|
+
|
154
|
+
const initTable = (sharedType) => {
|
155
|
+
const data = sharedType.toJSON();
|
156
|
+
const fields = data['fields'];
|
157
|
+
const records = translateToRecords(data['records'], fields);
|
158
|
+
const views = data['views'];
|
159
|
+
return {
|
160
|
+
records,
|
161
|
+
fields,
|
162
|
+
views
|
163
|
+
};
|
164
|
+
};
|
165
|
+
|
166
|
+
function translateArrayEvent(aiTable, activeViewId, sharedType, event) {
|
167
|
+
let offset = 0;
|
168
|
+
let targetPath = getShareTypeNumberPath(event.path);
|
169
|
+
const isRecordsTranslate = event.path.includes('records');
|
170
|
+
const isFieldsTranslate = event.path.includes('fields');
|
171
|
+
const isViewsTranslate = event.path.includes('views');
|
172
|
+
const actions = [];
|
173
|
+
event.changes.delta.forEach((delta) => {
|
174
|
+
if ('retain' in delta) {
|
175
|
+
offset += delta.retain ?? 0;
|
176
|
+
}
|
177
|
+
if ('delete' in delta) {
|
178
|
+
if (isAddOrRemove(targetPath)) {
|
179
|
+
if (isViewsTranslate) {
|
180
|
+
const removeView = aiTable.views()[offset];
|
181
|
+
if (removeView) {
|
182
|
+
actions.push({
|
183
|
+
type: ViewActionName.RemoveView,
|
184
|
+
path: [removeView._id]
|
185
|
+
});
|
186
|
+
}
|
187
|
+
}
|
188
|
+
else {
|
189
|
+
const type = isRecordsTranslate ? ActionName.RemoveRecord : ActionName.RemoveField;
|
190
|
+
const removeIds = getRemoveIds(event, type);
|
191
|
+
if (removeIds.length) {
|
192
|
+
removeIds.forEach((path) => {
|
193
|
+
actions.push({
|
194
|
+
type,
|
195
|
+
path
|
196
|
+
});
|
197
|
+
});
|
198
|
+
}
|
199
|
+
}
|
200
|
+
}
|
201
|
+
}
|
202
|
+
if ('insert' in delta) {
|
203
|
+
if (isArray(delta.insert)) {
|
204
|
+
if (isRecordsTranslate) {
|
205
|
+
if (isAddOrRemove(targetPath)) {
|
206
|
+
delta.insert?.map((item) => {
|
207
|
+
const data = item.toJSON();
|
208
|
+
const [fixedField, customField] = data;
|
209
|
+
const position = customField[customField.length - 1][activeViewId];
|
210
|
+
const path = translatePositionToPath(aiTable.records(), position, activeViewId);
|
211
|
+
actions.push({
|
212
|
+
type: ActionName.AddRecord,
|
213
|
+
path: path,
|
214
|
+
record: {
|
215
|
+
_id: fixedField[0]['_id'],
|
216
|
+
values: translateToRecordValues(customField, aiTable.fields())
|
217
|
+
}
|
218
|
+
});
|
219
|
+
});
|
220
|
+
}
|
221
|
+
else {
|
222
|
+
try {
|
223
|
+
delta.insert?.map((item) => {
|
224
|
+
const recordIndex = targetPath[0];
|
225
|
+
const fieldIndex = offset;
|
226
|
+
const recordId = getSharedRecordId(sharedType.get('records'), recordIndex);
|
227
|
+
const fieldId = getSharedMapValueId(sharedType.get('fields'), fieldIndex);
|
228
|
+
const path = [recordId, fieldId];
|
229
|
+
const fieldValue = AITableQueries.getFieldValue(aiTable, path);
|
230
|
+
// To exclude insert triggered by field inserts.
|
231
|
+
if (fieldValue !== item) {
|
232
|
+
actions.push({
|
233
|
+
type: ActionName.UpdateFieldValue,
|
234
|
+
path,
|
235
|
+
fieldValue,
|
236
|
+
newFieldValue: item
|
237
|
+
});
|
238
|
+
}
|
239
|
+
});
|
240
|
+
}
|
241
|
+
catch (error) { }
|
242
|
+
}
|
243
|
+
}
|
244
|
+
if (isFieldsTranslate) {
|
245
|
+
delta.insert?.map((item) => {
|
246
|
+
const data = item.toJSON();
|
247
|
+
const path = translatePositionToPath(aiTable.fields(), data['positions'][activeViewId], activeViewId);
|
248
|
+
actions.push({
|
249
|
+
type: ActionName.AddField,
|
250
|
+
path,
|
251
|
+
field: data
|
252
|
+
});
|
253
|
+
});
|
254
|
+
}
|
255
|
+
if (isViewsTranslate) {
|
256
|
+
delta.insert?.map((item, index) => {
|
257
|
+
const data = item.toJSON();
|
258
|
+
actions.push({
|
259
|
+
type: ViewActionName.AddView,
|
260
|
+
path: [offset + index],
|
261
|
+
view: data
|
262
|
+
});
|
263
|
+
});
|
264
|
+
}
|
265
|
+
}
|
266
|
+
}
|
267
|
+
});
|
268
|
+
return actions;
|
269
|
+
}
|
270
|
+
function isAddOrRemove(targetPath) {
|
271
|
+
return targetPath.length === 0;
|
272
|
+
}
|
273
|
+
function getRemoveIds(event, type) {
|
274
|
+
const ids = [];
|
275
|
+
if (!type) {
|
276
|
+
return ids;
|
277
|
+
}
|
278
|
+
Y.iterateDeletedStructs(event.transaction, event.transaction.deleteSet,
|
279
|
+
// @param {Item|GC} item
|
280
|
+
(item) => {
|
281
|
+
if (item instanceof Y.Item && item.deleted) {
|
282
|
+
if (type === ActionName.RemoveField && item.parentSub === '_id') {
|
283
|
+
ids.push(item.content.getContent());
|
284
|
+
}
|
285
|
+
if (type === ActionName.RemoveRecord) {
|
286
|
+
const content = item.content.getContent();
|
287
|
+
if (content[0] && content[0]['_id']) {
|
288
|
+
ids.push([content[0]['_id']]);
|
289
|
+
}
|
290
|
+
}
|
291
|
+
}
|
292
|
+
});
|
293
|
+
return ids;
|
294
|
+
}
|
295
|
+
|
296
|
+
function translateMapEvent(aiTable, sharedType, event) {
|
297
|
+
const isFieldsTranslate = event.path.includes('fields');
|
298
|
+
const isViewTranslate = event.path.includes('views');
|
299
|
+
if (isViewTranslate || isFieldsTranslate) {
|
300
|
+
let [targetPath] = getShareTypeNumberPath(event.path);
|
301
|
+
let targetElement;
|
302
|
+
const targetSyncElement = event.target;
|
303
|
+
if (isFieldsTranslate) {
|
304
|
+
const field = sharedType.get('fields')?.get(targetPath);
|
305
|
+
const fieldId = field && field.get('_id');
|
306
|
+
targetElement = fieldId && aiTable.fields().find((item) => item._id === field.get('_id'));
|
307
|
+
}
|
308
|
+
if (isViewTranslate) {
|
309
|
+
targetElement = aiTable.views()[targetPath];
|
310
|
+
}
|
311
|
+
if (targetElement) {
|
312
|
+
const keyChanges = Array.from(event.changes.keys.entries());
|
313
|
+
const newProperties = {};
|
314
|
+
const properties = {};
|
315
|
+
const entries = keyChanges.map(([key, info]) => {
|
316
|
+
const value = targetSyncElement.get(key);
|
317
|
+
return [key, info.action === 'delete' ? null : value instanceof Y.AbstractType ? value.toJSON() : value];
|
318
|
+
});
|
319
|
+
for (const [key, value] of entries) {
|
320
|
+
const k = key;
|
321
|
+
newProperties[k] = value;
|
322
|
+
}
|
323
|
+
const oldEntries = keyChanges.map(([key]) => [key, targetElement[key]]);
|
324
|
+
for (const [key, value] of oldEntries) {
|
325
|
+
const k = key;
|
326
|
+
properties[k] = value;
|
327
|
+
}
|
328
|
+
const type = isFieldsTranslate ? ActionName.SetField : ViewActionName.SetView;
|
329
|
+
return [
|
330
|
+
{
|
331
|
+
type,
|
332
|
+
properties: properties,
|
333
|
+
newProperties: newProperties,
|
334
|
+
path: [targetElement._id]
|
335
|
+
}
|
336
|
+
];
|
337
|
+
}
|
338
|
+
}
|
339
|
+
return [];
|
340
|
+
}
|
341
|
+
|
342
|
+
const IS_LOCAL = new WeakSet();
|
343
|
+
const IS_REMOTE = new WeakSet();
|
344
|
+
const IS_UNDO = new WeakSet();
|
345
|
+
const YjsAITable = {
|
346
|
+
isLocal: (aiTable) => {
|
347
|
+
return IS_LOCAL.has(aiTable);
|
348
|
+
},
|
349
|
+
asLocal: (aiTable, fn) => {
|
350
|
+
const wasLocal = YjsAITable.isLocal(aiTable);
|
351
|
+
IS_LOCAL.add(aiTable);
|
352
|
+
fn();
|
353
|
+
if (!wasLocal) {
|
354
|
+
IS_LOCAL.delete(aiTable);
|
355
|
+
}
|
356
|
+
},
|
357
|
+
isRemote: (aiTable) => {
|
358
|
+
return IS_REMOTE.has(aiTable);
|
359
|
+
},
|
360
|
+
asRemote: (aiTable, fn) => {
|
361
|
+
const wasRemote = YjsAITable.isRemote(aiTable);
|
362
|
+
IS_REMOTE.add(aiTable);
|
363
|
+
fn();
|
364
|
+
if (!wasRemote) {
|
365
|
+
Promise.resolve().then(() => IS_REMOTE.delete(aiTable));
|
366
|
+
}
|
367
|
+
},
|
368
|
+
isUndo: (aiTable) => {
|
369
|
+
return IS_UNDO.has(aiTable);
|
370
|
+
},
|
371
|
+
asUndo: (aiTable, fn) => {
|
372
|
+
const wasUndo = YjsAITable.isUndo(aiTable);
|
373
|
+
IS_UNDO.add(aiTable);
|
374
|
+
fn();
|
375
|
+
if (!wasUndo) {
|
376
|
+
Promise.resolve().then(() => IS_UNDO.delete(aiTable));
|
377
|
+
}
|
378
|
+
}
|
379
|
+
};
|
380
|
+
|
381
|
+
function translateYjsEvent(aiTable, activeViewId, sharedType, event) {
|
382
|
+
if (event instanceof Y.YArrayEvent) {
|
383
|
+
return translateArrayEvent(aiTable, activeViewId, sharedType, event);
|
384
|
+
}
|
385
|
+
if (event instanceof Y.YMapEvent) {
|
386
|
+
return translateMapEvent(aiTable, sharedType, event);
|
387
|
+
}
|
388
|
+
return [];
|
389
|
+
}
|
390
|
+
function applyEvents(aiTable, activeViewId, sharedType, events) {
|
391
|
+
events.forEach((event) => translateYjsEvent(aiTable, activeViewId, sharedType, event).forEach((item) => {
|
392
|
+
aiTable.apply(item);
|
393
|
+
}));
|
394
|
+
}
|
395
|
+
function applyYjsEvents(aiTable, activeViewId, sharedType, events) {
|
396
|
+
if (YjsAITable.isUndo(aiTable)) {
|
397
|
+
applyEvents(aiTable, activeViewId, sharedType, events);
|
398
|
+
}
|
399
|
+
else {
|
400
|
+
YjsAITable.asRemote(aiTable, () => {
|
401
|
+
applyEvents(aiTable, activeViewId, sharedType, events);
|
402
|
+
});
|
403
|
+
}
|
404
|
+
}
|
405
|
+
|
406
|
+
function updateFieldValue(sharedType, action) {
|
407
|
+
const sharedRecords = sharedType.get('records');
|
408
|
+
const sharedFields = sharedType.get('fields');
|
409
|
+
if (sharedRecords && sharedFields) {
|
410
|
+
const recordIndex = getSharedRecordIndex(sharedRecords, action.path[0]);
|
411
|
+
const fieldIndex = getSharedMapValueIndex(sharedFields, action.path[1]);
|
412
|
+
if (recordIndex > -1 && fieldIndex > -1) {
|
413
|
+
const record = sharedRecords?.get(recordIndex);
|
414
|
+
const customField = record.get(1);
|
415
|
+
customField.delete(fieldIndex);
|
416
|
+
customField.insert(fieldIndex, [action.newFieldValue]);
|
417
|
+
}
|
418
|
+
}
|
419
|
+
return sharedType;
|
420
|
+
}
|
421
|
+
|
422
|
+
function setNode(sharedType, action) {
|
423
|
+
let sharedNodes;
|
424
|
+
if (action.type === ActionName.SetField) {
|
425
|
+
sharedNodes = sharedType.get('fields');
|
426
|
+
}
|
427
|
+
if (action.type === ViewActionName.SetView) {
|
428
|
+
sharedNodes = sharedType.get('views');
|
429
|
+
}
|
430
|
+
if (sharedNodes) {
|
431
|
+
let nodeIndex = getSharedMapValueIndex(sharedNodes, action.path[0]);
|
432
|
+
if (nodeIndex > -1) {
|
433
|
+
const node = sharedNodes.get(nodeIndex);
|
434
|
+
Object.entries(action.newProperties).forEach(([key, value]) => {
|
435
|
+
if (value == null) {
|
436
|
+
node.delete(key);
|
437
|
+
}
|
438
|
+
else {
|
439
|
+
node.set(key, value);
|
440
|
+
}
|
441
|
+
});
|
442
|
+
Object.entries(action.properties).forEach(([key]) => {
|
443
|
+
if (!action.newProperties.hasOwnProperty(key)) {
|
444
|
+
node.delete(key);
|
445
|
+
}
|
446
|
+
});
|
447
|
+
}
|
448
|
+
}
|
449
|
+
return sharedType;
|
450
|
+
}
|
451
|
+
|
452
|
+
function addNode(sharedType, action) {
|
453
|
+
switch (action.type) {
|
454
|
+
case ActionName.AddRecord:
|
455
|
+
const records = sharedType.get('records');
|
456
|
+
records && records.push([toRecordSyncElement(action.record)]);
|
457
|
+
break;
|
458
|
+
case ViewActionName.AddView:
|
459
|
+
const views = sharedType.get('views');
|
460
|
+
views && views.push([toSyncElement(action.view)]);
|
461
|
+
break;
|
462
|
+
case ActionName.AddField:
|
463
|
+
const sharedFields = sharedType.get('fields');
|
464
|
+
const sharedRecords = sharedType.get('records');
|
465
|
+
if (sharedFields && sharedRecords) {
|
466
|
+
sharedFields.push([toSyncElement(action.field)]);
|
467
|
+
const path = action.path[0];
|
468
|
+
for (let value of sharedRecords) {
|
469
|
+
const newRecord = getDefaultFieldValue(action.field);
|
470
|
+
const customField = value.get(1);
|
471
|
+
customField.insert(path, [newRecord]);
|
472
|
+
}
|
473
|
+
}
|
474
|
+
break;
|
475
|
+
}
|
476
|
+
return sharedType;
|
477
|
+
}
|
478
|
+
|
479
|
+
function removeNode(sharedType, action) {
|
480
|
+
switch (action.type) {
|
481
|
+
case ActionName.RemoveRecord:
|
482
|
+
const records = sharedType.get('records');
|
483
|
+
if (records) {
|
484
|
+
const recordIndex = getSharedRecordIndex(records, action.path[0]);
|
485
|
+
if (recordIndex > -1) {
|
486
|
+
records.delete(recordIndex);
|
487
|
+
}
|
488
|
+
}
|
489
|
+
break;
|
490
|
+
case ViewActionName.RemoveView:
|
491
|
+
const views = sharedType.get('views');
|
492
|
+
if (views) {
|
493
|
+
const viewIndex = getSharedMapValueIndex(views, action.path[0]);
|
494
|
+
if (viewIndex > -1) {
|
495
|
+
views.delete(viewIndex);
|
496
|
+
}
|
497
|
+
}
|
498
|
+
break;
|
499
|
+
case ActionName.RemoveField:
|
500
|
+
const sharedFields = sharedType.get('fields');
|
501
|
+
const sharedRecords = sharedType.get('records');
|
502
|
+
if (sharedFields && sharedRecords) {
|
503
|
+
const fieldIndex = getSharedMapValueIndex(sharedFields, action.path[0]);
|
504
|
+
if (fieldIndex > -1) {
|
505
|
+
sharedFields.delete(fieldIndex);
|
506
|
+
for (let value of sharedRecords) {
|
507
|
+
value.get(1).delete(fieldIndex);
|
508
|
+
}
|
509
|
+
}
|
510
|
+
}
|
511
|
+
break;
|
512
|
+
}
|
513
|
+
return sharedType;
|
514
|
+
}
|
515
|
+
|
516
|
+
const actionMappers = {
|
517
|
+
add_record: addNode,
|
518
|
+
update_field_value: updateFieldValue,
|
519
|
+
remove_record: removeNode,
|
520
|
+
add_field: addNode,
|
521
|
+
set_field: setNode,
|
522
|
+
remove_field: removeNode,
|
523
|
+
set_view: setNode,
|
524
|
+
add_view: addNode,
|
525
|
+
remove_view: removeNode,
|
526
|
+
};
|
527
|
+
function applyActionOps(sharedType, actions, aiTable) {
|
528
|
+
if (actions.length > 0) {
|
529
|
+
sharedType.doc.transact(() => {
|
530
|
+
actions.forEach((action) => {
|
531
|
+
const apply = actionMappers[action.type];
|
532
|
+
if (apply) {
|
533
|
+
return apply(sharedType, action);
|
534
|
+
}
|
535
|
+
return null;
|
536
|
+
});
|
537
|
+
}, aiTable);
|
538
|
+
}
|
539
|
+
return sharedType;
|
540
|
+
}
|
541
|
+
|
542
|
+
const GeneralViewActions = {
|
543
|
+
transform(aiTable, action) {
|
544
|
+
const views = createDraft(aiTable.views());
|
545
|
+
applyView(aiTable, views, action);
|
546
|
+
aiTable.views.update(() => {
|
547
|
+
return finishDraft(views);
|
548
|
+
});
|
549
|
+
}
|
550
|
+
};
|
551
|
+
const applyView = (aiTable, views, action) => {
|
552
|
+
switch (action.type) {
|
553
|
+
case ViewActionName.SetView: {
|
554
|
+
const view = views.find((item) => item._id === action.path[0]);
|
555
|
+
if (view) {
|
556
|
+
for (const key in action.newProperties) {
|
557
|
+
const k = key;
|
558
|
+
const value = action.newProperties[k];
|
559
|
+
if (value == null) {
|
560
|
+
delete view[k];
|
561
|
+
}
|
562
|
+
else {
|
563
|
+
view[k] = value;
|
564
|
+
}
|
565
|
+
}
|
566
|
+
// properties that were previously defined, but are now missing, must be deleted
|
567
|
+
for (const key in action.properties) {
|
568
|
+
if (!action.newProperties.hasOwnProperty(key)) {
|
569
|
+
delete view[key];
|
570
|
+
}
|
571
|
+
}
|
572
|
+
}
|
573
|
+
break;
|
574
|
+
}
|
575
|
+
case ViewActionName.AddView: {
|
576
|
+
const [viewIndex] = action.path;
|
577
|
+
if (viewIndex > -1) {
|
578
|
+
views.splice(viewIndex, 0, action.view);
|
579
|
+
}
|
580
|
+
break;
|
581
|
+
}
|
582
|
+
case ViewActionName.RemoveView: {
|
583
|
+
const [viewId] = action.path;
|
584
|
+
const viewIndex = views.findIndex((item) => item._id === viewId);
|
585
|
+
if (viewIndex > -1) {
|
586
|
+
views.splice(viewIndex, 1);
|
587
|
+
}
|
588
|
+
break;
|
589
|
+
}
|
590
|
+
}
|
591
|
+
};
|
592
|
+
|
593
|
+
function setView(aiTable, value, path) {
|
594
|
+
const view = aiTable.views().find((item) => item._id === path[0]);
|
595
|
+
if (view) {
|
596
|
+
const properties = {};
|
597
|
+
const newProperties = {};
|
598
|
+
for (const key in value) {
|
599
|
+
const k = key;
|
600
|
+
if (JSON.stringify(view[k]) !== JSON.stringify(value[k])) {
|
601
|
+
if (view.hasOwnProperty(k)) {
|
602
|
+
properties[k] = view[k];
|
603
|
+
}
|
604
|
+
if (newProperties[k] !== null) {
|
605
|
+
newProperties[k] = value[k];
|
606
|
+
}
|
607
|
+
}
|
608
|
+
}
|
609
|
+
const operation = {
|
610
|
+
type: ViewActionName.SetView,
|
611
|
+
properties,
|
612
|
+
newProperties,
|
613
|
+
path
|
614
|
+
};
|
615
|
+
aiTable.apply(operation);
|
616
|
+
}
|
617
|
+
}
|
618
|
+
function addView(aiTable, view, path) {
|
619
|
+
const operation = {
|
620
|
+
type: ViewActionName.AddView,
|
621
|
+
view,
|
622
|
+
path
|
623
|
+
};
|
624
|
+
aiTable.apply(operation);
|
625
|
+
}
|
626
|
+
function removeView(aiTable, path) {
|
627
|
+
const operation = {
|
628
|
+
type: ViewActionName.RemoveView,
|
629
|
+
path
|
630
|
+
};
|
631
|
+
aiTable.apply(operation);
|
632
|
+
}
|
633
|
+
const ViewActions = {
|
634
|
+
setView,
|
635
|
+
addView,
|
636
|
+
removeView
|
637
|
+
};
|
638
|
+
|
639
|
+
const VIEW_ACTIONS = [ViewActionName.SetView, ViewActionName.AddView, ViewActionName.RemoveView];
|
640
|
+
|
641
|
+
const withView = (aiTable) => {
|
642
|
+
const viewTable = aiTable;
|
643
|
+
viewTable.apply = (action) => {
|
644
|
+
aiTable.actions.push(action);
|
645
|
+
if (VIEW_ACTIONS.includes(action.type)) {
|
646
|
+
GeneralViewActions.transform(viewTable, action);
|
647
|
+
}
|
648
|
+
else {
|
649
|
+
Actions.transform(aiTable, action);
|
650
|
+
}
|
651
|
+
if (!FLUSHING.get(aiTable)) {
|
652
|
+
FLUSHING.set(aiTable, true);
|
653
|
+
Promise.resolve().then(() => {
|
654
|
+
FLUSHING.set(aiTable, false);
|
655
|
+
aiTable.onChange();
|
656
|
+
aiTable.actions = [];
|
657
|
+
});
|
658
|
+
}
|
659
|
+
};
|
660
|
+
return aiTable;
|
661
|
+
};
|
662
|
+
|
663
|
+
/**
|
664
|
+
* Generated bundle index. Do not edit.
|
665
|
+
*/
|
666
|
+
|
667
|
+
export { Direction, FilterOperationSymbol, GeneralViewActions, GridSettings, LogicalOperator, Positions, VIEW_ACTIONS, ViewActionName, ViewActions, YjsAITable, actionMappers, addView, applyActionOps, applyEvents, applyView, applyYjsEvents, createSharedType, getShareTypeNumberPath, getSharedMapValueId, getSharedMapValueIndex, getSharedRecordId, getSharedRecordIndex, initSharedType, initTable, removeView, setView, toRecordSyncElement, toSharedType, toSyncElement, translatePositionToPath, translateToRecordValues, translateToRecords, translateYjsEvent, withView };
|
668
|
+
//# sourceMappingURL=ai-table-state.mjs.map
|