@ai-table/state 0.0.10 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,7 @@ import * as Y from 'yjs';
2
2
  import { ActionName, AITableQueries, getDefaultFieldValue, idCreator, Actions, FLUSHING, AITableFieldType } from '@ai-table/grid';
3
3
  import { isArray, isEmpty as isEmpty$1, isObject, isUndefinedOrNull, TinyDate } from 'ngx-tethys/util';
4
4
  import { createDraft, finishDraft } from 'immer';
5
+ import { isEqual } from 'lodash';
5
6
  import { fromUnixTime, subDays } from 'date-fns';
6
7
 
7
8
  class Positions {
@@ -663,6 +664,13 @@ function removeView$1(aiTable, records, fields, activeViewId) {
663
664
  });
664
665
  ViewActions.removeView(aiTable, [activeViewId]);
665
666
  }
667
+ function sortByViewPosition(data, activeView) {
668
+ const hasPositions = data.every((item) => item.positions && item.positions);
669
+ if (hasPositions) {
670
+ return [...data].sort((a, b) => a.positions[activeView._id] - b.positions[activeView._id]);
671
+ }
672
+ return data;
673
+ }
666
674
 
667
675
  const GeneralViewActions = {
668
676
  transform(aiTable, action) {
@@ -907,6 +915,7 @@ function isEmpty(value) {
907
915
  return isUndefinedOrNull(value) || value == '';
908
916
  }
909
917
 
918
+ const zhIntlCollator = typeof Intl !== 'undefined' ? new Intl.Collator('zh-CN') : undefined;
910
919
  class Field {
911
920
  stringInclude(str, searchStr) {
912
921
  return str.toLowerCase().includes(searchStr.trim().toLowerCase());
@@ -935,6 +944,35 @@ class Field {
935
944
  }
936
945
  }
937
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;
964
+ }
965
+ if (str1 == null) {
966
+ return -1;
967
+ }
968
+ if (str2 == null) {
969
+ return 1;
970
+ }
971
+ str1 = str1.trim();
972
+ str2 = str2.trim();
973
+ // test pinyin sort
974
+ return str1 === str2 ? 0 : zhIntlCollator ? zhIntlCollator.compare(str1, str2) : str1.localeCompare(str2, 'zh-CN') > 0 ? 1 : -1;
975
+ }
938
976
  }
939
977
 
940
978
  class TextField extends Field {
@@ -953,6 +991,12 @@ class TextField extends Field {
953
991
  static stringInclude(str, searchStr) {
954
992
  return str.toLowerCase().includes(searchStr.trim().toLowerCase());
955
993
  }
994
+ eq(cv1, cv2) {
995
+ return this.cellValueToString(cv1) === this.cellValueToString(cv2);
996
+ }
997
+ cellValueToString(cellValue) {
998
+ return cellValue;
999
+ }
956
1000
  }
957
1001
 
958
1002
  class SelectField extends Field {
@@ -970,6 +1014,28 @@ class SelectField extends Field {
970
1014
  return super.isMeetFilter(condition, cellValue);
971
1015
  }
972
1016
  }
1017
+ cellValueToString(cellValue, field) {
1018
+ return this.arrayValueToString(this.cellValueToArray(cellValue, field));
1019
+ }
1020
+ cellValueToArray(cellValue, field) {
1021
+ if (!cellValue) {
1022
+ return null;
1023
+ }
1024
+ const result = [];
1025
+ for (let i = 0, l = cellValue.length; i < l; i++) {
1026
+ const option = this.findOptionById(field, cellValue[i]);
1027
+ if (option) {
1028
+ result.push(option.text);
1029
+ }
1030
+ }
1031
+ return result;
1032
+ }
1033
+ findOptionById(field, id) {
1034
+ return field.settings.options.find(option => option._id === id) || null;
1035
+ }
1036
+ arrayValueToString(cellValues) {
1037
+ return cellValues && cellValues.length ? cellValues.join(', ') : null;
1038
+ }
973
1039
  }
974
1040
  function hasIntersect(array1, array2) {
975
1041
  if (!Array.isArray(array1) || !Array.isArray(array2)) {
@@ -1034,6 +1100,24 @@ class DateField extends Field {
1034
1100
  ];
1035
1101
  }
1036
1102
  }
