@ai-table/state 0.0.11 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. package/action/field.d.ts +14 -0
  2. package/action/field.d.ts.map +1 -0
  3. package/action/general.d.ts +5 -0
  4. package/action/general.d.ts.map +1 -0
  5. package/action/index.d.ts +17 -0
  6. package/action/index.d.ts.map +1 -0
  7. package/action/position.d.ts +9 -0
  8. package/action/position.d.ts.map +1 -0
  9. package/action/record.d.ts +13 -0
  10. package/action/record.d.ts.map +1 -0
  11. package/{view/action → action}/view.d.ts +1 -1
  12. package/action/view.d.ts.map +1 -0
  13. package/constants/field-menu-item.d.ts +22 -0
  14. package/constants/field-menu-item.d.ts.map +1 -0
  15. package/constants/index.d.ts +3 -0
  16. package/constants/index.d.ts.map +1 -0
  17. package/constants/view.d.ts +3 -0
  18. package/constants/view.d.ts.map +1 -0
  19. package/esm2022/action/field.mjs +57 -0
  20. package/esm2022/action/general.mjs +179 -0
  21. package/esm2022/action/index.mjs +13 -0
  22. package/esm2022/action/position.mjs +21 -0
  23. package/esm2022/action/record.mjs +44 -0
  24. package/esm2022/action/view.mjs +47 -0
  25. package/esm2022/constants/field-menu-item.mjs +39 -0
  26. package/esm2022/constants/index.mjs +3 -0
  27. package/esm2022/constants/view.mjs +3 -0
  28. package/esm2022/plugins/state.plugin.mjs +20 -0
  29. package/esm2022/public-api.mjs +4 -4
  30. package/esm2022/shared/to-table/array-event.mjs +9 -9
  31. package/esm2022/shared/to-table/index.mjs +2 -2
  32. package/esm2022/shared/to-table/map-event.mjs +3 -4
  33. package/esm2022/shared/to-yjs/add-node.mjs +5 -5
  34. package/esm2022/shared/to-yjs/index.mjs +1 -1
  35. package/esm2022/shared/to-yjs/remove-node.mjs +4 -5
  36. package/esm2022/shared/to-yjs/set-node.mjs +3 -4
  37. package/esm2022/shared/to-yjs/update-field-value.mjs +1 -1
  38. package/esm2022/shared/utils/init-shared.mjs +1 -1
  39. package/esm2022/types/action.mjs +23 -0
  40. package/esm2022/types/ai-table.mjs +2 -0
  41. package/esm2022/types/index.mjs +4 -1
  42. package/esm2022/types/view.mjs +1 -12
  43. package/esm2022/utils/build.mjs +9 -14
  44. package/esm2022/utils/common.mjs +21 -1
  45. package/esm2022/utils/field/add-fields.mjs +19 -0
  46. package/esm2022/utils/field/model/date.mjs +74 -0
  47. package/esm2022/utils/field/model/field.mjs +63 -0
  48. package/esm2022/utils/field/model/index.mjs +22 -0
  49. package/esm2022/utils/field/model/number.mjs +46 -0
  50. package/esm2022/utils/field/model/rate.mjs +38 -0
  51. package/esm2022/utils/field/model/select.mjs +55 -0
  52. package/esm2022/utils/field/model/text.mjs +27 -0
  53. package/esm2022/utils/field/sort-fields.mjs +5 -0
  54. package/esm2022/utils/index.mjs +9 -3
  55. package/esm2022/utils/record/add-records.mjs +48 -0
  56. package/esm2022/utils/record/filter.mjs +65 -0
  57. package/esm2022/utils/record/sort.mjs +34 -0
  58. package/esm2022/utils/record/update-field-value.mjs +5 -0
  59. package/esm2022/utils/view.mjs +6 -4
  60. package/esm2022/utils/weak-map.mjs +2 -0
  61. package/fesm2022/ai-table-state.mjs +813 -396
  62. package/fesm2022/ai-table-state.mjs.map +1 -1
  63. package/package.json +1 -1
  64. package/plugins/state.plugin.d.ts +3 -0
  65. package/plugins/state.plugin.d.ts.map +1 -0
  66. package/public-api.d.ts +3 -3
  67. package/public-api.d.ts.map +1 -1
  68. package/shared/to-table/array-event.d.ts +2 -3
  69. package/shared/to-table/array-event.d.ts.map +1 -1
  70. package/shared/to-table/index.d.ts +2 -2
  71. package/shared/to-table/index.d.ts.map +1 -1
  72. package/shared/to-table/map-event.d.ts +2 -2
  73. package/shared/to-table/map-event.d.ts.map +1 -1
  74. package/shared/to-yjs/add-node.d.ts +1 -2
  75. package/shared/to-yjs/add-node.d.ts.map +1 -1
  76. package/shared/to-yjs/index.d.ts +2 -2
  77. package/shared/to-yjs/index.d.ts.map +1 -1
  78. package/shared/to-yjs/remove-node.d.ts +1 -2
  79. package/shared/to-yjs/remove-node.d.ts.map +1 -1
  80. package/shared/to-yjs/set-node.d.ts +1 -2
  81. package/shared/to-yjs/set-node.d.ts.map +1 -1
  82. package/shared/to-yjs/update-field-value.d.ts +1 -2
  83. package/shared/to-yjs/update-field-value.d.ts.map +1 -1
  84. package/shared/utils/init-shared.d.ts.map +1 -1
  85. package/types/action.d.ts +90 -0
  86. package/types/action.d.ts.map +1 -0
  87. package/types/ai-table.d.ts +15 -0
  88. package/types/ai-table.d.ts.map +1 -0
  89. package/types/index.d.ts +3 -0
  90. package/types/index.d.ts.map +1 -1
  91. package/types/view.d.ts +4 -46
  92. package/types/view.d.ts.map +1 -1
  93. package/utils/build.d.ts +3 -4
  94. package/utils/build.d.ts.map +1 -1
  95. package/utils/common.d.ts +5 -0
  96. package/utils/common.d.ts.map +1 -1
  97. package/utils/field/add-fields.d.ts +4 -0
  98. package/utils/field/add-fields.d.ts.map +1 -0
  99. package/utils/field/model/date.d.ts +11 -0
  100. package/utils/field/model/date.d.ts.map +1 -0
  101. package/utils/field/model/field.d.ts +12 -0
  102. package/utils/field/model/field.d.ts.map +1 -0
  103. package/utils/field/model/index.d.ts.map +1 -0
  104. package/utils/field/model/number.d.ts +10 -0
  105. package/utils/field/model/number.d.ts.map +1 -0
  106. package/utils/field/model/rate.d.ts +10 -0
  107. package/utils/field/model/rate.d.ts.map +1 -0
  108. package/utils/field/model/select.d.ts +12 -0
  109. package/utils/field/model/select.d.ts.map +1 -0
  110. package/utils/field/{text.d.ts → model/text.d.ts} +3 -1
  111. package/utils/field/model/text.d.ts.map +1 -0
  112. package/utils/field/sort-fields.d.ts +4 -0
  113. package/utils/field/sort-fields.d.ts.map +1 -0
  114. package/utils/index.d.ts +8 -2
  115. package/utils/index.d.ts.map +1 -1
  116. package/utils/record/add-records.d.ts +5 -0
  117. package/utils/record/add-records.d.ts.map +1 -0
  118. package/utils/record/filter.d.ts +6 -0
  119. package/utils/record/filter.d.ts.map +1 -0
  120. package/utils/record/sort.d.ts +5 -0
  121. package/utils/record/sort.d.ts.map +1 -0
  122. package/utils/record/update-field-value.d.ts +4 -0
  123. package/utils/record/update-field-value.d.ts.map +1 -0
  124. package/utils/view.d.ts.map +1 -1
  125. package/utils/weak-map.d.ts +3 -0
  126. package/utils/weak-map.d.ts.map +1 -0
  127. package/esm2022/utils/field/date.mjs +0 -56
  128. package/esm2022/utils/field/field.mjs +0 -32
  129. package/esm2022/utils/field/index.mjs +0 -22
  130. package/esm2022/utils/field/number.mjs +0 -28
  131. package/esm2022/utils/field/rate.mjs +0 -20
  132. package/esm2022/utils/field/select.mjs +0 -33
  133. package/esm2022/utils/field/text.mjs +0 -21
  134. package/esm2022/utils/filter-records.mjs +0 -52
  135. package/esm2022/view/action/general.mjs +0 -137
  136. package/esm2022/view/action/index.mjs +0 -4
  137. package/esm2022/view/action/position.mjs +0 -21
  138. package/esm2022/view/action/view.mjs +0 -47
  139. package/esm2022/view/constants/view.mjs +0 -4
  140. package/esm2022/view/plugins/view.plugin.mjs +0 -34
  141. package/utils/field/date.d.ts +0 -8
  142. package/utils/field/date.d.ts.map +0 -1
  143. package/utils/field/field.d.ts +0 -8
  144. package/utils/field/field.d.ts.map +0 -1
  145. package/utils/field/index.d.ts.map +0 -1
  146. package/utils/field/number.d.ts +0 -7
  147. package/utils/field/number.d.ts.map +0 -1
  148. package/utils/field/rate.d.ts +0 -7
  149. package/utils/field/rate.d.ts.map +0 -1
  150. package/utils/field/select.d.ts +0 -7
  151. package/utils/field/select.d.ts.map +0 -1
  152. package/utils/field/text.d.ts.map +0 -1
  153. package/utils/filter-records.d.ts +0 -5
  154. package/utils/filter-records.d.ts.map +0 -1
  155. package/view/action/general.d.ts +0 -14
  156. package/view/action/general.d.ts.map +0 -1
  157. package/view/action/index.d.ts +0 -4
  158. package/view/action/index.d.ts.map +0 -1
  159. package/view/action/position.d.ts +0 -8
  160. package/view/action/position.d.ts.map +0 -1
  161. package/view/action/view.d.ts.map +0 -1
  162. package/view/constants/view.d.ts +0 -4
  163. package/view/constants/view.d.ts.map +0 -1
  164. package/view/plugins/view.plugin.d.ts +0 -3
  165. package/view/plugins/view.plugin.d.ts.map +0 -1
  166. /package/utils/field/{index.d.ts → model/index.d.ts} +0 -0
