@ai-table/state 0.0.12 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) 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 +7 -7
  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 -11
  60. package/esm2022/utils/weak-map.mjs +2 -0
  61. package/fesm2022/ai-table-state.mjs +710 -437
  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 +1 -47
  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/{date.d.ts → model/date.d.ts} +1 -1
  100. package/utils/field/model/date.d.ts.map +1 -0
  101. package/utils/field/{field.d.ts → model/field.d.ts} +1 -1
  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/{number.d.ts → model/number.d.ts} +1 -1
  105. package/utils/field/model/number.d.ts.map +1 -0
  106. package/utils/field/{rate.d.ts → model/rate.d.ts} +1 -1
  107. package/utils/field/model/rate.d.ts.map +1 -0
  108. package/utils/field/{select.d.ts → model/select.d.ts} +1 -1
  109. package/utils/field/model/select.d.ts.map +1 -0
  110. package/utils/field/{text.d.ts → model/text.d.ts} +1 -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 +1 -2
  125. package/utils/view.d.ts.map +1 -1
  126. package/utils/weak-map.d.ts +3 -0
  127. package/utils/weak-map.d.ts.map +1 -0
  128. package/esm2022/utils/field/date.mjs +0 -74
  129. package/esm2022/utils/field/field.mjs +0 -63
  130. package/esm2022/utils/field/index.mjs +0 -22
  131. package/esm2022/utils/field/number.mjs +0 -46
  132. package/esm2022/utils/field/rate.mjs +0 -38
  133. package/esm2022/utils/field/select.mjs +0 -55
  134. package/esm2022/utils/field/text.mjs +0 -27
  135. package/esm2022/utils/filter-records.mjs +0 -52
  136. package/esm2022/utils/sort-records.mjs +0 -34
  137. package/esm2022/view/action/general.mjs +0 -137
  138. package/esm2022/view/action/index.mjs +0 -4
  139. package/esm2022/view/action/position.mjs +0 -21
  140. package/esm2022/view/action/view.mjs +0 -47
  141. package/esm2022/view/constants/view.mjs +0 -4
  142. package/esm2022/view/plugins/view.plugin.mjs +0 -34
  143. package/utils/field/date.d.ts.map +0 -1
  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.map +0 -1
  147. package/utils/field/rate.d.ts.map +0 -1
  148. package/utils/field/select.d.ts.map +0 -1
  149. package/utils/field/text.d.ts.map +0 -1
  150. package/utils/filter-records.d.ts +0 -5
  151. package/utils/filter-records.d.ts.map +0 -1
  152. package/utils/sort-records.d.ts +0 -5
  153. package/utils/sort-records.d.ts.map +0 -1
  154. package/view/action/general.d.ts +0 -14
  155. package/view/action/general.d.ts.map +0 -1
  156. package/view/action/index.d.ts +0 -4
  157. package/view/action/index.d.ts.map +0 -1
  158. package/view/action/position.d.ts +0 -8
  159. package/view/action/position.d.ts.map +0 -1
  160. package/view/action/view.d.ts.map +0 -1
  161. package/view/constants/view.d.ts +0 -4
  162. package/view/constants/view.d.ts.map +0 -1
  163. package/view/plugins/view.plugin.d.ts +0 -3
  164. package/view/plugins/view.plugin.d.ts.map +0 -1
  165. /package/utils/field/{index.d.ts → model/index.d.ts} +0 -0
@@ -1,9 +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';
5
- import { isEqual } from 'lodash';
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';
6
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
+ };
7
46
 
8
47
  class Positions {
9
48
  }
@@ -35,17 +74,29 @@ var AITableFilterOperation;
35
74
  AITableFilterOperation["exists"] = "exists";
36
75
  AITableFilterOperation["notContain"] = "not_contain";
37
76
  })(AITableFilterOperation || (AITableFilterOperation = {}));
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
- var PositionActionName;
45
- (function (PositionActionName) {
46
- PositionActionName["AddRecordPosition"] = "add_record_position";
47
- PositionActionName["RemoveRecordPosition"] = "remove_record_position";
48
- })(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 = {}));
49
100
 
