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