chunk-KSOBIBXF.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. import {
  2. $,
  3. Be,
  4. F,
  5. Fn,
  6. Gn,
  7. Jd,
  8. Mh,
  9. N,
  10. Q,
  11. V,
  12. _t,
  13. an,
  14. et,
  15. gt,
  16. ko,
  17. mt,
  18. pt,
  19. qe,
  20. xs,
  21. zn
  22. } from "./chunk-BW43Z7N3.js";
  23. import {
  24. AR,
  25. BehaviorSubject,
  26. Bi,
  27. Fe,
  28. Gf,
  29. Ko,
  30. Mi,
  31. NE,
  32. Ot,
  33. Pt,
  34. Ra,
  35. SE,
  36. Vr,
  37. _n,
  38. ar,
  39. cn,
  40. d0,
  41. dn,
  42. en,
  43. filter,
  44. merge,
  45. mu,
  46. nt,
  47. nu,
  48. of,
  49. ot,
  50. re,
  51. switchMap,
  52. tE
  53. } from "./chunk-23V3HWTR.js";
  54. // node_modules/.pnpm/@univerjs+sheets-filter@0.5.5_@grpc+grpc-js@1.13.4_react@18.3.1_rxjs@7.8.1/node_modules/@univerjs/sheets-filter/lib/es/index.js
  55. var Fe2 = Object.defineProperty;
  56. var we = (i, r, e) => r in i ? Fe2(i, r, { enumerable: true, configurable: true, writable: true, value: e }) : i[r] = e;
  57. var w = (i, r, e) => we(i, typeof r != "symbol" ? r + "" : r, e);
  58. var tt = "sheets-filter.config";
  59. var re2 = {};
  60. var ce = "sheet.mutation.set-filter-range";
  61. var de = "sheet.mutation.set-filter-criteria";
  62. var ue = "sheet.mutation.remove-filter";
  63. var he = "sheet.mutation.re-calc-filter";
  64. var rt = /* @__PURE__ */ new Set([
  65. ce,
  66. de,
  67. ue,
  68. he
  69. ]);
  70. var N2 = ((i) => (i.EQUAL = "equal", i.GREATER_THAN = "greaterThan", i.GREATER_THAN_OR_EQUAL = "greaterThanOrEqual", i.LESS_THAN = "lessThan", i.LESS_THAN_OR_EQUAL = "lessThanOrEqual", i.NOT_EQUALS = "notEqual", i))(N2 || {});
  71. var nt2 = {
  72. operator: N2.GREATER_THAN,
  73. fn: (i, r) => $2(i) ? i > r : false
  74. };
  75. var it = {
  76. operator: N2.GREATER_THAN_OR_EQUAL,
  77. fn: (i, r) => $2(i) ? i >= r : false
  78. };
  79. var st = {
  80. operator: N2.LESS_THAN,
  81. fn: (i, r) => $2(i) ? i < r : false
  82. };
  83. var ot2 = {
  84. operator: N2.LESS_THAN_OR_EQUAL,
  85. fn: (i, r) => $2(i) ? i <= r : false
  86. };
  87. var lt = {
  88. operator: N2.EQUAL,
  89. fn: (i, r) => $2(i) ? i === r : false
  90. };
  91. var fe = {
  92. operator: N2.NOT_EQUALS,
  93. fn: (i, r) => {
  94. if (typeof r == "string") {
  95. if (r === " ")
  96. return i != null;
  97. const e = ge(i);
  98. return e && ut(r) ? !_e(r).test(e) : e !== r;
  99. }
  100. return $2(i) ? i !== r : true;
  101. }
  102. };
  103. var me = /* @__PURE__ */ new Map([]);
  104. var at = [nt2, it, st, ot2, lt, fe];
  105. at.forEach((i) => {
  106. me.set(i.operator, i);
  107. });
  108. function ct(i) {
  109. return !!i;
  110. }
  111. var dt = {
  112. fn: (i, r) => {
  113. const e = ge(i);
  114. return e === null ? r === "" : _e(r).test(e);
  115. }
  116. };
  117. function ne(i) {
  118. return i ? me.get(i) : dt;
  119. }
  120. function $2(i) {
  121. return typeof i == "number";
  122. }
  123. function ie(i) {
  124. return !!(typeof i == "number" || typeof i == "string" && d0(i));
  125. }
  126. function ge(i) {
  127. return typeof i == "boolean" || i == null ? null : typeof i == "string" ? i : i.toString();
  128. }
  129. function ut(i) {
  130. return typeof i == "number" ? false : i.indexOf("*") !== -1 || i.indexOf("?") !== -1;
  131. }
  132. function _e(i) {
  133. const r = i.replace(/[.+^${}()|[\]\\]/g, "\\$&").replaceAll("?", ".").replace(/[*]/g, ".$&");
  134. return new RegExp(`^${r}$`);
  135. }
  136. var G = () => /* @__PURE__ */ new Set();
  137. var D = class _D extends nt {
  138. constructor(e, t, n) {
  139. super();
  140. w(this, "_filteredOutRows$", new BehaviorSubject(G()));
  141. w(this, "filteredOutRows$", this._filteredOutRows$.asObservable());
  142. w(this, "_hasCriteria$", new BehaviorSubject(false));
  143. w(this, "hasCriteria$", this._hasCriteria$.asObservable());
  144. w(this, "_filterColumnByIndex", /* @__PURE__ */ new Map());
  145. w(this, "_alreadyFilteredOutRows", G());
  146. w(this, "_range");
  147. this.unitId = e, this.subUnitId = t, this._worksheet = n;
  148. }
  149. get filteredOutRows() {
  150. return this._filteredOutRows$.getValue();
  151. }
  152. set filteredOutRows(e) {
  153. this._alreadyFilteredOutRows = e, this._filteredOutRows$.next(e);
  154. }
  155. dispose() {
  156. super.dispose(), this._filteredOutRows$.complete(), this._hasCriteria$.complete();
  157. }
  158. /**
  159. * Serialize this filter model to the JSON format representation.
  160. */
  161. serialize() {
  162. const e = {
  163. ref: en.clone(this._range),
  164. filterColumns: this._getAllFilterColumns(true).sort(([t], [n]) => t - n).map(([t, n]) => n.serialize())
  165. };
  166. return this._alreadyFilteredOutRows && (e.cachedFilteredOut = Array.from(this._alreadyFilteredOutRows).sort()), e;
  167. }
  168. /**
  169. * Deserialize auto filter info to construct a `FilterModel` object.
  170. * @param unitId workbook id
  171. * @param subUnitId worksheet id
  172. * @param worksheet the Worksheet object
  173. * @param autoFilter auto filter data
  174. */
  175. static deserialize(e, t, n, s) {
  176. const o = new _D(e, t, n);
  177. return o._dump(s), o;
  178. }
  179. _dump(e) {
  180. var t;
  181. 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();
  182. }
  183. isRowFiltered(e) {
  184. return this._alreadyFilteredOutRows.has(e);
  185. }
  186. getRange() {
  187. if (!this._range)
  188. throw new Error("[FilterModel] could not get range before a range is set!");
  189. return this._range;
  190. }
  191. /**
  192. * Get filtered out rows except the specific column. This method is considered as "pure". In
  193. * another word it would not change `filteredOutRows` on `FilterModel` nor `FilterColumn`.
  194. * @param col
  195. */
  196. getFilteredOutRowsExceptCol(e) {
  197. return this._getAllFilterColumns(true).filter(([t]) => t !== e).reduce((t, [, n]) => {
  198. const s = n.calc({ getAlreadyFilteredOutRows: () => t });
  199. return s ? AR(t, s) : t;
  200. }, /* @__PURE__ */ new Set());
  201. }
  202. /**
  203. * Set range of the filter model, this would remove some `IFilterColumn`
  204. * if the new range not overlaps the old range.
  205. */
  206. setRange(e) {
  207. this._range = e, this._getAllFilterColumns(true).forEach(([t, n]) => {
  208. n.setRangeAndColumn({
  209. startRow: e.startRow,
  210. endRow: e.endRow,
  211. startColumn: t,
  212. endColumn: t
  213. }, t);
  214. });
  215. }
  216. /**
  217. * Set or remove filter criteria on a specific row.
  218. */
  219. setCriteria(e, t, n = false) {
  220. if (!this._range)
  221. throw new Error("[FilterModel] could not set criteria before a range is set!");
  222. if (!t) {
  223. this._removeCriteria(e), this._rebuildAlreadyFilteredOutRowsWithCache(), n && this._reCalcAllColumns(), this._emit(), this._emitHasCriteria();
  224. return;
  225. }
  226. this._setCriteriaWithoutReCalc(e, t), n && (this._rebuildAlreadyFilteredOutRowsWithCache(), this._reCalcWithNoCacheColumns(), this._emit(), this._emitHasCriteria());
  227. }
  228. getAllFilterColumns() {
  229. return this._getAllFilterColumns(true);
  230. }
  231. getFilterColumn(e) {
  232. var t;
  233. return (t = this._filterColumnByIndex.get(e)) != null ? t : null;
  234. }
  235. reCalc() {
  236. this._reCalcAllColumns(), this._emit();
  237. }
  238. _getAllFilterColumns(e = false) {
  239. const t = Array.from(this._filterColumnByIndex.entries());
  240. return e ? t : t.map(([n, s]) => s);
  241. }
  242. _reCalcAllColumns() {
  243. this._alreadyFilteredOutRows = G(), this._getAllFilterColumns().forEach((e) => e.__clearCache()), this._reCalcWithNoCacheColumns();
  244. }
  245. _setCriteriaWithoutReCalc(e, t) {
  246. const n = this._range;
  247. if (!n)
  248. throw new Error("[FilterModel] could not set criteria before a range is set!");
  249. const { startColumn: s, endColumn: o } = n;
  250. if (e > o || e < s)
  251. throw new Error(`[FilterModel] could not set criteria on column ${e} which is out of range!`);
  252. let l;
  253. this._filterColumnByIndex.has(e) ? l = this._filterColumnByIndex.get(e) : (l = new ht(
  254. this.unitId,
  255. this.subUnitId,
  256. this._worksheet,
  257. t,
  258. { getAlreadyFilteredOutRows: () => this._alreadyFilteredOutRows }
  259. ), l.setRangeAndColumn(n, e), this._filterColumnByIndex.set(e, l)), l.setCriteria(t);
  260. }
  261. _removeCriteria(e) {
  262. const t = this._filterColumnByIndex.get(e);
  263. t && (t.dispose(), this._filterColumnByIndex.delete(e));
  264. }
  265. _emit() {
  266. this._filteredOutRows$.next(this._alreadyFilteredOutRows);
  267. }
  268. _emitHasCriteria() {
  269. this._hasCriteria$.next(this._filterColumnByIndex.size > 0);
  270. }
  271. _rebuildAlreadyFilteredOutRowsWithCache() {
  272. const e = this._getAllFilterColumns().filter((t) => t.hasCache()).reduce((t, n) => AR(t, n.filteredOutRows), /* @__PURE__ */ new Set());
  273. this._alreadyFilteredOutRows = e;
  274. }
  275. _reCalcWithNoCacheColumns() {
  276. const e = this._getAllFilterColumns().filter((t) => !t.hasCache());
  277. for (const t of e) {
  278. const n = t.reCalc();
  279. n && (this._alreadyFilteredOutRows = AR(this._alreadyFilteredOutRows, n));
  280. }
  281. }
  282. };
  283. var ht = class extends nt {
  284. constructor(e, t, n, s, o) {
  285. super();
  286. w(this, "_filteredOutRows", null);
  287. w(this, "_filterFn", null);
  288. w(this, "_range", null);
  289. w(this, "_column", 0);
  290. w(this, "_filterByValues", false);
  291. this.unitId = e, this.subUnitId = t, this._worksheet = n, this._criteria = s, this._filterColumnContext = o;
  292. }
  293. get filteredOutRows() {
  294. return this._filteredOutRows;
  295. }
  296. dispose() {
  297. super.dispose(), this._filteredOutRows = null;
  298. }
  299. /**
  300. * @internal
  301. */
  302. __clearCache() {
  303. this._filteredOutRows = null;
  304. }
  305. serialize() {
  306. if (!this._criteria)
  307. throw new Error("[FilterColumn]: could not serialize without a filter column!");
  308. return re.deepClone({
  309. ...this._criteria,
  310. colId: this._column
  311. });
  312. }
  313. hasCache() {
  314. return this._filteredOutRows !== null;
  315. }
  316. // The first row should be omitted!
  317. setRangeAndColumn(e, t) {
  318. this._range = e, this._column = t;
  319. }
  320. setCriteria(e) {
  321. this._criteria = e, this._generateFilterFn(), this._filteredOutRows = null;
  322. }
  323. getColumnData() {
  324. return re.deepClone(this._criteria);
  325. }
  326. /**
  327. * Trigger new calculation on this `FilterModel` instance.
  328. *
  329. * @external DO NOT EVER call this method from `FilterColumn` itself. The whole process heavily relies on
  330. * `filteredOutByOthers`, and it is more comprehensible if we let `FilterModel` take full control over the process.
  331. */
  332. reCalc() {
  333. return this._filteredOutRows = this.calc(this._filterColumnContext), this._filteredOutRows;
  334. }
  335. calc(e) {
  336. if (!this._filterFn)
  337. throw new Error("[FilterColumn] cannot calculate without a filter fn!");
  338. if (!this._range)
  339. throw new Error("[FilterColumn] cannot calculate without a range!");
  340. if (typeof this._column != "number")
  341. throw new TypeError("[FilterColumn] cannot calculate without a column offset!");
  342. const t = this._column, n = { startColumn: t, endColumn: t, startRow: this._range.startRow + 1, endRow: this._range.endRow }, s = /* @__PURE__ */ new Set(), o = e.getAlreadyFilteredOutRows();
  343. for (const l of this._worksheet.iterateByColumn(n, false, false)) {
  344. const { row: a, rowSpan: c, col: h } = l;
  345. if (o.has(a) && (!c || c === 1))
  346. continue;
  347. const d = this._filterByValues ? tE(this._worksheet.getCell(a, h)) : Ft(this._worksheet, a, h);
  348. if (!this._filterFn(d) && (s.add(a), c))
  349. for (let u = 1; u < c; u++)
  350. s.add(a + u);
  351. }
  352. return s;
  353. }
  354. _generateFilterFn() {
  355. this._criteria && (this._filterFn = ft(this._criteria), this._filterByValues = !!this._criteria.filters);
  356. }
  357. };
  358. function ft(i) {
  359. if (i.filters)
  360. return mt2(i.filters);
  361. if (i.customFilters)
  362. return gt2(i.customFilters);
  363. throw new Error("[FilterModel]: other types of filters are not supported yet.");
  364. }
  365. function mt2(i) {
  366. const r = !!i.blank, e = new Set(i.filters);
  367. return (t) => t === void 0 || t === "" ? r : e.has(typeof t == "string" ? t : `${t}`);
  368. }
  369. function gt2(i) {
  370. const r = i.customFilters.map((e) => pt2(e));
  371. return Ct(r) ? i.and ? _t2(r) : Rt(r) : r[0];
  372. }
  373. function _t2(i) {
  374. const [r, e] = i;
  375. return (t) => r(t) && e(t);
  376. }
  377. function Rt(i) {
  378. const [r, e] = i;
  379. return (t) => r(t) || e(t);
  380. }
  381. function Ct(i) {
  382. return i.length === 2;
  383. }
  384. function pt2(i) {
  385. const r = i.val;
  386. if (i.operator === N2.NOT_EQUALS && !ie(r))
  387. return (n) => fe.fn(n, r);
  388. if (ct(i.operator)) {
  389. if (!ie(r))
  390. return () => false;
  391. const n = ne(i.operator), s = Number(r);
  392. return (o) => n.fn(o, s);
  393. }
  394. const e = ne(i.operator);
  395. return (t) => e.fn(t, r);
  396. }
  397. function Ft(i, r, e) {
  398. const t = i.getCell(r, e);
  399. if (!t)
  400. return null;
  401. const n = i.getCellRaw(r, e);
  402. return t && !n ? se(t) : n ? t.t === dn.NUMBER && typeof t.v == "string" ? n.v : se(n) : null;
  403. }
  404. function se(i) {
  405. var t, n;
  406. const r = (n = (t = i.p) == null ? void 0 : t.body) == null ? void 0 : n.dataStream;
  407. if (r)
  408. return r.trimEnd();
  409. const e = i.v;
  410. return typeof e == "string" ? i.t === dn.BOOLEAN ? e.toUpperCase() : e : typeof e == "number" ? i.t === dn.BOOLEAN ? e ? "TRUE" : "FALSE" : e : typeof e == "boolean" ? e ? "TRUE" : "FALSE" : "";
  411. }
  412. var wt = Object.defineProperty;
  413. var St = Object.getOwnPropertyDescriptor;
  414. var vt = (i, r, e, t) => {
  415. for (var n = t > 1 ? void 0 : t ? St(r, e) : r, s = i.length - 1, o; s >= 0; s--)
  416. (o = i[s]) && (n = (t ? o(r, e, n) : o(n)) || n);
  417. return t && n && wt(r, e, n), n;
  418. };
  419. var J = (i, r) => (e, t) => r(e, t, i);
  420. var Re = "SHEET_FILTER_PLUGIN";
  421. var M = class extends nt {
  422. constructor(r, e, t) {
  423. super();
  424. w(this, "_filterModels", /* @__PURE__ */ new Map());
  425. w(this, "_loadedUnitId$", new BehaviorSubject(null));
  426. w(this, "loadedUnitId$", this._loadedUnitId$.asObservable());
  427. w(this, "_errorMsg$", new BehaviorSubject(null));
  428. w(this, "errorMsg$", this._errorMsg$.asObservable());
  429. w(this, "_activeFilterModel$", new BehaviorSubject(null));
  430. w(this, "activeFilterModel$", this._activeFilterModel$.asObservable());
  431. this._resourcesManagerService = r, this._univerInstanceService = e, this._commandService = t, this._initModel(), this._initActiveFilterModel();
  432. }
  433. /** The current Workbook's active Worksheet's filter model (if there is one). */
  434. get activeFilterModel() {
  435. return this._activeFilterModel$.getValue();
  436. }
  437. /**
  438. *
  439. * @param unitId
  440. * @param subUnitId
  441. */
  442. ensureFilterModel(r, e) {
  443. const t = this.getFilterModel(r, e);
  444. if (t)
  445. return t;
  446. const n = this._univerInstanceService.getUniverSheetInstance(r);
  447. if (!n)
  448. throw new Error(`[SheetsFilterService]: could not create "FilterModel" on a non-existing workbook ${r}!`);
  449. const s = n.getSheetBySheetId(e);
  450. if (!s)
  451. throw new Error(`[SheetsFilterService]: could not create "FilterModel" on a non-existing worksheet ${e}!`);
  452. const o = new D(r, e, s);
  453. return this._cacheFilterModel(r, e, o), o;
  454. }
  455. getFilterModel(r, e) {
  456. var t, n;
  457. return (n = (t = this._filterModels.get(r)) == null ? void 0 : t.get(e)) != null ? n : null;
  458. }
  459. removeFilterModel(r, e) {
  460. const t = this.getFilterModel(r, e);
  461. return t ? (t.dispose(), this._filterModels.get(r).delete(e), true) : false;
  462. }
  463. setFilterErrorMsg(r) {
  464. this._errorMsg$.next(r);
  465. }
  466. _updateActiveFilterModel() {
  467. let r;
  468. try {
  469. if (r = this._univerInstanceService.getCurrentUnitForType(Fe.UNIVER_SHEET), !r) {
  470. this._activeFilterModel$.next(null);
  471. return;
  472. }
  473. } catch (o) {
  474. console.error("[SheetsFilterService]: could not get active workbook!", o);
  475. return;
  476. }
  477. const e = r.getActiveSheet(true);
  478. if (!e) {
  479. this._activeFilterModel$.next(null);
  480. return;
  481. }
  482. const t = e.getUnitId(), n = e.getSheetId(), s = this.getFilterModel(t, n);
  483. this._activeFilterModel$.next(s);
  484. }
  485. _initActiveFilterModel() {
  486. this.disposeWithMe(
  487. merge(
  488. // source1: executing filter related mutations
  489. Gf(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(filter(([r]) => r.type === ar.MUTATION && rt.has(r.id))),
  490. // source2: activate sheet changes
  491. this._univerInstanceService.getCurrentTypeOfUnit$(Fe.UNIVER_SHEET).pipe(switchMap((r) => {
  492. var e;
  493. return (e = r == null ? void 0 : r.activeSheet$) != null ? e : of(null);
  494. }))
  495. ).subscribe(() => this._updateActiveFilterModel())
  496. );
  497. }
  498. _serializeAutoFiltersForUnit(r) {
  499. const e = this._filterModels.get(r);
  500. if (!e)
  501. return "{}";
  502. const t = {};
  503. return e.forEach((n, s) => {
  504. t[s] = n.serialize();
  505. }), JSON.stringify(t);
  506. }
  507. _deserializeAutoFiltersForUnit(r, e) {
  508. const t = this._univerInstanceService.getUniverSheetInstance(r);
  509. Object.keys(e).forEach((n) => {
  510. const s = e[n], o = D.deserialize(r, n, t.getSheetBySheetId(n), s);
  511. this._cacheFilterModel(r, n, o);
  512. });
  513. }
  514. _initModel() {
  515. this._resourcesManagerService.registerPluginResource({
  516. pluginName: Re,
  517. businesses: [Fe.UNIVER_SHEET],
  518. toJson: (r) => this._serializeAutoFiltersForUnit(r),
  519. parseJson: (r) => JSON.parse(r),
  520. onLoad: (r, e) => {
  521. this._deserializeAutoFiltersForUnit(r, e), this._loadedUnitId$.next(r), this._updateActiveFilterModel();
  522. },
  523. onUnLoad: (r) => {
  524. const e = this._filterModels.get(r);
  525. e && (e.forEach((t) => t.dispose()), this._filterModels.delete(r));
  526. }
  527. });
  528. }
  529. _cacheFilterModel(r, e, t) {
  530. this._filterModels.has(r) || this._filterModels.set(r, /* @__PURE__ */ new Map()), this._filterModels.get(r).set(e, t);
  531. }
  532. };
  533. M = vt([
  534. J(0, Bi),
  535. J(1, _n),
  536. J(2, Pt)
  537. ], M);
  538. var R = {
  539. id: ce,
  540. type: ar.MUTATION,
  541. handler: (i, r) => {
  542. const { subUnitId: e, unitId: t, range: n } = r;
  543. return i.get(M).ensureFilterModel(t, e).setRange(n), true;
  544. }
  545. };
  546. var p = {
  547. id: de,
  548. type: ar.MUTATION,
  549. handler: (i, r) => {
  550. const { subUnitId: e, unitId: t, criteria: n, col: s, reCalc: o = true } = r, a = i.get(M).getFilterModel(t, e);
  551. return a ? (a.setCriteria(s, n, o), true) : false;
  552. }
  553. };
  554. var O = {
  555. id: ue,
  556. type: ar.MUTATION,
  557. handler: (i, r) => {
  558. const { unitId: e, subUnitId: t } = r;
  559. return i.get(M).removeFilterModel(e, t);
  560. }
  561. };
  562. var H = {
  563. id: he,
  564. type: ar.MUTATION,
  565. handler: (i, r) => {
  566. const { unitId: e, subUnitId: t } = r, s = i.get(M).getFilterModel(e, t);
  567. return s ? (s.reCalc(), true) : false;
  568. }
  569. };
  570. function Mt(i, r) {
  571. for (let e = 0; e < i.length; e++) {
  572. let t = e;
  573. if (i[e])
  574. for (let n = e + 1; n < i.length; n++)
  575. i[t] && i[n] && r(i[t], i[n]) && (i[t] = null, t = n);
  576. }
  577. return i.filter((e) => e !== null);
  578. }
  579. function I(i) {
  580. return Mt(i, (r, e) => r.id === p.id && e.id === p.id && r.params.unitId === e.params.unitId && r.params.subUnitId === e.params.subUnitId && r.params.col === e.params.col);
  581. }
  582. var Et = Object.defineProperty;
  583. var Ot2 = Object.getOwnPropertyDescriptor;
  584. var yt = (i, r, e, t) => {
  585. for (var n = t > 1 ? void 0 : t ? Ot2(r, e) : r, s = i.length - 1, o; s >= 0; s--)
  586. (o = i[s]) && (n = (t ? o(r, e, n) : o(n)) || n);
  587. return t && n && Et(r, e, n), n;
  588. };
  589. var P = (i, r) => (e, t) => r(e, t, i);
  590. var W = class extends nt {
  591. constructor(r, e, t, n, s) {
  592. super();
  593. w(this, "_disposableCollection", new Mi());
  594. this._commandService = r, this._sheetInterceptorService = e, this._sheetsFilterService = t, this._univerInstanceService = n, this._refRangeService = s, this._initCommands(), this._initRowFilteredInterceptor(), this._initInterceptors(), this._commandExecutedListener(), this._initErrorHandling();
  595. }
  596. _initCommands() {
  597. [
  598. p,
  599. R,
  600. H,
  601. O
  602. ].forEach((r) => this.disposeWithMe(this._commandService.registerCommand(r)));
  603. }
  604. _initInterceptors() {
  605. this.disposeWithMe(this._sheetInterceptorService.interceptCommand({
  606. getMutations: (r) => this._getUpdateFilter(r)
  607. })), this.disposeWithMe(this._commandService.onCommandExecuted((r) => {
  608. if (r.id === an.id) {
  609. const e = r.params, t = e.subUnitId, n = e.unitId;
  610. if (!t || !n)
  611. return;
  612. this._registerRefRange(n, t);
  613. }
  614. if (r.id === R.id) {
  615. const e = r.params, t = e.subUnitId, n = e.unitId;
  616. if (!t || !n)
  617. return;
  618. this._registerRefRange(e.unitId, e.subUnitId);
  619. }
  620. })), this.disposeWithMe(this._sheetsFilterService.loadedUnitId$.subscribe((r) => {
  621. if (r) {
  622. const e = this._univerInstanceService.getUniverSheetInstance(r), t = e == null ? void 0 : e.getActiveSheet();
  623. t && this._registerRefRange(r, t.getSheetId());
  624. }
  625. }));
  626. }
  627. _registerRefRange(r, e) {
  628. var l;
  629. this._disposableCollection.dispose();
  630. const t = this._univerInstanceService.getUniverSheetInstance(r), n = t == null ? void 0 : t.getSheetBySheetId(e);
  631. if (!t || !n)
  632. return;
  633. const s = (l = this._sheetsFilterService.getFilterModel(r, e)) == null ? void 0 : l.getRange(), o = (a) => {
  634. switch (a.id) {
  635. case mt.id: {
  636. const c = a.params, h = c.unitId || r, d = c.subUnitId || e;
  637. return this._handleInsertRowCommand(c, h, d);
  638. }
  639. case gt.id: {
  640. const c = a.params, h = c.unitId || r, d = c.subUnitId || e;
  641. return this._handleInsertColCommand(c, h, d);
  642. }
  643. case zn.id: {
  644. const c = a.params;
  645. return this._handleRemoveColCommand(c, r, e);
  646. }
  647. case Gn.id: {
  648. const c = a.params;
  649. return this._handleRemoveRowCommand(c, r, e);
  650. }
  651. case $.MoveColsCommandId: {
  652. const c = a.params;
  653. return this._handleMoveColsCommand(c, r, e);
  654. }
  655. case $.MoveRowsCommandId: {
  656. const c = a.params;
  657. return this._handleMoveRowsCommand(c, r, e);
  658. }
  659. case pt.id: {
  660. const c = a.params;
  661. return this._handleMoveRangeCommand(c, r, e);
  662. }
  663. }
  664. return { redos: [], undos: [] };
  665. };
  666. s && this._disposableCollection.add(this._refRangeService.registerRefRange(s, o, r, e));
  667. }
  668. _getUpdateFilter(r) {
  669. const { id: e } = r;
  670. switch (e) {
  671. case ko.id: {
  672. const t = r.params;
  673. return this._handleRemoveSheetCommand(t, t.unitId, t.subUnitId);
  674. }
  675. case xs.id: {
  676. const t = r.params, { targetSubUnitId: n, unitId: s, subUnitId: o } = t;
  677. return !s || !o || !n ? this._handleNull() : this._handleCopySheetCommand(s, o, n);
  678. }
  679. }
  680. return {
  681. redos: [],
  682. undos: []
  683. };
  684. }
  685. _handleInsertColCommand(r, e, t) {
  686. var _;
  687. const n = this._sheetsFilterService.getFilterModel(e, t), s = (_ = n == null ? void 0 : n.getRange()) != null ? _ : null;
  688. if (!n || !s)
  689. return this._handleNull();
  690. const { startColumn: o, endColumn: l } = s, { startColumn: a, endColumn: c } = r.range, h = c - a + 1;
  691. if (c > l)
  692. return this._handleNull();
  693. const d = [], u = [], m = a, g = {
  694. unitId: e,
  695. subUnitId: t,
  696. range: {
  697. ...s,
  698. startColumn: a <= o ? o + h : o,
  699. endColumn: l + h
  700. }
  701. }, C = {
  702. unitId: e,
  703. subUnitId: t,
  704. range: s
  705. };
  706. d.push({ id: R.id, params: g }), u.push({ id: R.id, params: C });
  707. const F2 = n.getAllFilterColumns().filter((f) => f[0] >= m);
  708. if (F2.length !== 0) {
  709. const { newRange: f, oldRange: v } = this.moveCriteria(e, t, F2, h);
  710. d.push(...f.redos, ...v.redos), u.push(...f.undos, ...v.undos);
  711. }
  712. return { redos: I(d), undos: I(u) };
  713. }
  714. _handleInsertRowCommand(r, e, t) {
  715. var C;
  716. const n = this._sheetsFilterService.getFilterModel(e, t), s = (C = n == null ? void 0 : n.getRange()) != null ? C : null;
  717. if (!n || !s)
  718. return this._handleNull();
  719. const { startRow: o, endRow: l } = s, { startRow: a, endRow: c } = r.range, h = c - a + 1;
  720. if (c > l)
  721. return this._handleNull();
  722. const d = [], u = [], m = {
  723. unitId: e,
  724. subUnitId: t,
  725. range: {
  726. ...s,
  727. startRow: a <= o ? o + h : o,
  728. endRow: l + h
  729. }
  730. }, g = {
  731. unitId: e,
  732. subUnitId: t,
  733. range: s
  734. };
  735. return d.push({ id: R.id, params: m }), u.push({ id: R.id, params: g }), {
  736. redos: I(d),
  737. undos: I(u)
  738. };
  739. }
  740. _handleRemoveColCommand(r, e, t) {
  741. var F2;
  742. const n = this._sheetsFilterService.getFilterModel(e, t), s = (F2 = n == null ? void 0 : n.getRange()) != null ? F2 : null;
  743. if (!n || !s)
  744. return this._handleNull();
  745. const { startColumn: o, endColumn: l } = s, { startColumn: a, endColumn: c } = r.range;
  746. if (a > l)
  747. return this._handleNull();
  748. const h = [], d = [], u = c < o ? 0 : Math.min(c, l) - Math.max(a, o) + 1, m = c - a + 1, g = n.getAllFilterColumns();
  749. g.forEach((_) => {
  750. const [f, v] = _;
  751. f <= c && f >= a && (h.push({ id: p.id, params: { unitId: e, subUnitId: t, col: f, criteria: null } }), d.push({ id: p.id, params: { unitId: e, subUnitId: t, col: f, criteria: { ...v.serialize(), colId: f } } }));
  752. });
  753. const C = g.filter((_) => {
  754. const [f, v] = _;
  755. return f > c;
  756. });
  757. let S = { undos: [], redos: [] };
  758. if (C.length > 0) {
  759. const { oldRange: _, newRange: f } = this.moveCriteria(e, t, C, -m);
  760. S = f, h.push(..._.redos), d.unshift(..._.undos);
  761. }
  762. if (u === l - o + 1) {
  763. const _ = {
  764. unitId: e,
  765. subUnitId: t
  766. };
  767. h.push({ id: O.id, params: _ }), d.unshift({ id: R.id, params: { range: s, unitId: e, subUnitId: t } });
  768. } else {
  769. const _ = o <= a ? o : u === 0 ? o - m : a, f = o <= a ? l - u : l - m, v = {
  770. unitId: e,
  771. subUnitId: t,
  772. range: { ...s, startColumn: _, endColumn: f }
  773. };
  774. h.push({ id: R.id, params: v }), d.unshift({ id: R.id, params: { range: s, unitId: e, subUnitId: t } }), h.push(...S.redos), d.unshift(...S.undos);
  775. }
  776. return {
  777. undos: d,
  778. redos: h
  779. };
  780. }
  781. _handleRemoveRowCommand(r, e, t) {
  782. var C;
  783. const n = this._sheetsFilterService.getFilterModel(e, t);
  784. if (!n)
  785. return this._handleNull();
  786. const s = n.getRange(), { startRow: o, endRow: l } = s, { startRow: a, endRow: c } = r.range;
  787. if (a > l)
  788. return this._handleNull();
  789. if (c < o)
  790. return {
  791. undos: [{ id: R.id, params: { range: s, unitId: e, subUnitId: t } }],
  792. redos: [{
  793. id: R.id,
  794. params: {
  795. range: {
  796. ...s,
  797. startRow: o - (c - a + 1),
  798. endRow: l - (c - a + 1)
  799. },
  800. unitId: e,
  801. subUnitId: t
  802. }
  803. }]
  804. };
  805. const h = [], d = [], u = n.getAllFilterColumns(), m = o <= c && o >= a;
  806. d.push({ id: R.id, params: { range: s, unitId: e, subUnitId: t } });
  807. const g = Math.min(c, l) - Math.max(a, o) + 1;
  808. if (g === l - o + 1 || m) {
  809. const S = {
  810. unitId: e,
  811. subUnitId: t
  812. };
  813. h.push({ id: O.id, params: S }), u.forEach((F2) => {
  814. const [_, f] = F2, v = {
  815. unitId: e,
  816. subUnitId: t,
  817. col: _,
  818. criteria: { ...f.serialize(), colId: _ }
  819. };
  820. d.push({ id: p.id, params: v });
  821. });
  822. } else {
  823. const S = (C = this._univerInstanceService.getUniverSheetInstance(e)) == null ? void 0 : C.getSheetBySheetId(t);
  824. if (!S)
  825. return this._handleNull();
  826. const F2 = [];
  827. for (let y = a; y <= c; y++)
  828. S.getRowFiltered(y) && F2.push(y);
  829. const _ = Math.min(o, a), f = _ + (l - o) - g + F2.length, v = {
  830. unitId: e,
  831. subUnitId: t,
  832. range: {
  833. ...s,
  834. startRow: _,
  835. endRow: f
  836. }
  837. };
  838. h.push({ id: R.id, params: v });
  839. }
  840. return {
  841. undos: I(d),
  842. redos: I(h)
  843. };
  844. }
  845. // eslint-disable-next-line max-lines-per-function
  846. _handleMoveColsCommand(r, e, t) {
  847. var _;
  848. const n = this._sheetsFilterService.getFilterModel(e, t), s = (_ = n == null ? void 0 : n.getRange()) != null ? _ : null;
  849. if (!n || !s)
  850. return this._handleNull();
  851. const { startColumn: o, endColumn: l } = s, { fromRange: a, toRange: c } = r;
  852. if (a.endColumn < o && c.startColumn <= o || a.startColumn > l && c.endColumn > l)
  853. return this._handleNull();
  854. const h = [], d = [], u = {};
  855. for (let f = o; f <= l; f++)
  856. u[f] = {
  857. colIndex: f,
  858. filter: n.getFilterColumn(f)
  859. };
  860. Ko(a.startColumn, a.endColumn - a.startColumn + 1, c.startColumn, u);
  861. let m = s.startColumn, g = s.endColumn;
  862. o >= a.startColumn && o <= a.endColumn && c.startColumn > a.startColumn && a.endColumn < l && (m = a.endColumn + 1), l >= a.startColumn && l <= a.endColumn && c.startColumn < a.startColumn && a.startColumn > o && (g = a.startColumn - 1);
  863. const C = Object.keys(u).map((f) => Number(f)), S = C.find((f) => u[f].colIndex === g), F2 = C.find((f) => u[f].colIndex === m);
  864. if (C.forEach((f) => {
  865. var X, Z;
  866. const { colIndex: v, filter: y } = u[f], A = f;
  867. if (y) {
  868. if (A >= F2 && A <= S) {
  869. const q = {
  870. unitId: e,
  871. subUnitId: t,
  872. col: A,
  873. criteria: { ...y.serialize(), colId: A }
  874. }, pe = {
  875. unitId: e,
  876. subUnitId: t,
  877. col: A,
  878. criteria: n.getFilterColumn(A) ? { ...(X = n.getFilterColumn(A)) == null ? void 0 : X.serialize(), colId: A } : null
  879. };
  880. h.push({ id: p.id, params: q }), d.push({ id: p.id, params: pe });
  881. }
  882. if (!((Z = u[v]) != null && Z.filter)) {
  883. const q = {
  884. unitId: e,
  885. subUnitId: t,
  886. col: v,
  887. criteria: null
  888. };
  889. h.push({ id: p.id, params: q }), d.push({ id: p.id, params: { unitId: e, subUnitId: t, col: v, criteria: { ...y.serialize(), colId: v } } });
  890. }
  891. }
  892. }), o !== F2 || l !== S) {
  893. const f = {
  894. unitId: e,
  895. subUnitId: t,
  896. range: {
  897. ...s,
  898. startColumn: F2,
  899. endColumn: S
  900. }
  901. };
  902. h.unshift({ id: R.id, params: f }), d.unshift({ id: R.id, params: { range: s, unitId: e, subUnitId: t } });
  903. }
  904. return {
  905. undos: d,
  906. redos: h
  907. };
  908. }
  909. _handleMoveRowsCommand(r, e, t) {
  910. var _;
  911. const n = this._sheetsFilterService.getFilterModel(e, t), s = (_ = n == null ? void 0 : n.getRange()) != null ? _ : null;
  912. if (!n || !s)
  913. return this._handleNull();
  914. const { startRow: o, endRow: l } = s, { fromRange: a, toRange: c } = r;
  915. if (a.endRow < o && c.startRow <= o || a.startRow > l && c.endRow > l)
  916. return this._handleNull();
  917. const h = [], d = [], u = {};
  918. for (let f = o; f <= l; f++)
  919. u[f] = {
  920. oldIndex: f
  921. };
  922. const m = o;
  923. let g = l;
  924. l >= a.startRow && l <= a.endRow && c.startRow < a.startRow && a.startRow > o && (g = a.startRow - 1), Ko(a.startRow, a.endRow - a.startRow + 1, c.startRow, u);
  925. const C = Object.keys(u).map((f) => Number(f)), S = C.find((f) => u[f].oldIndex === g), F2 = C.find((f) => u[f].oldIndex === m);
  926. if (o !== F2 || l !== S) {
  927. const f = {
  928. unitId: e,
  929. subUnitId: t,
  930. range: {
  931. ...s,
  932. startRow: F2,
  933. endRow: S
  934. }
  935. };
  936. h.push({ id: R.id, params: f }, { id: H.id, params: { unitId: e, subUnitId: t } }), d.push({ id: R.id, params: { range: s, unitId: e, subUnitId: t } }, { id: H.id, params: { unitId: e, subUnitId: t } });
  937. }
  938. return {
  939. redos: h,
  940. undos: d
  941. };
  942. }
  943. _handleMoveRangeCommand(r, e, t) {
  944. const { fromRange: n, toRange: s } = r, o = this._sheetsFilterService.getFilterModel(e, t);
  945. if (!o)
  946. return this._handleNull();
  947. const l = o.getRange();
  948. if (!l)
  949. return this._handleNull();
  950. const a = [], c = [];
  951. if (en.contains(n, l)) {
  952. const h = l.startRow - n.startRow, d = l.startColumn - n.startColumn, u = {
  953. startRow: s.startRow + h,
  954. startColumn: s.startColumn + d,
  955. endRow: s.startRow + h + (l.endRow - l.startRow),
  956. endColumn: s.startColumn + d + (l.endColumn - l.startColumn)
  957. }, m = {
  958. id: O.id,
  959. params: {
  960. unitId: e,
  961. subUnitId: t
  962. }
  963. }, g = { id: R.id, params: { unitId: e, subUnitId: t, range: u } }, C = { id: R.id, params: { unitId: e, subUnitId: t, range: l } };
  964. a.push(m, g), c.push(m, C);
  965. const S = o.getAllFilterColumns(), F2 = s.startColumn - n.startColumn;
  966. S.forEach((_) => {
  967. const [f, v] = _;
  968. v && (a.push({ id: p.id, params: { unitId: e, subUnitId: t, col: f + F2, criteria: { ...v.serialize(), colId: f + F2 } } }), c.push({ id: p.id, params: { unitId: e, subUnitId: t, col: f, criteria: { ...v.serialize(), colId: f } } }));
  969. });
  970. } else if (en.intersects(s, l)) {
  971. const h = {
  972. ...l,
  973. endRow: Math.max(l.endRow, s.endRow)
  974. };
  975. a.push({ id: R.id, params: { unitId: e, subUnitId: t, range: h } }), c.push({ id: R.id, params: { unitId: e, subUnitId: t, range: l } });
  976. }
  977. return {
  978. redos: a,
  979. undos: c
  980. };
  981. }
  982. _handleRemoveSheetCommand(r, e, t) {
  983. const n = this._sheetsFilterService.getFilterModel(e, t);
  984. if (!n)
  985. return this._handleNull();
  986. const s = n.getRange();
  987. if (!s)
  988. return this._handleNull();
  989. const o = [], l = [];
  990. return n.getAllFilterColumns().forEach(([c, h]) => {
  991. l.push({ id: p.id, params: { unitId: e, subUnitId: t, col: c, criteria: { ...h.serialize(), colId: c } } });
  992. }), o.push({ id: O.id, params: { unitId: e, subUnitId: t, range: s } }), l.unshift({ id: R.id, params: { range: s, unitId: e, subUnitId: t } }), {
  993. undos: l,
  994. redos: o
  995. };
  996. }
  997. _handleCopySheetCommand(r, e, t) {
  998. const n = this._sheetsFilterService.getFilterModel(r, e);
  999. if (!n)
  1000. return this._handleNull();
  1001. const s = n.getRange();
  1002. if (!s)
  1003. return this._handleNull();
  1004. const o = [], l = [], a = [], c = [];
  1005. return n.getAllFilterColumns().forEach(([d, u]) => {
  1006. o.push({ id: p.id, params: { unitId: r, subUnitId: t, col: d, criteria: { ...u.serialize(), colId: d } } }), a.push({ id: p.id, params: { unitId: r, subUnitId: t, col: d, criteria: null } });
  1007. }), a.push({ id: O.id, params: { unitId: r, subUnitId: t, range: s } }), o.unshift({ id: R.id, params: { range: s, unitId: r, subUnitId: t } }), {
  1008. undos: l,
  1009. redos: o,
  1010. preUndos: a,
  1011. preRedos: c
  1012. };
  1013. }
  1014. _handleNull() {
  1015. return { redos: [], undos: [] };
  1016. }
  1017. _initRowFilteredInterceptor() {
  1018. this.disposeWithMe(this._sheetInterceptorService.intercept(et.ROW_FILTERED, {
  1019. // sheet-interceptor.service.ts
  1020. handler: (r, e) => {
  1021. var t, n;
  1022. return r ? true : (n = (t = this._sheetsFilterService.getFilterModel(
  1023. e.unitId,
  1024. e.subUnitId
  1025. )) == null ? void 0 : t.isRowFiltered(e.row)) != null ? n : false;
  1026. }
  1027. }));
  1028. }
  1029. moveCriteria(r, e, t, n) {
  1030. const s = {
  1031. unitId: r,
  1032. subUnitId: e,
  1033. criteria: null,
  1034. col: -1
  1035. }, o = [], l = [], a = [], c = [];
  1036. return t.forEach((h) => {
  1037. const [d, u] = h;
  1038. l.push({
  1039. id: p.id,
  1040. params: {
  1041. ...s,
  1042. col: d
  1043. }
  1044. }), o.push({
  1045. id: p.id,
  1046. params: {
  1047. ...s,
  1048. col: d,
  1049. criteria: { ...u.serialize(), colId: d }
  1050. }
  1051. });
  1052. }), t.forEach((h) => {
  1053. const [d, u] = h;
  1054. c.push({
  1055. id: p.id,
  1056. params: {
  1057. ...s,
  1058. col: d + n,
  1059. criteria: { ...u.serialize(), colId: d + n }
  1060. }
  1061. }), a.push({
  1062. id: p.id,
  1063. params: {
  1064. ...s,
  1065. col: d + n,
  1066. criteria: null
  1067. }
  1068. });
  1069. }), {
  1070. newRange: {
  1071. redos: c,
  1072. undos: a
  1073. },
  1074. oldRange: {
  1075. redos: l,
  1076. undos: o
  1077. }
  1078. };
  1079. }
  1080. _commandExecutedListener() {
  1081. this.disposeWithMe(this._commandService.onCommandExecuted((r, e) => {
  1082. var c, h;
  1083. const { unitId: t, subUnitId: n } = r.params || {}, s = this._sheetsFilterService.getFilterModel(t, n);
  1084. if (!s)
  1085. return;
  1086. const o = Array.from(s.filteredOutRows).sort((d, u) => d - u), l = [];
  1087. let a = false;
  1088. if (r.id === Be.id) {
  1089. const { startRow: d, endRow: u } = r.params.range, m = o.filter((g) => g >= d && g <= u);
  1090. o.forEach((g) => {
  1091. if (g < d)
  1092. l.push(g);
  1093. else if (a = true, g <= u) {
  1094. const C = Math.max(d, l.length ? l[l.length - 1] + 1 : d);
  1095. l.push(C);
  1096. } else
  1097. l.push(g - (u - d + 1 - m.length));
  1098. });
  1099. }
  1100. if (r.id === qe.id) {
  1101. const { startRow: d, endRow: u } = r.params.range;
  1102. o.forEach((m) => {
  1103. m >= d ? (a = true, l.push(m + (u - d + 1))) : l.push(m);
  1104. });
  1105. }
  1106. if (a && (s.filteredOutRows = new Set(l)), r.id === Q.id && !(e != null && e.onlyLocal)) {
  1107. const d = this._getExtendRegion(t, n);
  1108. if (d) {
  1109. const u = r.params.cellValue;
  1110. if (u)
  1111. for (let m = d.startColumn; m <= d.endColumn; m++) {
  1112. const g = (c = u == null ? void 0 : u[d.startRow]) == null ? void 0 : c[m];
  1113. if (g && this._cellHasValue(g)) {
  1114. const C = (h = this._univerInstanceService.getUnit(t)) == null ? void 0 : h.getSheetBySheetId(n);
  1115. if (C) {
  1116. const S = Mh(d, { down: true }, C), F2 = this._sheetsFilterService.getFilterModel(t, n), _ = F2.getRange();
  1117. F2.setRange({
  1118. ..._,
  1119. endRow: S.endRow
  1120. }), this._registerRefRange(t, n);
  1121. }
  1122. }
  1123. }
  1124. }
  1125. }
  1126. }));
  1127. }
  1128. _getExtendRegion(r, e) {
  1129. var a;
  1130. const t = this._sheetsFilterService.getFilterModel(r, e);
  1131. if (!t)
  1132. return null;
  1133. const n = (a = this._univerInstanceService.getUnit(r)) == null ? void 0 : a.getSheetBySheetId(e);
  1134. if (!n)
  1135. return null;
  1136. const s = t.getRange();
  1137. if (!s)
  1138. return null;
  1139. const o = n.getRowCount() - 1, l = n.getRowManager();
  1140. for (let c = s.endRow + 1; c <= o; c++)
  1141. if (l.getRowRawVisible(c))
  1142. return {
  1143. startRow: c,
  1144. endRow: c,
  1145. startColumn: s.startColumn,
  1146. endColumn: s.endColumn
  1147. };
  1148. return null;
  1149. }
  1150. _initErrorHandling() {
  1151. this.disposeWithMe(this._commandService.beforeCommandExecuted((r) => {
  1152. const e = r.params, t = N(this._univerInstanceService);
  1153. if (!t)
  1154. return;
  1155. const { subUnitId: n, unitId: s } = t, o = this._sheetsFilterService.getFilterModel(s, n);
  1156. if (!o)
  1157. return;
  1158. const l = o.getRange();
  1159. if (r.id === Fn.id && e.fromRange.startRow <= l.startRow && e.fromRange.endRow < l.endRow && e.fromRange.endRow >= l.startRow)
  1160. throw this._sheetsFilterService.setFilterErrorMsg("sheets-filter.msg.filter-header-forbidden"), new Error("[SheetsFilterController]: Cannot move header row of filter");
  1161. }));
  1162. }
  1163. _cellHasValue(r) {
  1164. const e = Object.values(r);
  1165. return !(e.length === 0 || e.every((t) => t == null));
  1166. }
  1167. };
  1168. W = yt([
  1169. P(0, Pt),
  1170. P(1, ot(V)),
  1171. P(2, ot(M)),
  1172. P(3, _n),
  1173. P(4, ot(_t))
  1174. ], W);
  1175. var At = Object.defineProperty;
  1176. var Tt = Object.getOwnPropertyDescriptor;
  1177. var Nt = (i, r, e, t) => {
  1178. for (var n = t > 1 ? void 0 : t ? Tt(r, e) : r, s = i.length - 1, o; s >= 0; s--)
  1179. (o = i[s]) && (n = (t ? o(r, e, n) : o(n)) || n);
  1180. return t && n && At(r, e, n), n;
  1181. };
  1182. var oe = (i, r) => (e, t) => r(e, t, i);
  1183. var B;
  1184. var le = (B = class extends Ra {
  1185. constructor(i = re2, r, e) {
  1186. super(), this._config = i, this._injector = r, this._configService = e;
  1187. const { ...t } = nu(
  1188. {},
  1189. re2,
  1190. this._config
  1191. );
  1192. this._configService.setConfig(tt, t);
  1193. }
  1194. onStarting() {
  1195. [
  1196. [M],
  1197. [W]
  1198. ].forEach((i) => this._injector.add(i));
  1199. }
  1200. onReady() {
  1201. this._injector.get(W);
  1202. }
  1203. }, w(B, "type", Fe.UNIVER_SHEET), w(B, "pluginName", Re), B);
  1204. le = Nt([
  1205. oe(1, ot(Ot)),
  1206. oe(2, SE)
  1207. ], le);
  1208. var xt = {
  1209. id: "sheet.command.set-filter-range",
  1210. type: ar.COMMAND,
  1211. handler: (i, r) => {
  1212. const e = i.get(M), t = i.get(Pt), n = i.get(cn), s = i.get(_n), { unitId: o, subUnitId: l, range: a } = r;
  1213. if (!N(s, r) || e.getFilterModel(o, l))
  1214. return false;
  1215. if (a.endRow === a.startRow) {
  1216. const m = i.get(NE), g = i.get(Vr);
  1217. return m.emit(g.t("sheets-filter.command.not-valid-filter-range")), false;
  1218. }
  1219. const d = { id: R.id, params: { unitId: o, subUnitId: l, range: a } }, u = t.syncExecuteCommand(d.id, d.params);
  1220. return u && n.pushUndoRedo({
  1221. unitID: o,
  1222. undoMutations: [{ id: O.id, params: { unitId: o, subUnitId: l } }],
  1223. redoMutations: [d]
  1224. }), u;
  1225. }
  1226. };
  1227. var It = {
  1228. id: "sheet.command.remove-sheet-filter",
  1229. type: ar.COMMAND,
  1230. handler: (i, r) => {
  1231. const e = i.get(_n), t = i.get(M), n = i.get(Pt), s = i.get(cn), o = N(e, r);
  1232. if (!o)
  1233. return false;
  1234. const { unitId: l, subUnitId: a } = o, c = t.getFilterModel(l, a);
  1235. if (!c)
  1236. return false;
  1237. const h = c == null ? void 0 : c.serialize(), d = $t(l, a, h), u = n.syncExecuteCommand(O.id, { unitId: l, subUnitId: a });
  1238. return u && s.pushUndoRedo({
  1239. unitID: l,
  1240. undoMutations: d,
  1241. redoMutations: [{ id: O.id, params: { unitId: l, subUnitId: a } }]
  1242. }), u;
  1243. }
  1244. };
  1245. var Ht = {
  1246. id: "sheet.command.smart-toggle-filter",
  1247. type: ar.COMMAND,
  1248. handler: async (i) => {
  1249. const r = i.get(_n), e = i.get(M), t = i.get(Pt), n = r.getCurrentUnitForType(Fe.UNIVER_SHEET), s = n == null ? void 0 : n.getActiveSheet();
  1250. if (!s || !n)
  1251. return false;
  1252. const o = n.getUnitId(), l = s.getSheetId();
  1253. if (e.getFilterModel(o, l))
  1254. return t.executeCommand(It.id, { unitId: o, subUnitId: l });
  1255. const h = i.get(F).getCurrentLastSelection();
  1256. if (!h)
  1257. return false;
  1258. const d = h.range, u = Jd(h) ? Mh(d, { left: true, right: true, up: true, down: true }, s) : d;
  1259. return t.executeCommand(xt.id, {
  1260. unitId: o,
  1261. subUnitId: l,
  1262. range: u
  1263. });
  1264. }
  1265. };
  1266. var Dt = {
  1267. id: "sheet.command.set-filter-criteria",
  1268. type: ar.COMMAND,
  1269. handler: (i, r) => {
  1270. const e = i.get(M), t = i.get(Pt), n = i.get(cn), { unitId: s, subUnitId: o, col: l, criteria: a } = r, c = e.getFilterModel(s, o);
  1271. if (!c)
  1272. return false;
  1273. const h = c.getRange();
  1274. if (!h || l < h.startColumn || l > h.endColumn)
  1275. return false;
  1276. const d = c.getFilterColumn(l), u = Ut(s, o, l, d), m = {
  1277. id: p.id,
  1278. params: {
  1279. unitId: s,
  1280. subUnitId: o,
  1281. col: l,
  1282. criteria: a
  1283. }
  1284. }, g = t.syncExecuteCommand(m.id, m.params);
  1285. return g && n.pushUndoRedo({
  1286. unitID: s,
  1287. undoMutations: [u],
  1288. redoMutations: [m]
  1289. }), g;
  1290. }
  1291. };
  1292. var Wt = {
  1293. id: "sheet.command.clear-filter-criteria",
  1294. type: ar.COMMAND,
  1295. handler: (i, r) => {
  1296. const e = i.get(M), t = i.get(cn), n = i.get(Pt), s = i.get(_n), o = N(s, r);
  1297. if (!o)
  1298. return false;
  1299. const { unitId: l, subUnitId: a } = o, c = e.getFilterModel(o.unitId, o.subUnitId);
  1300. if (!c)
  1301. return false;
  1302. const h = c.serialize(), d = Ce(l, a, h), u = Pt2(l, a, h);
  1303. return mu(u, n).result ? (t.pushUndoRedo({
  1304. unitID: l,
  1305. undoMutations: d,
  1306. redoMutations: u
  1307. }), true) : false;
  1308. }
  1309. };
  1310. var Vt = {
  1311. id: "sheet.command.re-calc-filter",
  1312. type: ar.COMMAND,
  1313. handler: (i, r) => {
  1314. const e = i.get(M), t = i.get(Pt), n = i.get(_n), s = N(n, r);
  1315. if (!s)
  1316. return false;
  1317. const { unitId: o, subUnitId: l } = s;
  1318. return e.getFilterModel(s.unitId, s.subUnitId) ? t.executeCommand(H.id, { unitId: o, subUnitId: l }) : false;
  1319. }
  1320. };
  1321. function $t(i, r, e) {
  1322. const t = [], n = {
  1323. id: R.id,
  1324. params: {
  1325. unitId: i,
  1326. subUnitId: r,
  1327. range: e.ref
  1328. }
  1329. };
  1330. return t.push(n), Ce(i, r, e).forEach((o) => t.push(o)), t;
  1331. }
  1332. function Ce(i, r, e) {
  1333. var n;
  1334. const t = [];
  1335. return (n = e.filterColumns) == null || n.forEach((s) => {
  1336. const o = {
  1337. id: p.id,
  1338. params: {
  1339. unitId: i,
  1340. subUnitId: r,
  1341. col: s.colId,
  1342. criteria: s
  1343. }
  1344. };
  1345. t.push(o);
  1346. }), t;
  1347. }
  1348. function Pt2(i, r, e) {
  1349. var n;
  1350. const t = [];
  1351. return (n = e.filterColumns) == null || n.forEach((s) => {
  1352. const o = {
  1353. id: p.id,
  1354. params: {
  1355. unitId: i,
  1356. subUnitId: r,
  1357. col: s.colId,
  1358. criteria: null
  1359. }
  1360. };
  1361. t.push(o);
  1362. }), t;
  1363. }
  1364. function Ut(i, r, e, t) {
  1365. if (!t)
  1366. return {
  1367. id: p.id,
  1368. params: {
  1369. unitId: i,
  1370. subUnitId: r,
  1371. col: e,
  1372. criteria: null
  1373. }
  1374. };
  1375. const n = t.serialize();
  1376. return {
  1377. id: p.id,
  1378. params: {
  1379. unitId: i,
  1380. subUnitId: r,
  1381. col: e,
  1382. criteria: n
  1383. }
  1384. };
  1385. }
  1386. export {
  1387. rt,
  1388. N2 as N,
  1389. nt2 as nt,
  1390. it,
  1391. st,
  1392. ot2 as ot,
  1393. lt,
  1394. fe,
  1395. ne,
  1396. D,
  1397. ht,
  1398. Re,
  1399. M,
  1400. R,
  1401. p,
  1402. O,
  1403. H,
  1404. le,
  1405. xt,
  1406. It,
  1407. Ht,
  1408. Dt,
  1409. Wt,
  1410. Vt
  1411. };
  1412. //# sourceMappingURL=chunk-KSOBIBXF.js.map