@univerjs/sheets-filter 0.1.8

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.
@@ -0,0 +1,1033 @@
1
+ var ae = Object.defineProperty;
2
+ var ce = (s, r, e) => r in s ? ae(s, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[r] = e;
3
+ var _ = (s, r, e) => (ce(s, typeof r != "symbol" ? r + "" : r, e), e);
4
+ import { isNumeric as ue, Disposable as U, Rectangle as Y, mergeSets as B, Tools as V, extractPureTextFromCell as de, CellValueType as H, CommandType as T, UniverInstanceType as W, fromCallback as he, OnLifecycle as K, LifecycleStages as Z, IResourceManagerService as fe, IUniverInstanceService as k, ICommandService as ee, DisposableCollection as me, moveMatrixArray as D, Plugin as _e } from "@univerjs/core";
5
+ import { Inject as $, Injector as ge } from "@wendellhu/redi";
6
+ import { BehaviorSubject as I, merge as Ce, filter as Re, switchMap as pe, of as Fe } from "rxjs";
7
+ import { SheetInterceptorService as we, RefRangeService as Se, SetWorksheetActivateCommand as ve, RemoveSheetCommand as Me, INTERCEPTOR_POINT as Ee, RemoveRowMutation as Oe, InsertRowMutation as ye, MoveRangeCommand as Ae, EffectRefRangId as Q, RemoveRowCommand as Ne, RemoveColCommand as Te, InsertColCommand as xe, InsertRowCommand as $e } from "@univerjs/sheets";
8
+ var E = /* @__PURE__ */ ((s) => (s.EQUAL = "equal", s.GREATER_THAN = "greaterThan", s.GREATER_THAN_OR_EQUAL = "greaterThanOrEqual", s.LESS_THAN = "lessThan", s.LESS_THAN_OR_EQUAL = "lessThanOrEqual", s.NOT_EQUALS = "notEqual", s))(E || {});
9
+ const Pe = {
10
+ operator: E.GREATER_THAN,
11
+ fn: (s, r) => A(s) ? s > r : !1
12
+ }, Ie = {
13
+ operator: E.GREATER_THAN_OR_EQUAL,
14
+ fn: (s, r) => A(s) ? s >= r : !1
15
+ }, Le = {
16
+ operator: E.LESS_THAN,
17
+ fn: (s, r) => A(s) ? s < r : !1
18
+ }, Ue = {
19
+ operator: E.LESS_THAN_OR_EQUAL,
20
+ fn: (s, r) => A(s) ? s <= r : !1
21
+ }, Be = {
22
+ operator: E.EQUAL,
23
+ fn: (s, r) => A(s) ? s === r : !1
24
+ }, te = {
25
+ operator: E.NOT_EQUALS,
26
+ fn: (s, r) => {
27
+ if (typeof r == "string") {
28
+ if (r === " ")
29
+ return s != null;
30
+ const e = se(s);
31
+ return e && We(r) ? !ne(r).test(e) : e !== r;
32
+ }
33
+ return A(s) ? s !== r : !0;
34
+ }
35
+ }, re = /* @__PURE__ */ new Map([]), ze = [Pe, Ie, Le, Ue, Be, te];
36
+ ze.forEach((s) => {
37
+ re.set(s.operator, s);
38
+ });
39
+ function be(s) {
40
+ return !!s;
41
+ }
42
+ const He = {
43
+ fn: (s, r) => {
44
+ const e = se(s);
45
+ return e === null ? r === "" : ne(r).test(e);
46
+ }
47
+ };
48
+ function q(s) {
49
+ return s ? re.get(s) : He;
50
+ }
51
+ function A(s) {
52
+ return typeof s == "number";
53
+ }
54
+ function G(s) {
55
+ return !!(typeof s == "number" || typeof s == "string" && ue(s));
56
+ }
57
+ function se(s) {
58
+ return typeof s == "boolean" || s == null ? null : typeof s == "string" ? s : s.toString();
59
+ }
60
+ function We(s) {
61
+ return typeof s == "number" ? !1 : s.indexOf("*") !== -1 || s.indexOf("?") !== -1;
62
+ }
63
+ function ne(s) {
64
+ const r = s.replace(/[.+^${}()|[\]\\]/g, "\\$&").replaceAll("?", ".").replace(/[*]/g, ".$&");
65
+ return new RegExp(`^${r}$`);
66
+ }
67
+ const z = () => /* @__PURE__ */ new Set();
68
+ class L extends U {
69
+ constructor(e, t, n) {
70
+ super();
71
+ _(this, "_filteredOutRows$", new I(z()));
72
+ /** An observable value. A set of filtered out rows. */
73
+ _(this, "filteredOutRows$", this._filteredOutRows$.asObservable());
74
+ // TODO: we may need to update which cols have criteria rather than simple boolean
75
+ _(this, "_hasCriteria$", new I(!1));
76
+ _(this, "hasCriteria$", this._hasCriteria$.asObservable());
77
+ _(this, "_filterColumnByIndex", /* @__PURE__ */ new Map());
78
+ _(this, "_alreadyFilteredOutRows", z());
79
+ _(this, "_range");
80
+ this.unitId = e, this.subUnitId = t, this._worksheet = n;
81
+ }
82
+ get filteredOutRows() {
83
+ return this._filteredOutRows$.getValue();
84
+ }
85
+ set filteredOutRows(e) {
86
+ this._alreadyFilteredOutRows = e, this._filteredOutRows$.next(e);
87
+ }
88
+ dispose() {
89
+ super.dispose(), this._filteredOutRows$.complete(), this._hasCriteria$.complete();
90
+ }
91
+ /**
92
+ * Serialize this filter model to the JSON format representation.
93
+ */
94
+ serialize() {
95
+ const e = {
96
+ ref: Y.clone(this._range),
97
+ filterColumns: this._getAllFilterColumns(!0).sort(([t], [n]) => t - n).map(([t, n]) => n.serialize())
98
+ };
99
+ return this._alreadyFilteredOutRows && (e.cachedFilteredOut = Array.from(this._alreadyFilteredOutRows).sort()), e;
100
+ }
101
+ /**
102
+ * Deserialize auto filter info to construct a `FilterModel` object.
103
+ * @param unitId workbook id
104
+ * @param subUnitId worksheet id
105
+ * @param worksheet the Worksheet object
106
+ * @param autoFilter auto filter data
107
+ */
108
+ static deserialize(e, t, n, i) {
109
+ const o = new L(e, t, n);
110
+ return o._dump(i), o;
111
+ }
112
+ _dump(e) {
113
+ var t;
114
+ this.setRange(e.ref), (t = e.filterColumns) == null || t.forEach((n) => this._setCriteriaWithoutReCalc(n.colId, n)), e.cachedFilteredOut && (this._alreadyFilteredOutRows = new Set(e.cachedFilteredOut), this._emit()), this._emitHasCriteria();
115
+ }
116
+ isRowFiltered(e) {
117
+ return this._alreadyFilteredOutRows.has(e);
118
+ }
119
+ getRange() {
120
+ if (!this._range)
121
+ throw new Error("[FilterModel] could not get range before a range is set!");
122
+ return this._range;
123
+ }
124
+ /**
125
+ * Get filtered out rows except the specific column. This method is considered as "pure". In
126
+ * another word it would not change `filteredOutRows` on `FilterModel` nor `FilterColumn`.
127
+ * @param col
128
+ */
129
+ getFilteredOutRowsExceptCol(e) {
130
+ return this._getAllFilterColumns(!0).filter(([t]) => t !== e).reduce((t, [, n]) => {
131
+ const i = n.calc({ getAlreadyFilteredOutRows: () => t });
132
+ return i ? B(t, i) : t;
133
+ }, /* @__PURE__ */ new Set());
134
+ }
135
+ /**
136
+ * Set range of the filter model, this would remove some `IFilterColumn`
137
+ * if the new range not overlaps the old range.
138
+ */
139
+ setRange(e) {
140
+ this._range = e, this._getAllFilterColumns(!0).forEach(([t, n]) => {
141
+ n.setRangeAndColumn({
142
+ startRow: e.startRow,
143
+ endRow: e.endRow,
144
+ startColumn: t,
145
+ endColumn: t
146
+ }, t);
147
+ });
148
+ }
149
+ /**
150
+ * Set or remove filter criteria on a specific row.
151
+ */
152
+ setCriteria(e, t, n = !1) {
153
+ if (!this._range)
154
+ throw new Error("[FilterModel] could not set criteria before a range is set!");
155
+ if (!t) {
156
+ this._removeCriteria(e), this._rebuildAlreadyFilteredOutRowsWithCache(), n && this._reCalcAllColumns(), this._emit(), this._emitHasCriteria();
157
+ return;
158
+ }
159
+ this._setCriteriaWithoutReCalc(e, t), n && (this._rebuildAlreadyFilteredOutRowsWithCache(), this._reCalcWithNoCacheColumns(), this._emit(), this._emitHasCriteria());
160
+ }
161
+ getAllFilterColumns() {
162
+ return this._getAllFilterColumns(!0);
163
+ }
164
+ getFilterColumn(e) {
165
+ var t;
166
+ return (t = this._filterColumnByIndex.get(e)) != null ? t : null;
167
+ }
168
+ reCalc() {
169
+ this._reCalcAllColumns(), this._emit();
170
+ }
171
+ _getAllFilterColumns(e = !1) {
172
+ const t = Array.from(this._filterColumnByIndex.entries());
173
+ return e ? t : t.map(([n, i]) => i);
174
+ }
175
+ _reCalcAllColumns() {
176
+ this._alreadyFilteredOutRows = z(), this._getAllFilterColumns().forEach((e) => e.__clearCache()), this._reCalcWithNoCacheColumns();
177
+ }
178
+ _setCriteriaWithoutReCalc(e, t) {
179
+ const n = this._range;
180
+ if (!n)
181
+ throw new Error("[FilterModel] could not set criteria before a range is set!");
182
+ const { startColumn: i, endColumn: o } = n;
183
+ if (e > o || e < i)
184
+ throw new Error(`[FilterModel] could not set criteria on column ${e} which is out of range!`);
185
+ let l;
186
+ this._filterColumnByIndex.has(e) ? l = this._filterColumnByIndex.get(e) : (l = new je(
187
+ this.unitId,
188
+ this.subUnitId,
189
+ this._worksheet,
190
+ t,
191
+ { getAlreadyFilteredOutRows: () => this._alreadyFilteredOutRows }
192
+ ), l.setRangeAndColumn(n, e), this._filterColumnByIndex.set(e, l)), l.setCriteria(t);
193
+ }
194
+ _removeCriteria(e) {
195
+ const t = this._filterColumnByIndex.get(e);
196
+ t && (t.dispose(), this._filterColumnByIndex.delete(e));
197
+ }
198
+ _emit() {
199
+ this._filteredOutRows$.next(this._alreadyFilteredOutRows);
200
+ }
201
+ _emitHasCriteria() {
202
+ this._hasCriteria$.next(this._filterColumnByIndex.size > 0);
203
+ }
204
+ _rebuildAlreadyFilteredOutRowsWithCache() {
205
+ const e = this._getAllFilterColumns().filter((t) => t.hasCache()).reduce((t, n) => B(t, n.filteredOutRows), /* @__PURE__ */ new Set());
206
+ this._alreadyFilteredOutRows = e;
207
+ }
208
+ _reCalcWithNoCacheColumns() {
209
+ const e = this._getAllFilterColumns().filter((t) => !t.hasCache());
210
+ for (const t of e) {
211
+ const n = t.reCalc();
212
+ n && (this._alreadyFilteredOutRows = B(this._alreadyFilteredOutRows, n));
213
+ }
214
+ }
215
+ }
216
+ class je extends U {
217
+ constructor(e, t, n, i, o) {
218
+ super();
219
+ _(this, "_filteredOutRows", null);
220
+ /** Cache the filter function. */
221
+ _(this, "_filterFn", null);
222
+ _(this, "_range", null);
223
+ _(this, "_column", 0);
224
+ _(this, "_filterByValues", !1);
225
+ this.unitId = e, this.subUnitId = t, this._worksheet = n, this._criteria = i, this._filterColumnContext = o;
226
+ }
227
+ get filteredOutRows() {
228
+ return this._filteredOutRows;
229
+ }
230
+ dispose() {
231
+ super.dispose(), this._filteredOutRows = null;
232
+ }
233
+ /**
234
+ * @internal
235
+ */
236
+ __clearCache() {
237
+ this._filteredOutRows = null;
238
+ }
239
+ serialize() {
240
+ if (!this._criteria)
241
+ throw new Error("[FilterColumn]: could not serialize without a filter column!");
242
+ return V.deepClone({
243
+ ...this._criteria,
244
+ colId: this._column
245
+ });
246
+ }
247
+ hasCache() {
248
+ return this._filteredOutRows !== null;
249
+ }
250
+ // The first row should be omitted!
251
+ setRangeAndColumn(e, t) {
252
+ this._range = e, this._column = t;
253
+ }
254
+ setCriteria(e) {
255
+ this._criteria = e, this._generateFilterFn(), this._filteredOutRows = null;
256
+ }
257
+ getColumnData() {
258
+ return V.deepClone(this._criteria);
259
+ }
260
+ /**
261
+ * Trigger new calculation on this `FilterModel` instance.
262
+ *
263
+ * @external DO NOT EVER call this method from `FilterColumn` itself. The whole process heavily relies on
264
+ * `filteredOutByOthers`, and it is more comprehensible if we let `FilterModel` take full control over the process.
265
+ */
266
+ reCalc() {
267
+ return this._filteredOutRows = this.calc(this._filterColumnContext), this._filteredOutRows;
268
+ }
269
+ calc(e) {
270
+ if (!this._filterFn)
271
+ throw new Error("[FilterColumn] cannot calculate without a filter fn!");
272
+ if (!this._range)
273
+ throw new Error("[FilterColumn] cannot calculate without a range!");
274
+ if (typeof this._column != "number")
275
+ throw new TypeError("[FilterColumn] cannot calculate without a column offset!");
276
+ const t = this._column, n = { startColumn: t, endColumn: t, startRow: this._range.startRow + 1, endRow: this._range.endRow }, i = /* @__PURE__ */ new Set(), o = e.getAlreadyFilteredOutRows();
277
+ for (const l of this._worksheet.iterateByColumn(n, !1, !1)) {
278
+ const { row: a, rowSpan: u, col: c } = l;
279
+ if (o.has(a) && (!u || u === 1))
280
+ continue;
281
+ const h = this._filterByValues ? de(this._worksheet.getCell(a, c)) : Ye(this._worksheet, a, c);
282
+ if (!this._filterFn(h) && (i.add(a), u))
283
+ for (let m = 1; m < u; m++)
284
+ i.add(a + m);
285
+ }
286
+ return i;
287
+ }
288
+ _generateFilterFn() {
289
+ this._criteria && (this._filterFn = Ve(this._criteria), this._filterByValues = !!this._criteria.filters);
290
+ }
291
+ }
292
+ function Ve(s) {
293
+ if (s.filters)
294
+ return De(s.filters);
295
+ if (s.customFilters)
296
+ return Qe(s.customFilters);
297
+ throw new Error("[FilterModel]: other types of filters are not supported yet.");
298
+ }
299
+ function De(s) {
300
+ const r = !!s.blank, e = new Set(s.filters);
301
+ return (t) => t === void 0 || t === "" ? r : e.has(typeof t == "string" ? t : `${t}`);
302
+ }
303
+ function Qe(s) {
304
+ const r = s.customFilters.map((e) => Xe(e));
305
+ return Je(r) ? s.and ? qe(r) : Ge(r) : r[0];
306
+ }
307
+ function qe(s) {
308
+ const [r, e] = s;
309
+ return (t) => r(t) && e(t);
310
+ }
311
+ function Ge(s) {
312
+ const [r, e] = s;
313
+ return (t) => r(t) || e(t);
314
+ }
315
+ function Je(s) {
316
+ return s.length === 2;
317
+ }
318
+ function Xe(s) {
319
+ const r = s.val;
320
+ if (s.operator === E.NOT_EQUALS && !G(r))
321
+ return (n) => te.fn(n, r);
322
+ if (be(s.operator)) {
323
+ if (!G(r))
324
+ return () => !1;
325
+ const n = q(s.operator), i = Number(r);
326
+ return (o) => n.fn(o, i);
327
+ }
328
+ const e = q(s.operator);
329
+ return (t) => e.fn(t, r);
330
+ }
331
+ function Ye(s, r, e) {
332
+ const t = s.getCell(r, e);
333
+ if (!t)
334
+ return null;
335
+ const n = s.getCellRaw(r, e);
336
+ return t && !n ? J(t) : n ? t.t === H.NUMBER && typeof t.v == "string" ? n.v : J(n) : null;
337
+ }
338
+ function J(s) {
339
+ var t, n;
340
+ const r = (n = (t = s.p) == null ? void 0 : t.body) == null ? void 0 : n.dataStream;
341
+ if (r)
342
+ return r.trimEnd();
343
+ const e = s.v;
344
+ return typeof e == "string" ? s.t === H.BOOLEAN ? e.toUpperCase() : e : typeof e == "number" ? s.t === H.BOOLEAN ? e ? "TRUE" : "FALSE" : e : typeof e == "boolean" ? e ? "TRUE" : "FALSE" : "";
345
+ }
346
+ const C = {
347
+ id: "sheet.mutation.set-filter-range",
348
+ type: T.MUTATION,
349
+ handler: (s, r) => {
350
+ const { subUnitId: e, unitId: t, range: n } = r;
351
+ return s.get(v).ensureFilterModel(t, e).setRange(n), !0;
352
+ }
353
+ }, F = {
354
+ id: "sheet.mutation.set-filter-criteria",
355
+ type: T.MUTATION,
356
+ handler: (s, r) => {
357
+ const { subUnitId: e, unitId: t, criteria: n, col: i, reCalc: o = !0 } = r, a = s.get(v).getFilterModel(t, e);
358
+ return a ? (a.setCriteria(i, n, o), !0) : !1;
359
+ }
360
+ }, y = {
361
+ id: "sheet.mutation.remove-filter",
362
+ type: T.MUTATION,
363
+ handler: (s, r) => {
364
+ const { unitId: e, subUnitId: t } = r;
365
+ return s.get(v).removeFilterModel(e, t);
366
+ }
367
+ }, ie = {
368
+ id: "sheet.mutation.re-calc-filter",
369
+ type: T.MUTATION,
370
+ handler: (s, r) => {
371
+ const { unitId: e, subUnitId: t } = r, i = s.get(v).getFilterModel(e, t);
372
+ return i ? (i.reCalc(), !0) : !1;
373
+ }
374
+ };
375
+ var Ke = Object.defineProperty, Ze = Object.getOwnPropertyDescriptor, ke = (s, r, e, t) => {
376
+ for (var n = t > 1 ? void 0 : t ? Ze(r, e) : r, i = s.length - 1, o; i >= 0; i--)
377
+ (o = s[i]) && (n = (t ? o(r, e, n) : o(n)) || n);
378
+ return t && n && Ke(r, e, n), n;
379
+ }, b = (s, r) => (e, t) => r(e, t, s);
380
+ const et = /* @__PURE__ */ new Set([
381
+ C.id,
382
+ F.id,
383
+ y.id,
384
+ ie.id
385
+ ]), tt = "SHEET_FILTER_PLUGIN";
386
+ let v = class extends U {
387
+ constructor(r, e, t) {
388
+ super();
389
+ _(this, "_filterModels", /* @__PURE__ */ new Map());
390
+ _(this, "_loadedUnitId$", new I(null));
391
+ _(this, "loadedUnitId$", this._loadedUnitId$.asObservable());
392
+ _(this, "_activeFilterModel$", new I(null));
393
+ /** An observable value emitting the current Workbook's active Worksheet's filter model (if there is one). */
394
+ _(this, "activeFilterModel$", this._activeFilterModel$.asObservable());
395
+ this._resourcesManagerService = r, this._univerInstanceService = e, this._commandService = t, this._initModel(), this._initActiveFilterModel();
396
+ }
397
+ /** The current Workbook's active Worksheet's filter model (if there is one). */
398
+ get activeFilterModel() {
399
+ return this._activeFilterModel$.getValue();
400
+ }
401
+ /**
402
+ *
403
+ * @param unitId
404
+ * @param subUnitId
405
+ */
406
+ ensureFilterModel(r, e) {
407
+ const t = this.getFilterModel(r, e);
408
+ if (t)
409
+ return t;
410
+ const n = this._univerInstanceService.getUniverSheetInstance(r);
411
+ if (!n)
412
+ throw new Error(`[SheetsFilterService]: could not create "FilterModel" on a non-existing workbook ${r}!`);
413
+ const i = n.getSheetBySheetId(e);
414
+ if (!i)
415
+ throw new Error(`[SheetsFilterService]: could not create "FilterModel" on a non-existing worksheet ${e}!`);
416
+ const o = new L(r, e, i);
417
+ return this._cacheFilterModel(r, e, o), o;
418
+ }
419
+ getFilterModel(r, e) {
420
+ var t, n;
421
+ return (n = (t = this._filterModels.get(r)) == null ? void 0 : t.get(e)) != null ? n : null;
422
+ }
423
+ removeFilterModel(r, e) {
424
+ const t = this.getFilterModel(r, e);
425
+ return t ? (t.dispose(), this._filterModels.get(r).delete(e), !0) : !1;
426
+ }
427
+ _updateActiveFilterModel() {
428
+ let r;
429
+ try {
430
+ if (r = this._univerInstanceService.getCurrentUnitForType(W.UNIVER_SHEET), !r) {
431
+ this._activeFilterModel$.next(null);
432
+ return;
433
+ }
434
+ } catch {
435
+ return;
436
+ }
437
+ const e = r.getActiveSheet();
438
+ if (!e) {
439
+ this._activeFilterModel$.next(null);
440
+ return;
441
+ }
442
+ const t = e.getUnitId(), n = e.getSheetId(), i = this.getFilterModel(t, n);
443
+ this._activeFilterModel$.next(i);
444
+ }
445
+ _initActiveFilterModel() {
446
+ this.disposeWithMe(
447
+ Ce(
448
+ // source1: executing filter related mutations
449
+ he(this._commandService.onCommandExecuted).pipe(Re(([r]) => r.type === T.MUTATION && et.has(r.id))),
450
+ // source2: activte sheet changes
451
+ this._univerInstanceService.getCurrentTypeOfUnit$(W.UNIVER_SHEET).pipe(pe((r) => {
452
+ var e;
453
+ return (e = r == null ? void 0 : r.activeSheet$) != null ? e : Fe(null);
454
+ }))
455
+ ).subscribe(() => this._updateActiveFilterModel())
456
+ );
457
+ }
458
+ _serializeAutoFiltersForUnit(r) {
459
+ const e = this._filterModels.get(r);
460
+ if (!e)
461
+ return "{}";
462
+ const t = {};
463
+ return e.forEach((n, i) => {
464
+ t[i] = n.serialize();
465
+ }), JSON.stringify(t);
466
+ }
467
+ _deserializeAutoFiltersForUnit(r, e) {
468
+ const t = this._univerInstanceService.getUniverSheetInstance(r);
469
+ Object.keys(e).forEach((n) => {
470
+ const i = e[n], o = L.deserialize(r, n, t.getSheetBySheetId(n), i);
471
+ this._cacheFilterModel(r, n, o);
472
+ });
473
+ }
474
+ _initModel() {
475
+ this._resourcesManagerService.registerPluginResource({
476
+ pluginName: tt,
477
+ businesses: [2],
478
+ toJson: (r) => this._serializeAutoFiltersForUnit(r),
479
+ parseJson: (r) => JSON.parse(r),
480
+ onLoad: (r, e) => {
481
+ this._deserializeAutoFiltersForUnit(r, e), this._loadedUnitId$.next(r), this._updateActiveFilterModel();
482
+ },
483
+ onUnLoad: (r) => {
484
+ const e = this._filterModels.get(r);
485
+ e && (e.forEach((t) => t.dispose()), this._filterModels.delete(r));
486
+ }
487
+ });
488
+ }
489
+ _cacheFilterModel(r, e, t) {
490
+ this._filterModels.has(r) || this._filterModels.set(r, /* @__PURE__ */ new Map()), this._filterModels.get(r).set(e, t);
491
+ }
492
+ };
493
+ v = ke([
494
+ K(Z.Ready, v),
495
+ b(0, fe),
496
+ b(1, k),
497
+ b(2, ee)
498
+ ], v);
499
+ function rt(s, r) {
500
+ for (let e = 0; e < s.length; e++) {
501
+ let t = e;
502
+ if (s[e])
503
+ for (let n = e + 1; n < s.length; n++)
504
+ s[t] && s[n] && r(s[t], s[n]) && (s[t] = null, t = n);
505
+ }
506
+ return s.filter((e) => e !== null);
507
+ }
508
+ function M(s) {
509
+ return rt(s, (r, e) => r.id === F.id && e.id === F.id && r.params.unitId === e.params.unitId && r.params.subUnitId === e.params.subUnitId && r.params.col === e.params.col);
510
+ }
511
+ var st = Object.defineProperty, nt = Object.getOwnPropertyDescriptor, it = (s, r, e, t) => {
512
+ for (var n = t > 1 ? void 0 : t ? nt(r, e) : r, i = s.length - 1, o; i >= 0; i--)
513
+ (o = s[i]) && (n = (t ? o(r, e, n) : o(n)) || n);
514
+ return t && n && st(r, e, n), n;
515
+ }, N = (s, r) => (e, t) => r(e, t, s);
516
+ let P = class extends U {
517
+ constructor(s, r, e, t, n) {
518
+ super(), this._commandService = s, this._sheetInterceptorService = r, this._sheetsFilterService = e, this._univerInstanceService = t, this._refRangeService = n, this._initCommands(), this._initRowFilteredInterceptor(), this._initInterceptors(), this._commandExecutedListener();
519
+ }
520
+ _initCommands() {
521
+ [
522
+ F,
523
+ C,
524
+ ie,
525
+ y
526
+ ].forEach((s) => this.disposeWithMe(this._commandService.registerCommand(s)));
527
+ }
528
+ _initInterceptors() {
529
+ this.disposeWithMe(this._sheetInterceptorService.interceptCommand({
530
+ getMutations: (e) => this._getUpdateFilter(e)
531
+ }));
532
+ const s = new me(), r = (e, t) => {
533
+ var a;
534
+ const n = this._univerInstanceService.getUniverSheetInstance(e);
535
+ if (!n || !(n == null ? void 0 : n.getSheetBySheetId(t)))
536
+ return;
537
+ s.dispose();
538
+ const o = (a = this._sheetsFilterService.getFilterModel(e, t)) == null ? void 0 : a.getRange(), l = (u) => {
539
+ switch (u.id) {
540
+ case $e.id: {
541
+ const c = u.params, h = c.unitId || e, m = c.subUnitId || t;
542
+ return this._handleInsertRowCommand(c, h, m);
543
+ }
544
+ case xe.id: {
545
+ const c = u.params, h = c.unitId || e, m = c.subUnitId || t;
546
+ return this._handleInsertColCommand(c, h, m);
547
+ }
548
+ case Te.id: {
549
+ const c = u.params;
550
+ return this._handleRemoveColCommand(c, e, t);
551
+ }
552
+ case Ne.id: {
553
+ const c = u.params;
554
+ return this._handleRemoveRowCommand(c, e, t);
555
+ }
556
+ case Q.MoveColsCommandId: {
557
+ const c = u.params;
558
+ return this._handleMoveColsCommand(c, e, t);
559
+ }
560
+ case Q.MoveRowsCommandId: {
561
+ const c = u.params;
562
+ return this._handleMoveRowsCommand(c, e, t);
563
+ }
564
+ case Ae.id: {
565
+ const c = u.params;
566
+ return this._handleMoveRangeCommand(c, e, t);
567
+ }
568
+ }
569
+ return { redos: [], undos: [] };
570
+ };
571
+ o && s.add(this._refRangeService.registerRefRange(o, l, e, t));
572
+ };
573
+ this.disposeWithMe(this._commandService.onCommandExecuted((e) => {
574
+ if (e.id === ve.id) {
575
+ const t = e.params, n = t.subUnitId, i = t.unitId;
576
+ if (!n || !i)
577
+ return;
578
+ r(i, n);
579
+ }
580
+ if (e.id === C.id) {
581
+ const t = e.params, n = t.subUnitId, i = t.unitId;
582
+ if (!n || !i)
583
+ return;
584
+ r(t.unitId, t.subUnitId);
585
+ }
586
+ })), this.disposeWithMe(this._sheetsFilterService.loadedUnitId$.subscribe((e) => {
587
+ if (e) {
588
+ const t = this._univerInstanceService.getUniverSheetInstance(e), n = t == null ? void 0 : t.getActiveSheet();
589
+ n && r(e, n.getSheetId());
590
+ }
591
+ }));
592
+ }
593
+ _getUpdateFilter(s) {
594
+ const { id: r } = s;
595
+ switch (r) {
596
+ case Me.id: {
597
+ const e = s.params;
598
+ return this._handleRemoveSheetCommand(e, e.unitId, e.subUnitId);
599
+ }
600
+ }
601
+ return {
602
+ redos: [],
603
+ undos: []
604
+ };
605
+ }
606
+ _handleInsertColCommand(s, r, e) {
607
+ var f;
608
+ const t = this._sheetsFilterService.getFilterModel(r, e), n = (f = t == null ? void 0 : t.getRange()) != null ? f : null;
609
+ if (!t || !n)
610
+ return this._handleNull();
611
+ const { startColumn: i, endColumn: o } = n, { startColumn: l, endColumn: a } = s.range, u = a - l + 1;
612
+ if (a > o)
613
+ return this._handleNull();
614
+ const c = [], h = [], m = l, p = {
615
+ unitId: r,
616
+ subUnitId: e,
617
+ range: {
618
+ ...n,
619
+ startColumn: l <= i ? i + u : i,
620
+ endColumn: o + u
621
+ }
622
+ }, R = {
623
+ unitId: r,
624
+ subUnitId: e,
625
+ range: n
626
+ };
627
+ c.push({ id: C.id, params: p }), h.push({ id: C.id, params: R });
628
+ const d = t.getAllFilterColumns().filter((g) => g[0] >= m);
629
+ if (d.length !== 0) {
630
+ const { undos: g, redos: S } = this.moveCriteria(r, e, d, u);
631
+ c.push(...S), h.push(...g);
632
+ }
633
+ return { redos: M(c), undos: M(h) };
634
+ }
635
+ _handleInsertRowCommand(s, r, e) {
636
+ var R;
637
+ const t = this._sheetsFilterService.getFilterModel(r, e), n = (R = t == null ? void 0 : t.getRange()) != null ? R : null;
638
+ if (!t || !n)
639
+ return this._handleNull();
640
+ const { startRow: i, endRow: o } = n, { startRow: l, endRow: a } = s.range, u = a - l + 1;
641
+ if (a > o)
642
+ return this._handleNull();
643
+ const c = [], h = [], m = {
644
+ unitId: r,
645
+ subUnitId: e,
646
+ range: {
647
+ ...n,
648
+ startRow: l <= i ? i + u : i,
649
+ endRow: o + u
650
+ }
651
+ }, p = {
652
+ unitId: r,
653
+ subUnitId: e,
654
+ range: n
655
+ };
656
+ return c.push({ id: C.id, params: m }), h.push({ id: C.id, params: p }), {
657
+ redos: M(c),
658
+ undos: M(h)
659
+ };
660
+ }
661
+ _handleRemoveColCommand(s, r, e) {
662
+ var w;
663
+ const t = this._sheetsFilterService.getFilterModel(r, e), n = (w = t == null ? void 0 : t.getRange()) != null ? w : null;
664
+ if (!t || !n)
665
+ return this._handleNull();
666
+ const { startColumn: i, endColumn: o } = n, { startColumn: l, endColumn: a } = s.range;
667
+ if (l > o)
668
+ return this._handleNull();
669
+ const u = [], c = [], h = a < i ? 0 : Math.min(a, o) - Math.max(l, i) + 1, m = a - l + 1, p = t.getAllFilterColumns();
670
+ p.forEach((d) => {
671
+ const [f, g] = d;
672
+ f <= a && f >= l && (u.push({ id: F.id, params: { unitId: r, subUnitId: e, col: f, criteria: null } }), c.push({ id: F.id, params: { unitId: r, subUnitId: e, col: f, criteria: { ...g.serialize(), colId: f } } }));
673
+ });
674
+ const R = p.filter((d) => {
675
+ const [f, g] = d;
676
+ return f > a;
677
+ });
678
+ if (R.length > 0) {
679
+ const { undos: d, redos: f } = this.moveCriteria(r, e, R, -m);
680
+ u.push(...f), c.push(...d);
681
+ }
682
+ if (h === o - i + 1) {
683
+ const d = {
684
+ unitId: r,
685
+ subUnitId: e
686
+ };
687
+ u.push({ id: y.id, params: d });
688
+ } else if (i <= l) {
689
+ const d = o - h, f = {
690
+ unitId: r,
691
+ subUnitId: e,
692
+ range: {
693
+ ...n,
694
+ endColumn: d
695
+ }
696
+ };
697
+ u.push({ id: C.id, params: f });
698
+ } else {
699
+ const d = {
700
+ unitId: r,
701
+ subUnitId: e,
702
+ range: {
703
+ ...n,
704
+ startColumn: l,
705
+ endColumn: o - (a - l + 1)
706
+ }
707
+ };
708
+ u.push({ id: C.id, params: d });
709
+ }
710
+ return c.push({ id: C.id, params: { range: n, unitId: r, subUnitId: e } }), {
711
+ undos: M(c),
712
+ redos: M(u)
713
+ };
714
+ }
715
+ _handleRemoveRowCommand(s, r, e) {
716
+ var R;
717
+ const t = this._sheetsFilterService.getFilterModel(r, e);
718
+ if (!t)
719
+ return this._handleNull();
720
+ const n = t.getRange(), { startRow: i, endRow: o } = n, { startRow: l, endRow: a } = s.range;
721
+ if (l > o)
722
+ return this._handleNull();
723
+ const u = [], c = [], h = t.getAllFilterColumns(), m = i <= a && i >= l;
724
+ c.push({ id: C.id, params: { range: n, unitId: r, subUnitId: e } });
725
+ const p = Math.min(a, o) - Math.max(l, i) + 1;
726
+ if (p === o - i + 1 || m) {
727
+ const w = {
728
+ unitId: r,
729
+ subUnitId: e
730
+ };
731
+ u.push({ id: y.id, params: w }), h.forEach((d) => {
732
+ const [f, g] = d, S = {
733
+ unitId: r,
734
+ subUnitId: e,
735
+ col: f,
736
+ criteria: { ...g.serialize(), colId: f }
737
+ };
738
+ c.push({ id: F.id, params: S });
739
+ });
740
+ } else {
741
+ const w = (R = this._univerInstanceService.getUniverSheetInstance(r)) == null ? void 0 : R.getSheetBySheetId(e);
742
+ if (!w)
743
+ return this._handleNull();
744
+ const d = [];
745
+ for (let O = l; O <= a; O++)
746
+ w.getRowFiltered(O) && d.push(O);
747
+ const f = Math.min(i, l), g = f + (o - i) - p + d.length, S = {
748
+ unitId: r,
749
+ subUnitId: e,
750
+ range: {
751
+ ...n,
752
+ startRow: f,
753
+ endRow: g
754
+ }
755
+ };
756
+ u.push({ id: C.id, params: S });
757
+ }
758
+ return {
759
+ undos: M(c),
760
+ redos: M(u)
761
+ };
762
+ }
763
+ _handleMoveColsCommand(s, r, e) {
764
+ var w;
765
+ const t = this._sheetsFilterService.getFilterModel(r, e), n = (w = t == null ? void 0 : t.getRange()) != null ? w : null;
766
+ if (!t || !n)
767
+ return this._handleNull();
768
+ const { startColumn: i, endColumn: o } = n, { fromRange: l, toRange: a } = s;
769
+ if (l.endColumn < i && a.startColumn <= i || l.startColumn > o && a.endColumn > o)
770
+ return this._handleNull();
771
+ const u = [], c = [], h = {};
772
+ for (let d = i; d <= o; d++)
773
+ h[d] = {
774
+ colIndex: d,
775
+ filter: t.getFilterColumn(d)
776
+ };
777
+ D(l.startColumn, l.endColumn - l.startColumn + 1, a.startColumn, h);
778
+ const m = Object.keys(h).map((d) => Number(d)), p = Math.max(...m), R = Math.min(...m);
779
+ if (m.forEach((d) => {
780
+ var O, j;
781
+ const { colIndex: f, filter: g } = h[d], S = d;
782
+ if (g) {
783
+ const oe = {
784
+ unitId: r,
785
+ subUnitId: e,
786
+ col: S,
787
+ criteria: { ...g.serialize(), colId: S }
788
+ };
789
+ if (u.push({ id: F.id, params: oe }), c.push({ id: y.id, params: { unitId: r, subUnitId: e, col: S, criteria: { ...(O = t.getFilterColumn(S)) == null ? void 0 : O.serialize(), colId: S } } }), !((j = h[f]) != null && j.filter)) {
790
+ const le = {
791
+ unitId: r,
792
+ subUnitId: e,
793
+ col: f,
794
+ criteria: null
795
+ };
796
+ u.push({ id: F.id, params: le }), c.push({ id: F.id, params: { unitId: r, subUnitId: e, col: f, criteria: { ...g.serialize(), colId: f } } });
797
+ }
798
+ }
799
+ }), i !== R || o !== p) {
800
+ const d = {
801
+ unitId: r,
802
+ subUnitId: e,
803
+ range: {
804
+ ...n,
805
+ startColumn: R,
806
+ endColumn: p
807
+ }
808
+ };
809
+ u.unshift({ id: C.id, params: d }), c.push({ id: C.id, params: { range: n, unitId: r, subUnitId: e } });
810
+ }
811
+ return {
812
+ undos: c,
813
+ redos: u
814
+ };
815
+ }
816
+ _handleMoveRowsCommand(s, r, e) {
817
+ var w;
818
+ const t = this._sheetsFilterService.getFilterModel(r, e), n = (w = t == null ? void 0 : t.getRange()) != null ? w : null;
819
+ if (!t || !n)
820
+ return this._handleNull();
821
+ const { startRow: i, endRow: o } = n, { fromRange: l, toRange: a } = s;
822
+ if (l.endRow < i && a.startRow <= i || l.startRow > o && a.endRow > o)
823
+ return this._handleNull();
824
+ const u = [], c = [], h = {};
825
+ for (let d = i; d <= o; d++)
826
+ h[d] = {
827
+ offset: d - i
828
+ };
829
+ D(l.startRow, l.endRow - l.startRow + 1, a.startRow, h);
830
+ const m = Object.keys(h).map((d) => Number(d)), p = Math.max(...m), R = Math.min(...m);
831
+ if (i !== R || o !== p) {
832
+ const d = {
833
+ unitId: r,
834
+ subUnitId: e,
835
+ range: {
836
+ ...n,
837
+ startRow: R,
838
+ endRow: p
839
+ }
840
+ };
841
+ u.unshift({ id: C.id, params: d }), c.push({ id: C.id, params: { range: n, unitId: r, subUnitId: e } });
842
+ }
843
+ return {
844
+ redos: u,
845
+ undos: c
846
+ };
847
+ }
848
+ _handleMoveRangeCommand(s, r, e) {
849
+ const { fromRange: t, toRange: n } = s, i = this._sheetsFilterService.getFilterModel(r, e);
850
+ if (!i)
851
+ return this._handleNull();
852
+ const o = i.getRange();
853
+ if (!o)
854
+ return this._handleNull();
855
+ const l = [], a = [];
856
+ if (Y.contains(t, o)) {
857
+ const u = o.startRow - t.startRow, c = o.startColumn - t.startColumn, h = {
858
+ startRow: n.startRow + u,
859
+ startColumn: n.startColumn + c,
860
+ endRow: n.startRow + u + (o.endRow - o.startRow),
861
+ endColumn: n.startColumn + c + (o.endColumn - o.startColumn)
862
+ }, m = {
863
+ id: y.id,
864
+ params: {
865
+ unitId: r,
866
+ subUnitId: e
867
+ }
868
+ }, p = { id: C.id, params: { unitId: r, subUnitId: e, range: h } }, R = { id: C.id, params: { unitId: r, subUnitId: e, range: o } };
869
+ l.push(m, p), a.push(m, R);
870
+ const w = i.getAllFilterColumns(), d = n.startColumn - t.startColumn;
871
+ w.forEach((f) => {
872
+ const [g, S] = f;
873
+ S && (l.push({ id: F.id, params: { unitId: r, subUnitId: e, col: g + d, criteria: { ...S.serialize(), colId: g + d } } }), a.push({ id: F.id, params: { unitId: r, subUnitId: e, col: g, criteria: { ...S.serialize(), colId: g } } }));
874
+ });
875
+ }
876
+ return {
877
+ redos: l,
878
+ undos: a
879
+ };
880
+ }
881
+ _handleRemoveSheetCommand(s, r, e) {
882
+ const t = this._sheetsFilterService.getFilterModel(r, e);
883
+ if (!t)
884
+ return this._handleNull();
885
+ const n = t.getRange();
886
+ if (!n)
887
+ return this._handleNull();
888
+ const i = [], o = [];
889
+ return t.getAllFilterColumns().forEach((a) => {
890
+ const [u, c] = a;
891
+ o.push({ id: F.id, params: { unitId: r, subUnitId: e, col: a, criteria: { ...c.serialize(), colId: a } } });
892
+ }), i.push({ id: y.id, params: { unitId: r, subUnitId: e, range: n } }), o.unshift({ id: C.id, params: { range: n, unitId: r, subUnitId: e } }), {
893
+ undos: o,
894
+ redos: i
895
+ };
896
+ }
897
+ _handleNull() {
898
+ return { redos: [], undos: [] };
899
+ }
900
+ _initRowFilteredInterceptor() {
901
+ this.disposeWithMe(this._sheetInterceptorService.intercept(Ee.ROW_FILTERED, {
902
+ handler: (s, r) => {
903
+ var e, t;
904
+ return s ? !0 : (t = (e = this._sheetsFilterService.getFilterModel(
905
+ r.unitId,
906
+ r.subUnitId
907
+ )) == null ? void 0 : e.isRowFiltered(r.row)) != null ? t : !1;
908
+ }
909
+ }));
910
+ }
911
+ moveCriteria(s, r, e, t) {
912
+ const n = {
913
+ unitId: s,
914
+ subUnitId: r,
915
+ criteria: null,
916
+ col: -1
917
+ }, i = [], o = [];
918
+ return e.forEach((l) => {
919
+ const [a, u] = l;
920
+ o.push({
921
+ id: F.id,
922
+ params: {
923
+ ...n,
924
+ col: a
925
+ }
926
+ }), i.push({
927
+ id: F.id,
928
+ params: {
929
+ ...n,
930
+ col: a,
931
+ criteria: { ...u.serialize(), colId: a }
932
+ }
933
+ });
934
+ }), e.forEach((l) => {
935
+ const [a, u] = l;
936
+ o.push({
937
+ id: F.id,
938
+ params: {
939
+ ...n,
940
+ col: a + t,
941
+ criteria: { ...u.serialize(), colId: a + t }
942
+ }
943
+ }), i.push({
944
+ id: F.id,
945
+ params: {
946
+ ...n,
947
+ col: a + t,
948
+ criteria: null
949
+ }
950
+ });
951
+ }), {
952
+ redos: o,
953
+ undos: i
954
+ };
955
+ }
956
+ _commandExecutedListener() {
957
+ this.disposeWithMe(this._commandService.onCommandExecuted((s) => {
958
+ const { unitId: r, subUnitId: e } = s.params || {}, t = this._sheetsFilterService.getFilterModel(r, e);
959
+ if (!t)
960
+ return;
961
+ const n = Array.from(t.filteredOutRows).sort((l, a) => l - a), i = [];
962
+ let o = !1;
963
+ if (s.id === Oe.id) {
964
+ const { startRow: l, endRow: a } = s.params.range, u = n.filter((c) => c >= l && c <= a);
965
+ n.forEach((c) => {
966
+ if (c < l)
967
+ i.push(c);
968
+ else if (o = !0, c <= a) {
969
+ const h = Math.max(l, i.length ? i[i.length - 1] + 1 : l);
970
+ i.push(h);
971
+ } else
972
+ i.push(c - (a - l + 1 - u.length));
973
+ });
974
+ }
975
+ if (s.id === ye.id) {
976
+ const { startRow: l, endRow: a } = s.params.range;
977
+ n.forEach((u) => {
978
+ u >= l ? (o = !0, i.push(u + (a - l + 1))) : i.push(u);
979
+ });
980
+ }
981
+ o && (t.filteredOutRows = new Set(i));
982
+ }));
983
+ }
984
+ };
985
+ P = it([
986
+ K(Z.Ready, P),
987
+ N(0, ee),
988
+ N(1, $(we)),
989
+ N(2, $(v)),
990
+ N(3, k),
991
+ N(4, $(Se))
992
+ ], P);
993
+ var ot = Object.defineProperty, lt = Object.getOwnPropertyDescriptor, at = (s, r, e, t) => {
994
+ for (var n = t > 1 ? void 0 : t ? lt(r, e) : r, i = s.length - 1, o; i >= 0; i--)
995
+ (o = s[i]) && (n = (t ? o(r, e, n) : o(n)) || n);
996
+ return t && n && ot(r, e, n), n;
997
+ }, ct = (s, r) => (e, t) => r(e, t, s);
998
+ const ut = "UNIVER_SHEETS_FILTER_PLUGIN";
999
+ var x;
1000
+ let X = (x = class extends _e {
1001
+ constructor(s, r) {
1002
+ super(), this._injector = r;
1003
+ }
1004
+ onStarting(s) {
1005
+ [
1006
+ [v],
1007
+ [P]
1008
+ ].forEach((r) => s.add(r));
1009
+ }
1010
+ }, _(x, "type", W.UNIVER_SHEET), _(x, "pluginName", ut), x);
1011
+ X = at([
1012
+ ct(1, $(ge))
1013
+ ], X);
1014
+ export {
1015
+ E as CustomFilterOperator,
1016
+ et as FILTER_MUTATIONS,
1017
+ je as FilterColumn,
1018
+ L as FilterModel,
1019
+ ie as ReCalcSheetsFilterMutation,
1020
+ y as RemoveSheetsFilterMutation,
1021
+ tt as SHEET_FILTER_SNAPSHOT_ID,
1022
+ F as SetSheetsFilterCriteriaMutation,
1023
+ C as SetSheetsFilterRangeMutation,
1024
+ v as SheetsFilterService,
1025
+ X as UniverSheetsFilterPlugin,
1026
+ Be as equals,
1027
+ q as getCustomFilterFn,
1028
+ Pe as greaterThan,
1029
+ Ie as greaterThanOrEqualTo,
1030
+ Le as lessThan,
1031
+ Ue as lessThanOrEqualTo,
1032
+ te as notEquals
1033
+ };