@@ -1,8 +1,48 @@
1
1
  import * as Y from 'yjs';
2
- import { ActionName, AITableQueries, getDefaultFieldValue, idCreator, Actions, FLUSHING, AITableFieldType } from '@ai-table/grid';
3
2
  import { isArray, isEmpty as isEmpty$1, isObject, isUndefinedOrNull, TinyDate } from 'ngx-tethys/util';
4
- import { createDraft, finishDraft } from 'immer';
3
+ import { AITableQueries, getDefaultFieldValue, idCreator, AITableFieldType, Direction as Direction$1, AI_TABLE_GRID_FIELD_SERVICE_MAP } from '@ai-table/grid';
4
+ import _, { isEqual } from 'lodash';
5
5
  import { fromUnixTime, subDays } from 'date-fns';
6
+ import { createDraft, finishDraft } from 'immer';
7
+
8
+ const IS_LOCAL = new WeakSet();
9
+ const IS_REMOTE = new WeakSet();
10
+ const IS_UNDO = new WeakSet();
11
+ const YjsAITable = {
12
+ isLocal: (aiTable) => {
13
+ return IS_LOCAL.has(aiTable);
14
+ },
15
+ asLocal: (aiTable, fn) => {
16
+ const wasLocal = YjsAITable.isLocal(aiTable);
17
+ IS_LOCAL.add(aiTable);
18
+ fn();
19
+ if (!wasLocal) {
20
+ IS_LOCAL.delete(aiTable);
21
+ }
22
+ },
23
+ isRemote: (aiTable) => {
24
+ return IS_REMOTE.has(aiTable);
25
+ },
26
+ asRemote: (aiTable, fn) => {
27
+ const wasRemote = YjsAITable.isRemote(aiTable);
28
+ IS_REMOTE.add(aiTable);
29
+ fn();
30
+ if (!wasRemote) {
31
+ Promise.resolve().then(() => IS_REMOTE.delete(aiTable));
32
+ }
33
+ },
34
+ isUndo: (aiTable) => {
35
+ return IS_UNDO.has(aiTable);
36
+ },
37
+ asUndo: (aiTable, fn) => {
38
+ const wasUndo = YjsAITable.isUndo(aiTable);
39
+ IS_UNDO.add(aiTable);
40
+ fn();
41
+ if (!wasUndo) {
42
+ Promise.resolve().then(() => IS_UNDO.delete(aiTable));
43
+ }
44
+ }
45
+ };
6
46
 
7
47
  class Positions {
8
48
  }
@@ -34,17 +74,29 @@ var AITableFilterOperation;
34
74
  AITableFilterOperation["exists"] = "exists";
35
75
  AITableFilterOperation["notContain"] = "not_contain";
36
76
  })(AITableFilterOperation || (AITableFilterOperation = {}));
37
- var ViewActionName;
38
- (function (ViewActionName) {
39
- ViewActionName["SetView"] = "set_view";
40
- ViewActionName["AddView"] = "add_view";
41
- ViewActionName["RemoveView"] = "remove_view";
42
- })(ViewActionName || (ViewActionName = {}));
43
- var PositionActionName;
44
- (function (PositionActionName) {
45
- PositionActionName["AddRecordPosition"] = "add_record_position";
46
- PositionActionName["RemoveRecordPosition"] = "remove_record_position";
47
- })(PositionActionName || (PositionActionName = {}));
77
+
78
+ var ActionName;
79
+ (function (ActionName) {
80
+ ActionName["UpdateFieldValue"] = "update_field_value";
81
+ ActionName["AddRecord"] = "add_record";
82
+ ActionName["AddField"] = "add_field";
83
+ ActionName["MoveField"] = "move_field";
84
+ ActionName["MoveRecord"] = "move_record";
85
+ ActionName["RemoveField"] = "remove_field";
86
+ ActionName["RemoveRecord"] = "remove_record";
87
+ ActionName["SetField"] = "set_field";
88
+ ActionName["SetView"] = "set_view";
89
+ ActionName["AddView"] = "add_view";
90
+ ActionName["RemoveView"] = "remove_view";
91
+ ActionName["AddRecordPosition"] = "add_record_position";
92
+ ActionName["RemoveRecordPosition"] = "remove_record_position";
93
+ })(ActionName || (ActionName = {}));
94
+ var ExecuteType;
95
+ (function (ExecuteType) {
96
+ ExecuteType[ExecuteType["Execute"] = 0] = "Execute";
97
+ ExecuteType[ExecuteType["Undo"] = 1] = "Undo";
98
+ ExecuteType[ExecuteType["Redo"] = 2] = "Redo";
99
+ })(ExecuteType || (ExecuteType = {}));
48
100
 