1103
+ cellValueToString(_cellValue) {
1104
+ return null;
1105
+ }
1106
+ static _compare(cellValue1, cellValue2) {
1107
+ if (isEmpty(cellValue1?.timestamp) && isEmpty(cellValue2?.timestamp)) {
1108
+ return 0;
1109
+ }
1110
+ if (isEmpty(cellValue1?.timestamp)) {
1111
+ return -1;
1112
+ }
1113
+ if (isEmpty(cellValue2?.timestamp)) {
1114
+ return 1;
1115
+ }
1116
+ return cellValue1.timestamp === cellValue2.timestamp ? 0 : cellValue1.timestamp > cellValue2.timestamp ? 1 : -1;
1117
+ }
1118
+ compare(cellValue1, cellValue2) {
1119
+ return DateField._compare(cellValue1, cellValue2);
1120
+ }
1037
1121
  }
1038
1122
 
1039
1123
  class NumberField extends Field {
@@ -1059,6 +1143,24 @@ class NumberField extends Field {
1059
1143
  return super.isMeetFilter(condition, cellValue);
1060
1144
  }
1061
1145
  }
1146
+ cellValueToString(_cellValue) {
1147
+ return null;
1148
+ }
1149
+ static _compare(cellValue1, cellValue2) {
1150
+ if (isEmpty(cellValue1) && isEmpty(cellValue2)) {
1151
+ return 0;
1152
+ }
1153
+ if (isEmpty(cellValue1)) {
1154
+ return -1;
1155
+ }
1156
+ if (isEmpty(cellValue2)) {
1157
+ return 1;
1158
+ }
1159
+ return cellValue1 === cellValue2 ? 0 : cellValue1 > cellValue2 ? 1 : -1;
1160
+ }
1161
+ compare(cellValue1, cellValue2) {
1162
+ return NumberField._compare(cellValue1, cellValue2);
1163
+ }
1062
1164
  }
1063
1165
 
1064
1166
  class RateField extends Field {
@@ -1076,6 +1178,24 @@ class RateField extends Field {
1076
1178
  return super.isMeetFilter(condition, cellValue);
1077
1179
  }
1078
1180
  }
1181
+ cellValueToString(_cellValue) {
1182
+ return null;
1183
+ }
1184
+ static _compare(cellValue1, cellValue2) {
1185
+ if (isEmpty(cellValue1) && isEmpty(cellValue2)) {
1186
+ return 0;
1187
+ }
1188
+ if (isEmpty(cellValue1)) {
1189
+ return -1;
1190
+ }
1191
+ if (isEmpty(cellValue2)) {
1192
+ return 1;
1193
+ }
1194
+ return cellValue1 === cellValue2 ? 0 : cellValue1 > cellValue2 ? 1 : -1;
1195
+ }
1196
+ compare(cellValue1, cellValue2) {
1197
+ return RateField._compare(cellValue1, cellValue2);
1198
+ }
1079
1199
  }
1080
1200
 
1081
1201
  const ViewOperationMap = {
@@ -1143,24 +1263,48 @@ function doFilter(condition, field, cellValue) {
1143
1263
  return ViewOperationMap[field.type].isMeetFilter(condition, cellValue);
1144
1264
  }
1145
1265
 
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]);
1266
+ function getSortRecords(aiTable, records, activeView) {
1267
+ if (!activeView?.settings || !activeView.settings.sorts?.length) {
1268
+ return records;
1150
1269
  }
1151
- return data;
1270
+ const { is_keep_sort, sorts } = activeView.settings;
1271
+ if (is_keep_sort && sorts?.length) {
1272
+ return sortRecordsBySortInfo(aiTable, records, activeView);
1273
+ }
1274
+ return sortByViewPosition(records, activeView);
1275
+ }
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;
1152
1295
  }
1153
- function buildRecordsByView(records, fields, activeView) {
1296
+
1297
+ function buildRecordsByView(aiTable, records, fields, activeView) {
1154
1298
  const filteredRecords = getFilteredRecords(records, fields, activeView);
1155
- return sortByView(filteredRecords, activeView._id);
1299
+ return getSortRecords(aiTable, filteredRecords, activeView);
1156
1300
  }
1157
1301
  function buildFieldsByView(fields, activeView) {
1158
- return sortByView(fields, activeView._id);
1302
+ return sortByViewPosition(fields, activeView);
1159
1303
  }
1160
1304
 
1161
1305
  /**
1162
1306
  * Generated bundle index. Do not edit.
1163
1307
  */
1164
1308
 
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 };
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 };
1166
1310
  //# sourceMappingURL=ai-table-state.mjs.map