@univerjs/sheets-filter 1.3.0 → 1.4.0

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 (72) hide show
  1. package/lib/cjs/facade.js +8 -16
  2. package/lib/cjs/index.js +28 -2352
  3. package/lib/cjs/locale/ar-SA.js +1 -2
  4. package/lib/cjs/locale/ca-ES.js +1 -2
  5. package/lib/cjs/locale/de-DE.js +1 -2
  6. package/lib/cjs/locale/en-US.js +1 -2
  7. package/lib/cjs/locale/es-ES.js +1 -2
  8. package/lib/cjs/locale/fa-IR.js +1 -2
  9. package/lib/cjs/locale/fr-FR.js +1 -2
  10. package/lib/cjs/locale/id-ID.js +1 -2
  11. package/lib/cjs/locale/it-IT.js +1 -2
  12. package/lib/cjs/locale/ja-JP.js +1 -2
  13. package/lib/cjs/locale/ko-KR.js +1 -2
  14. package/lib/cjs/locale/pl-PL.js +1 -2
  15. package/lib/cjs/locale/pt-BR.js +1 -2
  16. package/lib/cjs/locale/ru-RU.js +1 -2
  17. package/lib/cjs/locale/sk-SK.js +1 -2
  18. package/lib/cjs/locale/vi-VN.js +1 -2
  19. package/lib/cjs/locale/zh-CN.js +1 -2
  20. package/lib/cjs/locale/zh-HK.js +1 -2
  21. package/lib/cjs/locale/zh-TW.js +1 -2
  22. package/lib/cjs/plugin-DG3Pao0f.js +2462 -0
  23. package/lib/cjs/worker.js +9 -0
  24. package/lib/es/facade.js +8 -16
  25. package/lib/es/index.js +1 -2325
  26. package/lib/es/locale/ar-SA.js +1 -2
  27. package/lib/es/locale/ca-ES.js +1 -2
  28. package/lib/es/locale/de-DE.js +1 -2
  29. package/lib/es/locale/en-US.js +1 -2
  30. package/lib/es/locale/es-ES.js +1 -2
  31. package/lib/es/locale/fa-IR.js +1 -2
  32. package/lib/es/locale/fr-FR.js +1 -2
  33. package/lib/es/locale/id-ID.js +1 -2
  34. package/lib/es/locale/it-IT.js +1 -2
  35. package/lib/es/locale/ja-JP.js +1 -2
  36. package/lib/es/locale/ko-KR.js +1 -2
  37. package/lib/es/locale/pl-PL.js +1 -2
  38. package/lib/es/locale/pt-BR.js +1 -2
  39. package/lib/es/locale/ru-RU.js +1 -2
  40. package/lib/es/locale/sk-SK.js +1 -2
  41. package/lib/es/locale/vi-VN.js +1 -2
  42. package/lib/es/locale/zh-CN.js +1 -2
  43. package/lib/es/locale/zh-HK.js +1 -2
  44. package/lib/es/locale/zh-TW.js +1 -2
  45. package/lib/es/plugin-JIK5U-UH.js +2307 -0
  46. package/lib/es/worker.js +3 -0
  47. package/lib/facade.js +8 -16
  48. package/lib/index.js +1 -2325
  49. package/lib/locale/ar-SA.js +1 -2
  50. package/lib/locale/ca-ES.js +1 -2
  51. package/lib/locale/de-DE.js +1 -2
  52. package/lib/locale/en-US.js +1 -2
  53. package/lib/locale/es-ES.js +1 -2
  54. package/lib/locale/fa-IR.js +1 -2
  55. package/lib/locale/fr-FR.js +1 -2
  56. package/lib/locale/id-ID.js +1 -2
  57. package/lib/locale/it-IT.js +1 -2
  58. package/lib/locale/ja-JP.js +1 -2
  59. package/lib/locale/ko-KR.js +1 -2
  60. package/lib/locale/pl-PL.js +1 -2
  61. package/lib/locale/pt-BR.js +1 -2
  62. package/lib/locale/ru-RU.js +1 -2
  63. package/lib/locale/sk-SK.js +1 -2
  64. package/lib/locale/vi-VN.js +1 -2
  65. package/lib/locale/zh-CN.js +1 -2
  66. package/lib/locale/zh-HK.js +1 -2
  67. package/lib/locale/zh-TW.js +1 -2
  68. package/lib/plugin-JIK5U-UH.js +2307 -0
  69. package/lib/types/worker.d.ts +16 -0
  70. package/lib/umd/index.js +1 -1
  71. package/lib/worker.js +3 -0
  72. package/package.json +16 -11
package/lib/index.js CHANGED
@@ -1,2327 +1,3 @@
1
- import { CellValueType, ColorKit, CommandType, DependentOn, Disposable, DisposableCollection, ErrorService, ICommandService, IConfigService, IResourceManagerService, IUndoRedoService, IUniverInstanceService, Inject, Injector, LocaleService, Optional, Plugin, Rectangle, Tools, UndoCommandId, UniverInstanceType, extractPureTextFromCell, fromCallback, isNumeric, merge, mergeSets, moveMatrixArray, regexp, sequenceExecute, touchDependencies } from "@univerjs/core";
2
- import { CopySheetCommand, EffectRefRangId, INTERCEPTOR_POINT, InsertColCommand, InsertColMutation, InsertRowCommand, InsertRowMutation, MarkDirtyFilterChangeMutation, MoveColsMutation, MoveRangeCommand, MoveRowsCommand, RefRangeService, RemoveColCommand, RemoveColMutation, RemoveRowCommand, RemoveRowMutation, RemoveSheetCommand, SetRangeValuesMutation, SetWorksheetActiveOperation, SheetInterceptorService, SheetsSelectionsService, UniverSheetsPlugin, ZebraCrossingCacheController, expandToContinuousRange, getSheetCommandTarget, isSingleCellSelection } from "@univerjs/sheets";
3
- import { BehaviorSubject, filter, merge as merge$1, of, switchMap } from "rxjs";
4
- import { COLOR_BLACK_RGB } from "@univerjs/engine-render";
5
- import { DataSyncPrimaryController } from "@univerjs/rpc";
6
- import { IActiveDirtyManagerService, ISheetRowFilteredService, UniverFormulaEnginePlugin } from "@univerjs/engine-formula";
1
+ import { C as notEquals, E as FILTER_MUTATIONS, S as lessThanOrEqualTo, T as FilterBy, _ as equals, a as RemoveSheetFilterCommand, b as greaterThanOrEqualTo, c as SmartToggleSheetsFilterCommand, d as SetSheetsFilterCriteriaMutation, f as SetSheetsFilterRangeMutation, g as FilterModel, h as FilterColumn, i as ReCalcSheetsFilterCommand, l as ReCalcSheetsFilterMutation, m as SheetsFilterService, n as SheetsFilterSyncController, o as SetSheetFilterRangeCommand, p as SHEET_FILTER_SNAPSHOT_ID, r as ClearSheetsFilterCriteriaCommand, s as SetSheetsFilterCriteriaCommand, t as UniverSheetsFilterPlugin, u as RemoveSheetsFilterMutation, v as getCustomFilterFn, w as CustomFilterOperator, x as lessThan, y as greaterThan } from "./plugin-JIK5U-UH.js";
7
2
 