49
101
  const createSharedType = () => {
50
102
  const doc = new Y.Doc();
@@ -185,7 +237,7 @@ function translateArrayEvent(aiTable, sharedType, event) {
185
237
  const removeView = aiTable.views()[offset];
186
238
  if (removeView) {
187
239
  actions.push({
188
- type: ViewActionName.RemoveView,
240
+ type: ActionName.RemoveView,
189
241
  path: [removeView._id]
190
242
  });
191
243
  }
@@ -235,7 +287,7 @@ function translateArrayEvent(aiTable, sharedType, event) {
235
287
  for (const key in item) {
236
288
  if (!record.positions[key] && record.positions[key] !== 0) {
237
289
  actions.push({
238
- type: PositionActionName.AddRecordPosition,
290
+ type: ActionName.AddRecordPosition,
239
291
  path: [record._id],
240
292
  position: {
241
293
  [key]: item[key]
@@ -246,7 +298,7 @@ function translateArrayEvent(aiTable, sharedType, event) {
246
298
  for (const key in record.positions) {
247
299
  if (!item[key] && item[key] !== 0) {
248
300
  actions.push({
249
- type: PositionActionName.RemoveRecordPosition,
301
+ type: ActionName.RemoveRecordPosition,
250
302
  path: [key, record._id]
251
303
  });
252
304
  }
@@ -287,7 +339,7 @@ function translateArrayEvent(aiTable, sharedType, event) {
287
339
  delta.insert?.map((item, index) => {
288
340
  const data = item.toJSON();
289
341
  actions.push({
290
- type: ViewActionName.AddView,
342
+ type: ActionName.AddView,
291
343
  path: [offset + index],
292
344
  view: data
293
345
  });
@@ -359,7 +411,7 @@ function translateMapEvent(aiTable, sharedType, event) {
359
411
  const k = key;
360
412
  properties[k] = value;
361
413
  }
362
- const type = isFieldsTranslate ? ActionName.SetField : ViewActionName.SetView;
414
+ const type = isFieldsTranslate ? ActionName.SetField : ActionName.SetView;
363
415
  return [
364
416
  {
365
417
  type,
@@ -373,45 +425,6 @@ function translateMapEvent(aiTable, sharedType, event) {
373
425
  return [];
374
426
  }
375
427
 
376
- const IS_LOCAL = new WeakSet();
377
- const IS_REMOTE = new WeakSet();
378
- const IS_UNDO = new WeakSet();
379
- const YjsAITable = {
380
- isLocal: (aiTable) => {
381
- return IS_LOCAL.has(aiTable);
382
- },
383
- asLocal: (aiTable, fn) => {
384
- const wasLocal = YjsAITable.isLocal(aiTable);
385
- IS_LOCAL.add(aiTable);
386
- fn();
387
- if (!wasLocal) {
388
- IS_LOCAL.delete(aiTable);
389
- }
390
- },
391
- isRemote: (aiTable) => {
392
- return IS_REMOTE.has(aiTable);
393
- },
394
- asRemote: (aiTable, fn) => {
395
- const wasRemote = YjsAITable.isRemote(aiTable);
396
- IS_REMOTE.add(aiTable);
397
- fn();
398
- if (!wasRemote) {
399
- Promise.resolve().then(() => IS_REMOTE.delete(aiTable));
400
- }
401
- },
402
- isUndo: (aiTable) => {
403
- return IS_UNDO.has(aiTable);
404
- },
405
- asUndo: (aiTable, fn) => {
406
- const wasUndo = YjsAITable.isUndo(aiTable);
407
- IS_UNDO.add(aiTable);
408
- fn();
409
- if (!wasUndo) {
410
- Promise.resolve().then(() => IS_UNDO.delete(aiTable));
411
- }
412
- }
413
- };
414
-
415
428
  function translateYjsEvent(aiTable, sharedType, event) {
416
429
  if (event instanceof Y.YArrayEvent) {
417
430
  return translateArrayEvent(aiTable, sharedType, event);
@@ -437,7 +450,7 @@ function applyYjsEvents(aiTable, sharedType, events) {
437
450
  }
438
451
  }
439
452
 
440
- function updateFieldValue(sharedType, action) {
453
+ function updateFieldValue$2(sharedType, action) {
441
454
  const sharedRecords = sharedType.get('records');
442
455
  const sharedFields = sharedType.get('fields');
443
456
  if (sharedRecords && sharedFields) {
@@ -458,7 +471,7 @@ function setNode(sharedType, action) {
458
471
  if (action.type === ActionName.SetField) {
459
472
  sharedNodes = sharedType.get('fields');
460
473
  }
461
- if (action.type === ViewActionName.SetView) {
474
+ if (action.type === ActionName.SetView) {
462
475
  sharedNodes = sharedType.get('views');
463
476
  }
464
477
  if (sharedNodes) {
@@ -491,10 +504,10 @@ function addNode(sharedType, action) {
491
504
  case ActionName.AddRecord:
492
505
  records && records.push([toRecordSyncElement(action.record)]);
493
506
  break;
494
- case ViewActionName.AddView:
507
+ case ActionName.AddView:
495
508
  views && views.push([toSyncElement(action.view)]);
496
509
  break;
497
- case PositionActionName.AddRecordPosition:
510
+ case ActionName.AddRecordPosition:
498
511
  if (records) {
499
512
  const recordIndex = getSharedRecordIndex(records, action.path[0]);
500
513
  const record = records.get(recordIndex);
@@ -534,7 +547,7 @@ function removeNode(sharedType, action) {
534
547
  }
535
548
  }
536
549
  break;
537
- case ViewActionName.RemoveView:
550
+ case ActionName.RemoveView:
538
551
  if (views) {
539
552
  const viewIndex = getSharedMapValueIndex(views, action.path[0]);
540
553
  if (viewIndex > -1) {
@@ -542,7 +555,7 @@ function removeNode(sharedType, action) {
542
555
  }
543
556
  }
544
557
  break;
545
- case PositionActionName.RemoveRecordPosition:
558
+ case ActionName.RemoveRecordPosition:
546
559
  if (records) {
547
560
  const recordIndex = getSharedRecordIndex(records, action.path[1]);
548
561
  const record = records.get(recordIndex);
@@ -571,7 +584,7 @@ function removeNode(sharedType, action) {
571
584
 
572
585
  const actionMappers = {
573
586
  add_record: addNode,
574
- update_field_value: updateFieldValue,
587
+ update_field_value: updateFieldValue$2,
575
588
  remove_record: removeNode,
576
589
  add_field: addNode,
577
590
  set_field: setNode,
@@ -597,343 +610,92 @@ function applyActionOps(sharedType, actions, aiTable) {
597
610
  return sharedType;
598
611
  }
599
612
 
600
- function createDefaultPositions(views, activeId, data, index) {
601
- const positions = {};
602
- const position = getPosition(data, activeId, index);
603
- const maxPosition = data[data.length - 1].positions[activeId];
604
- views.forEach((element) => {
605
- positions[element._id] = element._id === activeId ? position : maxPosition + 1;
606
- });
607
- return positions;
608
- }
609
- function getPosition(data, activeViewId, index) {
610
- let position = data.length - 1;
611
- if (index !== 0 && index !== data.length) {
612
- const previousViewPosition = data[index - 1].positions[activeViewId];
613
- const nextViewPosition = data[index].positions[activeViewId];
614
- position = (previousViewPosition + nextViewPosition) / 2;
613
+ function isEmpty(value) {
614
+ if (isArray(value)) {
615
+ return isEmpty$1(value);
615
616
  }
616
- else {
617
- position = index;
617
+ if (isObject(value)) {
618
+ return Reflect.ownKeys(value).length === 0;
618
619
  }
619
- return position;
620
+ return isUndefinedOrNull(value) || value == '';
620
621
  }
621
- function addView$1(aiTable, type) {
622
- let index = aiTable.views().length;
623
- const newId = idCreator();
624
- let newView = {
625
- _id: newId,
626
- name: '表格视图 ' + index
627
- };
628
- let originViewId = aiTable.views()[aiTable.views().length - 1]._id;
629
- if (type === 'copy') {
630
- originViewId = aiTable.activeViewId();
631
- const copyView = aiTable.views().find((item) => item._id === aiTable.activeViewId());
632
- newView = {
633
- ...copyView,
634
- _id: newId,
635
- name: copyView.name + '-副本'
636
- };
637
- index = aiTable.views().indexOf(copyView) + 1;
622
+ function getNewIdsByCount(count) {
623
+ if (count <= 0)
624
+ return [];
625
+ const newIds = [];
626
+ for (let i = 0; i < count; i++) {
627
+ newIds.push(idCreator());
638
628
  }
639
- ViewActions.addView(aiTable, newView, [index]);
640
- aiTable.records().forEach((record) => {
641
- PositionActions.addRecordPosition(aiTable, { [newId]: record.positions[originViewId] }, [record._id]);
642
- });
643
- aiTable.fields().forEach((field) => {
644
- Actions.setField(aiTable, {
645
- positions: {
646
- ...field.positions,
647
- [newId]: field.positions[originViewId]
648
- }
649
- }, [field._id]);
650
- });
651
- return newView;
629
+ return newIds;
652
630
  }
653
- function removeView$1(aiTable, records, fields, activeViewId) {
654
- records.forEach((record) => {
655
- PositionActions.removeRecordPosition(aiTable, [activeViewId, record._id]);
656
- });
657
- fields.forEach((field) => {
658
- const positions = { ...field.positions };
659
- delete positions[activeViewId];
660
- Actions.setField(aiTable, {
661
- positions
662
- }, [field._id]);
663
- });
664
- ViewActions.removeView(aiTable, [activeViewId]);
631
+ function isPathEqual(path, another) {
632
+ return path.length === another.length && path.every((n, i) => n === another[i]);
633
+ }
634
+ function sortByViewPosition(data, activeView) {
635
+ const hasPositions = data.every((item) => item.positions && item.positions);
636
+ if (hasPositions) {
637
+ return [...data].sort((a, b) => a.positions[activeView._id] - b.positions[activeView._id]);
638
+ }
639
+ return data;
665
640
  }
666
641
 
667
- const GeneralViewActions = {
668
- transform(aiTable, action) {
669
- const views = createDraft(aiTable.views());
670
- applyView(aiTable, views, action);
671
- aiTable.views.set(finishDraft(views));
642
+ const zhIntlCollator = typeof Intl !== 'undefined' ? new Intl.Collator('zh-CN') : undefined;
643
+ class Field {
644
+ stringInclude(str, searchStr) {
645
+ return str.toLowerCase().includes(searchStr.trim().toLowerCase());
672
646
  }
673
- };
674
- const applyView = (aiTable, views, action) => {
675
- switch (action.type) {
676
- case ViewActionName.SetView: {
677
- const view = views.find((item) => item._id === action.path[0]);
678
- if (view) {
679
- for (const key in action.newProperties) {
680
- const k = key;
681
- const value = action.newProperties[k];
682
- if (value == null) {
683
- delete view[k];
684
- }
685
- else {
686
- view[k] = value;
687
- }
688
- }
689
- // properties that were previously defined, but are now missing, must be deleted
690
- for (const key in action.properties) {
691
- if (!action.newProperties.hasOwnProperty(key)) {
692
- delete view[key];
693
- }
694
- }
647
+ isMeetFilter(condition, cellValue) {
648
+ switch (condition.operation) {
649
+ case AITableFilterOperation.empty:
650
+ case AITableFilterOperation.exists: {
651
+ return this.isEmptyOrNot(condition.operation, cellValue);
695
652
  }
696
- break;
697
- }
698
- case ViewActionName.AddView: {
699
- const [viewIndex] = action.path;
700
- if (viewIndex > -1) {
701
- views.splice(viewIndex, 0, action.view);
653
+ default: {
654
+ return true;
702
655
  }
703
- break;
704
656
  }
705
- case ViewActionName.RemoveView: {
706
- const [viewId] = action.path;
707
- const viewIndex = views.findIndex((item) => item._id === viewId);
708
- if (viewIndex > -1) {
709
- views.splice(viewIndex, 1);
657
+ }
658
+ isEmptyOrNot(operation, cellValue) {
659
+ switch (operation) {
660
+ case AITableFilterOperation.empty: {
661
+ return isEmpty(cellValue);
662
+ }
663
+ case AITableFilterOperation.exists: {
664
+ return !isEmpty(cellValue);
665
+ }
666
+ default: {
667
+ throw new Error('compare operator type error');
710
668
  }
711
- break;
712
669
  }
713
670
  }
714
- };
715
- const GeneralActions = {
716
- transform(aiTable, action) {
717
- const records = createDraft(aiTable.records());
718
- const fields = createDraft(aiTable.fields());
719
- apply(aiTable, records, fields, action);
720
- if (action.type === ActionName.AddRecord) {
721
- aiTable.records.set(finishDraft(records));
671
+ eq(cv1, cv2) {
672
+ return isEqual(cv1, cv2);
673
+ }
674
+ compare(cellValue1, cellValue2, field) {
675
+ if (this.eq(cellValue1, cellValue2)) {
676
+ return 0;
722
677
  }
723
- if (action.type === ActionName.AddField) {
724
- aiTable.fields.set(finishDraft(fields));
678
+ if (cellValue1 == null) {
679
+ return -1;
725
680
  }
726
- }
727
- };
728
- const apply = (aiTable, records, fields, action) => {
729
- switch (action.type) {
730
- case ActionName.AddRecord: {
731
- const [recordIndex] = action.path;
732
- if (recordIndex > -1) {
733
- if (!action.record.positions) {
734
- const activeView = aiTable.views().find((item) => item._id === aiTable.activeViewId());
735
- let index = recordIndex;
736
- if (activeView?.settings?.conditions) {
737
- index = records.length;
738
- }
739
- action.record.positions = createDefaultPositions(aiTable.views(), aiTable.activeViewId(), aiTable.records(), index);
740
- }
741
- records.splice(recordIndex, 0, action.record);
742
- }
743
- break;
681
+ if (cellValue2 == null) {
682
+ return 1;
744
683
  }
745
- case ActionName.AddField: {
746
- const [fieldIndex] = action.path;
747
- if (fieldIndex > -1) {
748
- const newField = action.field;
749
- newField.positions = createDefaultPositions(aiTable.views(), aiTable.activeViewId(), aiTable.fields(), action.path[0]);
750
- fields.splice(fieldIndex, 0, newField);
751
- const newRecord = {
752
- [newField._id]: getDefaultFieldValue(action.field)
753
- };
754
- records.forEach((item) => {
755
- item.values = {
756
- ...item.values,
757
- ...newRecord
758
- };
759
- });
760
- }
761
- break;
762
- }
763
- }
764
- };
765
- const GeneralPositionActions = {
766
- transform(aiTable, action) {
767
- const records = createDraft(aiTable.records());
768
- const fields = createDraft(aiTable.fields());
769
- applyPosition(aiTable, records, action);
770
- aiTable.records.update(() => {
771
- return finishDraft(records);
772
- });
773
- aiTable.fields.update(() => {
774
- return finishDraft(fields);
775
- });
776
- }
777
- };
778
- const applyPosition = (aiTable, records, action) => {
779
- switch (action.type) {
780
- case PositionActionName.AddRecordPosition: {
781
- const { position, path } = action;
782
- const record = records.find((item) => item._id === path[0]);
783
- if (record) {
784
- record.positions = {
785
- ...record.positions,
786
- ...position
787
- };
788
- }
789
- break;
790
- }
791
- case PositionActionName.RemoveRecordPosition: {
792
- const { path } = action;
793
- const record = records.find((item) => item._id === path[1]);
794
- delete record?.positions[path[0]];
795
- break;
796
- }
797
- }
798
- };
799
-
800
- function setView(aiTable, value, path) {
801
- const view = aiTable.views().find((item) => item._id === path[0]);
802
- if (view) {
803
- const properties = {};
804
- const newProperties = {};
805
- for (const key in value) {
806
- const k = key;
807
- if (JSON.stringify(view[k]) !== JSON.stringify(value[k])) {
808
- if (view.hasOwnProperty(k)) {
809
- properties[k] = view[k];
810
- }
811
- if (newProperties[k] !== null) {
812
- newProperties[k] = value[k];
813
- }
814
- }
815
- }
816
- const operation = {
817
- type: ViewActionName.SetView,
818
- properties,
819
- newProperties,
820
- path
821
- };
822
- aiTable.apply(operation);
823
- }
824
- }
825
- function addView(aiTable, view, path) {
826
- const operation = {
827
- type: ViewActionName.AddView,
828
- view,
829
- path
830
- };
831
- aiTable.apply(operation);
832
- }
833
- function removeView(aiTable, path) {
834
- const operation = {
835
- type: ViewActionName.RemoveView,
836
- path
837
- };
838
- aiTable.apply(operation);
839
- }
840
- const ViewActions = {
841
- setView,
842
- addView,
843
- removeView
844
- };
845
-
846
- function addRecordPosition(aiTable, position, path) {
847
- const operation = {
848
- type: PositionActionName.AddRecordPosition,
849
- position,
850
- path
851
- };
852
- aiTable.apply(operation);
853
- }
854
- function removeRecordPosition(aiTable, path) {
855
- const operation = {
856
- type: PositionActionName.RemoveRecordPosition,
857
- path
858
- };
859
- aiTable.apply(operation);
860
- }
861
- const PositionActions = {
862
- addRecordPosition,
863
- removeRecordPosition
864
- };
865
-
866
- const VIEW_ACTIONS = [ViewActionName.SetView, ViewActionName.AddView, ViewActionName.RemoveView];
867
- const POSITION_ACTIONS = [PositionActionName.AddRecordPosition, PositionActionName.RemoveRecordPosition];
868
-
869
- const withView = (aiTable) => {
870
- const viewTable = aiTable;
871
- viewTable.apply = (action) => {
872
- const sharedActions = viewTable.actions;
873
- sharedActions.push(action);
874
- if (VIEW_ACTIONS.includes(action.type)) {
875
- GeneralViewActions.transform(viewTable, action);
876
- }
877
- else if (POSITION_ACTIONS.includes(action.type)) {
878
- GeneralPositionActions.transform(viewTable, action);
879
- }
880
- else {
881
- if (action.type === ActionName.AddField || action.type === ActionName.AddRecord) {
882
- GeneralActions.transform(viewTable, action);
883
- }
884
- else {
885
- Actions.transform(aiTable, action);
886
- }
887
- }
888
- if (!FLUSHING.get(aiTable)) {
889
- FLUSHING.set(aiTable, true);
890
- Promise.resolve().then(() => {
891
- FLUSHING.set(aiTable, false);
892
- aiTable.onChange();
893
- aiTable.actions = [];
894
- });
895
- }
896
- };
897
- return aiTable;
898
- };
899
-
900
- function isEmpty(value) {
901
- if (isArray(value)) {
902
- return isEmpty$1(value);
903
- }
904
- if (isObject(value)) {
905
- return Reflect.ownKeys(value).length === 0;
906
- }
907
- return isUndefinedOrNull(value) || value == '';
908
- }
909
-
910
- class Field {
911
- stringInclude(str, searchStr) {
912
- return str.toLowerCase().includes(searchStr.trim().toLowerCase());
913
- }
914
- isMeetFilter(condition, cellValue) {
915
- switch (condition.operation) {
916
- case AITableFilterOperation.empty:
917
- case AITableFilterOperation.exists: {
918
- return this.isEmptyOrNot(condition.operation, cellValue);
919
- }
920
- default: {
921
- return true;
922
- }
923
- }
924
- }
925
- isEmptyOrNot(operation, cellValue) {
926
- switch (operation) {
927
- case AITableFilterOperation.empty: {
928
- return isEmpty(cellValue);
929
- }
930
- case AITableFilterOperation.exists: {
931
- return !isEmpty(cellValue);
932
- }
933
- default: {
934
- throw new Error('compare operator type error');
935
- }
684
+ let str1 = this.cellValueToString(cellValue1, field);
685
+ let str2 = this.cellValueToString(cellValue2, field);
686
+ if (str1 === str2) {
687
+ return 0;
936
688
  }
689
+ if (str1 == null) {
690
+ return -1;
691
+ }
692
+ if (str2 == null) {
693
+ return 1;
694
+ }
695
+ str1 = str1.trim();
696
+ str2 = str2.trim();
697
+ // test pinyin sort
698
+ return str1 === str2 ? 0 : zhIntlCollator ? zhIntlCollator.compare(str1, str2) : str1.localeCompare(str2, 'zh-CN') > 0 ? 1 : -1;
937
699
  }
938
700
  }
939
701
 
@@ -953,6 +715,12 @@ class TextField extends Field {
953
715
  static stringInclude(str, searchStr) {
954
716
  return str.toLowerCase().includes(searchStr.trim().toLowerCase());
955
717
  }
718
+ eq(cv1, cv2) {
719
+ return this.cellValueToString(cv1) === this.cellValueToString(cv2);
720
+ }
721
+ cellValueToString(cellValue) {
722
+ return cellValue;
723
+ }
956
724
  }
957
725
 
958
726
  class SelectField extends Field {
@@ -970,6 +738,28 @@ class SelectField extends Field {
970
738
  return super.isMeetFilter(condition, cellValue);
971
739
  }
972
740
  }
741
+ cellValueToString(cellValue, field) {
742
+ return this.arrayValueToString(this.cellValueToArray(cellValue, field));
743
+ }
744
+ cellValueToArray(cellValue, field) {
745
+ if (!cellValue) {
746
+ return null;
747
+ }
748
+ const result = [];
749
+ for (let i = 0, l = cellValue.length; i < l; i++) {
750
+ const option = this.findOptionById(field, cellValue[i]);
751
+ if (option) {
752
+ result.push(option.text);
753
+ }
754
+ }
755
+ return result;
756
+ }
757
+ findOptionById(field, id) {
758
+ return field.settings.options.find(option => option._id === id) || null;
759
+ }
760
+ arrayValueToString(cellValues) {
761
+ return cellValues && cellValues.length ? cellValues.join(', ') : null;
762
+ }
973
763
  }
974
764
  function hasIntersect(array1, array2) {
975
765
  if (!Array.isArray(array1) || !Array.isArray(array2)) {
@@ -1034,6 +824,24 @@ class DateField extends Field {
1034
824
  ];
1035
825
  }
1036
826
  }
827
+ cellValueToString(_cellValue) {
828
+ return null;
829
+ }
830
+ static _compare(cellValue1, cellValue2) {
831
+ if (isEmpty(cellValue1?.timestamp) && isEmpty(cellValue2?.timestamp)) {
832
+ return 0;
833
+ }
834
+ if (isEmpty(cellValue1?.timestamp)) {
835
+ return -1;
836
+ }
837
+ if (isEmpty(cellValue2?.timestamp)) {
838
+ return 1;
839
+ }
840
+ return cellValue1.timestamp === cellValue2.timestamp ? 0 : cellValue1.timestamp > cellValue2.timestamp ? 1 : -1;
841
+ }
842
+ compare(cellValue1, cellValue2) {
843
+ return DateField._compare(cellValue1, cellValue2);
844
+ }
1037
845
  }
1038
846
 
1039
847
  class NumberField extends Field {
@@ -1059,6 +867,24 @@ class NumberField extends Field {
1059
867
  return super.isMeetFilter(condition, cellValue);
1060
868
  }
1061
869
  }
870
+ cellValueToString(_cellValue) {
871
+ return null;
872
+ }
873
+ static _compare(cellValue1, cellValue2) {
874
+ if (isEmpty(cellValue1) && isEmpty(cellValue2)) {
875
+ return 0;
876
+ }
877
+ if (isEmpty(cellValue1)) {
878
+ return -1;
879
+ }
880
+ if (isEmpty(cellValue2)) {
881
+ return 1;
882
+ }
883
+ return cellValue1 === cellValue2 ? 0 : cellValue1 > cellValue2 ? 1 : -1;
884
+ }
885
+ compare(cellValue1, cellValue2) {
886
+ return NumberField._compare(cellValue1, cellValue2);
887
+ }
1062
888
  }
1063
889
 
1064
890
  class RateField extends Field {
@@ -1076,6 +902,24 @@ class RateField extends Field {
1076
902
  return super.isMeetFilter(condition, cellValue);
1077
903
  }
1078
904
  }
905
+ cellValueToString(_cellValue) {
906
+ return null;
907
+ }
908
+ static _compare(cellValue1, cellValue2) {
909
+ if (isEmpty(cellValue1) && isEmpty(cellValue2)) {
910
+ return 0;
911
+ }
912
+ if (isEmpty(cellValue1)) {
913
+ return -1;
914
+ }
915
+ if (isEmpty(cellValue2)) {
916
+ return 1;
917
+ }
918
+ return cellValue1 === cellValue2 ? 0 : cellValue1 > cellValue2 ? 1 : -1;
919
+ }
920
+ compare(cellValue1, cellValue2) {
921
+ return RateField._compare(cellValue1, cellValue2);
922
+ }
1079
923
  }
1080
924
 
1081
925
  const ViewOperationMap = {
@@ -1094,7 +938,38 @@ const ViewOperationMap = {
1094
938
  [AITableFieldType.updatedBy]: new SelectField()
1095
939
  };
1096
940
 
1097
- function getFilteredRecords(records, fields, activeView) {
941
+ function getSortRecords(aiTable, records, activeView) {
942
+ if (!activeView?.settings || !activeView.settings.sorts?.length) {
943
+ return records;
944
+ }
945
+ const { is_keep_sort, sorts } = activeView.settings;
946
+ if (is_keep_sort && sorts?.length) {
947
+ return sortRecordsBySortInfo(aiTable, records, activeView);
948
+ }
949
+ return sortByViewPosition(records, activeView);
950
+ }
951
+ function sortRecordsBySortInfo(aiTable, records, activeView) {
952
+ const shallowRows = [...records];
953
+ if (activeView.settings?.sorts?.length) {
954
+ shallowRows.sort((prev, current) => {
955
+ return activeView.settings.sorts.reduce((acc, rule) => {
956
+ const field = aiTable.fieldsMap()[rule.sort_by];
957
+ if (!field || acc !== 0) {
958
+ return acc;
959
+ }
960
+ const fieldMethod = ViewOperationMap[field.type];
961
+ const cellValue1 = AITableQueries.getFieldValue(aiTable, [prev._id, field._id]);
962
+ const cellValue2 = AITableQueries.getFieldValue(aiTable, [current._id, field._id]);
963
+ const res = fieldMethod.compare(cellValue1, cellValue2, field);
964
+ return res * rule.direction;
965
+ }, 0);
966
+ });
967
+ return shallowRows;
968
+ }
969
+ return shallowRows;
970
+ }
971
+
972
+ function getFilteredRecords(aiTable, records, fields, activeView) {
1098
973
  const { conditions, condition_logical } = activeView.settings || {};
1099
974
  if (!conditions) {
1100
975
  return records;
@@ -1103,7 +978,11 @@ function getFilteredRecords(records, fields, activeView) {
1103
978
  if (!illegalConditions.length) {
1104
979
  return records;
1105
980
  }
981
+ const recordsWillHidden = aiTable.recordsWillHidden();
1106
982
  return records.filter((record) => {
983
+ if (recordsWillHidden && recordsWillHidden.length && recordsWillHidden.includes(record._id)) {
984
+ return true;
985
+ }
1107
986
  return checkConditions(fields, record, { conditions: illegalConditions, condition_logical });
1108
987
  });
1109
988
  }
@@ -1142,25 +1021,563 @@ function doFilter(condition, field, cellValue) {
1142
1021
  }
1143
1022
  return ViewOperationMap[field.type].isMeetFilter(condition, cellValue);
1144
1023
  }
1145
-
1146
- function sortByView(data, activeViewId) {
1147
- const hasPositions = data.every((item) => item.positions && item.positions);
1148
- if (hasPositions) {
1149
- return [...data].sort((a, b) => a.positions[activeViewId] - b.positions[activeViewId]);
1024
+ function getDefaultRecordDataByFilter(recordValues, conditions, conditionLogical) {
1025
+ if (conditions.length === 1) {
1026
+ // recordValues[conditions[0].field_id] = conditions[0].value;
1150
1027
  }
1151
- return data;
1028
+ else {
1029
+ //...
1030
+ }
1031
+ return recordValues;
1032
+ }
1033
+
1034
+ function getSortFields(aiTable, fields, activeView) {
1035
+ return sortByViewPosition(fields, activeView);
1036
+ }
1037
+
1038
+ function buildRecordsByView(aiTable, records, fields, activeView) {
1039
+ const filteredRecords = getFilteredRecords(aiTable, records, fields, activeView);
1040
+ return getSortRecords(aiTable, filteredRecords, activeView);
1041
+ }
1042
+ function buildFieldsByView(aiTable, fields, activeView) {
1043
+ return getSortFields(aiTable, fields, activeView);
1044
+ }
1045
+
1046
+ function addField(aiTable, field, path) {
1047
+ const operation = {
1048
+ type: ActionName.AddField,
1049
+ field,
1050
+ path
1051
+ };
1052
+ aiTable.apply(operation);
1053
+ }
1054
+ function moveField(aiTable, path, newPath) {
1055
+ const operation = {
1056
+ type: ActionName.MoveField,
1057
+ path,
1058
+ newPath
1059
+ };
1060
+ aiTable.apply(operation);
1152
1061
  }
1153
- function buildRecordsByView(records, fields, activeView) {
1154
- const filteredRecords = getFilteredRecords(records, fields, activeView);
1155
- return sortByView(filteredRecords, activeView._id);
1062
+ function removeField(aiTable, path) {
1063
+ const operation = {
1064
+ type: ActionName.RemoveField,
1065
+ path
1066
+ };
1067
+ aiTable.apply(operation);
1156
1068
  }
1157
- function buildFieldsByView(fields, activeView) {
1158
- return sortByView(fields, activeView._id);
1069
+ function setField(aiTable, value, path) {
1070
+ const field = AITableQueries.getField(aiTable, path);
1071
+ if (field) {
1072
+ const properties = {};
1073
+ const newProperties = {};
1074
+ for (const key in value) {
1075
+ const k = key;
1076
+ if (JSON.stringify(field[k]) !== JSON.stringify(value[k])) {
1077
+ if (field.hasOwnProperty(k)) {
1078
+ properties[k] = field[k];
1079
+ }
1080
+ if (newProperties[k] !== null) {
1081
+ newProperties[k] = value[k];
1082
+ }
1083
+ }
1084
+ }
1085
+ const operation = {
1086
+ type: ActionName.SetField,
1087
+ properties,
1088
+ newProperties,
1089
+ path
1090
+ };
1091
+ aiTable.apply(operation);
1092
+ }
1159
1093
  }
1094
+ const FieldActions = {
1095
+ addField,
1096
+ moveField,
1097
+ removeField,
1098
+ setField
1099
+ };
1100
+
1101
+ const apply = (aiTable, records, fields, views, action) => {
1102
+ switch (action.type) {
1103
+ case ActionName.UpdateFieldValue: {
1104
+ const [recordId, fieldId] = action.path;
1105
+ if (recordId && fieldId) {
1106
+ const recordIndex = aiTable.records().findIndex((item) => item._id === recordId);
1107
+ records[recordIndex].values[fieldId] = action.newFieldValue;
1108
+ }
1109
+ break;
1110
+ }
1111
+ case ActionName.AddRecord: {
1112
+ const [recordIndex] = action.path;
1113
+ if (recordIndex > -1) {
1114
+ if (!action.record.positions) {
1115
+ const activeView = aiTable.views().find((item) => item._id === aiTable.activeViewId());
1116
+ let index = recordIndex;
1117
+ if (activeView?.settings?.conditions) {
1118
+ index = records.length;
1119
+ }
1120
+ action.record.positions = createDefaultPositions(aiTable.views(), aiTable.activeViewId(), aiTable.records(), index);
1121
+ }
1122
+ records.splice(recordIndex, 0, action.record);
1123
+ }
1124
+ break;
1125
+ }
1126
+ case ActionName.AddField: {
1127
+ const [fieldIndex] = action.path;
1128
+ if (fieldIndex > -1) {
1129
+ const newField = action.field;
1130
+ newField.positions = createDefaultPositions(aiTable.views(), aiTable.activeViewId(), aiTable.fields(), action.path[0]);
1131
+ fields.splice(fieldIndex, 0, newField);
1132
+ const newRecord = {
1133
+ [newField._id]: getDefaultFieldValue(action.field)
1134
+ };
1135
+ records.forEach((item) => {
1136
+ item.values = {
1137
+ ...item.values,
1138
+ ...newRecord
1139
+ };
1140
+ });
1141
+ }
1142
+ break;
1143
+ }
1144
+ case ActionName.MoveRecord: {
1145
+ if (isPathEqual(action.path, action.newPath)) {
1146
+ return;
1147
+ }
1148
+ const record = records[action.path[0]];
1149
+ records.splice(action.path[0], 1);
1150
+ records.splice(action.newPath[0], 0, record);
1151
+ break;
1152
+ }
1153
+ case ActionName.MoveField: {
1154
+ if (isPathEqual(action.path, action.newPath)) {
1155
+ return;
1156
+ }
1157
+ const field = fields[action.path[0]];
1158
+ fields.splice(action.path[0], 1);
1159
+ fields.splice(action.newPath[0], 0, field);
1160
+ break;
1161
+ }
1162
+ case ActionName.RemoveField: {
1163
+ const [fieldId] = action.path;
1164
+ const fieldIndex = aiTable.fields().findIndex((item) => item._id === fieldId);
1165
+ if (fieldIndex > -1) {
1166
+ fields.splice(fieldIndex, 1);
1167
+ records.forEach((item) => {
1168
+ delete item.values[fieldId];
1169
+ });
1170
+ }
1171
+ break;
1172
+ }
1173
+ case ActionName.RemoveRecord: {
1174
+ const [recordId] = action.path;
1175
+ const recordIndex = aiTable.records().findIndex((item) => item._id === recordId);
1176
+ if (recordIndex > -1) {
1177
+ records.splice(recordIndex, 1);
1178
+ }
1179
+ break;
1180
+ }
1181
+ case ActionName.SetField: {
1182
+ const field = fields.find((item) => item._id === action.path[0]);
1183
+ if (field) {
1184
+ for (const key in action.newProperties) {
1185
+ const k = key;
1186
+ const value = action.newProperties[k];
1187
+ if (value == null) {
1188
+ delete field[k];
1189
+ }
1190
+ else {
1191
+ field[k] = value;
1192
+ }
1193
+ }
1194
+ // properties that were previously defined, but are now missing, must be deleted
1195
+ for (const key in action.properties) {
1196
+ if (!action.newProperties.hasOwnProperty(key)) {
1197
+ delete field[key];
1198
+ }
1199
+ }
1200
+ }
1201
+ break;
1202
+ }
1203
+ case ActionName.SetView: {
1204
+ const view = views.find((item) => item._id === action.path[0]);
1205
+ if (view) {
1206
+ for (const key in action.newProperties) {
1207
+ const k = key;
1208
+ const value = action.newProperties[k];
1209
+ if (value == null) {
1210
+ delete view[k];
1211
+ }
1212
+ else {
1213
+ view[k] = value;
1214
+ }
1215
+ }
1216
+ // properties that were previously defined, but are now missing, must be deleted
1217
+ for (const key in action.properties) {
1218
+ if (!action.newProperties.hasOwnProperty(key)) {
1219
+ delete view[key];
1220
+ }
1221
+ }
1222
+ }
1223
+ break;
1224
+ }
1225
+ case ActionName.AddView: {
1226
+ const [viewIndex] = action.path;
1227
+ if (viewIndex > -1) {
1228
+ views.splice(viewIndex, 0, action.view);
1229
+ }
1230
+ break;
1231
+ }
1232
+ case ActionName.RemoveView: {
1233
+ const [viewId] = action.path;
1234
+ const viewIndex = views.findIndex((item) => item._id === viewId);
1235
+ if (viewIndex > -1) {
1236
+ views.splice(viewIndex, 1);
1237
+ }
1238
+ break;
1239
+ }
1240
+ case ActionName.AddRecordPosition: {
1241
+ const { position, path } = action;
1242
+ const record = records.find((item) => item._id === path[0]);
1243
+ if (record) {
1244
+ record.positions = {
1245
+ ...record.positions,
1246
+ ...position
1247
+ };
1248
+ }
1249
+ break;
1250
+ }
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
+ }
1258
+ return {
1259
+ records,
1260
+ fields,
1261
+ views
1262
+ };
1263
+ };
1264
+ const GeneralActions = {
1265
+ transform(aiTable, action) {
1266
+ const records = createDraft(aiTable.records());
1267
+ const fields = createDraft(aiTable.fields());
1268
+ const views = createDraft(aiTable.views());
1269
+ apply(aiTable, records, fields, views, action);
1270
+ aiTable.fields.set(finishDraft(fields));
1271
+ aiTable.records.set(finishDraft(records));
1272
+ aiTable.views.set(finishDraft(views));
1273
+ }
1274
+ };
1275
+
1276
+ function setRecordPosition(aiTable, position, path) {
1277
+ const operation = {
1278
+ type: ActionName.AddRecordPosition,
1279
+ position,
1280
+ path
1281
+ };
1282
+ aiTable.apply(operation);
1283
+ }
1284
+ function removeRecordPosition(aiTable, path) {
1285
+ const operation = {
1286
+ type: ActionName.RemoveRecordPosition,
1287
+ path
1288
+ };
1289
+ aiTable.apply(operation);
1290
+ }
1291
+ const PositionActions = {
1292
+ setRecordPosition,
1293
+ removeRecordPosition
1294
+ };
1295
+
1296
+ function updateFieldValue$1(aiTable, value, path) {
1297
+ const field = AITableQueries.getFieldValue(aiTable, path);
1298
+ if (field !== value) {
1299
+ const operation = {
1300
+ type: ActionName.UpdateFieldValue,
1301
+ fieldValue: field,
1302
+ newFieldValue: value,
1303
+ path
1304
+ };
1305
+ aiTable.apply(operation);
1306
+ }
1307
+ }
1308
+ function addRecord(aiTable, record, path) {
1309
+ const operation = {
1310
+ type: ActionName.AddRecord,
1311
+ record,
1312
+ path
1313
+ };
1314
+ aiTable.apply(operation);
1315
+ }
1316
+ function moveRecord(aiTable, path, newPath) {
1317
+ const operation = {
1318
+ type: ActionName.MoveRecord,
1319
+ path,
1320
+ newPath
1321
+ };
1322
+ aiTable.apply(operation);
1323
+ }
1324
+ function removeRecord(aiTable, path) {
1325
+ const operation = {
1326
+ type: ActionName.RemoveRecord,
1327
+ path
1328
+ };
1329
+ aiTable.apply(operation);
1330
+ }
1331
+ const RecordActions = {
1332
+ addRecord,
1333
+ updateFieldValue: updateFieldValue$1,
1334
+ moveRecord,
1335
+ removeRecord
1336
+ };
1337
+
1338
+ function setView(aiTable, value, path) {
1339
+ const view = aiTable.views().find((item) => item._id === path[0]);
1340
+ if (view) {
1341
+ const properties = {};
1342
+ const newProperties = {};
1343
+ for (const key in value) {
1344
+ const k = key;
1345
+ if (JSON.stringify(view[k]) !== JSON.stringify(value[k])) {
1346
+ if (view.hasOwnProperty(k)) {
1347
+ properties[k] = view[k];
1348
+ }
1349
+ if (newProperties[k] !== null) {
1350
+ newProperties[k] = value[k];
1351
+ }
1352
+ }
1353
+ }
1354
+ const operation = {
1355
+ type: ActionName.SetView,
1356
+ properties,
1357
+ newProperties,
1358
+ path
1359
+ };
1360
+ aiTable.apply(operation);
1361
+ }
1362
+ }
1363
+ function addView$1(aiTable, view, path) {
1364
+ const operation = {
1365
+ type: ActionName.AddView,
1366
+ view,
1367
+ path
1368
+ };
1369
+ aiTable.apply(operation);
1370
+ }
1371
+ function removeView$1(aiTable, path) {
1372
+ const operation = {
1373
+ type: ActionName.RemoveView,
1374
+ path
1375
+ };
1376
+ aiTable.apply(operation);
1377
+ }
1378
+ const ViewActions = {
1379
+ setView,
1380
+ addView: addView$1,
1381
+ removeView: removeView$1
1382
+ };
1383
+
1384
+ const Actions = {
1385
+ ...GeneralActions,
1386
+ ...RecordActions,
1387
+ ...FieldActions,
1388
+ ...ViewActions,
1389
+ ...PositionActions
1390
+ };
1391
+
1392
+ function addFields(aiTable, options) {
1393
+ const { originId, direction = Direction$1.after, defaultValue, isDuplicate, count = 1 } = options;
1394
+ const activeView = aiTable.viewsMap()[aiTable.activeViewId()];
1395
+ const fields = getSortFields(aiTable, aiTable.fields(), activeView);
1396
+ let addIndex = fields.findIndex((item) => item._id === originId);
1397
+ if (direction === Direction$1.after) {
1398
+ addIndex++;
1399
+ }
1400
+ const newRecordIds = getNewIdsByCount(count);
1401
+ newRecordIds.forEach((id, index) => {
1402
+ const newField = { _id: id, ...defaultValue };
1403
+ Actions.addField(aiTable, newField, [addIndex + index]);
1404
+ });
1405
+ }
1406
+
1407
+ function createDefaultPositions(views, activeId, data, index) {
1408
+ const positions = {};
1409
+ const position = getPosition(data, activeId, index);
1410
+ const maxPosition = data[data.length - 1].positions[activeId];
1411
+ views.forEach((element) => {
1412
+ positions[element._id] = element._id === activeId ? position : maxPosition + 1;
1413
+ });
1414
+ return positions;
1415
+ }
1416
+ function getPosition(data, activeViewId, index) {
1417
+ let position = data.length - 1;
1418
+ if (index !== 0 && index !== data.length) {
1419
+ const previousViewPosition = data[index - 1].positions[activeViewId];
1420
+ const nextViewPosition = data[index].positions[activeViewId];
1421
+ position = (previousViewPosition + nextViewPosition) / 2;
1422
+ }
1423
+ else {
1424
+ position = index;
1425
+ }
1426
+ return position;
1427
+ }
1428
+ function addView(aiTable, type) {
1429
+ let index = aiTable.views().length;
1430
+ const newId = idCreator();
1431
+ let newView = {
1432
+ _id: newId,
1433
+ name: '表格视图 ' + index
1434
+ };
1435
+ let originViewId = aiTable.views()[aiTable.views().length - 1]._id;
1436
+ if (type === 'copy') {
1437
+ originViewId = aiTable.activeViewId();
1438
+ const copyView = aiTable.views().find((item) => item._id === aiTable.activeViewId());
1439
+ newView = {
1440
+ ...copyView,
1441
+ _id: newId,
1442
+ name: copyView.name + '-副本'
1443
+ };
1444
+ index = aiTable.views().indexOf(copyView) + 1;
1445
+ }
1446
+ ViewActions.addView(aiTable, newView, [index]);
1447
+ aiTable.records().forEach((record) => {
1448
+ PositionActions.setRecordPosition(aiTable, { [newId]: record.positions[originViewId] }, [record._id]);
1449
+ });
1450
+ aiTable.fields().forEach((field) => {
1451
+ Actions.setField(aiTable, {
1452
+ positions: {
1453
+ ...field.positions,
1454
+ [newId]: field.positions[originViewId]
1455
+ }
1456
+ }, [field._id]);
1457
+ });
1458
+ return newView;
1459
+ }
1460
+ function removeView(aiTable, records, fields, activeViewId) {
1461
+ records.forEach((record) => {
1462
+ PositionActions.removeRecordPosition(aiTable, [activeViewId, record._id]);
1463
+ });
1464
+ fields.forEach((field) => {
1465
+ const positions = { ...field.positions };
1466
+ delete positions[activeViewId];
1467
+ Actions.setField(aiTable, {
1468
+ positions
1469
+ }, [field._id]);
1470
+ });
1471
+ ViewActions.removeView(aiTable, [activeViewId]);
1472
+ }
1473
+
1474
+ function addRecords(aiTable, options) {
1475
+ const { originId, direction = Direction$1.after, isDuplicate, count = 1 } = options;
1476
+ const activeView = aiTable.viewsMap()[aiTable.activeViewId()];
1477
+ const records = getSortRecords(aiTable, aiTable.records(), activeView);
1478
+ let addIndex = records.findIndex((item) => item._id === originId);
1479
+ if (direction === Direction$1.after) {
1480
+ addIndex++;
1481
+ }
1482
+ const newRecordIds = getNewIdsByCount(count);
1483
+ const newRecordValues = getDefaultRecordValues(aiTable, isDuplicate, originId);
1484
+ // TODO: 判断如果存在筛选条件,且 newRecordValues 中没有一项满足筛选条件
1485
+ // 把 id 添加到 RECORDS_WILL_HIDDEN 中
1486
+ if (activeView.settings?.conditions?.length) {
1487
+ aiTable.recordsWillHidden?.update((value) => {
1488
+ value.push(...newRecordIds);
1489
+ return [...value];
1490
+ });
1491
+ }
1492
+ newRecordIds.forEach((id, index) => {
1493
+ const newRecord = { _id: id, values: newRecordValues };
1494
+ Actions.addRecord(aiTable, newRecord, [addIndex + index]);
1495
+ });
1496
+ }
1497
+ function getDefaultRecordValues(aiTable, isDuplicate = false, recordId) {
1498
+ let newRecordValues = {};
1499
+ if (isDuplicate && recordId) {
1500
+ newRecordValues = aiTable.recordsMap()[recordId].values;
1501
+ }
1502
+ else {
1503
+ const activeView = aiTable.viewsMap()[aiTable.activeViewId()];
1504
+ const fields = getSortFields(aiTable, aiTable.fields(), activeView);
1505
+ fields.map((item) => {
1506
+ newRecordValues[item._id] = getDefaultFieldValue(item);
1507
+ });
1508
+ const { conditions, condition_logical } = activeView.settings || {};
1509
+ if (conditions && conditions.length) {
1510
+ newRecordValues = getDefaultRecordDataByFilter(newRecordValues, conditions, condition_logical);
1511
+ }
1512
+ }
1513
+ return newRecordValues;
1514
+ }
1515
+
1516
+ function updateFieldValue(aiTable, options) {
1517
+ Actions.updateFieldValue(aiTable, options.value, options.path);
1518
+ }
1519
+
1520
+ const FLUSHING = new WeakMap();
1521
+
1522
+ const withState = (aiTable) => {
1523
+ const viewTable = aiTable;
1524
+ viewTable.actions = [];
1525
+ viewTable.apply = (action) => {
1526
+ viewTable.actions.push(action);
1527
+ Actions.transform(viewTable, action);
1528
+ if (!FLUSHING.get(viewTable)) {
1529
+ FLUSHING.set(viewTable, true);
1530
+ Promise.resolve().then(() => {
1531
+ FLUSHING.set(viewTable, false);
1532
+ viewTable.onChange();
1533
+ viewTable.actions = [];
1534
+ });
1535
+ }
1536
+ };
1537
+ return aiTable;
1538
+ };
1539
+
1540
+ const DividerMenuItem = {
1541
+ type: 'divider'
1542
+ };
1543
+ const EditFieldPropertyItem = {
1544
+ type: 'editFieldProperty',
1545
+ name: '编辑列',
1546
+ icon: 'edit',
1547
+ exec: (aiTable, field, origin, position) => {
1548
+ const fieldService = AI_TABLE_GRID_FIELD_SERVICE_MAP.get(aiTable);
1549
+ const copyField = _.cloneDeep(field());
1550
+ if (origin && position) {
1551
+ const popoverRef = fieldService?.editFieldProperty(aiTable, {
1552
+ field: copyField,
1553
+ isUpdate: true,
1554
+ origin: origin,
1555
+ position
1556
+ });
1557
+ if (popoverRef && fieldService && !fieldService.aiFieldConfig?.fieldPropertyEditor) {
1558
+ popoverRef.componentInstance.setField.subscribe((value) => {
1559
+ Actions.setField(aiTable, value.field, value.path);
1560
+ });
1561
+ }
1562
+ return popoverRef;
1563
+ }
1564
+ return undefined;
1565
+ }
1566
+ };
1567
+ const RemoveFieldItem = {
1568
+ type: 'removeField',
1569
+ name: '删除列',
1570
+ icon: 'trash',
1571
+ exec: (aiTable, field) => {
1572
+ Actions.removeField(aiTable, [field()._id]);
1573
+ }
1574
+ };
1575
+
1576
+ const VIEW_ACTIONS = [ActionName.SetView, ActionName.AddView, ActionName.RemoveView];
1160
1577
 
1161
1578
  /**
1162
1579
  * Generated bundle index. Do not edit.
1163
1580
  */
1164
1581
 
1165
- export { AITableFilterLogical, AITableFilterOperation, Direction, GeneralActions, GeneralPositionActions, GeneralViewActions, POSITION_ACTIONS, PositionActionName, PositionActions, Positions, VIEW_ACTIONS, ViewActionName, ViewActions, ViewOperationMap, YjsAITable, actionMappers, addRecordPosition, addView$1 as addView, apply, applyActionOps, applyEvents, applyPosition, applyView, applyYjsEvents, buildFieldsByView, buildRecordsByView, createDefaultPositions, createSharedType, doFilter, getFilteredRecords, getPosition, getShareTypeNumberPath, getSharedMapValueId, getSharedMapValueIndex, getSharedRecordId, getSharedRecordIndex, initSharedType, initTable, isEmpty, removeRecordPosition, removeView$1 as removeView, sortByView, toRecordSyncElement, toSharedType, toSyncElement, translatePositionToPath, translateToRecordValues, translateToRecords, translateYjsEvent, withView };
1582
+ export { AITableFilterLogical, AITableFilterOperation, ActionName, Actions, Direction, DividerMenuItem, EditFieldPropertyItem, ExecuteType, FLUSHING, Positions, RemoveFieldItem, VIEW_ACTIONS, ViewOperationMap, YjsAITable, actionMappers, addFields, addRecords, addView, applyActionOps, applyEvents, applyYjsEvents, buildFieldsByView, buildRecordsByView, createDefaultPositions, createSharedType, doFilter, getDefaultRecordDataByFilter, getDefaultRecordValues, getFilteredRecords, getNewIdsByCount, getPosition, getShareTypeNumberPath, getSharedMapValueId, getSharedMapValueIndex, getSharedRecordId, getSharedRecordIndex, getSortFields, getSortRecords, initSharedType, initTable, isEmpty, isPathEqual, removeView, sortByViewPosition, sortRecordsBySortInfo, toRecordSyncElement, toSharedType, toSyncElement, translatePositionToPath, translateToRecordValues, translateToRecords, translateYjsEvent, updateFieldValue, withState };
1166
1583
  //# sourceMappingURL=ai-table-state.mjs.map