50
101
  const createSharedType = () => {
51
102
  const doc = new Y.Doc();
@@ -186,7 +237,7 @@ function translateArrayEvent(aiTable, sharedType, event) {
186
237
  const removeView = aiTable.views()[offset];
187
238
  if (removeView) {
188
239
  actions.push({
189
- type: ViewActionName.RemoveView,
240
+ type: ActionName.RemoveView,
190
241
  path: [removeView._id]
191
242
  });
192
243
  }
@@ -236,7 +287,7 @@ function translateArrayEvent(aiTable, sharedType, event) {
236
287
  for (const key in item) {
237
288
  if (!record.positions[key] && record.positions[key] !== 0) {
238
289
  actions.push({
239
- type: PositionActionName.AddRecordPosition,
290
+ type: ActionName.AddRecordPosition,
240
291
  path: [record._id],
241
292
  position: {
242
293
  [key]: item[key]
@@ -247,7 +298,7 @@ function translateArrayEvent(aiTable, sharedType, event) {
247
298
  for (const key in record.positions) {
248
299
  if (!item[key] && item[key] !== 0) {
249
300
  actions.push({
250
- type: PositionActionName.RemoveRecordPosition,
301
+ type: ActionName.RemoveRecordPosition,
251
302
  path: [key, record._id]
252
303
  });
253
304
  }
@@ -288,7 +339,7 @@ function translateArrayEvent(aiTable, sharedType, event) {
288
339
  delta.insert?.map((item, index) => {
289
340
  const data = item.toJSON();
290
341
  actions.push({
291
- type: ViewActionName.AddView,
342
+ type: ActionName.AddView,
292
343
  path: [offset + index],
293
344
  view: data
294
345
  });
@@ -360,7 +411,7 @@ function translateMapEvent(aiTable, sharedType, event) {
360
411
  const k = key;
361
412
  properties[k] = value;
362
413
  }
363
- const type = isFieldsTranslate ? ActionName.SetField : ViewActionName.SetView;
414
+ const type = isFieldsTranslate ? ActionName.SetField : ActionName.SetView;
364
415
  return [
365
416
  {
366
417
  type,
@@ -374,45 +425,6 @@ function translateMapEvent(aiTable, sharedType, event) {
374
425
  return [];
375
426
  }
376
427
 
377
- const IS_LOCAL = new WeakSet();
378
- const IS_REMOTE = new WeakSet();
379
- const IS_UNDO = new WeakSet();
380
- const YjsAITable = {
381
- isLocal: (aiTable) => {
382
- return IS_LOCAL.has(aiTable);
383
- },
384
- asLocal: (aiTable, fn) => {
385
- const wasLocal = YjsAITable.isLocal(aiTable);
386
- IS_LOCAL.add(aiTable);
387
- fn();
388
- if (!wasLocal) {
389
- IS_LOCAL.delete(aiTable);
390
- }
391
- },
392
- isRemote: (aiTable) => {
393
- return IS_REMOTE.has(aiTable);
394
- },
395
- asRemote: (aiTable, fn) => {
396
- const wasRemote = YjsAITable.isRemote(aiTable);
397
- IS_REMOTE.add(aiTable);
398
- fn();
399
- if (!wasRemote) {
400
- Promise.resolve().then(() => IS_REMOTE.delete(aiTable));
401
- }
402
- },
403
- isUndo: (aiTable) => {
404
- return IS_UNDO.has(aiTable);
405
- },
406
- asUndo: (aiTable, fn) => {
407
- const wasUndo = YjsAITable.isUndo(aiTable);
408
- IS_UNDO.add(aiTable);
409
- fn();
410
- if (!wasUndo) {
411
- Promise.resolve().then(() => IS_UNDO.delete(aiTable));
412
- }
413
- }
414
- };
415
-
416
428
  function translateYjsEvent(aiTable, sharedType, event) {
417
429
  if (event instanceof Y.YArrayEvent) {
418
430
  return translateArrayEvent(aiTable, sharedType, event);
@@ -438,7 +450,7 @@ function applyYjsEvents(aiTable, sharedType, events) {
438
450
  }
439
451
  }
440
452
 
441
- function updateFieldValue(sharedType, action) {
453
+ function updateFieldValue$2(sharedType, action) {
442
454
  const sharedRecords = sharedType.get('records');
443
455
  const sharedFields = sharedType.get('fields');
444
456
  if (sharedRecords && sharedFields) {
@@ -459,7 +471,7 @@ function setNode(sharedType, action) {
459
471
  if (action.type === ActionName.SetField) {
460
472
  sharedNodes = sharedType.get('fields');
461
473
  }
462
- if (action.type === ViewActionName.SetView) {
474
+ if (action.type === ActionName.SetView) {
463
475
  sharedNodes = sharedType.get('views');
464
476
  }
465
477
  if (sharedNodes) {
@@ -492,10 +504,10 @@ function addNode(sharedType, action) {
492
504
  case ActionName.AddRecord:
493
505
  records && records.push([toRecordSyncElement(action.record)]);
494
506
  break;
495
- case ViewActionName.AddView:
507
+ case ActionName.AddView:
496
508
  views && views.push([toSyncElement(action.view)]);
497
509
  break;
498
- case PositionActionName.AddRecordPosition:
510
+ case ActionName.AddRecordPosition:
499
511
  if (records) {
500
512
  const recordIndex = getSharedRecordIndex(records, action.path[0]);
501
513
  const record = records.get(recordIndex);
@@ -535,7 +547,7 @@ function removeNode(sharedType, action) {
535
547
  }
536
548
  }
537
549
  break;
538
- case ViewActionName.RemoveView:
550
+ case ActionName.RemoveView:
539
551
  if (views) {
540
552
  const viewIndex = getSharedMapValueIndex(views, action.path[0]);
541
553
  if (viewIndex > -1) {
@@ -543,7 +555,7 @@ function removeNode(sharedType, action) {
543
555
  }
544
556
  }
545
557
  break;
546
- case PositionActionName.RemoveRecordPosition:
558
+ case ActionName.RemoveRecordPosition:
547
559
  if (records) {
548
560
  const recordIndex = getSharedRecordIndex(records, action.path[1]);
549
561
  const record = records.get(recordIndex);
@@ -572,7 +584,7 @@ function removeNode(sharedType, action) {
572
584
 
573
585
  const actionMappers = {
574
586
  add_record: addNode,
575
- update_field_value: updateFieldValue,
587
+ update_field_value: updateFieldValue$2,
576
588
  remove_record: removeNode,
577
589
  add_field: addNode,
578
590
  set_field: setNode,
@@ -598,71 +610,26 @@ function applyActionOps(sharedType, actions, aiTable) {
598
610
  return sharedType;
599
611
  }
600
612
 
601
- function createDefaultPositions(views, activeId, data, index) {
602
- const positions = {};
603
- const position = getPosition(data, activeId, index);
604
- const maxPosition = data[data.length - 1].positions[activeId];
605
- views.forEach((element) => {
606
- positions[element._id] = element._id === activeId ? position : maxPosition + 1;
607
- });
608
- return positions;
609
- }
610
- function getPosition(data, activeViewId, index) {
611
- let position = data.length - 1;
612
- if (index !== 0 && index !== data.length) {
613
- const previousViewPosition = data[index - 1].positions[activeViewId];
614
- const nextViewPosition = data[index].positions[activeViewId];
615
- position = (previousViewPosition + nextViewPosition) / 2;
613
+ function isEmpty(value) {
614
+ if (isArray(value)) {
615
+ return isEmpty$1(value);
616
616
  }
617
- else {
618
- position = index;
617
+ if (isObject(value)) {
618
+ return Reflect.ownKeys(value).length === 0;
619
619
  }
620
- return position;
620
+ return isUndefinedOrNull(value) || value == '';
621
621
  }
622
- function addView$1(aiTable, type) {
623
- let index = aiTable.views().length;
624
- const newId = idCreator();
625
- let newView = {
626
- _id: newId,
627
- name: '表格视图 ' + index
628
- };
629
- let originViewId = aiTable.views()[aiTable.views().length - 1]._id;
630
- if (type === 'copy') {
631
- originViewId = aiTable.activeViewId();
632
- const copyView = aiTable.views().find((item) => item._id === aiTable.activeViewId());
633
- newView = {
634
- ...copyView,
635
- _id: newId,
636
- name: copyView.name + '-副本'
637
- };
638
- index = aiTable.views().indexOf(copyView) + 1;
639
- }
640
- ViewActions.addView(aiTable, newView, [index]);
641
- aiTable.records().forEach((record) => {
642
- PositionActions.addRecordPosition(aiTable, { [newId]: record.positions[originViewId] }, [record._id]);
643
- });
644
- aiTable.fields().forEach((field) => {
645
- Actions.setField(aiTable, {
646
- positions: {
647
- ...field.positions,
648
- [newId]: field.positions[originViewId]
649
- }
650
- }, [field._id]);
651
- });
652
- return newView;
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());
628
+ }
629
+ return newIds;
653
630
  }
654
- function removeView$1(aiTable, records, fields, activeViewId) {
655
- records.forEach((record) => {
656
- PositionActions.removeRecordPosition(aiTable, [activeViewId, record._id]);
657
- });
658
- fields.forEach((field) => {
659
- const positions = { ...field.positions };
660
- delete positions[activeViewId];
661
- Actions.setField(aiTable, {
662
- positions
663
- }, [field._id]);
664
- });
665
- ViewActions.removeView(aiTable, [activeViewId]);
631
+ function isPathEqual(path, another) {
632
+ return path.length === another.length && path.every((n, i) => n === another[i]);
666
633
  }
667
634
  function sortByViewPosition(data, activeView) {
668
635
  const hasPositions = data.every((item) => item.positions && item.positions);
@@ -672,295 +639,52 @@ function sortByViewPosition(data, activeView) {
672
639
  return data;
673
640
  }
674
641
 
675
- const GeneralViewActions = {
676
- transform(aiTable, action) {
677
- const views = createDraft(aiTable.views());
678
- applyView(aiTable, views, action);
679
- 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());
680
646
  }
681
- };
682
- const applyView = (aiTable, views, action) => {
683
- switch (action.type) {
684
- case ViewActionName.SetView: {
685
- const view = views.find((item) => item._id === action.path[0]);
686
- if (view) {
687
- for (const key in action.newProperties) {
688
- const k = key;
689
- const value = action.newProperties[k];
690
- if (value == null) {
691
- delete view[k];
692
- }
693
- else {
694
- view[k] = value;
695
- }
696
- }
697
- // properties that were previously defined, but are now missing, must be deleted
698
- for (const key in action.properties) {
699
- if (!action.newProperties.hasOwnProperty(key)) {
700
- delete view[key];
701
- }
702
- }
647
+ isMeetFilter(condition, cellValue) {
648
+ switch (condition.operation) {
649
+ case AITableFilterOperation.empty:
650
+ case AITableFilterOperation.exists: {
651
+ return this.isEmptyOrNot(condition.operation, cellValue);
703
652
  }
704
- break;
705
- }
706
- case ViewActionName.AddView: {
707
- const [viewIndex] = action.path;
708
- if (viewIndex > -1) {
709
- views.splice(viewIndex, 0, action.view);
653
+ default: {
654
+ return true;
710
655
  }
711
- break;
712
656
  }
713
- case ViewActionName.RemoveView: {
714
- const [viewId] = action.path;
715
- const viewIndex = views.findIndex((item) => item._id === viewId);
716
- if (viewIndex > -1) {
717
- 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');
718
668
  }
719
- break;
720
669
  }
721
670
  }
722
- };
723
- const GeneralActions = {
724
- transform(aiTable, action) {
725
- const records = createDraft(aiTable.records());
726
- const fields = createDraft(aiTable.fields());
727
- apply(aiTable, records, fields, action);
728
- if (action.type === ActionName.AddRecord) {
729
- 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;
730
677
  }
731
- if (action.type === ActionName.AddField) {
732
- aiTable.fields.set(finishDraft(fields));
678
+ if (cellValue1 == null) {
679
+ return -1;
733
680
  }
734
- }
735
- };
736
- const apply = (aiTable, records, fields, action) => {
737
- switch (action.type) {
738
- case ActionName.AddRecord: {
739
- const [recordIndex] = action.path;
740
- if (recordIndex > -1) {
741
- if (!action.record.positions) {
742
- const activeView = aiTable.views().find((item) => item._id === aiTable.activeViewId());
743
- let index = recordIndex;
744
- if (activeView?.settings?.conditions) {
745
- index = records.length;
746
- }
747
- action.record.positions = createDefaultPositions(aiTable.views(), aiTable.activeViewId(), aiTable.records(), index);
748
- }
749
- records.splice(recordIndex, 0, action.record);
750
- }
751
- break;
681
+ if (cellValue2 == null) {
682
+ return 1;
752
683
  }
753
- case ActionName.AddField: {
754
- const [fieldIndex] = action.path;
755
- if (fieldIndex > -1) {
756
- const newField = action.field;
757
- newField.positions = createDefaultPositions(aiTable.views(), aiTable.activeViewId(), aiTable.fields(), action.path[0]);
758
- fields.splice(fieldIndex, 0, newField);
759
- const newRecord = {
760
- [newField._id]: getDefaultFieldValue(action.field)
761
- };
762
- records.forEach((item) => {
763
- item.values = {
764
- ...item.values,
765
- ...newRecord
766
- };
767
- });
768
- }
769
- break;
770
- }
771
- }
772
- };
773
- const GeneralPositionActions = {
774
- transform(aiTable, action) {
775
- const records = createDraft(aiTable.records());
776
- const fields = createDraft(aiTable.fields());
777
- applyPosition(aiTable, records, action);
778
- aiTable.records.update(() => {
779
- return finishDraft(records);
780
- });
781
- aiTable.fields.update(() => {
782
- return finishDraft(fields);
783
- });
784
- }
785
- };
786
- const applyPosition = (aiTable, records, action) => {
787
- switch (action.type) {
788
- case PositionActionName.AddRecordPosition: {
789
- const { position, path } = action;
790
- const record = records.find((item) => item._id === path[0]);
791
- if (record) {
792
- record.positions = {
793
- ...record.positions,
794
- ...position
795
- };
796
- }
797
- break;
798
- }
799
- case PositionActionName.RemoveRecordPosition: {
800
- const { path } = action;
801
- const record = records.find((item) => item._id === path[1]);
802
- delete record?.positions[path[0]];
803
- break;
804
- }
805
- }
806
- };
807
-
808
- function setView(aiTable, value, path) {
809
- const view = aiTable.views().find((item) => item._id === path[0]);
810
- if (view) {
811
- const properties = {};
812
- const newProperties = {};
813
- for (const key in value) {
814
- const k = key;
815
- if (JSON.stringify(view[k]) !== JSON.stringify(value[k])) {
816
- if (view.hasOwnProperty(k)) {
817
- properties[k] = view[k];
818
- }
819
- if (newProperties[k] !== null) {
820
- newProperties[k] = value[k];
821
- }
822
- }
823
- }
824
- const operation = {
825
- type: ViewActionName.SetView,
826
- properties,
827
- newProperties,
828
- path
829
- };
830
- aiTable.apply(operation);
831
- }
832
- }
833
- function addView(aiTable, view, path) {
834
- const operation = {
835
- type: ViewActionName.AddView,
836
- view,
837
- path
838
- };
839
- aiTable.apply(operation);
840
- }
841
- function removeView(aiTable, path) {
842
- const operation = {
843
- type: ViewActionName.RemoveView,
844
- path
845
- };
846
- aiTable.apply(operation);
847
- }
848
- const ViewActions = {
849
- setView,
850
- addView,
851
- removeView
852
- };
853
-
854
- function addRecordPosition(aiTable, position, path) {
855
- const operation = {
856
- type: PositionActionName.AddRecordPosition,
857
- position,
858
- path
859
- };
860
- aiTable.apply(operation);
861
- }
862
- function removeRecordPosition(aiTable, path) {
863
- const operation = {
864
- type: PositionActionName.RemoveRecordPosition,
865
- path
866
- };
867
- aiTable.apply(operation);
868
- }
869
- const PositionActions = {
870
- addRecordPosition,
871
- removeRecordPosition
872
- };
873
-
874
- const VIEW_ACTIONS = [ViewActionName.SetView, ViewActionName.AddView, ViewActionName.RemoveView];
875
- const POSITION_ACTIONS = [PositionActionName.AddRecordPosition, PositionActionName.RemoveRecordPosition];
876
-
877
- const withView = (aiTable) => {
878
- const viewTable = aiTable;
879
- viewTable.apply = (action) => {
880
- const sharedActions = viewTable.actions;
881
- sharedActions.push(action);
882
- if (VIEW_ACTIONS.includes(action.type)) {
883
- GeneralViewActions.transform(viewTable, action);
884
- }
885
- else if (POSITION_ACTIONS.includes(action.type)) {
886
- GeneralPositionActions.transform(viewTable, action);
887
- }
888
- else {
889
- if (action.type === ActionName.AddField || action.type === ActionName.AddRecord) {
890
- GeneralActions.transform(viewTable, action);
891
- }
892
- else {
893
- Actions.transform(aiTable, action);
894
- }
895
- }
896
- if (!FLUSHING.get(aiTable)) {
897
- FLUSHING.set(aiTable, true);
898
- Promise.resolve().then(() => {
899
- FLUSHING.set(aiTable, false);
900
- aiTable.onChange();
901
- aiTable.actions = [];
902
- });
903
- }
904
- };
905
- return aiTable;
906
- };
907
-
908
- function isEmpty(value) {
909
- if (isArray(value)) {
910
- return isEmpty$1(value);
911
- }
912
- if (isObject(value)) {
913
- return Reflect.ownKeys(value).length === 0;
914
- }
915
- return isUndefinedOrNull(value) || value == '';
916
- }
917
-
918
- const zhIntlCollator = typeof Intl !== 'undefined' ? new Intl.Collator('zh-CN') : undefined;
919
- class Field {
920
- stringInclude(str, searchStr) {
921
- return str.toLowerCase().includes(searchStr.trim().toLowerCase());
922
- }
923
- isMeetFilter(condition, cellValue) {
924
- switch (condition.operation) {
925
- case AITableFilterOperation.empty:
926
- case AITableFilterOperation.exists: {
927
- return this.isEmptyOrNot(condition.operation, cellValue);
928
- }
929
- default: {
930
- return true;
931
- }
932
- }
933
- }
934
- isEmptyOrNot(operation, cellValue) {
935
- switch (operation) {
936
- case AITableFilterOperation.empty: {
937
- return isEmpty(cellValue);
938
- }
939
- case AITableFilterOperation.exists: {
940
- return !isEmpty(cellValue);
941
- }
942
- default: {
943
- throw new Error('compare operator type error');
944
- }
945
- }
946
- }
947
- eq(cv1, cv2) {
948
- return isEqual(cv1, cv2);
949
- }
950
- compare(cellValue1, cellValue2, field) {
951
- if (this.eq(cellValue1, cellValue2)) {
952
- return 0;
953
- }
954
- if (cellValue1 == null) {
955
- return -1;
956
- }
957
- if (cellValue2 == null) {
958
- return 1;
959
- }
960
- let str1 = this.cellValueToString(cellValue1, field);
961
- let str2 = this.cellValueToString(cellValue2, field);
962
- if (str1 === str2) {
963
- return 0;
684
+ let str1 = this.cellValueToString(cellValue1, field);
685
+ let str2 = this.cellValueToString(cellValue2, field);
686
+ if (str1 === str2) {
687
+ return 0;
964
688
  }
965
689
  if (str1 == null) {
966
690
  return -1;
@@ -1214,7 +938,38 @@ const ViewOperationMap = {
1214
938
  [AITableFieldType.updatedBy]: new SelectField()
1215
939
  };
1216
940
 
1217
- 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) {
1218
973
  const { conditions, condition_logical } = activeView.settings || {};
1219
974
  if (!conditions) {
1220
975
  return records;
@@ -1223,7 +978,11 @@ function getFilteredRecords(records, fields, activeView) {
1223
978
  if (!illegalConditions.length) {
1224
979
  return records;
1225
980
  }
981
+ const recordsWillHidden = aiTable.recordsWillHidden();
1226
982
  return records.filter((record) => {
983
+ if (recordsWillHidden && recordsWillHidden.length && recordsWillHidden.includes(record._id)) {
984
+ return true;
985
+ }
1227
986
  return checkConditions(fields, record, { conditions: illegalConditions, condition_logical });
1228
987
  });
1229
988
  }
@@ -1262,49 +1021,563 @@ function doFilter(condition, field, cellValue) {
1262
1021
  }
1263
1022
  return ViewOperationMap[field.type].isMeetFilter(condition, cellValue);
1264
1023
  }
1265
-
1266
- function getSortRecords(aiTable, records, activeView) {
1267
- if (!activeView?.settings || !activeView.settings.sorts?.length) {
1268
- return records;
1024
+ function getDefaultRecordDataByFilter(recordValues, conditions, conditionLogical) {
1025
+ if (conditions.length === 1) {
1026
+ // recordValues[conditions[0].field_id] = conditions[0].value;
1269
1027
  }
1270
- const { is_keep_sort, sorts } = activeView.settings;
1271
- if (is_keep_sort && sorts?.length) {
1272
- return sortRecordsBySortInfo(aiTable, records, activeView);
1028
+ else {
1029
+ //...
1273
1030
  }
1274
- return sortByViewPosition(records, activeView);
1031
+ return recordValues;
1275
1032
  }
1276
- function sortRecordsBySortInfo(aiTable, records, activeView) {
1277
- const shallowRows = [...records];
1278
- if (activeView.settings?.sorts?.length) {
1279
- shallowRows.sort((prev, current) => {
1280
- return activeView.settings.sorts.reduce((acc, rule) => {
1281
- const field = aiTable.fieldsMap()[rule.sort_by];
1282
- if (!field || acc !== 0) {
1283
- return acc;
1284
- }
1285
- const fieldMethod = ViewOperationMap[field.type];
1286
- const cellValue1 = AITableQueries.getFieldValue(aiTable, [prev._id, field._id]);
1287
- const cellValue2 = AITableQueries.getFieldValue(aiTable, [current._id, field._id]);
1288
- const res = fieldMethod.compare(cellValue1, cellValue2, field);
1289
- return res * rule.direction;
1290
- }, 0);
1291
- });
1292
- return shallowRows;
1293
- }
1294
- return shallowRows;
1033
+
1034
+ function getSortFields(aiTable, fields, activeView) {
1035
+ return sortByViewPosition(fields, activeView);
1295
1036
  }
1296
1037
 
1297
1038
  function buildRecordsByView(aiTable, records, fields, activeView) {
1298
- const filteredRecords = getFilteredRecords(records, fields, activeView);
1039
+ const filteredRecords = getFilteredRecords(aiTable, records, fields, activeView);
1299
1040
  return getSortRecords(aiTable, filteredRecords, activeView);
1300
1041
  }
1301
- function buildFieldsByView(fields, activeView) {
1302
- return sortByViewPosition(fields, activeView);
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);
1061
+ }
1062
+ function removeField(aiTable, path) {
1063
+ const operation = {
1064
+ type: ActionName.RemoveField,
1065
+ path
1066
+ };
1067
+ aiTable.apply(operation);
1068
+ }
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
+ }
1303
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];
1304
1577
 
1305
1578
  /**
1306
1579
  * Generated bundle index. Do not edit.
1307
1580
  */
1308
1581
 
1309
- 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, sortByViewPosition, 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 };
1310
1583
  //# sourceMappingURL=ai-table-state.mjs.map