8
- //#region src/common/const.ts
9
- /**
10
- * Copyright 2023-present DreamNum Co., Ltd.
11
- *
12
- * Licensed under the Apache License, Version 2.0 (the "License");
13
- * you may not use this file except in compliance with the License.
14
- * You may obtain a copy of the License at
15
- *
16
- * http://www.apache.org/licenses/LICENSE-2.0
17
- *
18
- * Unless required by applicable law or agreed to in writing, software
19
- * distributed under the License is distributed on an "AS IS" BASIS,
20
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
- * See the License for the specific language governing permissions and
22
- * limitations under the License.
23
- */
24
- const SetSheetsFilterRangeMutationId = "sheet.mutation.set-filter-range";
25
- const SetSheetsFilterCriteriaMutationId = "sheet.mutation.set-filter-criteria";
26
- const RemoveSheetsFilterMutationId = "sheet.mutation.remove-filter";
27
- const ReCalcSheetsFilterMutationId = "sheet.mutation.re-calc-filter";
28
- const FILTER_MUTATIONS = /* @__PURE__ */ new Set([
29
- SetSheetsFilterRangeMutationId,
30
- SetSheetsFilterCriteriaMutationId,
31
- RemoveSheetsFilterMutationId,
32
- ReCalcSheetsFilterMutationId
33
- ]);
34
-
35
- //#endregion
36
- //#region src/models/types.ts
37
- /**
38
- * The filter types.
39
- */
40
- let FilterBy = /* @__PURE__ */ function(FilterBy) {
41
- FilterBy[FilterBy["VALUES"] = 0] = "VALUES";
42
- FilterBy[FilterBy["COLORS"] = 1] = "COLORS";
43
- FilterBy[FilterBy["CONDITIONS"] = 2] = "CONDITIONS";
44
- return FilterBy;
45
- }({});
46
- /**
47
- * Basic custom filter operators.
48
- *
49
- * @internal
50
- * doesNotContain, isBlank, isNotBlank are not defined in OOXML. They are represented by regex-like values.
51
- */
52
- let CustomFilterOperator = /* @__PURE__ */ function(CustomFilterOperator) {
53
- /** "EQUAL" operator. */
54
- CustomFilterOperator["EQUAL"] = "equal";
55
- /** "GREATER_THAN" operator. */
56
- CustomFilterOperator["GREATER_THAN"] = "greaterThan";
57
- /** "GREATER_THAN_OR_EQUAL" operator. */
58
- CustomFilterOperator["GREATER_THAN_OR_EQUAL"] = "greaterThanOrEqual";
59
- /** "LESS_THAN" operator. */
60
- CustomFilterOperator["LESS_THAN"] = "lessThan";
61
- /** "LESS_THAN_OR_EQUAL" operator. */
62
- CustomFilterOperator["LESS_THAN_OR_EQUAL"] = "lessThanOrEqual";
63
- /** "NOT_EQUALS" operator. */
64
- CustomFilterOperator["NOT_EQUALS"] = "notEqual";
65
- return CustomFilterOperator;
66
- }({});
67
-
68
- //#endregion
69
- //#region src/models/custom-filters.ts
70
- const greaterThan = {
71
- operator: "greaterThan",
72
- fn: (value, compare) => {
73
- if (!ensureNumber(value)) return false;
74
- return value > compare;
75
- }
76
- };
77
- const greaterThanOrEqualTo = {
78
- operator: "greaterThanOrEqual",
79
- fn: (value, compare) => {
80
- if (!ensureNumber(value)) return false;
81
- return value >= compare;
82
- }
83
- };
84
- const lessThan = {
85
- operator: "lessThan",
86
- fn: (value, compare) => {
87
- if (!ensureNumber(value)) return false;
88
- return value < compare;
89
- }
90
- };
91
- const lessThanOrEqualTo = {
92
- operator: "lessThanOrEqual",
93
- fn: (value, compare) => {
94
- if (!ensureNumber(value)) return false;
95
- return value <= compare;
96
- }
97
- };
98
- const equals = {
99
- operator: "equal",
100
- fn: (value, compare) => {
101
- if (!ensureNumber(value)) return false;
102
- return value === compare;
103
- }
104
- };
105
- const notEquals = {
106
- operator: "notEqual",
107
- fn: (value, compare) => {
108
- if (typeof compare === "string") {
109
- if (compare === " ") {
110
- if (value !== void 0 && value !== null) return true;
111
- return false;
112
- }
113
- const ensuredString = ensureString(value);
114
- if (ensuredString && isWildCardString(compare)) return !regexp.createFromWildcard(compare).test(ensuredString);
115
- return ensuredString !== compare;
116
- }
117
- if (!ensureNumber(value)) return true;
118
- return value !== compare;
119
- }
120
- };
121
- const CustomFilterFnRegistry = /* @__PURE__ */ new Map([]);
122
- [
123
- greaterThan,
124
- greaterThanOrEqualTo,
125
- lessThan,
126
- lessThanOrEqualTo,
127
- equals,
128
- notEquals
129
- ].forEach((fn) => {
130
- CustomFilterFnRegistry.set(fn.operator, fn);
131
- });
132
- function isNumericFilterFn(operator) {
133
- return !!operator;
134
- }
135
- /** This operators matches texts. */
136
- const textMatch = { fn: (value, compare) => {
137
- const ensured = ensureString(value);
138
- if (ensured === null) {
139
- if (compare === "") return true;
140
- return false;
141
- }
142
- return regexp.createFromWildcard(compare).test(ensured);
143
- } };
144
- function getCustomFilterFn(operator) {
145
- if (!operator) return textMatch;
146
- return CustomFilterFnRegistry.get(operator);
147
- }
148
- function ensureNumber(value) {
149
- return typeof value === "number";
150
- }
151
- function ensureNumeric(value) {
152
- if (typeof value === "number") return true;
153
- if (typeof value === "string" && isNumeric(value)) return true;
154
- return false;
155
- }
156
- function ensureString(value) {
157
- if (typeof value === "boolean" || value == null) return null;
158
- return typeof value === "string" ? value : value.toString();
159
- }
160
- function isWildCardString(str) {
161
- if (typeof str === "number") return false;
162
- return str.indexOf("*") !== -1 || str.indexOf("?") !== -1;
163
- }
164
-
165
- //#endregion
166
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/typeof.js
167
- function _typeof(o) {
168
- "@babel/helpers - typeof";
169
- return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
170
- return typeof o;
171
- } : function(o) {
172
- return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
173
- }, _typeof(o);
174
- }
175
-
176
- //#endregion
177
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/toPrimitive.js
178
- function toPrimitive(t, r) {
179
- if ("object" != _typeof(t) || !t) return t;
180
- var e = t[Symbol.toPrimitive];
181
- if (void 0 !== e) {
182
- var i = e.call(t, r || "default");
183
- if ("object" != _typeof(i)) return i;
184
- throw new TypeError("@@toPrimitive must return a primitive value.");
185
- }
186
- return ("string" === r ? String : Number)(t);
187
- }
188
-
189
- //#endregion
190
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/toPropertyKey.js
191
- function toPropertyKey(t) {
192
- var i = toPrimitive(t, "string");
193
- return "symbol" == _typeof(i) ? i : i + "";
194
- }
195
-
196
- //#endregion
197
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/defineProperty.js
198
- function _defineProperty(e, r, t) {
199
- return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
200
- value: t,
201
- enumerable: !0,
202
- configurable: !0,
203
- writable: !0
204
- }) : e[r] = t, e;
205
- }
206
-
207
- //#endregion
208
- //#region src/models/filter-model.ts
209
- const EMPTY = () => /* @__PURE__ */ new Set();
210
- /**
211
- * This is the in-memory model of filter.
212
- */
213
- var FilterModel = class FilterModel extends Disposable {
214
- get filteredOutRows() {
215
- return this._filteredOutRows$.getValue();
216
- }
217
- set filteredOutRows(rows) {
218
- this._alreadyFilteredOutRows = rows;
219
- this._filteredOutRows$.next(rows);
220
- }
221
- constructor(unitId, subUnitId, _worksheet) {
222
- super();
223
- this.unitId = unitId;
224
- this.subUnitId = subUnitId;
225
- this._worksheet = _worksheet;
226
- _defineProperty(this, "_filteredOutRows$", new BehaviorSubject(EMPTY()));
227
- _defineProperty(this, "filteredOutRows$", this._filteredOutRows$.asObservable());
228
- _defineProperty(this, "_hasCriteria$", new BehaviorSubject(false));
229
- _defineProperty(this, "hasCriteria$", this._hasCriteria$.asObservable());
230
- _defineProperty(this, "_filterColumnByIndex", /* @__PURE__ */ new Map());
231
- _defineProperty(this, "_alreadyFilteredOutRows", EMPTY());
232
- _defineProperty(this, "_range", void 0);
233
- }
234
- dispose() {
235
- super.dispose();
236
- this._filteredOutRows$.complete();
237
- this._hasCriteria$.complete();
238
- this._worksheet = null;
239
- }
240
- /**
241
- * Serialize this filter model to the JSON format representation.
242
- */
243
- serialize() {
244
- const result = {
245
- ref: Rectangle.clone(this._range),
246
- filterColumns: this._getAllFilterColumns(true).sort(([offset1], [offset2]) => offset1 - offset2).map(([_, filterColumn]) => filterColumn.serialize())
247
- };
248
- if (this._alreadyFilteredOutRows) result.cachedFilteredOut = Array.from(this._alreadyFilteredOutRows).sort();
249
- return result;
250
- }
251
- /**
252
- * Deserialize auto filter info to construct a `FilterModel` object.
253
- * @param unitId workbook id
254
- * @param subUnitId worksheet id
255
- * @param worksheet the Worksheet object
256
- * @param autoFilter auto filter data
257
- */
258
- static deserialize(unitId, subUnitId, worksheet, autoFilter) {
259
- const filterModel = new FilterModel(unitId, subUnitId, worksheet);
260
- filterModel._dump(autoFilter);
261
- return filterModel;
262
- }
263
- _dump(autoFilter) {
264
- var _autoFilter$filterCol;
265
- this.setRange(autoFilter.ref);
266
- (_autoFilter$filterCol = autoFilter.filterColumns) === null || _autoFilter$filterCol === void 0 || _autoFilter$filterCol.filter((filterColumn) => {
267
- if (!filterColumn.filters && !filterColumn.colorFilters && !filterColumn.customFilters) return false;
268
- return true;
269
- }).forEach((filterColumn) => this._setCriteriaWithoutReCalc(filterColumn.colId, filterColumn));
270
- if (autoFilter.cachedFilteredOut) {
271
- this._alreadyFilteredOutRows = new Set(autoFilter.cachedFilteredOut);
272
- this._emit();
273
- } else if (autoFilter.filterColumns && autoFilter.filterColumns.length > 0) {
274
- this._reCalcAllColumns();
275
- this._emit();
276
- }
277
- this._emitHasCriteria();
278
- }
279
- isRowFiltered(row) {
280
- return this._alreadyFilteredOutRows.has(row);
281
- }
282
- getRange() {
283
- if (!this._range) throw new Error("[FilterModel] could not get range before a range is set!");
284
- return this._range;
285
- }
286
- /**
287
- * Get filtered out rows except the specific column. This method is considered as "pure". In
288
- * another word it would not change `filteredOutRows` on `FilterModel` nor `FilterColumn`.
289
- * @param col
290
- */
291
- getFilteredOutRowsExceptCol(col) {
292
- return this._getAllFilterColumns(true).filter(([colOffset]) => colOffset !== col).reduce((acc, [, filterColumn]) => {
293
- const newResult = filterColumn.calc({ getAlreadyFilteredOutRows: () => acc });
294
- if (newResult) return mergeSets(acc, newResult);
295
- return acc;
296
- }, /* @__PURE__ */ new Set());
297
- }
298
- /**
299
- * Set range of the filter model, this would remove some `IFilterColumn`
300
- * if the new range not overlaps the old range.
301
- */
302
- setRange(range) {
303
- this._range = range;
304
- this._getAllFilterColumns(true).forEach(([col, filterColumn]) => {
305
- filterColumn.setRangeAndColumn({
306
- startRow: range.startRow,
307
- endRow: range.endRow,
308
- startColumn: col,
309
- endColumn: col
310
- }, col);
311
- });
312
- }
313
- /**
314
- * Set or remove filter criteria on a specific row.
315
- */
316
- setCriteria(col, criteria, reCalc = false) {
317
- if (!this._range) throw new Error("[FilterModel] could not set criteria before a range is set!");
318
- if (!criteria) {
319
- this._removeCriteria(col);
320
- this._rebuildAlreadyFilteredOutRowsWithCache();
321
- if (reCalc) this._reCalcAllColumns();
322
- this._emit();
323
- this._emitHasCriteria();
324
- return;
325
- }
326
- this._setCriteriaWithoutReCalc(col, criteria);
327
- if (reCalc) {
328
- this._rebuildAlreadyFilteredOutRowsWithCache();
329
- this._getAllFilterColumns().forEach((filterColumn) => filterColumn.__clearCache());
330
- this._reCalcWithNoCacheColumns();
331
- this._emit();
332
- this._emitHasCriteria();
333
- }
334
- }
335
- getAllFilterColumns() {
336
- return this._getAllFilterColumns(true);
337
- }
338
- getFilterColumn(index) {
339
- return this._filterColumnByIndex.get(index) ?? null;
340
- }
341
- reCalc() {
342
- this._reCalcAllColumns();
343
- this._emit();
344
- }
345
- _getAllFilterColumns(withCol = false) {
346
- const columns = Array.from(this._filterColumnByIndex.entries());
347
- if (withCol) return columns;
348
- return columns.map(([_, filterColumn]) => filterColumn);
349
- }
350
- _reCalcAllColumns() {
351
- this._alreadyFilteredOutRows = EMPTY();
352
- this._getAllFilterColumns().forEach((filterColumn) => filterColumn.__clearCache());
353
- this._reCalcWithNoCacheColumns();
354
- }
355
- _setCriteriaWithoutReCalc(col, criteria) {
356
- const range = this._range;
357
- if (!range) throw new Error("[FilterModel] could not set criteria before a range is set!");
358
- const { startColumn, endColumn } = range;
359
- if (col > endColumn || col < startColumn) throw new Error(`[FilterModel] could not set criteria on column ${col} which is out of range!`);
360
- let filterColumn;
361
- if (this._filterColumnByIndex.has(col)) filterColumn = this._filterColumnByIndex.get(col);
362
- else {
363
- filterColumn = new FilterColumn(this.unitId, this.subUnitId, this._worksheet, criteria, { getAlreadyFilteredOutRows: () => this._alreadyFilteredOutRows });
364
- filterColumn.setRangeAndColumn(range, col);
365
- this._filterColumnByIndex.set(col, filterColumn);
366
- }
367
- filterColumn.setCriteria(criteria);
368
- }
369
- _removeCriteria(col) {
370
- const filterColumn = this._filterColumnByIndex.get(col);
371
- if (filterColumn) {
372
- filterColumn.dispose();
373
- this._filterColumnByIndex.delete(col);
374
- }
375
- }
376
- _emit() {
377
- this._filteredOutRows$.next(this._alreadyFilteredOutRows);
378
- }
379
- _emitHasCriteria() {
380
- this._hasCriteria$.next(this._filterColumnByIndex.size > 0);
381
- }
382
- _rebuildAlreadyFilteredOutRowsWithCache() {
383
- const newFilteredOutRows = this._getAllFilterColumns().filter((filterColumn) => filterColumn.hasCache()).reduce((acc, filterColumn) => {
384
- return mergeSets(acc, filterColumn.filteredOutRows);
385
- }, /* @__PURE__ */ new Set());
386
- this._alreadyFilteredOutRows = newFilteredOutRows;
387
- }
388
- _reCalcWithNoCacheColumns() {
389
- const noCacheFilteredOutRows = this._getAllFilterColumns().filter((filterColumn) => !filterColumn.hasCache());
390
- for (const filterColumn of noCacheFilteredOutRows) {
391
- const filteredRows = filterColumn.reCalc();
392
- if (filteredRows) this._alreadyFilteredOutRows = mergeSets(this._alreadyFilteredOutRows, filteredRows);
393
- }
394
- }
395
- };
396
- /**
397
- * This is the filter criteria on a specific column.
398
- */
399
- var FilterColumn = class extends Disposable {
400
- get filteredOutRows() {
401
- return this._filteredOutRows;
402
- }
403
- get filterBy() {
404
- return this._filterBy;
405
- }
406
- constructor(unitId, subUnitId, _worksheet, _criteria, _filterColumnContext) {
407
- super();
408
- this.unitId = unitId;
409
- this.subUnitId = subUnitId;
410
- this._worksheet = _worksheet;
411
- this._criteria = _criteria;
412
- this._filterColumnContext = _filterColumnContext;
413
- _defineProperty(this, "_filteredOutRows", null);
414
- _defineProperty(this, "_filterFn", null);
415
- _defineProperty(this, "_range", null);
416
- _defineProperty(this, "_column", 0);
417
- _defineProperty(this, "_filterBy", 0);
418
- }
419
- dispose() {
420
- super.dispose();
421
- this._filteredOutRows = null;
422
- }
423
- /**
424
- * @internal
425
- */
426
- __clearCache() {
427
- this._filteredOutRows = null;
428
- }
429
- serialize() {
430
- if (!this._criteria) throw new Error("[FilterColumn]: could not serialize without a filter column!");
431
- return Tools.deepClone({
432
- ...this._criteria,
433
- colId: this._column
434
- });
435
- }
436
- hasCache() {
437
- return this._filteredOutRows !== null;
438
- }
439
- setRangeAndColumn(range, column) {
440
- this._range = range;
441
- this._column = column;
442
- }
443
- setCriteria(criteria) {
444
- this._criteria = criteria;
445
- this._generateFilterFn();
446
- this._filteredOutRows = null;
447
- }
448
- getColumnData() {
449
- return Tools.deepClone(this._criteria);
450
- }
451
- /**
452
- * Trigger new calculation on this `FilterModel` instance.
453
- *
454
- * @external DO NOT EVER call this method from `FilterColumn` itself. The whole process heavily relies on
455
- * `filteredOutByOthers`, and it is more comprehensible if we let `FilterModel` take full control over the process.
456
- */
457
- reCalc() {
458
- this._filteredOutRows = this.calc(this._filterColumnContext);
459
- return this._filteredOutRows;
460
- }
461
- calc(context) {
462
- if (!this._filterFn) throw new Error("[FilterColumn] cannot calculate without a filter fn!");
463
- if (!this._range) throw new Error("[FilterColumn] cannot calculate without a range!");
464
- if (typeof this._column !== "number") throw new TypeError("[FilterColumn] cannot calculate without a column offset!");
465
- const column = this._column;
466
- const iterateRange = {
467
- startColumn: column,
468
- endColumn: column,
469
- startRow: this._range.startRow + 1,
470
- endRow: this._range.endRow
471
- };
472
- const filteredOutRows = /* @__PURE__ */ new Set();
473
- const filteredOutByOthers = context.getAlreadyFilteredOutRows();
474
- for (const range of this._worksheet.iterateByColumn(iterateRange, false, false)) {
475
- const { row, rowSpan, col } = range;
476
- if (filteredOutByOthers.has(row) && (!rowSpan || rowSpan === 1)) continue;
477
- if (!(this._filterBy === 0 ? this._filterFn(extractPureTextFromCell(this._worksheet.getCell(row, col))) : this._filterBy === 1 ? this._filterFn(this._worksheet.getComposedCellStyle(row, col)) : this._filterFn(getFilterValueForConditionalFiltering(this._worksheet, row, col)))) {
478
- filteredOutRows.add(row);
479
- if (rowSpan) for (let i = 1; i < rowSpan; i++) filteredOutRows.add(row + i);
480
- }
481
- }
482
- return filteredOutRows;
483
- }
484
- _generateFilterFn() {
485
- if (!this._criteria) return;
486
- this._filterFn = generateFilterFn(this._criteria);
487
- this._filterBy = this._criteria.filters ? 0 : this._criteria.colorFilters ? 1 : 2;
488
- }
489
- };
490
- /**
491
- * This functions take a `IFilterColumn` as input and return a function that can be used to filter rows.
492
- * @param column
493
- * @returns the filter function that takes the cell's value and return a boolean.
494
- */
495
- function generateFilterFn(column) {
496
- if (column.filters) return filterByValuesFnFactory(column.filters);
497
- if (column.colorFilters) return filterByColorsFnFactory(column.colorFilters);
498
- if (column.customFilters) return customFilterFnFactory(column.customFilters);
499
- throw new Error("[FilterModel]: other types of filters are not supported yet.");
500
- }
501
- function filterByValuesFnFactory(values) {
502
- const includeBlank = !!values.blank;
503
- const valuesSet = new Set(values.filters);
504
- return (value) => {
505
- if (value === void 0 || value === "") return includeBlank;
506
- return valuesSet.has(typeof value === "string" ? value : `${value}`);
507
- };
508
- }
509
- function filterByColorsFnFactory(colorFilters) {
510
- if (colorFilters.cellFillColors) {
511
- const fillColorsSet = new Set(colorFilters.cellFillColors);
512
- return (cellStyle) => {
513
- var _cellStyle$bg;
514
- if (!cellStyle || !((_cellStyle$bg = cellStyle.bg) === null || _cellStyle$bg === void 0 ? void 0 : _cellStyle$bg.rgb)) {
515
- if (fillColorsSet.has(null)) return true;
516
- return false;
517
- }
518
- const bg = new ColorKit(cellStyle.bg.rgb).toRgbString();
519
- return fillColorsSet.has(bg);
520
- };
521
- }
522
- if (colorFilters.cellTextColors) {
523
- const textColorsSet = new Set(colorFilters.cellTextColors);
524
- return (cellStyle) => {
525
- var _cellStyle$cl;
526
- if (!cellStyle || !((_cellStyle$cl = cellStyle.cl) === null || _cellStyle$cl === void 0 ? void 0 : _cellStyle$cl.rgb)) {
527
- if (textColorsSet.has(COLOR_BLACK_RGB)) return true;
528
- return false;
529
- }
530
- const cl = new ColorKit(cellStyle.cl.rgb).toRgbString();
531
- return textColorsSet.has(cl);
532
- };
533
- }
534
- throw new Error("[FilterModel]: color filters are not supported yet.");
535
- }
536
- function customFilterFnFactory(customFilters) {
537
- const customFilterFns = customFilters.customFilters.map((filter) => generateCustomFilterFn(filter));
538
- if (isCompoundCustomFilter(customFilterFns)) {
539
- if (customFilters.and) return AND(customFilterFns);
540
- return OR(customFilterFns);
541
- }
542
- return customFilterFns[0];
543
- }
544
- function AND(filterFns) {
545
- const [fn1, fn2] = filterFns;
546
- return (value) => fn1(value) && fn2(value);
547
- }
548
- function OR(filterFns) {
549
- const [fn1, fn2] = filterFns;
550
- return (value) => fn1(value) || fn2(value);
551
- }
552
- function isCompoundCustomFilter(filter) {
553
- return filter.length === 2;
554
- }
555
- function generateCustomFilterFn(filter) {
556
- const compare = filter.val;
557
- if (filter.operator === "notEqual") {
558
- if (!ensureNumeric(compare)) return (value) => notEquals.fn(value, compare);
559
- }
560
- if (isNumericFilterFn(filter.operator)) {
561
- if (!ensureNumeric(compare)) return () => false;
562
- const customFilterFn = getCustomFilterFn(filter.operator);
563
- const ensuredNumber = Number(compare);
564
- return (value) => customFilterFn.fn(value, ensuredNumber);
565
- }
566
- const customFilterFn = getCustomFilterFn(filter.operator);
567
- return (value) => customFilterFn.fn(value, compare);
568
- }
569
- function getFilterValueForConditionalFiltering(worksheet, row, col) {
570
- const interceptedCell = worksheet.getCell(row, col);
571
- if (!interceptedCell) return null;
572
- const rawCell = worksheet.getCellRaw(row, col);
573
- if (interceptedCell && !rawCell) return extractFilterValueFromCell(interceptedCell);
574
- if (!rawCell) return null;
575
- if (interceptedCell.t === CellValueType.NUMBER && typeof interceptedCell.v === "string") return rawCell.v;
576
- if (interceptedCell.t === CellValueType.NUMBER) return Number(rawCell.v);
577
- return extractFilterValueFromCell(rawCell);
578
- }
579
- function extractFilterValueFromCell(cell) {
580
- var _cell$p;
581
- const richTextValue = (_cell$p = cell.p) === null || _cell$p === void 0 || (_cell$p = _cell$p.body) === null || _cell$p === void 0 ? void 0 : _cell$p.dataStream;
582
- if (richTextValue) return richTextValue.trimEnd();
583
- const rawValue = cell.v;
584
- if (typeof rawValue === "string") {
585
- if (cell.t === CellValueType.BOOLEAN) return rawValue.toUpperCase();
586
- return rawValue;
587
- }
588
- if (typeof rawValue === "number") {
589
- if (cell.t === CellValueType.BOOLEAN) return rawValue ? "TRUE" : "FALSE";
590
- return rawValue;
591
- }
592
- if (typeof rawValue === "boolean") return rawValue ? "TRUE" : "FALSE";
593
- return "";
594
- }
595
-
596
- //#endregion
597
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/decorateParam.js
598
- function __decorateParam(paramIndex, decorator) {
599
- return function(target, key) {
600
- decorator(target, key, paramIndex);
601
- };
602
- }
603
-
604
- //#endregion
605
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/decorate.js
606
- function __decorate(decorators, target, key, desc) {
607
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
608
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
609
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
610
- return c > 3 && r && Object.defineProperty(target, key, r), r;
611
- }
612
-
613
- //#endregion
614
- //#region src/services/sheet-filter.service.ts
615
- const SHEET_FILTER_SNAPSHOT_ID = "SHEET_FILTER_PLUGIN";
616
- let SheetsFilterService = class SheetsFilterService extends Disposable {
617
- /** The current Workbook's active Worksheet's filter model (if there is one). */
618
- get activeFilterModel() {
619
- return this._activeFilterModel$.getValue();
620
- }
621
- constructor(_resourcesManagerService, _univerInstanceService, _commandService) {
622
- super();
623
- this._resourcesManagerService = _resourcesManagerService;
624
- this._univerInstanceService = _univerInstanceService;
625
- this._commandService = _commandService;
626
- _defineProperty(this, "_filterModels", /* @__PURE__ */ new Map());
627
- _defineProperty(this, "_loadedUnitId$", new BehaviorSubject(null));
628
- _defineProperty(this, "loadedUnitId$", this._loadedUnitId$.asObservable());
629
- _defineProperty(this, "_errorMsg$", new BehaviorSubject(null));
630
- _defineProperty(this, "errorMsg$", this._errorMsg$.asObservable());
631
- _defineProperty(this, "_activeFilterModel$", new BehaviorSubject(null));
632
- _defineProperty(this, "activeFilterModel$", this._activeFilterModel$.asObservable());
633
- this._initModel();
634
- this._initActiveFilterModel();
635
- }
636
- /**
637
- *
638
- * @param unitId
639
- * @param subUnitId
640
- */
641
- ensureFilterModel(unitId, subUnitId) {
642
- const already = this.getFilterModel(unitId, subUnitId);
643
- if (already) return already;
644
- const workbook = this._univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_SHEET);
645
- if (!workbook) throw new Error(`[SheetsFilterService]: could not create "FilterModel" on a non-existing workbook ${unitId}!`);
646
- const worksheet = workbook.getSheetBySheetId(subUnitId);
647
- if (!worksheet) throw new Error(`[SheetsFilterService]: could not create "FilterModel" on a non-existing worksheet ${subUnitId}!`);
648
- const filterModel = new FilterModel(unitId, subUnitId, worksheet);
649
- this._cacheFilterModel(unitId, subUnitId, filterModel);
650
- return filterModel;
651
- }
652
- getFilterModel(unitId, subUnitId) {
653
- var _this$_filterModels$g;
654
- return ((_this$_filterModels$g = this._filterModels.get(unitId)) === null || _this$_filterModels$g === void 0 ? void 0 : _this$_filterModels$g.get(subUnitId)) ?? null;
655
- }
656
- removeFilterModel(unitId, subUnitId) {
657
- const already = this.getFilterModel(unitId, subUnitId);
658
- if (already) {
659
- already.dispose();
660
- this._filterModels.get(unitId).delete(subUnitId);
661
- return true;
662
- }
663
- return false;
664
- }
665
- setFilterErrorMsg(content) {
666
- this._errorMsg$.next(content);
667
- }
668
- _updateActiveFilterModel() {
669
- let workbook;
670
- try {
671
- workbook = this._univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_SHEET);
672
- if (!workbook) {
673
- this._activeFilterModel$.next(null);
674
- return;
675
- }
676
- } catch (err) {
677
- console.error("[SheetsFilterService]: could not get active workbook!", err);
678
- return;
679
- }
680
- const activeSheet = workbook.getActiveSheet(true);
681
- if (!activeSheet) {
682
- this._activeFilterModel$.next(null);
683
- return;
684
- }
685
- const unitId = activeSheet.getUnitId();
686
- const subUnitId = activeSheet.getSheetId();
687
- const filterModel = this.getFilterModel(unitId, subUnitId);
688
- this._activeFilterModel$.next(filterModel);
689
- }
690
- _initActiveFilterModel() {
691
- this.disposeWithMe(merge$1(fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(filter(([command]) => command.type === CommandType.MUTATION && FILTER_MUTATIONS.has(command.id))), this._univerInstanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_SHEET).pipe(switchMap((workbook) => (workbook === null || workbook === void 0 ? void 0 : workbook.activeSheet$) ?? of(null)))).subscribe(() => this._updateActiveFilterModel()));
692
- }
693
- _serializeAutoFiltersForUnit(unitId) {
694
- const allFilterModels = this._filterModels.get(unitId);
695
- if (!allFilterModels) return "{}";
696
- const json = {};
697
- allFilterModels.forEach((model, worksheetId) => {
698
- json[worksheetId] = model.serialize();
699
- });
700
- return JSON.stringify(json);
701
- }
702
- _deserializeAutoFiltersForUnit(unitId, json) {
703
- const workbook = this._univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_SHEET);
704
- Object.keys(json).forEach((worksheetId) => {
705
- const autoFilter = json[worksheetId];
706
- const filterModel = FilterModel.deserialize(unitId, worksheetId, workbook.getSheetBySheetId(worksheetId), autoFilter);
707
- this._cacheFilterModel(unitId, worksheetId, filterModel);
708
- });
709
- }
710
- dispose() {
711
- super.dispose();
712
- this._loadedUnitId$.complete();
713
- this._errorMsg$.complete();
714
- this._activeFilterModel$.complete();
715
- this._filterModels.forEach((allFilterModels) => {
716
- allFilterModels.forEach((model) => model.dispose());
717
- allFilterModels.clear();
718
- });
719
- this._filterModels.clear();
720
- }
721
- _initModel() {
722
- this._resourcesManagerService.registerPluginResource({
723
- pluginName: SHEET_FILTER_SNAPSHOT_ID,
724
- businesses: [UniverInstanceType.UNIVER_SHEET],
725
- toJson: (id) => this._serializeAutoFiltersForUnit(id),
726
- parseJson: (json) => JSON.parse(json),
727
- onLoad: (unitId, value) => {
728
- this._deserializeAutoFiltersForUnit(unitId, value);
729
- this._loadedUnitId$.next(unitId);
730
- this._updateActiveFilterModel();
731
- },
732
- onUnLoad: (unitId) => {
733
- const allFilterModels = this._filterModels.get(unitId);
734
- if (allFilterModels) {
735
- allFilterModels.forEach((model) => model.dispose());
736
- this._filterModels.delete(unitId);
737
- }
738
- }
739
- });
740
- }
741
- _cacheFilterModel(unitId, subUnitId, filterModel) {
742
- if (!this._filterModels.has(unitId)) this._filterModels.set(unitId, /* @__PURE__ */ new Map());
743
- this._filterModels.get(unitId).set(subUnitId, filterModel);
744
- }
745
- };
746
- SheetsFilterService = __decorate([
747
- __decorateParam(0, IResourceManagerService),
748
- __decorateParam(1, IUniverInstanceService),
749
- __decorateParam(2, ICommandService)
750
- ], SheetsFilterService);
751
-
752
- //#endregion
753
- //#region src/commands/mutations/sheets-filter.mutation.ts
754
- /**
755
- * A {@link CommandType.MUTATION} to set filter range in a {@link Worksheet}. If no {@link FilterModel} exists,
756
- * a new `FilterModel` will be created.
757
- *
758
- * Since there could only be a filter on a worksheet, when you want to update the range, you
759
- * don't necessarily need to remove the filter first, you can just execute this mutation.
760
- */
761
- const SetSheetsFilterRangeMutation = {
762
- id: SetSheetsFilterRangeMutationId,
763
- type: CommandType.MUTATION,
764
- handler: (accessor, params) => {
765
- const { subUnitId, unitId, range } = params;
766
- const filterModel = accessor.get(SheetsFilterService).ensureFilterModel(unitId, subUnitId);
767
- filterModel.setRange(range);
768
- params.filteredOutRows = Array.from(filterModel.filteredOutRows);
769
- return true;
770
- }
771
- };
772
- /**
773
- * A {@link CommandType.MUTATION} to set filter criteria of a given column of a {@link FilterModel}.
774
- */
775
- const SetSheetsFilterCriteriaMutation = {
776
- id: SetSheetsFilterCriteriaMutationId,
777
- type: CommandType.MUTATION,
778
- handler: (accessor, params) => {
779
- const { subUnitId, unitId, criteria, col, reCalc = true } = params;
780
- const filterModel = accessor.get(SheetsFilterService).getFilterModel(unitId, subUnitId);
781
- if (!filterModel) return false;
782
- filterModel.setCriteria(col, criteria, reCalc);
783
- params.filteredOutRows = Array.from(filterModel.filteredOutRows);
784
- return true;
785
- }
786
- };
787
- /**
788
- * A {@link CommandType.MUTATION} to remove a {@link FilterModel} in a {@link Worksheet}.
789
- */
790
- const RemoveSheetsFilterMutation = {
791
- id: RemoveSheetsFilterMutationId,
792
- type: CommandType.MUTATION,
793
- handler: (accessor, params) => {
794
- const { unitId, subUnitId } = params;
795
- const removed = accessor.get(SheetsFilterService).removeFilterModel(unitId, subUnitId);
796
- if (removed) params.filteredOutRows = [];
797
- return removed;
798
- }
799
- };
800
- /**
801
- * A {@link CommandType.MUTATION} to re-calculate a {@link FilterModel}.
802
- */
803
- const ReCalcSheetsFilterMutation = {
804
- id: ReCalcSheetsFilterMutationId,
805
- type: CommandType.MUTATION,
806
- handler: (accessor, params) => {
807
- const { unitId, subUnitId } = params;
808
- const filterModel = accessor.get(SheetsFilterService).getFilterModel(unitId, subUnitId);
809
- if (!filterModel) return false;
810
- filterModel.reCalc();
811
- params.filteredOutRows = Array.from(filterModel.filteredOutRows);
812
- return true;
813
- }
814
- };
815
-
816
- //#endregion
817
- //#region src/commands/commands/sheets-filter.command.ts
818
- /**
819
- * A {@link CommandType.COMMAND} to set filter range in a Worksheet. Its params {@link ISetSheetFilterRangeCommandParams}
820
- * is required. If the {@link FilterModel} does not exist, it will be created.
821
- */
822
- const SetSheetFilterRangeCommand = {
823
- id: "sheet.command.set-filter-range",
824
- type: CommandType.COMMAND,
825
- handler: (accessor, params) => {
826
- const sheetsFilterService = accessor.get(SheetsFilterService);
827
- const commandService = accessor.get(ICommandService);
828
- const undoRedoService = accessor.get(IUndoRedoService);
829
- const instanceSrv = accessor.get(IUniverInstanceService);
830
- const { unitId, subUnitId, range } = params;
831
- if (!getSheetCommandTarget(instanceSrv, params)) return false;
832
- if (sheetsFilterService.getFilterModel(unitId, subUnitId)) return false;
833
- if (range.endRow === range.startRow) {
834
- const errorService = accessor.get(ErrorService);
835
- const localeService = accessor.get(LocaleService);
836
- errorService.emit(localeService.t("sheets-filter.command.not-valid-filter-range"));
837
- return false;
838
- }
839
- const redoMutation = {
840
- id: SetSheetsFilterRangeMutation.id,
841
- params: {
842
- unitId,
843
- subUnitId,
844
- range
845
- }
846
- };
847
- const result = commandService.syncExecuteCommand(redoMutation.id, redoMutation.params);
848
- if (result) undoRedoService.pushUndoRedo({
849
- unitID: unitId,
850
- undoMutations: [{
851
- id: RemoveSheetsFilterMutation.id,
852
- params: {
853
- unitId,
854
- subUnitId
855
- }
856
- }],
857
- redoMutations: [redoMutation]
858
- });
859
- return result;
860
- }
861
- };
862
- /**
863
- * A {@link CommandType.COMMAND} to remove filter in a Worksheet. Its params {@link ISheetCommandSharedParams} is
864
- * required. If the {@link FilterModel} does not exist, it will fail to execute.
865
- */
866
- const RemoveSheetFilterCommand = {
867
- id: "sheet.command.remove-sheet-filter",
868
- type: CommandType.COMMAND,
869
- handler: (accessor, params) => {
870
- const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
871
- if (!target) return false;
872
- const sheetsFilterService = accessor.get(SheetsFilterService);
873
- const { unitId, subUnitId } = target;
874
- const filterModel = sheetsFilterService.getFilterModel(unitId, subUnitId);
875
- if (!filterModel) return false;
876
- const filterRange = filterModel.getRange();
877
- if (!filterRange) return false;
878
- const commandService = accessor.get(ICommandService);
879
- const undoRedoService = accessor.get(IUndoRedoService);
880
- const undoMutations = destructFilterModel(unitId, subUnitId, filterModel.serialize());
881
- const result = commandService.syncExecuteCommand(RemoveSheetsFilterMutation.id, {
882
- unitId,
883
- subUnitId
884
- });
885
- if (result) {
886
- undoRedoService.pushUndoRedo({
887
- unitID: unitId,
888
- undoMutations,
889
- redoMutations: [{
890
- id: RemoveSheetsFilterMutation.id,
891
- params: {
892
- unitId,
893
- subUnitId
894
- }
895
- }]
896
- });
897
- commandService.executeCommand(MarkDirtyFilterChangeMutation.id, {
898
- unitId,
899
- subUnitId,
900
- filterRange
901
- });
902
- }
903
- return result;
904
- }
905
- };
906
- /**
907
- * A {@link CommandType.COMMAND} to toggle filter in the current {@link Worksheet}.
908
- */
909
- const SmartToggleSheetsFilterCommand = {
910
- id: "sheet.command.smart-toggle-filter",
911
- type: CommandType.COMMAND,
912
- handler: async (accessor) => {
913
- const univerInstanceService = accessor.get(IUniverInstanceService);
914
- const sheetsFilterService = accessor.get(SheetsFilterService);
915
- const commandService = accessor.get(ICommandService);
916
- const currentWorkbook = univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_SHEET);
917
- const currentWorksheet = currentWorkbook === null || currentWorkbook === void 0 ? void 0 : currentWorkbook.getActiveSheet();
918
- if (!currentWorksheet || !currentWorkbook) return false;
919
- const unitId = currentWorkbook.getUnitId();
920
- const subUnitId = currentWorksheet.getSheetId();
921
- if (sheetsFilterService.getFilterModel(unitId, subUnitId)) return commandService.executeCommand(RemoveSheetFilterCommand.id, {
922
- unitId,
923
- subUnitId
924
- });
925
- const lastSelection = accessor.get(SheetsSelectionsService).getCurrentLastSelection();
926
- if (!lastSelection) return false;
927
- const startRange = lastSelection.range;
928
- const targetFilterRange = isSingleCellSelection(lastSelection) ? expandToContinuousRange(startRange, {
929
- left: true,
930
- right: true,
931
- up: true,
932
- down: true
933
- }, currentWorksheet) : startRange.startRow === startRange.endRow ? expandToContinuousRange(startRange, { down: true }, currentWorksheet) : startRange;
934
- return commandService.executeCommand(SetSheetFilterRangeCommand.id, {
935
- unitId,
936
- subUnitId,
937
- range: targetFilterRange
938
- });
939
- }
940
- };
941
- /**
942
- * A {@link CommandType.COMMAND} to set filter criteria to a column in the targeting {@link FilterModel}. Its params
943
- * {@link ISetSheetsFilterCriteriaCommandParams} is required.
944
- */
945
- const SetSheetsFilterCriteriaCommand = {
946
- id: "sheet.command.set-filter-criteria",
947
- type: CommandType.COMMAND,
948
- handler: (accessor, params) => {
949
- const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
950
- if (!target) return false;
951
- const sheetsFilterService = accessor.get(SheetsFilterService);
952
- const { unitId, subUnitId } = target;
953
- const filterModel = sheetsFilterService.getFilterModel(unitId, subUnitId);
954
- if (!filterModel) return false;
955
- const { col, criteria } = params;
956
- const filterRange = filterModel.getRange();
957
- if (!filterRange || col < filterRange.startColumn || col > filterRange.endColumn) return false;
958
- const commandService = accessor.get(ICommandService);
959
- const undoRedoService = accessor.get(IUndoRedoService);
960
- const undoMutation = destructFilterColumn(unitId, subUnitId, col, filterModel.getFilterColumn(col));
961
- const redoMutation = {
962
- id: SetSheetsFilterCriteriaMutation.id,
963
- params: {
964
- unitId,
965
- subUnitId,
966
- col,
967
- criteria
968
- }
969
- };
970
- const result = commandService.syncExecuteCommand(redoMutation.id, redoMutation.params);
971
- if (result) {
972
- undoRedoService.pushUndoRedo({
973
- unitID: unitId,
974
- undoMutations: [undoMutation],
975
- redoMutations: [redoMutation]
976
- });
977
- commandService.executeCommand(MarkDirtyFilterChangeMutation.id, {
978
- unitId,
979
- subUnitId,
980
- filterRange
981
- });
982
- }
983
- return result;
984
- }
985
- };
986
- /**
987
- * A {@link CommandType.COMMAND} to clear all filter criteria in the targeting {@link FilterModel}. Its params
988
- * {@link ISheetCommandSharedParams} is required.
989
- */
990
- const ClearSheetsFilterCriteriaCommand = {
991
- id: "sheet.command.clear-filter-criteria",
992
- type: CommandType.COMMAND,
993
- handler: (accessor, params) => {
994
- const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
995
- if (!target) return false;
996
- const sheetsFilterService = accessor.get(SheetsFilterService);
997
- const { unitId, subUnitId } = target;
998
- const filterModel = sheetsFilterService.getFilterModel(unitId, subUnitId);
999
- if (!filterModel) return false;
1000
- const filterRange = filterModel.getRange();
1001
- if (!filterRange) return false;
1002
- const undoRedoService = accessor.get(IUndoRedoService);
1003
- const commandService = accessor.get(ICommandService);
1004
- const autoFilter = filterModel.serialize();
1005
- const undoMutations = destructFilterCriteria(unitId, subUnitId, autoFilter);
1006
- const redoMutations = generateRemoveCriteriaMutations(unitId, subUnitId, autoFilter);
1007
- if (sequenceExecute(redoMutations, commandService).result) {
1008
- undoRedoService.pushUndoRedo({
1009
- unitID: unitId,
1010
- undoMutations,
1011
- redoMutations
1012
- });
1013
- commandService.executeCommand(MarkDirtyFilterChangeMutation.id, {
1014
- unitId,
1015
- subUnitId,
1016
- filterRange
1017
- });
1018
- return true;
1019
- }
1020
- return false;
1021
- }
1022
- };
1023
- /**
1024
- * A {@link CommandType.COMMAND} forcing the currently active {@link FilterModel} to re-calculate all filter criteria.
1025
- * Its params {@link ISheetCommandSharedParams} is required.
1026
- */
1027
- const ReCalcSheetsFilterCommand = {
1028
- id: "sheet.command.re-calc-filter",
1029
- type: CommandType.COMMAND,
1030
- handler: (accessor, params) => {
1031
- const sheetsFilterService = accessor.get(SheetsFilterService);
1032
- const commandService = accessor.get(ICommandService);
1033
- const instanceSrv = accessor.get(IUniverInstanceService);
1034
- const commandTarget = getSheetCommandTarget(instanceSrv, params);
1035
- if (!commandTarget) return false;
1036
- const { unitId, subUnitId } = commandTarget;
1037
- if (!sheetsFilterService.getFilterModel(commandTarget.unitId, commandTarget.subUnitId)) return false;
1038
- return commandService.executeCommand(ReCalcSheetsFilterMutation.id, {
1039
- unitId,
1040
- subUnitId
1041
- });
1042
- }
1043
- };
1044
- /**
1045
- * Destruct a `FilterModel` to a list of mutations.
1046
- * @param {string} unitId - the unit id of the Workbook
1047
- * @param {string} subUnitId - the sub unit id of the Worksheet
1048
- * @param {IAutoFilter} autoFilter - the to be destructed FilterModel
1049
- * @returns a list of mutations those can be used to reconstruct the FilterModel
1050
- */
1051
- function destructFilterModel(unitId, subUnitId, autoFilter) {
1052
- const mutations = [];
1053
- const setFilterMutation = {
1054
- id: SetSheetsFilterRangeMutation.id,
1055
- params: {
1056
- unitId,
1057
- subUnitId,
1058
- range: autoFilter.ref
1059
- }
1060
- };
1061
- mutations.push(setFilterMutation);
1062
- destructFilterCriteria(unitId, subUnitId, autoFilter).forEach((m) => mutations.push(m));
1063
- return mutations;
1064
- }
1065
- /**
1066
- * Transform a {@link FilterModel} to a list of mutations to set the filter criteria.
1067
- * @param unitId - the unit id of the {@link Workbook}
1068
- * @param subUnitId - the sub unit id of the {@link Worksheet}
1069
- * @param autoFilter - the to be destructed {@link FilterModel}
1070
- * @returns {IMutationInfo<ISetSheetsFilterCriteriaMutationParams>} a list of mutations those can be used to
1071
- * reconstruct the {@link FilterModel}
1072
- */
1073
- function destructFilterCriteria(unitId, subUnitId, autoFilter) {
1074
- var _autoFilter$filterCol;
1075
- const mutations = [];
1076
- (_autoFilter$filterCol = autoFilter.filterColumns) === null || _autoFilter$filterCol === void 0 || _autoFilter$filterCol.forEach((filterColumn) => {
1077
- const setFilterCriteriaMutation = {
1078
- id: SetSheetsFilterCriteriaMutation.id,
1079
- params: {
1080
- unitId,
1081
- subUnitId,
1082
- col: filterColumn.colId,
1083
- criteria: filterColumn
1084
- }
1085
- };
1086
- mutations.push(setFilterCriteriaMutation);
1087
- });
1088
- return mutations;
1089
- }
1090
- /** Generate mutations to remove all criteria on a `FilterModel` */
1091
- function generateRemoveCriteriaMutations(unitId, subUnitId, autoFilter) {
1092
- var _autoFilter$filterCol2;
1093
- const mutations = [];
1094
- (_autoFilter$filterCol2 = autoFilter.filterColumns) === null || _autoFilter$filterCol2 === void 0 || _autoFilter$filterCol2.forEach((filterColumn) => {
1095
- const removeFilterCriteriaMutation = {
1096
- id: SetSheetsFilterCriteriaMutation.id,
1097
- params: {
1098
- unitId,
1099
- subUnitId,
1100
- col: filterColumn.colId,
1101
- criteria: null
1102
- }
1103
- };
1104
- mutations.push(removeFilterCriteriaMutation);
1105
- });
1106
- return mutations;
1107
- }
1108
- /**
1109
- * Prepare the undo mutation, it should rollback to the old criteria if there's already a `FilterColumn`,
1110
- * or remove the filter criteria when there is no `FilterColumn`.
1111
- * @param {string} unitId
1112
- * @param {string} subUnitId
1113
- * @param {number} colId
1114
- * @param {Nullable<FilterColumn>} filterColumn
1115
- * @returns {IMutationInfo<ISetSheetsFilterCriteriaMutationParams>} the undo mutation
1116
- */
1117
- function destructFilterColumn(unitId, subUnitId, colId, filterColumn) {
1118
- if (!filterColumn) return {
1119
- id: SetSheetsFilterCriteriaMutation.id,
1120
- params: {
1121
- unitId,
1122
- subUnitId,
1123
- col: colId,
1124
- criteria: null
1125
- }
1126
- };
1127
- const serialize = filterColumn.serialize();
1128
- return {
1129
- id: SetSheetsFilterCriteriaMutation.id,
1130
- params: {
1131
- unitId,
1132
- subUnitId,
1133
- col: colId,
1134
- criteria: serialize
1135
- }
1136
- };
1137
- }
1138
-
1139
- //#endregion
1140
- //#region src/config/config.ts
1141
- /**
1142
- * Copyright 2023-present DreamNum Co., Ltd.
1143
- *
1144
- * Licensed under the Apache License, Version 2.0 (the "License");
1145
- * you may not use this file except in compliance with the License.
1146
- * You may obtain a copy of the License at
1147
- *
1148
- * http://www.apache.org/licenses/LICENSE-2.0
1149
- *
1150
- * Unless required by applicable law or agreed to in writing, software
1151
- * distributed under the License is distributed on an "AS IS" BASIS,
1152
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1153
- * See the License for the specific language governing permissions and
1154
- * limitations under the License.
1155
- */
1156
- const SHEETS_FILTER_PLUGIN_CONFIG_KEY = "sheets-filter.config";
1157
- const configSymbol = Symbol(SHEETS_FILTER_PLUGIN_CONFIG_KEY);
1158
- const defaultPluginConfig = {};
1159
-
1160
- //#endregion
1161
- //#region src/utils.ts
1162
- function objectsShaker(target, isEqual) {
1163
- for (let i = 0; i < target.length; i++) {
1164
- let cur = i;
1165
- if (target[i]) {
1166
- for (let j = i + 1; j < target.length; j++) if (target[cur] && target[j] && isEqual(target[cur], target[j])) {
1167
- target[cur] = null;
1168
- cur = j;
1169
- }
1170
- }
1171
- }
1172
- return target.filter((o) => o !== null);
1173
- }
1174
- function mergeSetFilterCriteria(mutations) {
1175
- return objectsShaker(mutations, (o1, o2) => o1.id === SetSheetsFilterCriteriaMutation.id && o2.id === SetSheetsFilterCriteriaMutation.id && o1.params.unitId === o2.params.unitId && o1.params.subUnitId === o2.params.subUnitId && o1.params.col === o2.params.col);
1176
- }
1177
-
1178
- //#endregion
1179
- //#region src/controllers/sheets-filter.controller.ts
1180
- let SheetsFilterController = class SheetsFilterController extends Disposable {
1181
- constructor(_commandService, _sheetInterceptorService, _sheetsFilterService, _univerInstanceService, _refRangeService, _dataSyncPrimaryController, _zebraCrossingCacheController) {
1182
- super();
1183
- this._commandService = _commandService;
1184
- this._sheetInterceptorService = _sheetInterceptorService;
1185
- this._sheetsFilterService = _sheetsFilterService;
1186
- this._univerInstanceService = _univerInstanceService;
1187
- this._refRangeService = _refRangeService;
1188
- this._dataSyncPrimaryController = _dataSyncPrimaryController;
1189
- this._zebraCrossingCacheController = _zebraCrossingCacheController;
1190
- _defineProperty(this, "_disposableCollection", new DisposableCollection());
1191
- this._initCommands();
1192
- this._initRowFilteredInterceptor();
1193
- this._initInterceptors();
1194
- this._commandExecutedListener();
1195
- this._initErrorHandling();
1196
- this._initZebraCrossingCacheListener();
1197
- }
1198
- _initZebraCrossingCacheListener() {
1199
- this.disposeWithMe(this._sheetsFilterService.activeFilterModel$.subscribe((filterModel) => {
1200
- if (!filterModel) return;
1201
- this.disposeWithMe(filterModel.filteredOutRows$.subscribe(() => {
1202
- this._zebraCrossingCacheController.updateZebraCrossingCache(filterModel.unitId, filterModel.subUnitId);
1203
- }));
1204
- }));
1205
- }
1206
- _initCommands() {
1207
- [
1208
- SetSheetFilterRangeCommand,
1209
- RemoveSheetFilterCommand,
1210
- SetSheetsFilterCriteriaCommand,
1211
- ClearSheetsFilterCriteriaCommand,
1212
- ReCalcSheetsFilterCommand
1213
- ].forEach((command) => {
1214
- this.disposeWithMe(this._commandService.registerCommand(command));
1215
- });
1216
- [
1217
- SetSheetsFilterCriteriaMutation,
1218
- SetSheetsFilterRangeMutation,
1219
- ReCalcSheetsFilterMutation,
1220
- RemoveSheetsFilterMutation
1221
- ].forEach((command) => {
1222
- var _this$_dataSyncPrimar;
1223
- this.disposeWithMe(this._commandService.registerCommand(command));
1224
- (_this$_dataSyncPrimar = this._dataSyncPrimaryController) === null || _this$_dataSyncPrimar === void 0 || _this$_dataSyncPrimar.registerSyncingMutations(command);
1225
- });
1226
- }
1227
- _initInterceptors() {
1228
- this.disposeWithMe(this._sheetInterceptorService.interceptCommand({ getMutations: (command) => this._getUpdateFilter(command) }));
1229
- this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo) => {
1230
- if (commandInfo.id === SetWorksheetActiveOperation.id) {
1231
- const params = commandInfo.params;
1232
- const sheetId = params.subUnitId;
1233
- const unitId = params.unitId;
1234
- if (!sheetId || !unitId) return;
1235
- this._registerRefRange(unitId, sheetId);
1236
- }
1237
- if (commandInfo.id === SetSheetsFilterRangeMutation.id) {
1238
- const params = commandInfo.params;
1239
- const sheetId = params.subUnitId;
1240
- const unitId = params.unitId;
1241
- if (!sheetId || !unitId) return;
1242
- this._registerRefRange(params.unitId, params.subUnitId);
1243
- }
1244
- }));
1245
- this.disposeWithMe(this._sheetsFilterService.loadedUnitId$.subscribe((unitId) => {
1246
- if (unitId) {
1247
- const workbook = this._univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_SHEET);
1248
- const sheet = workbook === null || workbook === void 0 ? void 0 : workbook.getActiveSheet();
1249
- if (sheet) this._registerRefRange(unitId, sheet.getSheetId());
1250
- }
1251
- }));
1252
- }
1253
- _registerRefRange(unitId, subUnitId) {
1254
- var _this$_sheetsFilterSe;
1255
- this._disposableCollection.dispose();
1256
- const workbook = this._univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_SHEET);
1257
- const workSheet = workbook === null || workbook === void 0 ? void 0 : workbook.getSheetBySheetId(subUnitId);
1258
- if (!workbook || !workSheet) return;
1259
- const range = (_this$_sheetsFilterSe = this._sheetsFilterService.getFilterModel(unitId, subUnitId)) === null || _this$_sheetsFilterSe === void 0 ? void 0 : _this$_sheetsFilterSe.getRange();
1260
- const handler = (config) => {
1261
- switch (config.id) {
1262
- case InsertRowCommand.id: {
1263
- const params = config.params;
1264
- const _unitId = params.unitId || unitId;
1265
- const _subUnitId = params.subUnitId || subUnitId;
1266
- return this._handleInsertRowCommand(params, _unitId, _subUnitId);
1267
- }
1268
- case InsertColCommand.id: {
1269
- const params = config.params;
1270
- const _unitId = params.unitId || unitId;
1271
- const _subUnitId = params.subUnitId || subUnitId;
1272
- return this.handleInsertColCommand(params.range, _unitId, _subUnitId);
1273
- }
1274
- case RemoveColCommand.id: {
1275
- const params = config.params;
1276
- return this.handleRemoveColCommand(params.range, unitId, subUnitId);
1277
- }
1278
- case RemoveRowCommand.id: {
1279
- const params = config.params;
1280
- return this._handleRemoveRowCommand(params, unitId, subUnitId);
1281
- }
1282
- case EffectRefRangId.MoveColsCommandId: {
1283
- const params = config.params;
1284
- return this.handleMoveColsCommand({
1285
- fromRange: params.fromRange,
1286
- toRange: params.toRange
1287
- }, unitId, subUnitId);
1288
- }
1289
- case EffectRefRangId.MoveRowsCommandId: {
1290
- const params = config.params;
1291
- return this._handleMoveRowsCommand(params, unitId, subUnitId);
1292
- }
1293
- case MoveRangeCommand.id: {
1294
- const params = config.params;
1295
- return this._handleMoveRangeCommand(params, unitId, subUnitId);
1296
- }
1297
- }
1298
- return {
1299
- redos: [],
1300
- undos: []
1301
- };
1302
- };
1303
- if (range) this._disposableCollection.add(this._refRangeService.registerRefRange(range, handler, unitId, subUnitId));
1304
- }
1305
- _getUpdateFilter(command) {
1306
- const { id } = command;
1307
- switch (id) {
1308
- case RemoveSheetCommand.id: {
1309
- const params = command.params;
1310
- return this._handleRemoveSheetCommand(params, params.unitId, params.subUnitId);
1311
- }
1312
- case CopySheetCommand.id: {
1313
- const { targetSubUnitId, unitId, subUnitId } = command.params;
1314
- if (!unitId || !subUnitId || !targetSubUnitId) return this._handleNull();
1315
- return this._handleCopySheetCommand(unitId, subUnitId, targetSubUnitId);
1316
- }
1317
- }
1318
- return {
1319
- redos: [],
1320
- undos: []
1321
- };
1322
- }
1323
- handleInsertColCommand(range, unitId, subUnitId) {
1324
- const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
1325
- const filterRange = (filterModel === null || filterModel === void 0 ? void 0 : filterModel.getRange()) ?? null;
1326
- if (!filterModel || !filterRange) return this._handleNull();
1327
- const { startColumn, endColumn } = filterRange;
1328
- const { startColumn: insertStartColumn, endColumn: insertEndColumn } = range;
1329
- const count = insertEndColumn - insertStartColumn + 1;
1330
- if (insertEndColumn > endColumn) return this._handleNull();
1331
- const redos = [];
1332
- const undos = [];
1333
- const anchor = insertStartColumn;
1334
- const setFilterRangeMutationParams = {
1335
- unitId,
1336
- subUnitId,
1337
- range: {
1338
- ...filterRange,
1339
- startColumn: insertStartColumn <= startColumn ? startColumn + count : startColumn,
1340
- endColumn: endColumn + count
1341
- }
1342
- };
1343
- const undoSetFilterRangeMutationParams = {
1344
- unitId,
1345
- subUnitId,
1346
- range: filterRange
1347
- };
1348
- redos.push({
1349
- id: SetSheetsFilterRangeMutation.id,
1350
- params: setFilterRangeMutationParams
1351
- });
1352
- undos.push({
1353
- id: SetSheetsFilterRangeMutation.id,
1354
- params: undoSetFilterRangeMutationParams
1355
- });
1356
- const effected = filterModel.getAllFilterColumns().filter((column) => column[0] >= anchor);
1357
- if (effected.length !== 0) {
1358
- const { newRange, oldRange } = this._moveCriteria(unitId, subUnitId, effected, count);
1359
- redos.push(...oldRange.redos, ...newRange.redos);
1360
- undos.push(...newRange.undos, ...oldRange.undos);
1361
- }
1362
- return {
1363
- redos: mergeSetFilterCriteria(redos),
1364
- undos: mergeSetFilterCriteria(undos)
1365
- };
1366
- }
1367
- _handleInsertRowCommand(config, unitId, subUnitId) {
1368
- const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
1369
- const filterRange = (filterModel === null || filterModel === void 0 ? void 0 : filterModel.getRange()) ?? null;
1370
- if (!filterModel || !filterRange) return this._handleNull();
1371
- const { startRow, endRow } = filterRange;
1372
- const { startRow: insertStartRow, endRow: insertEndRow } = config.range;
1373
- const rowCount = insertEndRow - insertStartRow + 1;
1374
- if (insertEndRow > endRow) return this._handleNull();
1375
- const redos = [];
1376
- const undos = [];
1377
- const setFilterRangeParams = {
1378
- unitId,
1379
- subUnitId,
1380
- range: {
1381
- ...filterRange,
1382
- startRow: insertStartRow <= startRow ? startRow + rowCount : startRow,
1383
- endRow: endRow + rowCount
1384
- }
1385
- };
1386
- const undoSetFilterRangeMutationParams = {
1387
- unitId,
1388
- subUnitId,
1389
- range: filterRange
1390
- };
1391
- redos.push({
1392
- id: SetSheetsFilterRangeMutation.id,
1393
- params: setFilterRangeParams
1394
- });
1395
- undos.push({
1396
- id: SetSheetsFilterRangeMutation.id,
1397
- params: undoSetFilterRangeMutationParams
1398
- });
1399
- return {
1400
- redos: mergeSetFilterCriteria(redos),
1401
- undos: mergeSetFilterCriteria(undos)
1402
- };
1403
- }
1404
- handleRemoveColCommand(range, unitId, subUnitId) {
1405
- const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
1406
- const filterRange = (filterModel === null || filterModel === void 0 ? void 0 : filterModel.getRange()) ?? null;
1407
- if (!filterModel || !filterRange) return this._handleNull();
1408
- const { startColumn, endColumn } = filterRange;
1409
- const { startColumn: removeStartColumn, endColumn: removeEndColumn } = range;
1410
- if (removeStartColumn > endColumn) return this._handleNull();
1411
- const redos = [];
1412
- const undos = [];
1413
- const rangeRemoveCount = removeEndColumn < startColumn ? 0 : Math.min(removeEndColumn, endColumn) - Math.max(removeStartColumn, startColumn) + 1;
1414
- const removeCount = removeEndColumn - removeStartColumn + 1;
1415
- const filterColumn = filterModel.getAllFilterColumns();
1416
- filterColumn.forEach((column) => {
1417
- const [col, filter] = column;
1418
- if (col <= removeEndColumn && col >= removeStartColumn) {
1419
- redos.push({
1420
- id: SetSheetsFilterCriteriaMutation.id,
1421
- params: {
1422
- unitId,
1423
- subUnitId,
1424
- col,
1425
- criteria: null
1426
- }
1427
- });
1428
- undos.push({
1429
- id: SetSheetsFilterCriteriaMutation.id,
1430
- params: {
1431
- unitId,
1432
- subUnitId,
1433
- col,
1434
- criteria: {
1435
- ...filter.serialize(),
1436
- colId: col
1437
- }
1438
- }
1439
- });
1440
- }
1441
- });
1442
- const shifted = filterColumn.filter((column) => {
1443
- const [col, _] = column;
1444
- return col > removeEndColumn;
1445
- });
1446
- let newRangeCriteria = {
1447
- undos: [],
1448
- redos: []
1449
- };
1450
- if (shifted.length > 0) {
1451
- const { oldRange, newRange } = this._moveCriteria(unitId, subUnitId, shifted, -removeCount);
1452
- newRangeCriteria = newRange;
1453
- redos.push(...oldRange.redos);
1454
- undos.unshift(...oldRange.undos);
1455
- }
1456
- if (rangeRemoveCount === endColumn - startColumn + 1) {
1457
- const removeFilterRangeMutationParams = {
1458
- unitId,
1459
- subUnitId
1460
- };
1461
- redos.push({
1462
- id: RemoveSheetsFilterMutation.id,
1463
- params: removeFilterRangeMutationParams
1464
- });
1465
- undos.unshift({
1466
- id: SetSheetsFilterRangeMutation.id,
1467
- params: {
1468
- range: filterRange,
1469
- unitId,
1470
- subUnitId
1471
- }
1472
- });
1473
- } else {
1474
- const newStartColumn = startColumn <= removeStartColumn ? startColumn : rangeRemoveCount === 0 ? startColumn - removeCount : removeStartColumn;
1475
- const newEndColumn = startColumn <= removeStartColumn ? endColumn - rangeRemoveCount : endColumn - removeCount;
1476
- const setFilterRangeMutationParams = {
1477
- unitId,
1478
- subUnitId,
1479
- range: {
1480
- ...filterRange,
1481
- startColumn: newStartColumn,
1482
- endColumn: newEndColumn
1483
- }
1484
- };
1485
- redos.push({
1486
- id: SetSheetsFilterRangeMutation.id,
1487
- params: setFilterRangeMutationParams
1488
- });
1489
- undos.unshift({
1490
- id: SetSheetsFilterRangeMutation.id,
1491
- params: {
1492
- range: filterRange,
1493
- unitId,
1494
- subUnitId
1495
- }
1496
- });
1497
- redos.push(...newRangeCriteria.redos);
1498
- undos.unshift(...newRangeCriteria.undos);
1499
- }
1500
- return {
1501
- undos,
1502
- redos
1503
- };
1504
- }
1505
- _handleRemoveRowCommand(config, unitId, subUnitId) {
1506
- const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
1507
- if (!filterModel) return this._handleNull();
1508
- const filterRange = filterModel.getRange();
1509
- const { startRow, endRow } = filterRange;
1510
- const { startRow: removeStartRow, endRow: removeEndRow } = config.range;
1511
- if (removeStartRow > endRow) return this._handleNull();
1512
- if (removeEndRow < startRow) return {
1513
- undos: [{
1514
- id: SetSheetsFilterRangeMutation.id,
1515
- params: {
1516
- range: filterRange,
1517
- unitId,
1518
- subUnitId
1519
- }
1520
- }],
1521
- redos: [{
1522
- id: SetSheetsFilterRangeMutation.id,
1523
- params: {
1524
- range: {
1525
- ...filterRange,
1526
- startRow: startRow - (removeEndRow - removeStartRow + 1),
1527
- endRow: endRow - (removeEndRow - removeStartRow + 1)
1528
- },
1529
- unitId,
1530
- subUnitId
1531
- }
1532
- }]
1533
- };
1534
- const redos = [];
1535
- const undos = [];
1536
- const filterColumn = filterModel.getAllFilterColumns();
1537
- const filterHeaderIsRemoved = startRow <= removeEndRow && startRow >= removeStartRow;
1538
- undos.push({
1539
- id: SetSheetsFilterRangeMutation.id,
1540
- params: {
1541
- range: filterRange,
1542
- unitId,
1543
- subUnitId
1544
- }
1545
- });
1546
- const count = Math.min(removeEndRow, endRow) - Math.max(removeStartRow, startRow) + 1;
1547
- if (count === endRow - startRow + 1 || filterHeaderIsRemoved) {
1548
- const removeFilterRangeMutationParams = {
1549
- unitId,
1550
- subUnitId
1551
- };
1552
- redos.push({
1553
- id: RemoveSheetsFilterMutation.id,
1554
- params: removeFilterRangeMutationParams
1555
- });
1556
- filterColumn.forEach((column) => {
1557
- const [offset, filter] = column;
1558
- const setCriteriaMutationParams = {
1559
- unitId,
1560
- subUnitId,
1561
- col: offset,
1562
- criteria: {
1563
- ...filter.serialize(),
1564
- colId: offset
1565
- }
1566
- };
1567
- undos.push({
1568
- id: SetSheetsFilterCriteriaMutation.id,
1569
- params: setCriteriaMutationParams
1570
- });
1571
- });
1572
- } else {
1573
- var _this$_univerInstance;
1574
- const worksheet = (_this$_univerInstance = this._univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_SHEET)) === null || _this$_univerInstance === void 0 ? void 0 : _this$_univerInstance.getSheetBySheetId(subUnitId);
1575
- if (!worksheet) return this._handleNull();
1576
- const hiddenRowCount = this._getFilteredRowCount(worksheet, removeStartRow, removeEndRow);
1577
- const afterStartRow = Math.min(startRow, removeStartRow);
1578
- const afterEndRow = afterStartRow + (endRow - startRow) - count + hiddenRowCount;
1579
- const setFilterRangeMutationParams = {
1580
- unitId,
1581
- subUnitId,
1582
- range: {
1583
- ...filterRange,
1584
- startRow: afterStartRow,
1585
- endRow: afterEndRow
1586
- }
1587
- };
1588
- redos.push({
1589
- id: SetSheetsFilterRangeMutation.id,
1590
- params: setFilterRangeMutationParams
1591
- });
1592
- }
1593
- return {
1594
- undos: mergeSetFilterCriteria(undos),
1595
- redos: mergeSetFilterCriteria(redos)
1596
- };
1597
- }
1598
- _getFilteredRowCount(worksheet, startRow, endRow) {
1599
- let count = 0;
1600
- for (let row = startRow; row <= endRow; row++) if (worksheet.getRowFiltered(row)) count++;
1601
- return count;
1602
- }
1603
- handleMoveColsCommand({ fromRange, toRange }, unitId, subUnitId) {
1604
- const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
1605
- const filterRange = (filterModel === null || filterModel === void 0 ? void 0 : filterModel.getRange()) ?? null;
1606
- if (!filterModel || !filterRange) return this._handleNull();
1607
- const { startColumn, endColumn } = filterRange;
1608
- if (fromRange.endColumn < startColumn && toRange.startColumn <= startColumn || fromRange.startColumn > endColumn && toRange.endColumn > endColumn) return this._handleNull();
1609
- const redos = [];
1610
- const undos = [];
1611
- const filterCol = {};
1612
- for (let col = startColumn; col <= endColumn; col++) filterCol[col] = {
1613
- colIndex: col,
1614
- filter: filterModel.getFilterColumn(col)
1615
- };
1616
- moveMatrixArray(fromRange.startColumn, fromRange.endColumn - fromRange.startColumn + 1, toRange.startColumn, filterCol);
1617
- let startBorder = filterRange.startColumn;
1618
- let endBorder = filterRange.endColumn;
1619
- if (startColumn >= fromRange.startColumn && startColumn <= fromRange.endColumn && toRange.startColumn > fromRange.startColumn && fromRange.endColumn < endColumn) startBorder = fromRange.endColumn + 1;
1620
- if (endColumn >= fromRange.startColumn && endColumn <= fromRange.endColumn && toRange.startColumn < fromRange.startColumn && fromRange.startColumn > startColumn) endBorder = fromRange.startColumn - 1;
1621
- const numberCols = Object.keys(filterCol).map((col) => Number(col));
1622
- const newEnd = numberCols.find((col) => filterCol[col].colIndex === endBorder);
1623
- const newStart = numberCols.find((col) => filterCol[col].colIndex === startBorder);
1624
- numberCols.forEach((col) => {
1625
- const { colIndex: oldColIndex, filter } = filterCol[col];
1626
- const newColIndex = col;
1627
- if (filter) {
1628
- var _filterCol$oldColInde;
1629
- if (newColIndex >= newStart && newColIndex <= newEnd) {
1630
- var _filterModel$getFilte;
1631
- const setCriteriaMutationParams = {
1632
- unitId,
1633
- subUnitId,
1634
- col: newColIndex,
1635
- criteria: {
1636
- ...filter.serialize(),
1637
- colId: newColIndex
1638
- }
1639
- };
1640
- const undoSetCriteriaMutationParams = {
1641
- unitId,
1642
- subUnitId,
1643
- col: newColIndex,
1644
- criteria: filterModel.getFilterColumn(newColIndex) ? {
1645
- ...(_filterModel$getFilte = filterModel.getFilterColumn(newColIndex)) === null || _filterModel$getFilte === void 0 ? void 0 : _filterModel$getFilte.serialize(),
1646
- colId: newColIndex
1647
- } : null
1648
- };
1649
- redos.push({
1650
- id: SetSheetsFilterCriteriaMutation.id,
1651
- params: setCriteriaMutationParams
1652
- });
1653
- undos.push({
1654
- id: SetSheetsFilterCriteriaMutation.id,
1655
- params: undoSetCriteriaMutationParams
1656
- });
1657
- }
1658
- if (!((_filterCol$oldColInde = filterCol[oldColIndex]) === null || _filterCol$oldColInde === void 0 ? void 0 : _filterCol$oldColInde.filter)) {
1659
- const setCriteriaMutationParams = {
1660
- unitId,
1661
- subUnitId,
1662
- col: oldColIndex,
1663
- criteria: null
1664
- };
1665
- redos.push({
1666
- id: SetSheetsFilterCriteriaMutation.id,
1667
- params: setCriteriaMutationParams
1668
- });
1669
- undos.push({
1670
- id: SetSheetsFilterCriteriaMutation.id,
1671
- params: {
1672
- unitId,
1673
- subUnitId,
1674
- col: oldColIndex,
1675
- criteria: {
1676
- ...filter.serialize(),
1677
- colId: oldColIndex
1678
- }
1679
- }
1680
- });
1681
- }
1682
- }
1683
- });
1684
- if (startColumn !== newStart || endColumn !== newEnd) {
1685
- const setFilterRangeMutationParams = {
1686
- unitId,
1687
- subUnitId,
1688
- range: {
1689
- ...filterRange,
1690
- startColumn: newStart,
1691
- endColumn: newEnd
1692
- }
1693
- };
1694
- redos.unshift({
1695
- id: SetSheetsFilterRangeMutation.id,
1696
- params: setFilterRangeMutationParams
1697
- });
1698
- undos.unshift({
1699
- id: SetSheetsFilterRangeMutation.id,
1700
- params: {
1701
- range: filterRange,
1702
- unitId,
1703
- subUnitId
1704
- }
1705
- });
1706
- }
1707
- return {
1708
- undos,
1709
- redos
1710
- };
1711
- }
1712
- _handleMoveRowsCommand(config, unitId, subUnitId) {
1713
- const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
1714
- const filterRange = (filterModel === null || filterModel === void 0 ? void 0 : filterModel.getRange()) ?? null;
1715
- if (!filterModel || !filterRange) return this._handleNull();
1716
- const { startRow, endRow } = filterRange;
1717
- const { fromRange, toRange } = config;
1718
- if (fromRange.endRow < startRow && toRange.startRow <= startRow || fromRange.startRow > endRow && toRange.endRow > endRow) return this._handleNull();
1719
- const redos = [];
1720
- const undos = [];
1721
- const filterRow = {};
1722
- for (let row = startRow; row <= endRow; row++) filterRow[row] = { oldIndex: row };
1723
- const startBorder = startRow;
1724
- let endBorder = endRow;
1725
- if (endRow >= fromRange.startRow && endRow <= fromRange.endRow && toRange.startRow < fromRange.startRow && fromRange.startRow > startRow) endBorder = fromRange.startRow - 1;
1726
- moveMatrixArray(fromRange.startRow, fromRange.endRow - fromRange.startRow + 1, toRange.startRow, filterRow);
1727
- const numberRows = Object.keys(filterRow).map((row) => Number(row));
1728
- const newEnd = numberRows.find((row) => filterRow[row].oldIndex === endBorder);
1729
- const newStart = numberRows.find((row) => filterRow[row].oldIndex === startBorder);
1730
- if (startRow !== newStart || endRow !== newEnd) {
1731
- const setFilterRangeMutationParams = {
1732
- unitId,
1733
- subUnitId,
1734
- range: {
1735
- ...filterRange,
1736
- startRow: newStart,
1737
- endRow: newEnd
1738
- }
1739
- };
1740
- redos.push({
1741
- id: SetSheetsFilterRangeMutation.id,
1742
- params: setFilterRangeMutationParams
1743
- }, {
1744
- id: ReCalcSheetsFilterMutation.id,
1745
- params: {
1746
- unitId,
1747
- subUnitId
1748
- }
1749
- });
1750
- undos.push({
1751
- id: SetSheetsFilterRangeMutation.id,
1752
- params: {
1753
- range: filterRange,
1754
- unitId,
1755
- subUnitId
1756
- }
1757
- }, {
1758
- id: ReCalcSheetsFilterMutation.id,
1759
- params: {
1760
- unitId,
1761
- subUnitId
1762
- }
1763
- });
1764
- }
1765
- return {
1766
- redos,
1767
- undos
1768
- };
1769
- }
1770
- _handleMoveRangeCommand(config, unitId, subUnitId) {
1771
- const { fromRange, toRange } = config;
1772
- const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
1773
- if (!filterModel) return this._handleNull();
1774
- const filterRange = filterModel.getRange();
1775
- if (!filterRange) return this._handleNull();
1776
- const redos = [];
1777
- const undos = [];
1778
- if (Rectangle.contains(fromRange, filterRange)) {
1779
- const rowOffset = filterRange.startRow - fromRange.startRow;
1780
- const colOffset = filterRange.startColumn - fromRange.startColumn;
1781
- const newFilterRange = {
1782
- startRow: toRange.startRow + rowOffset,
1783
- startColumn: toRange.startColumn + colOffset,
1784
- endRow: toRange.startRow + rowOffset + (filterRange.endRow - filterRange.startRow),
1785
- endColumn: toRange.startColumn + colOffset + (filterRange.endColumn - filterRange.startColumn)
1786
- };
1787
- const removeFilter = {
1788
- id: RemoveSheetsFilterMutation.id,
1789
- params: {
1790
- unitId,
1791
- subUnitId
1792
- }
1793
- };
1794
- const setNewFilterRange = {
1795
- id: SetSheetsFilterRangeMutation.id,
1796
- params: {
1797
- unitId,
1798
- subUnitId,
1799
- range: newFilterRange
1800
- }
1801
- };
1802
- const setOldFilterRange = {
1803
- id: SetSheetsFilterRangeMutation.id,
1804
- params: {
1805
- unitId,
1806
- subUnitId,
1807
- range: filterRange
1808
- }
1809
- };
1810
- redos.push(removeFilter, setNewFilterRange);
1811
- undos.push(removeFilter, setOldFilterRange);
1812
- const filterColumn = filterModel.getAllFilterColumns();
1813
- const moveColDelta = toRange.startColumn - fromRange.startColumn;
1814
- filterColumn.forEach((column) => {
1815
- const [col, criteria] = column;
1816
- if (criteria) {
1817
- redos.push({
1818
- id: SetSheetsFilterCriteriaMutation.id,
1819
- params: {
1820
- unitId,
1821
- subUnitId,
1822
- col: col + moveColDelta,
1823
- criteria: {
1824
- ...criteria.serialize(),
1825
- colId: col + moveColDelta
1826
- }
1827
- }
1828
- });
1829
- undos.push({
1830
- id: SetSheetsFilterCriteriaMutation.id,
1831
- params: {
1832
- unitId,
1833
- subUnitId,
1834
- col,
1835
- criteria: {
1836
- ...criteria.serialize(),
1837
- colId: col
1838
- }
1839
- }
1840
- });
1841
- }
1842
- });
1843
- } else if (Rectangle.intersects(toRange, filterRange)) {
1844
- const newFilterRange = {
1845
- ...filterRange,
1846
- endRow: Math.max(filterRange.endRow, toRange.endRow)
1847
- };
1848
- redos.push({
1849
- id: SetSheetsFilterRangeMutation.id,
1850
- params: {
1851
- unitId,
1852
- subUnitId,
1853
- range: newFilterRange
1854
- }
1855
- });
1856
- undos.push({
1857
- id: SetSheetsFilterRangeMutation.id,
1858
- params: {
1859
- unitId,
1860
- subUnitId,
1861
- range: filterRange
1862
- }
1863
- });
1864
- }
1865
- return {
1866
- redos,
1867
- undos
1868
- };
1869
- }
1870
- _handleRemoveSheetCommand(config, unitId, subUnitId) {
1871
- const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
1872
- if (!filterModel) return this._handleNull();
1873
- const filterRange = filterModel.getRange();
1874
- if (!filterRange) return this._handleNull();
1875
- const redos = [];
1876
- const undos = [];
1877
- filterModel.getAllFilterColumns().forEach(([col, filter]) => {
1878
- undos.push({
1879
- id: SetSheetsFilterCriteriaMutation.id,
1880
- params: {
1881
- unitId,
1882
- subUnitId,
1883
- col,
1884
- criteria: {
1885
- ...filter.serialize(),
1886
- colId: col
1887
- }
1888
- }
1889
- });
1890
- });
1891
- redos.push({
1892
- id: RemoveSheetsFilterMutation.id,
1893
- params: {
1894
- unitId,
1895
- subUnitId,
1896
- range: filterRange
1897
- }
1898
- });
1899
- undos.unshift({
1900
- id: SetSheetsFilterRangeMutation.id,
1901
- params: {
1902
- range: filterRange,
1903
- unitId,
1904
- subUnitId
1905
- }
1906
- });
1907
- return {
1908
- undos,
1909
- redos
1910
- };
1911
- }
1912
- _handleCopySheetCommand(unitId, subUnitId, targetSubUnitId) {
1913
- const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
1914
- if (!filterModel) return this._handleNull();
1915
- const filterRange = filterModel.getRange();
1916
- if (!filterRange) return this._handleNull();
1917
- const redos = [];
1918
- const undos = [];
1919
- const preUndos = [];
1920
- const preRedos = [];
1921
- filterModel.getAllFilterColumns().forEach(([col, filter]) => {
1922
- redos.push({
1923
- id: SetSheetsFilterCriteriaMutation.id,
1924
- params: {
1925
- unitId,
1926
- subUnitId: targetSubUnitId,
1927
- col,
1928
- criteria: {
1929
- ...filter.serialize(),
1930
- colId: col
1931
- }
1932
- }
1933
- });
1934
- preUndos.push({
1935
- id: SetSheetsFilterCriteriaMutation.id,
1936
- params: {
1937
- unitId,
1938
- subUnitId: targetSubUnitId,
1939
- col,
1940
- criteria: null
1941
- }
1942
- });
1943
- });
1944
- preUndos.push({
1945
- id: RemoveSheetsFilterMutation.id,
1946
- params: {
1947
- unitId,
1948
- subUnitId: targetSubUnitId,
1949
- range: filterRange
1950
- }
1951
- });
1952
- redos.unshift({
1953
- id: SetSheetsFilterRangeMutation.id,
1954
- params: {
1955
- range: filterRange,
1956
- unitId,
1957
- subUnitId: targetSubUnitId
1958
- }
1959
- });
1960
- return {
1961
- undos,
1962
- redos,
1963
- preUndos,
1964
- preRedos
1965
- };
1966
- }
1967
- _handleNull() {
1968
- return {
1969
- redos: [],
1970
- undos: []
1971
- };
1972
- }
1973
- _initRowFilteredInterceptor() {
1974
- this.disposeWithMe(this._sheetInterceptorService.intercept(INTERCEPTOR_POINT.ROW_FILTERED, { handler: (filtered, rowLocation) => {
1975
- var _this$_sheetsFilterSe2;
1976
- if (filtered) return true;
1977
- return ((_this$_sheetsFilterSe2 = this._sheetsFilterService.getFilterModel(rowLocation.unitId, rowLocation.subUnitId)) === null || _this$_sheetsFilterSe2 === void 0 ? void 0 : _this$_sheetsFilterSe2.isRowFiltered(rowLocation.row)) ?? false;
1978
- } }));
1979
- }
1980
- _moveCriteria(unitId, subUnitId, target, step) {
1981
- const defaultSetCriteriaMutationParams = {
1982
- unitId,
1983
- subUnitId,
1984
- criteria: null,
1985
- col: -1
1986
- };
1987
- const oldUndos = [];
1988
- const oldRedos = [];
1989
- const newUndos = [];
1990
- const newRedos = [];
1991
- target.forEach((column) => {
1992
- const [offset, filter] = column;
1993
- oldRedos.push({
1994
- id: SetSheetsFilterCriteriaMutation.id,
1995
- params: {
1996
- ...defaultSetCriteriaMutationParams,
1997
- col: offset
1998
- }
1999
- });
2000
- oldUndos.push({
2001
- id: SetSheetsFilterCriteriaMutation.id,
2002
- params: {
2003
- ...defaultSetCriteriaMutationParams,
2004
- col: offset,
2005
- criteria: {
2006
- ...filter.serialize(),
2007
- colId: offset
2008
- }
2009
- }
2010
- });
2011
- });
2012
- target.forEach((column) => {
2013
- const [offset, filter] = column;
2014
- newRedos.push({
2015
- id: SetSheetsFilterCriteriaMutation.id,
2016
- params: {
2017
- ...defaultSetCriteriaMutationParams,
2018
- col: offset + step,
2019
- criteria: {
2020
- ...filter.serialize(),
2021
- colId: offset + step
2022
- }
2023
- }
2024
- });
2025
- newUndos.push({
2026
- id: SetSheetsFilterCriteriaMutation.id,
2027
- params: {
2028
- ...defaultSetCriteriaMutationParams,
2029
- col: offset + step,
2030
- criteria: null
2031
- }
2032
- });
2033
- });
2034
- return {
2035
- newRange: {
2036
- redos: newRedos,
2037
- undos: newUndos
2038
- },
2039
- oldRange: {
2040
- redos: oldRedos,
2041
- undos: oldUndos
2042
- }
2043
- };
2044
- }
2045
- _commandExecutedListener() {
2046
- this.disposeWithMe(this._commandService.onCommandExecuted((command, options) => {
2047
- var _command$params;
2048
- const { unitId, subUnitId } = command.params || {};
2049
- const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
2050
- if (!filterModel) return;
2051
- const filteredOutRows = Array.from(filterModel.filteredOutRows).sort((a, b) => a - b);
2052
- const newFilteredOutRows = [];
2053
- let changed = false;
2054
- if (command.id === RemoveRowMutation.id) {
2055
- const { startRow, endRow } = command.params.range;
2056
- const filterOutInRemove = filteredOutRows.filter((row) => row >= startRow && row <= endRow);
2057
- filteredOutRows.forEach((row) => {
2058
- if (row < startRow) newFilteredOutRows.push(row);
2059
- else {
2060
- changed = true;
2061
- if (row <= endRow) {
2062
- const newIndex = Math.max(startRow, newFilteredOutRows.length ? newFilteredOutRows[newFilteredOutRows.length - 1] + 1 : startRow);
2063
- newFilteredOutRows.push(newIndex);
2064
- } else newFilteredOutRows.push(row - (endRow - startRow + 1 - filterOutInRemove.length));
2065
- }
2066
- });
2067
- }
2068
- if (command.id === InsertRowMutation.id) {
2069
- const { startRow, endRow } = command.params.range;
2070
- filteredOutRows.forEach((row) => {
2071
- if (row >= startRow) {
2072
- changed = true;
2073
- newFilteredOutRows.push(row + (endRow - startRow + 1));
2074
- } else newFilteredOutRows.push(row);
2075
- });
2076
- }
2077
- if (changed) filterModel.filteredOutRows = new Set(newFilteredOutRows);
2078
- const trigger = (_command$params = command.params) === null || _command$params === void 0 ? void 0 : _command$params.trigger;
2079
- if (command.id === SetRangeValuesMutation.id && !(options === null || options === void 0 ? void 0 : options.onlyLocal) && trigger !== UndoCommandId) {
2080
- const extendRegion = this._getExtendRegion(unitId, subUnitId);
2081
- if (extendRegion) {
2082
- const cellValue = command.params.cellValue;
2083
- if (cellValue) for (let col = extendRegion.startColumn; col <= extendRegion.endColumn; col++) {
2084
- var _cellValue$extendRegi;
2085
- const cell = cellValue === null || cellValue === void 0 || (_cellValue$extendRegi = cellValue[extendRegion.startRow]) === null || _cellValue$extendRegi === void 0 ? void 0 : _cellValue$extendRegi[col];
2086
- if (cell && this._cellHasValue(cell)) {
2087
- var _this$_univerInstance2;
2088
- const worksheet = (_this$_univerInstance2 = this._univerInstanceService.getUnit(unitId)) === null || _this$_univerInstance2 === void 0 ? void 0 : _this$_univerInstance2.getSheetBySheetId(subUnitId);
2089
- if (worksheet) {
2090
- const extendedRange = expandToContinuousRange(extendRegion, { down: true }, worksheet);
2091
- const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
2092
- const filterRange = filterModel.getRange();
2093
- filterModel.setRange({
2094
- ...filterRange,
2095
- endRow: extendedRange.endRow
2096
- });
2097
- filterModel.reCalc();
2098
- this._registerRefRange(unitId, subUnitId);
2099
- break;
2100
- }
2101
- }
2102
- }
2103
- }
2104
- }
2105
- }));
2106
- }
2107
- _getExtendRegion(unitId, subUnitId) {
2108
- var _this$_univerInstance3;
2109
- const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
2110
- if (!filterModel) return null;
2111
- const worksheet = (_this$_univerInstance3 = this._univerInstanceService.getUnit(unitId)) === null || _this$_univerInstance3 === void 0 ? void 0 : _this$_univerInstance3.getSheetBySheetId(subUnitId);
2112
- if (!worksheet) return null;
2113
- const filterRange = filterModel.getRange();
2114
- if (!filterRange) return null;
2115
- const maxRowIndex = worksheet.getRowCount() - 1;
2116
- const rowManager = worksheet.getRowManager();
2117
- for (let row = filterRange.endRow + 1; row <= maxRowIndex; row++) if (rowManager.getRowRawVisible(row)) return {
2118
- startRow: row,
2119
- endRow: row,
2120
- startColumn: filterRange.startColumn,
2121
- endColumn: filterRange.endColumn
2122
- };
2123
- return null;
2124
- }
2125
- _initErrorHandling() {
2126
- this.disposeWithMe(this._commandService.beforeCommandExecuted((command) => {
2127
- const params = command.params;
2128
- const target = getSheetCommandTarget(this._univerInstanceService, params);
2129
- if (!target) return;
2130
- const { subUnitId, unitId } = target;
2131
- const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
2132
- if (!filterModel) return;
2133
- const filterRange = filterModel.getRange();
2134
- if (command.id === MoveRowsCommand.id && params.fromRange.startRow <= filterRange.startRow && params.fromRange.endRow < filterRange.endRow && params.fromRange.endRow >= filterRange.startRow) {
2135
- this._sheetsFilterService.setFilterErrorMsg("sheets-filter.msg.filter-header-forbidden");
2136
- throw new Error("[SheetsFilterController]: Cannot move header row of filter");
2137
- }
2138
- }));
2139
- }
2140
- _cellHasValue(cell) {
2141
- const values = Object.values(cell);
2142
- if (values.length === 0 || values.every((v) => v == null)) return false;
2143
- return true;
2144
- }
2145
- };
2146
- SheetsFilterController = __decorate([
2147
- __decorateParam(0, ICommandService),
2148
- __decorateParam(1, Inject(SheetInterceptorService)),
2149
- __decorateParam(2, Inject(SheetsFilterService)),
2150
- __decorateParam(3, IUniverInstanceService),
2151
- __decorateParam(4, Inject(RefRangeService)),
2152
- __decorateParam(5, Optional(DataSyncPrimaryController)),
2153
- __decorateParam(6, Inject(ZebraCrossingCacheController))
2154
- ], SheetsFilterController);
2155
-
2156
- //#endregion
2157
- //#region src/controllers/sheets-filter-sync.controller.ts
2158
- const sheetsFilterOnlyLocalMutationIds = [SetSheetsFilterCriteriaMutation.id, ReCalcSheetsFilterMutation.id];
2159
- const effectedByOnlyLocalMutationIds = [
2160
- InsertColMutation.id,
2161
- RemoveColMutation.id,
2162
- MoveColsMutation.id
2163
- ];
2164
- let SheetsFilterSyncController = class SheetsFilterSyncController extends Disposable {
2165
- get visible() {
2166
- return this._visible$.getValue();
2167
- }
2168
- get enabled() {
2169
- return this._enabled$.getValue();
2170
- }
2171
- constructor(_sheetsFilterController, _commandService, _configService) {
2172
- super();
2173
- this._sheetsFilterController = _sheetsFilterController;
2174
- this._commandService = _commandService;
2175
- this._configService = _configService;
2176
- _defineProperty(this, "_d", new DisposableCollection());
2177
- _defineProperty(this, "_visible$", new BehaviorSubject(false));
2178
- _defineProperty(this, "visible$", this._visible$.asObservable());
2179
- _defineProperty(this, "_enabled$", new BehaviorSubject(true));
2180
- _defineProperty(this, "enabled$", this._enabled$.asObservable());
2181
- const config = this._configService.getConfig(SHEETS_FILTER_PLUGIN_CONFIG_KEY);
2182
- if (config === null || config === void 0 ? void 0 : config.enableSyncSwitch) {
2183
- this._visible$.next(true);
2184
- if (typeof config.enableSyncSwitch === "object") this.setEnabled(config.enableSyncSwitch.defaultValue ?? true);
2185
- }
2186
- }
2187
- setEnabled(enabled) {
2188
- this._enabled$.next(enabled);
2189
- if (enabled) this._d.dispose();
2190
- else this._initOnlyLocalListener();
2191
- }
2192
- _initOnlyLocalListener() {
2193
- this._d.add(this._commandService.beforeCommandExecuted((commandInfo, options) => {
2194
- if (sheetsFilterOnlyLocalMutationIds.includes(commandInfo.id)) {
2195
- if (!options) options = {};
2196
- options.onlyLocal = true;
2197
- }
2198
- }));
2199
- this._d.add(this._commandService.onCommandExecuted((commandInfo, options) => {
2200
- if (effectedByOnlyLocalMutationIds.includes(commandInfo.id) && (options === null || options === void 0 ? void 0 : options.fromCollab)) {
2201
- if (commandInfo.id === InsertColMutation.id) {
2202
- const { range, unitId, subUnitId } = commandInfo.params;
2203
- const { redos } = this._sheetsFilterController.handleInsertColCommand(range, unitId, subUnitId);
2204
- sequenceExecute(redos, this._commandService, options);
2205
- } else if (commandInfo.id === RemoveColMutation.id) {
2206
- const { range, unitId, subUnitId } = commandInfo.params;
2207
- const { redos } = this._sheetsFilterController.handleRemoveColCommand(range, unitId, subUnitId);
2208
- sequenceExecute(redos, this._commandService, options);
2209
- } else if (commandInfo.id === MoveColsMutation.id) {
2210
- const { sourceRange: fromRange, targetRange: toRange, unitId, subUnitId } = commandInfo.params;
2211
- const { redos } = this._sheetsFilterController.handleMoveColsCommand({
2212
- fromRange,
2213
- toRange
2214
- }, unitId, subUnitId);
2215
- sequenceExecute(redos, this._commandService, options);
2216
- }
2217
- }
2218
- }));
2219
- }
2220
- };
2221
- SheetsFilterSyncController = __decorate([
2222
- __decorateParam(0, Inject(SheetsFilterController)),
2223
- __decorateParam(1, ICommandService),
2224
- __decorateParam(2, IConfigService)
2225
- ], SheetsFilterSyncController);
2226
-
2227
- //#endregion
2228
- //#region package.json
2229
- var name = "@univerjs/sheets-filter";
2230
- var version = "1.3.0";
2231
-
2232
- //#endregion
2233
- //#region src/services/sheet-filter-formula.service.ts
2234
- let SheetsFilterFormulaService = class SheetsFilterFormulaService extends Disposable {
2235
- constructor(_activeDirtyManagerService, _sheetRowFilteredService, _sheetsFilterService, _univerInstanceService) {
2236
- super();
2237
- this._activeDirtyManagerService = _activeDirtyManagerService;
2238
- this._sheetRowFilteredService = _sheetRowFilteredService;
2239
- this._sheetsFilterService = _sheetsFilterService;
2240
- this._univerInstanceService = _univerInstanceService;
2241
- this._initFormulaDirtyRange();
2242
- this._registerSheetRowFiltered();
2243
- }
2244
- _initFormulaDirtyRange() {
2245
- FILTER_MUTATIONS.forEach((commandId) => {
2246
- this._activeDirtyManagerService.register(commandId, {
2247
- commandId,
2248
- getDirtyData: (commandInfo) => {
2249
- const { unitId, subUnitId } = commandInfo.params;
2250
- return {
2251
- dirtyRanges: this._getHideRowMutation(unitId, subUnitId),
2252
- clearDependencyTreeCache: { [unitId]: { [subUnitId]: "1" } }
2253
- };
2254
- }
2255
- });
2256
- });
2257
- }
2258
- _getHideRowMutation(unitId, subUnitId) {
2259
- var _this$_sheetsFilterSe, _this$_univerInstance;
2260
- const range = (_this$_sheetsFilterSe = this._sheetsFilterService.getFilterModel(unitId, subUnitId)) === null || _this$_sheetsFilterSe === void 0 ? void 0 : _this$_sheetsFilterSe.getRange();
2261
- const sheet = (_this$_univerInstance = this._univerInstanceService.getUnit(unitId)) === null || _this$_univerInstance === void 0 ? void 0 : _this$_univerInstance.getSheetBySheetId(subUnitId);
2262
- if (range == null || sheet == null) return [];
2263
- const { startRow, endRow } = range;
2264
- return [{
2265
- unitId,
2266
- sheetId: subUnitId,
2267
- range: {
2268
- startRow,
2269
- startColumn: 0,
2270
- endRow,
2271
- endColumn: sheet.getColumnCount() - 1
2272
- }
2273
- }];
2274
- }
2275
- _registerSheetRowFiltered() {
2276
- this.disposeWithMe(this._sheetRowFilteredService.register((unitId, subUnitId, row) => {
2277
- var _this$_sheetsFilterSe2;
2278
- return ((_this$_sheetsFilterSe2 = this._sheetsFilterService.getFilterModel(unitId, subUnitId)) === null || _this$_sheetsFilterSe2 === void 0 ? void 0 : _this$_sheetsFilterSe2.isRowFiltered(row)) ?? false;
2279
- }));
2280
- }
2281
- };
2282
- SheetsFilterFormulaService = __decorate([
2283
- __decorateParam(0, Inject(IActiveDirtyManagerService)),
2284
- __decorateParam(1, Inject(ISheetRowFilteredService)),
2285
- __decorateParam(2, Inject(SheetsFilterService)),
2286
- __decorateParam(3, IUniverInstanceService)
2287
- ], SheetsFilterFormulaService);
2288
-
2289
- //#endregion
2290
- //#region src/plugin.ts
2291
- let UniverSheetsFilterPlugin = class UniverSheetsFilterPlugin extends Plugin {
2292
- constructor(_config = defaultPluginConfig, _injector, _configService) {
2293
- super();
2294
- this._config = _config;
2295
- this._injector = _injector;
2296
- this._configService = _configService;
2297
- const { ...rest } = merge({}, defaultPluginConfig, this._config);
2298
- this._configService.setConfig(SHEETS_FILTER_PLUGIN_CONFIG_KEY, rest);
2299
- }
2300
- onStarting() {
2301
- [
2302
- [SheetsFilterFormulaService],
2303
- [SheetsFilterService],
2304
- [SheetsFilterController],
2305
- [SheetsFilterSyncController]
2306
- ].forEach((d) => this._injector.add(d));
2307
- }
2308
- onReady() {
2309
- touchDependencies(this._injector, [
2310
- [SheetsFilterFormulaService],
2311
- [SheetsFilterController],
2312
- [SheetsFilterSyncController]
2313
- ]);
2314
- }
2315
- };
2316
- _defineProperty(UniverSheetsFilterPlugin, "type", UniverInstanceType.UNIVER_SHEET);
2317
- _defineProperty(UniverSheetsFilterPlugin, "pluginName", SHEET_FILTER_SNAPSHOT_ID);
2318
- _defineProperty(UniverSheetsFilterPlugin, "packageName", name);
2319
- _defineProperty(UniverSheetsFilterPlugin, "version", version);
2320
- UniverSheetsFilterPlugin = __decorate([
2321
- DependentOn(UniverFormulaEnginePlugin, UniverSheetsPlugin),
2322
- __decorateParam(1, Inject(Injector)),
2323
- __decorateParam(2, IConfigService)
2324
- ], UniverSheetsFilterPlugin);
2325
-
2326
- //#endregion
2327
3
  export { ClearSheetsFilterCriteriaCommand, CustomFilterOperator, FILTER_MUTATIONS, FilterBy, FilterColumn, FilterModel, ReCalcSheetsFilterCommand, ReCalcSheetsFilterMutation, RemoveSheetFilterCommand, RemoveSheetsFilterMutation, SHEET_FILTER_SNAPSHOT_ID, SetSheetFilterRangeCommand, SetSheetsFilterCriteriaCommand, SetSheetsFilterCriteriaMutation, SetSheetsFilterRangeMutation, SheetsFilterService, SheetsFilterSyncController, SmartToggleSheetsFilterCommand, UniverSheetsFilterPlugin, equals, getCustomFilterFn, greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo, notEquals };