@univerjs/sheets-filter 0.1.10 → 0.1.12
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.
- package/lib/cjs/index.js +1 -1
- package/lib/es/index.js +91 -93
- package/lib/types/commands/sheets-filter.mutation.d.ts +2 -2
- package/lib/types/models/custom-filters.d.ts +1 -1
- package/lib/types/models/filter-model.d.ts +2 -2
- package/lib/types/plugin.d.ts +1 -1
- package/lib/types/services/sheet-filter.service.d.ts +2 -2
- package/lib/umd/index.js +1 -1
- package/package.json +10 -10
- package/lib/types/controllers/{sheets-fiter.controller.d.ts → sheets-filter.controller.d.ts} +2 -2
package/lib/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var se=Object.defineProperty;var ie=(r,i,e)=>i in r?se(r,i,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[i]=e;var g=(r,i,e)=>(ie(r,typeof i!="symbol"?i+"":i,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@univerjs/core"),I=require("@wendellhu/redi"),y=require("rxjs"),M=require("@univerjs/sheets");var E=(r=>(r.EQUAL="equal",r.GREATER_THAN="greaterThan",r.GREATER_THAN_OR_EQUAL="greaterThanOrEqual",r.LESS_THAN="lessThan",r.LESS_THAN_OR_EQUAL="lessThanOrEqual",r.NOT_EQUALS="notEqual",r))(E||{});const V={operator:E.GREATER_THAN,fn:(r,i)=>N(r)?r>i:!1},q={operator:E.GREATER_THAN_OR_EQUAL,fn:(r,i)=>N(r)?r>=i:!1},Q={operator:E.LESS_THAN,fn:(r,i)=>N(r)?r<i:!1},G={operator:E.LESS_THAN_OR_EQUAL,fn:(r,i)=>N(r)?r<=i:!1},J={operator:E.EQUAL,fn:(r,i)=>N(r)?r===i:!1},j={operator:E.NOT_EQUALS,fn:(r,i)=>{if(typeof i=="string"){if(i===" ")return r!=null;const e=Y(r);return e&&ae(i)?!K(i).test(e):e!==i}return N(r)?r!==i:!0}},X=new Map([]),ne=[V,q,Q,G,J,j];ne.forEach(r=>{X.set(r.operator,r)});function oe(r){return!!r}const le={fn:(r,i)=>{const e=Y(r);return e===null?i==="":K(i).test(e)}};function B(r){return r?X.get(r):le}function N(r){return typeof r=="number"}function W(r){return!!(typeof r=="number"||typeof r=="string"&&d.isNumeric(r))}function Y(r){return typeof r=="boolean"||r==null?null:typeof r=="string"?r:r.toString()}function ae(r){return typeof r=="number"?!1:r.indexOf("*")!==-1||r.indexOf("?")!==-1}function K(r){const i=r.replace(/[.+^${}()|[\]\\]/g,"\\$&").replaceAll("?",".").replace(/[*]/g,".$&");return new RegExp(`^${i}$`)}const U=()=>new Set;class P extends d.Disposable{constructor(e,t,s){super();g(this,"_filteredOutRows$",new y.BehaviorSubject(U()));g(this,"filteredOutRows$",this._filteredOutRows$.asObservable());g(this,"_hasCriteria$",new y.BehaviorSubject(!1));g(this,"hasCriteria$",this._hasCriteria$.asObservable());g(this,"_filterColumnByIndex",new Map);g(this,"_alreadyFilteredOutRows",U());g(this,"_range");this.unitId=e,this.subUnitId=t,this._worksheet=s}get filteredOutRows(){return this._filteredOutRows$.getValue()}set filteredOutRows(e){this._alreadyFilteredOutRows=e,this._filteredOutRows$.next(e)}dispose(){super.dispose(),this._filteredOutRows$.complete(),this._hasCriteria$.complete()}serialize(){const e={ref:d.Rectangle.clone(this._range),filterColumns:this._getAllFilterColumns(!0).sort(([t],[s])=>t-s).map(([t,s])=>s.serialize())};return this._alreadyFilteredOutRows&&(e.cachedFilteredOut=Array.from(this._alreadyFilteredOutRows).sort()),e}static deserialize(e,t,s,n){const o=new P(e,t,s);return o._dump(n),o}_dump(e){var t;this.setRange(e.ref),(t=e.filterColumns)==null||t.forEach(s=>this._setCriteriaWithoutReCalc(s.colId,s)),e.cachedFilteredOut&&(this._alreadyFilteredOutRows=new Set(e.cachedFilteredOut),this._emit()),this._emitHasCriteria()}isRowFiltered(e){return this._alreadyFilteredOutRows.has(e)}getRange(){if(!this._range)throw new Error("[FilterModel] could not get range before a range is set!");return this._range}getFilteredOutRowsExceptCol(e){return this._getAllFilterColumns(!0).filter(([t])=>t!==e).reduce((t,[,s])=>{const n=s.calc({getAlreadyFilteredOutRows:()=>t});return n?d.mergeSets(t,n):t},new Set)}setRange(e){this._range=e,this._getAllFilterColumns(!0).forEach(([t,s])=>{s.setRangeAndColumn({startRow:e.startRow,endRow:e.endRow,startColumn:t,endColumn:t},t)})}setCriteria(e,t,s=!1){if(!this._range)throw new Error("[FilterModel] could not set criteria before a range is set!");if(!t){this._removeCriteria(e),this._rebuildAlreadyFilteredOutRowsWithCache(),s&&this._reCalcAllColumns(),this._emit(),this._emitHasCriteria();return}this._setCriteriaWithoutReCalc(e,t),s&&(this._rebuildAlreadyFilteredOutRowsWithCache(),this._reCalcWithNoCacheColumns(),this._emit(),this._emitHasCriteria())}getAllFilterColumns(){return this._getAllFilterColumns(!0)}getFilterColumn(e){var t;return(t=this._filterColumnByIndex.get(e))!=null?t:null}reCalc(){this._reCalcAllColumns(),this._emit()}_getAllFilterColumns(e=!1){const t=Array.from(this._filterColumnByIndex.entries());return e?t:t.map(([s,n])=>n)}_reCalcAllColumns(){this._alreadyFilteredOutRows=U(),this._getAllFilterColumns().forEach(e=>e.__clearCache()),this._reCalcWithNoCacheColumns()}_setCriteriaWithoutReCalc(e,t){const s=this._range;if(!s)throw new Error("[FilterModel] could not set criteria before a range is set!");const{startColumn:n,endColumn:o}=s;if(e>o||e<n)throw new Error(`[FilterModel] could not set criteria on column ${e} which is out of range!`);let l;this._filterColumnByIndex.has(e)?l=this._filterColumnByIndex.get(e):(l=new Z(this.unitId,this.subUnitId,this._worksheet,t,{getAlreadyFilteredOutRows:()=>this._alreadyFilteredOutRows}),l.setRangeAndColumn(s,e),this._filterColumnByIndex.set(e,l)),l.setCriteria(t)}_removeCriteria(e){const t=this._filterColumnByIndex.get(e);t&&(t.dispose(),this._filterColumnByIndex.delete(e))}_emit(){this._filteredOutRows$.next(this._alreadyFilteredOutRows)}_emitHasCriteria(){this._hasCriteria$.next(this._filterColumnByIndex.size>0)}_rebuildAlreadyFilteredOutRowsWithCache(){const e=this._getAllFilterColumns().filter(t=>t.hasCache()).reduce((t,s)=>d.mergeSets(t,s.filteredOutRows),new Set);this._alreadyFilteredOutRows=e}_reCalcWithNoCacheColumns(){const e=this._getAllFilterColumns().filter(t=>!t.hasCache());for(const t of e){const s=t.reCalc();s&&(this._alreadyFilteredOutRows=d.mergeSets(this._alreadyFilteredOutRows,s))}}}class Z extends d.Disposable{constructor(e,t,s,n,o){super();g(this,"_filteredOutRows",null);g(this,"_filterFn",null);g(this,"_range",null);g(this,"_column",0);g(this,"_filterByValues",!1);this.unitId=e,this.subUnitId=t,this._worksheet=s,this._criteria=n,this._filterColumnContext=o}get filteredOutRows(){return this._filteredOutRows}dispose(){super.dispose(),this._filteredOutRows=null}__clearCache(){this._filteredOutRows=null}serialize(){if(!this._criteria)throw new Error("[FilterColumn]: could not serialize without a filter column!");return d.Tools.deepClone({...this._criteria,colId:this._column})}hasCache(){return this._filteredOutRows!==null}setRangeAndColumn(e,t){this._range=e,this._column=t}setCriteria(e){this._criteria=e,this._generateFilterFn(),this._filteredOutRows=null}getColumnData(){return d.Tools.deepClone(this._criteria)}reCalc(){return this._filteredOutRows=this.calc(this._filterColumnContext),this._filteredOutRows}calc(e){if(!this._filterFn)throw new Error("[FilterColumn] cannot calculate without a filter fn!");if(!this._range)throw new Error("[FilterColumn] cannot calculate without a range!");if(typeof this._column!="number")throw new TypeError("[FilterColumn] cannot calculate without a column offset!");const t=this._column,s={startColumn:t,endColumn:t,startRow:this._range.startRow+1,endRow:this._range.endRow},n=new Set,o=e.getAlreadyFilteredOutRows();for(const l of this._worksheet.iterateByColumn(s,!1,!1)){const{row:a,rowSpan:u,col:c}=l;if(o.has(a)&&(!u||u===1))continue;const f=this._filterByValues?d.extractPureTextFromCell(this._worksheet.getCell(a,c)):ge(this._worksheet,a,c);if(!this._filterFn(f)&&(n.add(a),u))for(let _=1;_<u;_++)n.add(a+_)}return n}_generateFilterFn(){this._criteria&&(this._filterFn=ce(this._criteria),this._filterByValues=!!this._criteria.filters)}}function ce(r){if(r.filters)return ue(r.filters);if(r.customFilters)return he(r.customFilters);throw new Error("[FilterModel]: other types of filters are not supported yet.")}function ue(r){const i=!!r.blank,e=new Set(r.filters);return t=>t===void 0||t===""?i:e.has(typeof t=="string"?t:`${t}`)}function he(r){const i=r.customFilters.map(e=>_e(e));return me(i)?r.and?de(i):fe(i):i[0]}function de(r){const[i,e]=r;return t=>i(t)&&e(t)}function fe(r){const[i,e]=r;return t=>i(t)||e(t)}function me(r){return r.length===2}function _e(r){const i=r.val;if(r.operator===E.NOT_EQUALS&&!W(i))return s=>j.fn(s,i);if(oe(r.operator)){if(!W(i))return()=>!1;const s=B(r.operator),n=Number(i);return o=>s.fn(o,n)}const e=B(r.operator);return t=>e.fn(t,i)}function ge(r,i,e){const t=r.getCell(i,e);if(!t)return null;const s=r.getCellRaw(i,e);return t&&!s?D(t):s?t.t===d.CellValueType.NUMBER&&typeof t.v=="string"?s.v:D(s):null}function D(r){var t,s;const i=(s=(t=r.p)==null?void 0:t.body)==null?void 0:s.dataStream;if(i)return i.trimEnd();const e=r.v;return typeof e=="string"?r.t===d.CellValueType.BOOLEAN?e.toUpperCase():e:typeof e=="number"?r.t===d.CellValueType.BOOLEAN?e?"TRUE":"FALSE":e:typeof e=="boolean"?e?"TRUE":"FALSE":""}const C={id:"sheet.mutation.set-filter-range",type:d.CommandType.MUTATION,handler:(r,i)=>{const{subUnitId:e,unitId:t,range:s}=i;return r.get(exports.SheetsFilterService).ensureFilterModel(t,e).setRange(s),!0}},F={id:"sheet.mutation.set-filter-criteria",type:d.CommandType.MUTATION,handler:(r,i)=>{const{subUnitId:e,unitId:t,criteria:s,col:n,reCalc:o=!0}=i,a=r.get(exports.SheetsFilterService).getFilterModel(t,e);return a?(a.setCriteria(n,s,o),!0):!1}},T={id:"sheet.mutation.remove-filter",type:d.CommandType.MUTATION,handler:(r,i)=>{const{unitId:e,subUnitId:t}=i;return r.get(exports.SheetsFilterService).removeFilterModel(e,t)}},z={id:"sheet.mutation.re-calc-filter",type:d.CommandType.MUTATION,handler:(r,i)=>{const{unitId:e,subUnitId:t}=i,n=r.get(exports.SheetsFilterService).getFilterModel(e,t);return n?(n.reCalc(),!0):!1}};var Ce=Object.defineProperty,Re=Object.getOwnPropertyDescriptor,pe=(r,i,e,t)=>{for(var s=t>1?void 0:t?Re(i,e):i,n=r.length-1,o;n>=0;n--)(o=r[n])&&(s=(t?o(i,e,s):o(s))||s);return t&&s&&Ce(i,e,s),s},b=(r,i)=>(e,t)=>i(e,t,r);const k=new Set([C.id,F.id,T.id,z.id]),ee="SHEET_FILTER_PLUGIN";exports.SheetsFilterService=class extends d.Disposable{constructor(e,t,s){super();g(this,"_filterModels",new Map);g(this,"_loadedUnitId$",new y.BehaviorSubject(null));g(this,"loadedUnitId$",this._loadedUnitId$.asObservable());g(this,"_activeFilterModel$",new y.BehaviorSubject(null));g(this,"activeFilterModel$",this._activeFilterModel$.asObservable());this._resourcesManagerService=e,this._univerInstanceService=t,this._commandService=s,this._initModel(),this._initActiveFilterModel()}get activeFilterModel(){return this._activeFilterModel$.getValue()}ensureFilterModel(e,t){const s=this.getFilterModel(e,t);if(s)return s;const n=this._univerInstanceService.getUniverSheetInstance(e);if(!n)throw new Error(`[SheetsFilterService]: could not create "FilterModel" on a non-existing workbook ${e}!`);const o=n.getSheetBySheetId(t);if(!o)throw new Error(`[SheetsFilterService]: could not create "FilterModel" on a non-existing worksheet ${t}!`);const l=new P(e,t,o);return this._cacheFilterModel(e,t,l),l}getFilterModel(e,t){var s,n;return(n=(s=this._filterModels.get(e))==null?void 0:s.get(t))!=null?n:null}removeFilterModel(e,t){const s=this.getFilterModel(e,t);return s?(s.dispose(),this._filterModels.get(e).delete(t),!0):!1}_updateActiveFilterModel(){let e;try{if(e=this._univerInstanceService.getCurrentUnitForType(d.UniverInstanceType.UNIVER_SHEET),!e){this._activeFilterModel$.next(null);return}}catch{return}const t=e.getActiveSheet();if(!t){this._activeFilterModel$.next(null);return}const s=t.getUnitId(),n=t.getSheetId(),o=this.getFilterModel(s,n);this._activeFilterModel$.next(o)}_initActiveFilterModel(){this.disposeWithMe(y.merge(d.fromCallback(this._commandService.onCommandExecuted).pipe(y.filter(([e])=>e.type===d.CommandType.MUTATION&&k.has(e.id))),this._univerInstanceService.getCurrentTypeOfUnit$(d.UniverInstanceType.UNIVER_SHEET).pipe(y.switchMap(e=>{var t;return(t=e==null?void 0:e.activeSheet$)!=null?t:y.of(null)}))).subscribe(()=>this._updateActiveFilterModel()))}_serializeAutoFiltersForUnit(e){const t=this._filterModels.get(e);if(!t)return"{}";const s={};return t.forEach((n,o)=>{s[o]=n.serialize()}),JSON.stringify(s)}_deserializeAutoFiltersForUnit(e,t){const s=this._univerInstanceService.getUniverSheetInstance(e);Object.keys(t).forEach(n=>{const o=t[n],l=P.deserialize(e,n,s.getSheetBySheetId(n),o);this._cacheFilterModel(e,n,l)})}_initModel(){this._resourcesManagerService.registerPluginResource({pluginName:ee,businesses:[2],toJson:e=>this._serializeAutoFiltersForUnit(e),parseJson:e=>JSON.parse(e),onLoad:(e,t)=>{this._deserializeAutoFiltersForUnit(e,t),this._loadedUnitId$.next(e),this._updateActiveFilterModel()},onUnLoad:e=>{const t=this._filterModels.get(e);t&&(t.forEach(s=>s.dispose()),this._filterModels.delete(e))}})}_cacheFilterModel(e,t,s){this._filterModels.has(e)||this._filterModels.set(e,new Map),this._filterModels.get(e).set(t,s)}};exports.SheetsFilterService=pe([d.OnLifecycle(d.LifecycleStages.Ready,exports.SheetsFilterService),b(0,d.IResourceManagerService),b(1,d.IUniverInstanceService),b(2,d.ICommandService)],exports.SheetsFilterService);function Fe(r,i){for(let e=0;e<r.length;e++){let t=e;if(r[e])for(let s=e+1;s<r.length;s++)r[t]&&r[s]&&i(r[t],r[s])&&(r[t]=null,t=s)}return r.filter(e=>e!==null)}function O(r){return Fe(r,(i,e)=>i.id===F.id&&e.id===F.id&&i.params.unitId===e.params.unitId&&i.params.subUnitId===e.params.subUnitId&&i.params.col===e.params.col)}var Se=Object.defineProperty,we=Object.getOwnPropertyDescriptor,ve=(r,i,e,t)=>{for(var s=t>1?void 0:t?we(i,e):i,n=r.length-1,o;n>=0;n--)(o=r[n])&&(s=(t?o(i,e,s):o(s))||s);return t&&s&&Se(i,e,s),s},x=(r,i)=>(e,t)=>i(e,t,r);let L=class extends d.Disposable{constructor(r,i,e,t,s){super(),this._commandService=r,this._sheetInterceptorService=i,this._sheetsFilterService=e,this._univerInstanceService=t,this._refRangeService=s,this._initCommands(),this._initRowFilteredInterceptor(),this._initInterceptors(),this._commandExecutedListener()}_initCommands(){[F,C,z,T].forEach(r=>this.disposeWithMe(this._commandService.registerCommand(r)))}_initInterceptors(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:e=>this._getUpdateFilter(e)}));const r=new d.DisposableCollection,i=(e,t)=>{var a;const s=this._univerInstanceService.getUniverSheetInstance(e);if(!s||!(s==null?void 0:s.getSheetBySheetId(t)))return;r.dispose();const o=(a=this._sheetsFilterService.getFilterModel(e,t))==null?void 0:a.getRange(),l=u=>{switch(u.id){case M.InsertRowCommand.id:{const c=u.params,f=c.unitId||e,_=c.subUnitId||t;return this._handleInsertRowCommand(c,f,_)}case M.InsertColCommand.id:{const c=u.params,f=c.unitId||e,_=c.subUnitId||t;return this._handleInsertColCommand(c,f,_)}case M.RemoveColCommand.id:{const c=u.params;return this._handleRemoveColCommand(c,e,t)}case M.RemoveRowCommand.id:{const c=u.params;return this._handleRemoveRowCommand(c,e,t)}case M.EffectRefRangId.MoveColsCommandId:{const c=u.params;return this._handleMoveColsCommand(c,e,t)}case M.EffectRefRangId.MoveRowsCommandId:{const c=u.params;return this._handleMoveRowsCommand(c,e,t)}case M.MoveRangeCommand.id:{const c=u.params;return this._handleMoveRangeCommand(c,e,t)}}return{redos:[],undos:[]}};o&&r.add(this._refRangeService.registerRefRange(o,l,e,t))};this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.id===M.SetWorksheetActivateCommand.id){const t=e.params,s=t.subUnitId,n=t.unitId;if(!s||!n)return;i(n,s)}if(e.id===C.id){const t=e.params,s=t.subUnitId,n=t.unitId;if(!s||!n)return;i(t.unitId,t.subUnitId)}})),this.disposeWithMe(this._sheetsFilterService.loadedUnitId$.subscribe(e=>{if(e){const t=this._univerInstanceService.getUniverSheetInstance(e),s=t==null?void 0:t.getActiveSheet();s&&i(e,s.getSheetId())}}))}_getUpdateFilter(r){const{id:i}=r;switch(i){case M.RemoveSheetCommand.id:{const e=r.params;return this._handleRemoveSheetCommand(e,e.unitId,e.subUnitId)}}return{redos:[],undos:[]}}_handleInsertColCommand(r,i,e){var m;const t=this._sheetsFilterService.getFilterModel(i,e),s=(m=t==null?void 0:t.getRange())!=null?m:null;if(!t||!s)return this._handleNull();const{startColumn:n,endColumn:o}=s,{startColumn:l,endColumn:a}=r.range,u=a-l+1;if(a>o)return this._handleNull();const c=[],f=[],_=l,S={unitId:i,subUnitId:e,range:{...s,startColumn:l<=n?n+u:n,endColumn:o+u}},p={unitId:i,subUnitId:e,range:s};c.push({id:C.id,params:S}),f.push({id:C.id,params:p});const h=t.getAllFilterColumns().filter(R=>R[0]>=_);if(h.length!==0){const{undos:R,redos:v}=this.moveCriteria(i,e,h,u);c.push(...v),f.push(...R)}return{redos:O(c),undos:O(f)}}_handleInsertRowCommand(r,i,e){var p;const t=this._sheetsFilterService.getFilterModel(i,e),s=(p=t==null?void 0:t.getRange())!=null?p:null;if(!t||!s)return this._handleNull();const{startRow:n,endRow:o}=s,{startRow:l,endRow:a}=r.range,u=a-l+1;if(a>o)return this._handleNull();const c=[],f=[],_={unitId:i,subUnitId:e,range:{...s,startRow:l<=n?n+u:n,endRow:o+u}},S={unitId:i,subUnitId:e,range:s};return c.push({id:C.id,params:_}),f.push({id:C.id,params:S}),{redos:O(c),undos:O(f)}}_handleRemoveColCommand(r,i,e){var w;const t=this._sheetsFilterService.getFilterModel(i,e),s=(w=t==null?void 0:t.getRange())!=null?w:null;if(!t||!s)return this._handleNull();const{startColumn:n,endColumn:o}=s,{startColumn:l,endColumn:a}=r.range;if(l>o)return this._handleNull();const u=[],c=[],f=a<n?0:Math.min(a,o)-Math.max(l,n)+1,_=a-l+1,S=t.getAllFilterColumns();S.forEach(h=>{const[m,R]=h;m<=a&&m>=l&&(u.push({id:F.id,params:{unitId:i,subUnitId:e,col:m,criteria:null}}),c.push({id:F.id,params:{unitId:i,subUnitId:e,col:m,criteria:{...R.serialize(),colId:m}}}))});const p=S.filter(h=>{const[m,R]=h;return m>a});if(p.length>0){const{undos:h,redos:m}=this.moveCriteria(i,e,p,-_);u.push(...m),c.push(...h)}if(f===o-n+1){const h={unitId:i,subUnitId:e};u.push({id:T.id,params:h})}else if(n<=l){const h=o-f,m={unitId:i,subUnitId:e,range:{...s,endColumn:h}};u.push({id:C.id,params:m})}else{const h={unitId:i,subUnitId:e,range:{...s,startColumn:l,endColumn:o-(a-l+1)}};u.push({id:C.id,params:h})}return c.push({id:C.id,params:{range:s,unitId:i,subUnitId:e}}),{undos:O(c),redos:O(u)}}_handleRemoveRowCommand(r,i,e){var p;const t=this._sheetsFilterService.getFilterModel(i,e);if(!t)return this._handleNull();const s=t.getRange(),{startRow:n,endRow:o}=s,{startRow:l,endRow:a}=r.range;if(l>o)return this._handleNull();const u=[],c=[],f=t.getAllFilterColumns(),_=n<=a&&n>=l;c.push({id:C.id,params:{range:s,unitId:i,subUnitId:e}});const S=Math.min(a,o)-Math.max(l,n)+1;if(S===o-n+1||_){const w={unitId:i,subUnitId:e};u.push({id:T.id,params:w}),f.forEach(h=>{const[m,R]=h,v={unitId:i,subUnitId:e,col:m,criteria:{...R.serialize(),colId:m}};c.push({id:F.id,params:v})})}else{const w=(p=this._univerInstanceService.getUniverSheetInstance(i))==null?void 0:p.getSheetBySheetId(e);if(!w)return this._handleNull();const h=[];for(let A=l;A<=a;A++)w.getRowFiltered(A)&&h.push(A);const m=Math.min(n,l),R=m+(o-n)-S+h.length,v={unitId:i,subUnitId:e,range:{...s,startRow:m,endRow:R}};u.push({id:C.id,params:v})}return{undos:O(c),redos:O(u)}}_handleMoveColsCommand(r,i,e){var w;const t=this._sheetsFilterService.getFilterModel(i,e),s=(w=t==null?void 0:t.getRange())!=null?w:null;if(!t||!s)return this._handleNull();const{startColumn:n,endColumn:o}=s,{fromRange:l,toRange:a}=r;if(l.endColumn<n&&a.startColumn<=n||l.startColumn>o&&a.endColumn>o)return this._handleNull();const u=[],c=[],f={};for(let h=n;h<=o;h++)f[h]={colIndex:h,filter:t.getFilterColumn(h)};d.moveMatrixArray(l.startColumn,l.endColumn-l.startColumn+1,a.startColumn,f);const _=Object.keys(f).map(h=>Number(h)),S=Math.max(..._),p=Math.min(..._);if(_.forEach(h=>{var A,H;const{colIndex:m,filter:R}=f[h],v=h;if(R){const te={unitId:i,subUnitId:e,col:v,criteria:{...R.serialize(),colId:v}};if(u.push({id:F.id,params:te}),c.push({id:T.id,params:{unitId:i,subUnitId:e,col:v,criteria:{...(A=t.getFilterColumn(v))==null?void 0:A.serialize(),colId:v}}}),!((H=f[m])!=null&&H.filter)){const re={unitId:i,subUnitId:e,col:m,criteria:null};u.push({id:F.id,params:re}),c.push({id:F.id,params:{unitId:i,subUnitId:e,col:m,criteria:{...R.serialize(),colId:m}}})}}}),n!==p||o!==S){const h={unitId:i,subUnitId:e,range:{...s,startColumn:p,endColumn:S}};u.unshift({id:C.id,params:h}),c.push({id:C.id,params:{range:s,unitId:i,subUnitId:e}})}return{undos:c,redos:u}}_handleMoveRowsCommand(r,i,e){var w;const t=this._sheetsFilterService.getFilterModel(i,e),s=(w=t==null?void 0:t.getRange())!=null?w:null;if(!t||!s)return this._handleNull();const{startRow:n,endRow:o}=s,{fromRange:l,toRange:a}=r;if(l.endRow<n&&a.startRow<=n||l.startRow>o&&a.endRow>o)return this._handleNull();const u=[],c=[],f={};for(let h=n;h<=o;h++)f[h]={offset:h-n};d.moveMatrixArray(l.startRow,l.endRow-l.startRow+1,a.startRow,f);const _=Object.keys(f).map(h=>Number(h)),S=Math.max(..._),p=Math.min(..._);if(n!==p||o!==S){const h={unitId:i,subUnitId:e,range:{...s,startRow:p,endRow:S}};u.unshift({id:C.id,params:h}),c.push({id:C.id,params:{range:s,unitId:i,subUnitId:e}})}return{redos:u,undos:c}}_handleMoveRangeCommand(r,i,e){const{fromRange:t,toRange:s}=r,n=this._sheetsFilterService.getFilterModel(i,e);if(!n)return this._handleNull();const o=n.getRange();if(!o)return this._handleNull();const l=[],a=[];if(d.Rectangle.contains(t,o)){const u=o.startRow-t.startRow,c=o.startColumn-t.startColumn,f={startRow:s.startRow+u,startColumn:s.startColumn+c,endRow:s.startRow+u+(o.endRow-o.startRow),endColumn:s.startColumn+c+(o.endColumn-o.startColumn)},_={id:T.id,params:{unitId:i,subUnitId:e}},S={id:C.id,params:{unitId:i,subUnitId:e,range:f}},p={id:C.id,params:{unitId:i,subUnitId:e,range:o}};l.push(_,S),a.push(_,p);const w=n.getAllFilterColumns(),h=s.startColumn-t.startColumn;w.forEach(m=>{const[R,v]=m;v&&(l.push({id:F.id,params:{unitId:i,subUnitId:e,col:R+h,criteria:{...v.serialize(),colId:R+h}}}),a.push({id:F.id,params:{unitId:i,subUnitId:e,col:R,criteria:{...v.serialize(),colId:R}}}))})}return{redos:l,undos:a}}_handleRemoveSheetCommand(r,i,e){const t=this._sheetsFilterService.getFilterModel(i,e);if(!t)return this._handleNull();const s=t.getRange();if(!s)return this._handleNull();const n=[],o=[];return t.getAllFilterColumns().forEach(a=>{const[u,c]=a;o.push({id:F.id,params:{unitId:i,subUnitId:e,col:a,criteria:{...c.serialize(),colId:a}}})}),n.push({id:T.id,params:{unitId:i,subUnitId:e,range:s}}),o.unshift({id:C.id,params:{range:s,unitId:i,subUnitId:e}}),{undos:o,redos:n}}_handleNull(){return{redos:[],undos:[]}}_initRowFilteredInterceptor(){this.disposeWithMe(this._sheetInterceptorService.intercept(M.INTERCEPTOR_POINT.ROW_FILTERED,{handler:(r,i)=>{var e,t;return r?!0:(t=(e=this._sheetsFilterService.getFilterModel(i.unitId,i.subUnitId))==null?void 0:e.isRowFiltered(i.row))!=null?t:!1}}))}moveCriteria(r,i,e,t){const s={unitId:r,subUnitId:i,criteria:null,col:-1},n=[],o=[];return e.forEach(l=>{const[a,u]=l;o.push({id:F.id,params:{...s,col:a}}),n.push({id:F.id,params:{...s,col:a,criteria:{...u.serialize(),colId:a}}})}),e.forEach(l=>{const[a,u]=l;o.push({id:F.id,params:{...s,col:a+t,criteria:{...u.serialize(),colId:a+t}}}),n.push({id:F.id,params:{...s,col:a+t,criteria:null}})}),{redos:o,undos:n}}_commandExecutedListener(){this.disposeWithMe(this._commandService.onCommandExecuted(r=>{const{unitId:i,subUnitId:e}=r.params||{},t=this._sheetsFilterService.getFilterModel(i,e);if(!t)return;const s=Array.from(t.filteredOutRows).sort((l,a)=>l-a),n=[];let o=!1;if(r.id===M.RemoveRowMutation.id){const{startRow:l,endRow:a}=r.params.range,u=s.filter(c=>c>=l&&c<=a);s.forEach(c=>{if(c<l)n.push(c);else if(o=!0,c<=a){const f=Math.max(l,n.length?n[n.length-1]+1:l);n.push(f)}else n.push(c-(a-l+1-u.length))})}if(r.id===M.InsertRowMutation.id){const{startRow:l,endRow:a}=r.params.range;s.forEach(u=>{u>=l?(o=!0,n.push(u+(a-l+1))):n.push(u)})}o&&(t.filteredOutRows=new Set(n))}))}};L=ve([d.OnLifecycle(d.LifecycleStages.Ready,L),x(0,d.ICommandService),x(1,I.Inject(M.SheetInterceptorService)),x(2,I.Inject(exports.SheetsFilterService)),x(3,d.IUniverInstanceService),x(4,I.Inject(M.RefRangeService))],L);var Me=Object.defineProperty,Ee=Object.getOwnPropertyDescriptor,Oe=(r,i,e,t)=>{for(var s=t>1?void 0:t?Ee(i,e):i,n=r.length-1,o;n>=0;n--)(o=r[n])&&(s=(t?o(i,e,s):o(s))||s);return t&&s&&Me(i,e,s),s},ye=(r,i)=>(e,t)=>i(e,t,r);const Te="UNIVER_SHEETS_FILTER_PLUGIN";var $;exports.UniverSheetsFilterPlugin=($=class extends d.Plugin{constructor(i,e){super(),this._injector=e}onStarting(i){[[exports.SheetsFilterService],[L]].forEach(e=>i.add(e))}},g($,"type",d.UniverInstanceType.UNIVER_SHEET),g($,"pluginName",Te),$);exports.UniverSheetsFilterPlugin=Oe([ye(1,I.Inject(I.Injector))],exports.UniverSheetsFilterPlugin);exports.CustomFilterOperator=E;exports.FILTER_MUTATIONS=k;exports.FilterColumn=Z;exports.FilterModel=P;exports.ReCalcSheetsFilterMutation=z;exports.RemoveSheetsFilterMutation=T;exports.SHEET_FILTER_SNAPSHOT_ID=ee;exports.SetSheetsFilterCriteriaMutation=F;exports.SetSheetsFilterRangeMutation=C;exports.equals=J;exports.getCustomFilterFn=B;exports.greaterThan=V;exports.greaterThanOrEqualTo=q;exports.lessThan=Q;exports.lessThanOrEqualTo=G;exports.notEquals=j;
|
|
1
|
+
"use strict";var se=Object.defineProperty;var ie=(r,i,e)=>i in r?se(r,i,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[i]=e;var g=(r,i,e)=>(ie(r,typeof i!="symbol"?i+"":i,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@univerjs/core"),I=require("@wendellhu/redi"),y=require("rxjs"),M=require("@univerjs/sheets");var E=(r=>(r.EQUAL="equal",r.GREATER_THAN="greaterThan",r.GREATER_THAN_OR_EQUAL="greaterThanOrEqual",r.LESS_THAN="lessThan",r.LESS_THAN_OR_EQUAL="lessThanOrEqual",r.NOT_EQUALS="notEqual",r))(E||{});const V={operator:E.GREATER_THAN,fn:(r,i)=>N(r)?r>i:!1},Q={operator:E.GREATER_THAN_OR_EQUAL,fn:(r,i)=>N(r)?r>=i:!1},G={operator:E.LESS_THAN,fn:(r,i)=>N(r)?r<i:!1},J={operator:E.LESS_THAN_OR_EQUAL,fn:(r,i)=>N(r)?r<=i:!1},X={operator:E.EQUAL,fn:(r,i)=>N(r)?r===i:!1},j={operator:E.NOT_EQUALS,fn:(r,i)=>{if(typeof i=="string"){if(i===" ")return r!=null;const e=K(r);return e&&ae(i)?!Z(i).test(e):e!==i}return N(r)?r!==i:!0}},Y=new Map([]),ne=[V,Q,G,J,X,j];ne.forEach(r=>{Y.set(r.operator,r)});function oe(r){return!!r}const le={fn:(r,i)=>{const e=K(r);return e===null?i==="":Z(i).test(e)}};function B(r){return r?Y.get(r):le}function N(r){return typeof r=="number"}function D(r){return!!(typeof r=="number"||typeof r=="string"&&d.isNumeric(r))}function K(r){return typeof r=="boolean"||r==null?null:typeof r=="string"?r:r.toString()}function ae(r){return typeof r=="number"?!1:r.indexOf("*")!==-1||r.indexOf("?")!==-1}function Z(r){const i=r.replace(/[.+^${}()|[\]\\]/g,"\\$&").replaceAll("?",".").replace(/[*]/g,".$&");return new RegExp(`^${i}$`)}const U=()=>new Set;class P extends d.Disposable{constructor(e,t,s){super();g(this,"_filteredOutRows$",new y.BehaviorSubject(U()));g(this,"filteredOutRows$",this._filteredOutRows$.asObservable());g(this,"_hasCriteria$",new y.BehaviorSubject(!1));g(this,"hasCriteria$",this._hasCriteria$.asObservable());g(this,"_filterColumnByIndex",new Map);g(this,"_alreadyFilteredOutRows",U());g(this,"_range");this.unitId=e,this.subUnitId=t,this._worksheet=s}get filteredOutRows(){return this._filteredOutRows$.getValue()}set filteredOutRows(e){this._alreadyFilteredOutRows=e,this._filteredOutRows$.next(e)}dispose(){super.dispose(),this._filteredOutRows$.complete(),this._hasCriteria$.complete()}serialize(){const e={ref:d.Rectangle.clone(this._range),filterColumns:this._getAllFilterColumns(!0).sort(([t],[s])=>t-s).map(([t,s])=>s.serialize())};return this._alreadyFilteredOutRows&&(e.cachedFilteredOut=Array.from(this._alreadyFilteredOutRows).sort()),e}static deserialize(e,t,s,n){const o=new P(e,t,s);return o._dump(n),o}_dump(e){var t;this.setRange(e.ref),(t=e.filterColumns)==null||t.forEach(s=>this._setCriteriaWithoutReCalc(s.colId,s)),e.cachedFilteredOut&&(this._alreadyFilteredOutRows=new Set(e.cachedFilteredOut),this._emit()),this._emitHasCriteria()}isRowFiltered(e){return this._alreadyFilteredOutRows.has(e)}getRange(){if(!this._range)throw new Error("[FilterModel] could not get range before a range is set!");return this._range}getFilteredOutRowsExceptCol(e){return this._getAllFilterColumns(!0).filter(([t])=>t!==e).reduce((t,[,s])=>{const n=s.calc({getAlreadyFilteredOutRows:()=>t});return n?d.mergeSets(t,n):t},new Set)}setRange(e){this._range=e,this._getAllFilterColumns(!0).forEach(([t,s])=>{s.setRangeAndColumn({startRow:e.startRow,endRow:e.endRow,startColumn:t,endColumn:t},t)})}setCriteria(e,t,s=!1){if(!this._range)throw new Error("[FilterModel] could not set criteria before a range is set!");if(!t){this._removeCriteria(e),this._rebuildAlreadyFilteredOutRowsWithCache(),s&&this._reCalcAllColumns(),this._emit(),this._emitHasCriteria();return}this._setCriteriaWithoutReCalc(e,t),s&&(this._rebuildAlreadyFilteredOutRowsWithCache(),this._reCalcWithNoCacheColumns(),this._emit(),this._emitHasCriteria())}getAllFilterColumns(){return this._getAllFilterColumns(!0)}getFilterColumn(e){var t;return(t=this._filterColumnByIndex.get(e))!=null?t:null}reCalc(){this._reCalcAllColumns(),this._emit()}_getAllFilterColumns(e=!1){const t=Array.from(this._filterColumnByIndex.entries());return e?t:t.map(([s,n])=>n)}_reCalcAllColumns(){this._alreadyFilteredOutRows=U(),this._getAllFilterColumns().forEach(e=>e.__clearCache()),this._reCalcWithNoCacheColumns()}_setCriteriaWithoutReCalc(e,t){const s=this._range;if(!s)throw new Error("[FilterModel] could not set criteria before a range is set!");const{startColumn:n,endColumn:o}=s;if(e>o||e<n)throw new Error(`[FilterModel] could not set criteria on column ${e} which is out of range!`);let l;this._filterColumnByIndex.has(e)?l=this._filterColumnByIndex.get(e):(l=new k(this.unitId,this.subUnitId,this._worksheet,t,{getAlreadyFilteredOutRows:()=>this._alreadyFilteredOutRows}),l.setRangeAndColumn(s,e),this._filterColumnByIndex.set(e,l)),l.setCriteria(t)}_removeCriteria(e){const t=this._filterColumnByIndex.get(e);t&&(t.dispose(),this._filterColumnByIndex.delete(e))}_emit(){this._filteredOutRows$.next(this._alreadyFilteredOutRows)}_emitHasCriteria(){this._hasCriteria$.next(this._filterColumnByIndex.size>0)}_rebuildAlreadyFilteredOutRowsWithCache(){const e=this._getAllFilterColumns().filter(t=>t.hasCache()).reduce((t,s)=>d.mergeSets(t,s.filteredOutRows),new Set);this._alreadyFilteredOutRows=e}_reCalcWithNoCacheColumns(){const e=this._getAllFilterColumns().filter(t=>!t.hasCache());for(const t of e){const s=t.reCalc();s&&(this._alreadyFilteredOutRows=d.mergeSets(this._alreadyFilteredOutRows,s))}}}class k extends d.Disposable{constructor(e,t,s,n,o){super();g(this,"_filteredOutRows",null);g(this,"_filterFn",null);g(this,"_range",null);g(this,"_column",0);g(this,"_filterByValues",!1);this.unitId=e,this.subUnitId=t,this._worksheet=s,this._criteria=n,this._filterColumnContext=o}get filteredOutRows(){return this._filteredOutRows}dispose(){super.dispose(),this._filteredOutRows=null}__clearCache(){this._filteredOutRows=null}serialize(){if(!this._criteria)throw new Error("[FilterColumn]: could not serialize without a filter column!");return d.Tools.deepClone({...this._criteria,colId:this._column})}hasCache(){return this._filteredOutRows!==null}setRangeAndColumn(e,t){this._range=e,this._column=t}setCriteria(e){this._criteria=e,this._generateFilterFn(),this._filteredOutRows=null}getColumnData(){return d.Tools.deepClone(this._criteria)}reCalc(){return this._filteredOutRows=this.calc(this._filterColumnContext),this._filteredOutRows}calc(e){if(!this._filterFn)throw new Error("[FilterColumn] cannot calculate without a filter fn!");if(!this._range)throw new Error("[FilterColumn] cannot calculate without a range!");if(typeof this._column!="number")throw new TypeError("[FilterColumn] cannot calculate without a column offset!");const t=this._column,s={startColumn:t,endColumn:t,startRow:this._range.startRow+1,endRow:this._range.endRow},n=new Set,o=e.getAlreadyFilteredOutRows();for(const l of this._worksheet.iterateByColumn(s,!1,!1)){const{row:a,rowSpan:u,col:c}=l;if(o.has(a)&&(!u||u===1))continue;const f=this._filterByValues?d.extractPureTextFromCell(this._worksheet.getCell(a,c)):ge(this._worksheet,a,c);if(!this._filterFn(f)&&(n.add(a),u))for(let _=1;_<u;_++)n.add(a+_)}return n}_generateFilterFn(){this._criteria&&(this._filterFn=ce(this._criteria),this._filterByValues=!!this._criteria.filters)}}function ce(r){if(r.filters)return ue(r.filters);if(r.customFilters)return he(r.customFilters);throw new Error("[FilterModel]: other types of filters are not supported yet.")}function ue(r){const i=!!r.blank,e=new Set(r.filters);return t=>t===void 0||t===""?i:e.has(typeof t=="string"?t:`${t}`)}function he(r){const i=r.customFilters.map(e=>_e(e));return me(i)?r.and?de(i):fe(i):i[0]}function de(r){const[i,e]=r;return t=>i(t)&&e(t)}function fe(r){const[i,e]=r;return t=>i(t)||e(t)}function me(r){return r.length===2}function _e(r){const i=r.val;if(r.operator===E.NOT_EQUALS&&!D(i))return s=>j.fn(s,i);if(oe(r.operator)){if(!D(i))return()=>!1;const s=B(r.operator),n=Number(i);return o=>s.fn(o,n)}const e=B(r.operator);return t=>e.fn(t,i)}function ge(r,i,e){const t=r.getCell(i,e);if(!t)return null;const s=r.getCellRaw(i,e);return t&&!s?q(t):s?t.t===d.CellValueType.NUMBER&&typeof t.v=="string"?s.v:q(s):null}function q(r){var t,s;const i=(s=(t=r.p)==null?void 0:t.body)==null?void 0:s.dataStream;if(i)return i.trimEnd();const e=r.v;return typeof e=="string"?r.t===d.CellValueType.BOOLEAN?e.toUpperCase():e:typeof e=="number"?r.t===d.CellValueType.BOOLEAN?e?"TRUE":"FALSE":e:typeof e=="boolean"?e?"TRUE":"FALSE":""}const C={id:"sheet.mutation.set-filter-range",type:d.CommandType.MUTATION,handler:(r,i)=>{const{subUnitId:e,unitId:t,range:s}=i;return r.get(exports.SheetsFilterService).ensureFilterModel(t,e).setRange(s),!0}},F={id:"sheet.mutation.set-filter-criteria",type:d.CommandType.MUTATION,handler:(r,i)=>{const{subUnitId:e,unitId:t,criteria:s,col:n,reCalc:o=!0}=i,a=r.get(exports.SheetsFilterService).getFilterModel(t,e);return a?(a.setCriteria(n,s,o),!0):!1}},T={id:"sheet.mutation.remove-filter",type:d.CommandType.MUTATION,handler:(r,i)=>{const{unitId:e,subUnitId:t}=i;return r.get(exports.SheetsFilterService).removeFilterModel(e,t)}},z={id:"sheet.mutation.re-calc-filter",type:d.CommandType.MUTATION,handler:(r,i)=>{const{unitId:e,subUnitId:t}=i,n=r.get(exports.SheetsFilterService).getFilterModel(e,t);return n?(n.reCalc(),!0):!1}};var Ce=Object.defineProperty,Re=Object.getOwnPropertyDescriptor,pe=(r,i,e,t)=>{for(var s=t>1?void 0:t?Re(i,e):i,n=r.length-1,o;n>=0;n--)(o=r[n])&&(s=(t?o(i,e,s):o(s))||s);return t&&s&&Ce(i,e,s),s},b=(r,i)=>(e,t)=>i(e,t,r);const ee=new Set([C.id,F.id,T.id,z.id]),H="SHEET_FILTER_PLUGIN";exports.SheetsFilterService=class extends d.Disposable{constructor(e,t,s){super();g(this,"_filterModels",new Map);g(this,"_loadedUnitId$",new y.BehaviorSubject(null));g(this,"loadedUnitId$",this._loadedUnitId$.asObservable());g(this,"_activeFilterModel$",new y.BehaviorSubject(null));g(this,"activeFilterModel$",this._activeFilterModel$.asObservable());this._resourcesManagerService=e,this._univerInstanceService=t,this._commandService=s,this._initModel(),this._initActiveFilterModel()}get activeFilterModel(){return this._activeFilterModel$.getValue()}ensureFilterModel(e,t){const s=this.getFilterModel(e,t);if(s)return s;const n=this._univerInstanceService.getUniverSheetInstance(e);if(!n)throw new Error(`[SheetsFilterService]: could not create "FilterModel" on a non-existing workbook ${e}!`);const o=n.getSheetBySheetId(t);if(!o)throw new Error(`[SheetsFilterService]: could not create "FilterModel" on a non-existing worksheet ${t}!`);const l=new P(e,t,o);return this._cacheFilterModel(e,t,l),l}getFilterModel(e,t){var s,n;return(n=(s=this._filterModels.get(e))==null?void 0:s.get(t))!=null?n:null}removeFilterModel(e,t){const s=this.getFilterModel(e,t);return s?(s.dispose(),this._filterModels.get(e).delete(t),!0):!1}_updateActiveFilterModel(){let e;try{if(e=this._univerInstanceService.getCurrentUnitForType(d.UniverInstanceType.UNIVER_SHEET),!e){this._activeFilterModel$.next(null);return}}catch{return}const t=e.getActiveSheet();if(!t){this._activeFilterModel$.next(null);return}const s=t.getUnitId(),n=t.getSheetId(),o=this.getFilterModel(s,n);this._activeFilterModel$.next(o)}_initActiveFilterModel(){this.disposeWithMe(y.merge(d.fromCallback(this._commandService.onCommandExecuted).pipe(y.filter(([e])=>e.type===d.CommandType.MUTATION&&ee.has(e.id))),this._univerInstanceService.getCurrentTypeOfUnit$(d.UniverInstanceType.UNIVER_SHEET).pipe(y.switchMap(e=>{var t;return(t=e==null?void 0:e.activeSheet$)!=null?t:y.of(null)}))).subscribe(()=>this._updateActiveFilterModel()))}_serializeAutoFiltersForUnit(e){const t=this._filterModels.get(e);if(!t)return"{}";const s={};return t.forEach((n,o)=>{s[o]=n.serialize()}),JSON.stringify(s)}_deserializeAutoFiltersForUnit(e,t){const s=this._univerInstanceService.getUniverSheetInstance(e);Object.keys(t).forEach(n=>{const o=t[n],l=P.deserialize(e,n,s.getSheetBySheetId(n),o);this._cacheFilterModel(e,n,l)})}_initModel(){this._resourcesManagerService.registerPluginResource({pluginName:H,businesses:[2],toJson:e=>this._serializeAutoFiltersForUnit(e),parseJson:e=>JSON.parse(e),onLoad:(e,t)=>{this._deserializeAutoFiltersForUnit(e,t),this._loadedUnitId$.next(e),this._updateActiveFilterModel()},onUnLoad:e=>{const t=this._filterModels.get(e);t&&(t.forEach(s=>s.dispose()),this._filterModels.delete(e))}})}_cacheFilterModel(e,t,s){this._filterModels.has(e)||this._filterModels.set(e,new Map),this._filterModels.get(e).set(t,s)}};exports.SheetsFilterService=pe([d.OnLifecycle(d.LifecycleStages.Ready,exports.SheetsFilterService),b(0,d.IResourceManagerService),b(1,d.IUniverInstanceService),b(2,d.ICommandService)],exports.SheetsFilterService);function Fe(r,i){for(let e=0;e<r.length;e++){let t=e;if(r[e])for(let s=e+1;s<r.length;s++)r[t]&&r[s]&&i(r[t],r[s])&&(r[t]=null,t=s)}return r.filter(e=>e!==null)}function O(r){return Fe(r,(i,e)=>i.id===F.id&&e.id===F.id&&i.params.unitId===e.params.unitId&&i.params.subUnitId===e.params.subUnitId&&i.params.col===e.params.col)}var Se=Object.defineProperty,we=Object.getOwnPropertyDescriptor,ve=(r,i,e,t)=>{for(var s=t>1?void 0:t?we(i,e):i,n=r.length-1,o;n>=0;n--)(o=r[n])&&(s=(t?o(i,e,s):o(s))||s);return t&&s&&Se(i,e,s),s},x=(r,i)=>(e,t)=>i(e,t,r);let L=class extends d.Disposable{constructor(r,i,e,t,s){super(),this._commandService=r,this._sheetInterceptorService=i,this._sheetsFilterService=e,this._univerInstanceService=t,this._refRangeService=s,this._initCommands(),this._initRowFilteredInterceptor(),this._initInterceptors(),this._commandExecutedListener()}_initCommands(){[F,C,z,T].forEach(r=>this.disposeWithMe(this._commandService.registerCommand(r)))}_initInterceptors(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:e=>this._getUpdateFilter(e)}));const r=new d.DisposableCollection,i=(e,t)=>{var a;const s=this._univerInstanceService.getUniverSheetInstance(e);if(!s||!(s==null?void 0:s.getSheetBySheetId(t)))return;r.dispose();const o=(a=this._sheetsFilterService.getFilterModel(e,t))==null?void 0:a.getRange(),l=u=>{switch(u.id){case M.InsertRowCommand.id:{const c=u.params,f=c.unitId||e,_=c.subUnitId||t;return this._handleInsertRowCommand(c,f,_)}case M.InsertColCommand.id:{const c=u.params,f=c.unitId||e,_=c.subUnitId||t;return this._handleInsertColCommand(c,f,_)}case M.RemoveColCommand.id:{const c=u.params;return this._handleRemoveColCommand(c,e,t)}case M.RemoveRowCommand.id:{const c=u.params;return this._handleRemoveRowCommand(c,e,t)}case M.EffectRefRangId.MoveColsCommandId:{const c=u.params;return this._handleMoveColsCommand(c,e,t)}case M.EffectRefRangId.MoveRowsCommandId:{const c=u.params;return this._handleMoveRowsCommand(c,e,t)}case M.MoveRangeCommand.id:{const c=u.params;return this._handleMoveRangeCommand(c,e,t)}}return{redos:[],undos:[]}};o&&r.add(this._refRangeService.registerRefRange(o,l,e,t))};this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.id===M.SetWorksheetActivateCommand.id){const t=e.params,s=t.subUnitId,n=t.unitId;if(!s||!n)return;i(n,s)}if(e.id===C.id){const t=e.params,s=t.subUnitId,n=t.unitId;if(!s||!n)return;i(t.unitId,t.subUnitId)}})),this.disposeWithMe(this._sheetsFilterService.loadedUnitId$.subscribe(e=>{if(e){const t=this._univerInstanceService.getUniverSheetInstance(e),s=t==null?void 0:t.getActiveSheet();s&&i(e,s.getSheetId())}}))}_getUpdateFilter(r){const{id:i}=r;switch(i){case M.RemoveSheetCommand.id:{const e=r.params;return this._handleRemoveSheetCommand(e,e.unitId,e.subUnitId)}}return{redos:[],undos:[]}}_handleInsertColCommand(r,i,e){var m;const t=this._sheetsFilterService.getFilterModel(i,e),s=(m=t==null?void 0:t.getRange())!=null?m:null;if(!t||!s)return this._handleNull();const{startColumn:n,endColumn:o}=s,{startColumn:l,endColumn:a}=r.range,u=a-l+1;if(a>o)return this._handleNull();const c=[],f=[],_=l,S={unitId:i,subUnitId:e,range:{...s,startColumn:l<=n?n+u:n,endColumn:o+u}},p={unitId:i,subUnitId:e,range:s};c.push({id:C.id,params:S}),f.push({id:C.id,params:p});const h=t.getAllFilterColumns().filter(R=>R[0]>=_);if(h.length!==0){const{undos:R,redos:v}=this.moveCriteria(i,e,h,u);c.push(...v),f.push(...R)}return{redos:O(c),undos:O(f)}}_handleInsertRowCommand(r,i,e){var p;const t=this._sheetsFilterService.getFilterModel(i,e),s=(p=t==null?void 0:t.getRange())!=null?p:null;if(!t||!s)return this._handleNull();const{startRow:n,endRow:o}=s,{startRow:l,endRow:a}=r.range,u=a-l+1;if(a>o)return this._handleNull();const c=[],f=[],_={unitId:i,subUnitId:e,range:{...s,startRow:l<=n?n+u:n,endRow:o+u}},S={unitId:i,subUnitId:e,range:s};return c.push({id:C.id,params:_}),f.push({id:C.id,params:S}),{redos:O(c),undos:O(f)}}_handleRemoveColCommand(r,i,e){var w;const t=this._sheetsFilterService.getFilterModel(i,e),s=(w=t==null?void 0:t.getRange())!=null?w:null;if(!t||!s)return this._handleNull();const{startColumn:n,endColumn:o}=s,{startColumn:l,endColumn:a}=r.range;if(l>o)return this._handleNull();const u=[],c=[],f=a<n?0:Math.min(a,o)-Math.max(l,n)+1,_=a-l+1,S=t.getAllFilterColumns();S.forEach(h=>{const[m,R]=h;m<=a&&m>=l&&(u.push({id:F.id,params:{unitId:i,subUnitId:e,col:m,criteria:null}}),c.push({id:F.id,params:{unitId:i,subUnitId:e,col:m,criteria:{...R.serialize(),colId:m}}}))});const p=S.filter(h=>{const[m,R]=h;return m>a});if(p.length>0){const{undos:h,redos:m}=this.moveCriteria(i,e,p,-_);u.push(...m),c.push(...h)}if(f===o-n+1){const h={unitId:i,subUnitId:e};u.push({id:T.id,params:h})}else if(n<=l){const h=o-f,m={unitId:i,subUnitId:e,range:{...s,endColumn:h}};u.push({id:C.id,params:m})}else{const h={unitId:i,subUnitId:e,range:{...s,startColumn:l,endColumn:o-(a-l+1)}};u.push({id:C.id,params:h})}return c.push({id:C.id,params:{range:s,unitId:i,subUnitId:e}}),{undos:O(c),redos:O(u)}}_handleRemoveRowCommand(r,i,e){var p;const t=this._sheetsFilterService.getFilterModel(i,e);if(!t)return this._handleNull();const s=t.getRange(),{startRow:n,endRow:o}=s,{startRow:l,endRow:a}=r.range;if(l>o)return this._handleNull();const u=[],c=[],f=t.getAllFilterColumns(),_=n<=a&&n>=l;c.push({id:C.id,params:{range:s,unitId:i,subUnitId:e}});const S=Math.min(a,o)-Math.max(l,n)+1;if(S===o-n+1||_){const w={unitId:i,subUnitId:e};u.push({id:T.id,params:w}),f.forEach(h=>{const[m,R]=h,v={unitId:i,subUnitId:e,col:m,criteria:{...R.serialize(),colId:m}};c.push({id:F.id,params:v})})}else{const w=(p=this._univerInstanceService.getUniverSheetInstance(i))==null?void 0:p.getSheetBySheetId(e);if(!w)return this._handleNull();const h=[];for(let A=l;A<=a;A++)w.getRowFiltered(A)&&h.push(A);const m=Math.min(n,l),R=m+(o-n)-S+h.length,v={unitId:i,subUnitId:e,range:{...s,startRow:m,endRow:R}};u.push({id:C.id,params:v})}return{undos:O(c),redos:O(u)}}_handleMoveColsCommand(r,i,e){var w;const t=this._sheetsFilterService.getFilterModel(i,e),s=(w=t==null?void 0:t.getRange())!=null?w:null;if(!t||!s)return this._handleNull();const{startColumn:n,endColumn:o}=s,{fromRange:l,toRange:a}=r;if(l.endColumn<n&&a.startColumn<=n||l.startColumn>o&&a.endColumn>o)return this._handleNull();const u=[],c=[],f={};for(let h=n;h<=o;h++)f[h]={colIndex:h,filter:t.getFilterColumn(h)};d.moveMatrixArray(l.startColumn,l.endColumn-l.startColumn+1,a.startColumn,f);const _=Object.keys(f).map(h=>Number(h)),S=Math.max(..._),p=Math.min(..._);if(_.forEach(h=>{var A,W;const{colIndex:m,filter:R}=f[h],v=h;if(R){const te={unitId:i,subUnitId:e,col:v,criteria:{...R.serialize(),colId:v}};if(u.push({id:F.id,params:te}),c.push({id:T.id,params:{unitId:i,subUnitId:e,col:v,criteria:{...(A=t.getFilterColumn(v))==null?void 0:A.serialize(),colId:v}}}),!((W=f[m])!=null&&W.filter)){const re={unitId:i,subUnitId:e,col:m,criteria:null};u.push({id:F.id,params:re}),c.push({id:F.id,params:{unitId:i,subUnitId:e,col:m,criteria:{...R.serialize(),colId:m}}})}}}),n!==p||o!==S){const h={unitId:i,subUnitId:e,range:{...s,startColumn:p,endColumn:S}};u.unshift({id:C.id,params:h}),c.push({id:C.id,params:{range:s,unitId:i,subUnitId:e}})}return{undos:c,redos:u}}_handleMoveRowsCommand(r,i,e){var w;const t=this._sheetsFilterService.getFilterModel(i,e),s=(w=t==null?void 0:t.getRange())!=null?w:null;if(!t||!s)return this._handleNull();const{startRow:n,endRow:o}=s,{fromRange:l,toRange:a}=r;if(l.endRow<n&&a.startRow<=n||l.startRow>o&&a.endRow>o)return this._handleNull();const u=[],c=[],f={};for(let h=n;h<=o;h++)f[h]={offset:h-n};d.moveMatrixArray(l.startRow,l.endRow-l.startRow+1,a.startRow,f);const _=Object.keys(f).map(h=>Number(h)),S=Math.max(..._),p=Math.min(..._);if(n!==p||o!==S){const h={unitId:i,subUnitId:e,range:{...s,startRow:p,endRow:S}};u.unshift({id:C.id,params:h}),c.push({id:C.id,params:{range:s,unitId:i,subUnitId:e}})}return{redos:u,undos:c}}_handleMoveRangeCommand(r,i,e){const{fromRange:t,toRange:s}=r,n=this._sheetsFilterService.getFilterModel(i,e);if(!n)return this._handleNull();const o=n.getRange();if(!o)return this._handleNull();const l=[],a=[];if(d.Rectangle.contains(t,o)){const u=o.startRow-t.startRow,c=o.startColumn-t.startColumn,f={startRow:s.startRow+u,startColumn:s.startColumn+c,endRow:s.startRow+u+(o.endRow-o.startRow),endColumn:s.startColumn+c+(o.endColumn-o.startColumn)},_={id:T.id,params:{unitId:i,subUnitId:e}},S={id:C.id,params:{unitId:i,subUnitId:e,range:f}},p={id:C.id,params:{unitId:i,subUnitId:e,range:o}};l.push(_,S),a.push(_,p);const w=n.getAllFilterColumns(),h=s.startColumn-t.startColumn;w.forEach(m=>{const[R,v]=m;v&&(l.push({id:F.id,params:{unitId:i,subUnitId:e,col:R+h,criteria:{...v.serialize(),colId:R+h}}}),a.push({id:F.id,params:{unitId:i,subUnitId:e,col:R,criteria:{...v.serialize(),colId:R}}}))})}return{redos:l,undos:a}}_handleRemoveSheetCommand(r,i,e){const t=this._sheetsFilterService.getFilterModel(i,e);if(!t)return this._handleNull();const s=t.getRange();if(!s)return this._handleNull();const n=[],o=[];return t.getAllFilterColumns().forEach(a=>{const[u,c]=a;o.push({id:F.id,params:{unitId:i,subUnitId:e,col:a,criteria:{...c.serialize(),colId:a}}})}),n.push({id:T.id,params:{unitId:i,subUnitId:e,range:s}}),o.unshift({id:C.id,params:{range:s,unitId:i,subUnitId:e}}),{undos:o,redos:n}}_handleNull(){return{redos:[],undos:[]}}_initRowFilteredInterceptor(){this.disposeWithMe(this._sheetInterceptorService.intercept(M.INTERCEPTOR_POINT.ROW_FILTERED,{handler:(r,i)=>{var e,t;return r?!0:(t=(e=this._sheetsFilterService.getFilterModel(i.unitId,i.subUnitId))==null?void 0:e.isRowFiltered(i.row))!=null?t:!1}}))}moveCriteria(r,i,e,t){const s={unitId:r,subUnitId:i,criteria:null,col:-1},n=[],o=[];return e.forEach(l=>{const[a,u]=l;o.push({id:F.id,params:{...s,col:a}}),n.push({id:F.id,params:{...s,col:a,criteria:{...u.serialize(),colId:a}}})}),e.forEach(l=>{const[a,u]=l;o.push({id:F.id,params:{...s,col:a+t,criteria:{...u.serialize(),colId:a+t}}}),n.push({id:F.id,params:{...s,col:a+t,criteria:null}})}),{redos:o,undos:n}}_commandExecutedListener(){this.disposeWithMe(this._commandService.onCommandExecuted(r=>{const{unitId:i,subUnitId:e}=r.params||{},t=this._sheetsFilterService.getFilterModel(i,e);if(!t)return;const s=Array.from(t.filteredOutRows).sort((l,a)=>l-a),n=[];let o=!1;if(r.id===M.RemoveRowMutation.id){const{startRow:l,endRow:a}=r.params.range,u=s.filter(c=>c>=l&&c<=a);s.forEach(c=>{if(c<l)n.push(c);else if(o=!0,c<=a){const f=Math.max(l,n.length?n[n.length-1]+1:l);n.push(f)}else n.push(c-(a-l+1-u.length))})}if(r.id===M.InsertRowMutation.id){const{startRow:l,endRow:a}=r.params.range;s.forEach(u=>{u>=l?(o=!0,n.push(u+(a-l+1))):n.push(u)})}o&&(t.filteredOutRows=new Set(n))}))}};L=ve([d.OnLifecycle(d.LifecycleStages.Ready,L),x(0,d.ICommandService),x(1,I.Inject(M.SheetInterceptorService)),x(2,I.Inject(exports.SheetsFilterService)),x(3,d.IUniverInstanceService),x(4,I.Inject(M.RefRangeService))],L);var Me=Object.defineProperty,Ee=Object.getOwnPropertyDescriptor,Oe=(r,i,e,t)=>{for(var s=t>1?void 0:t?Ee(i,e):i,n=r.length-1,o;n>=0;n--)(o=r[n])&&(s=(t?o(i,e,s):o(s))||s);return t&&s&&Me(i,e,s),s},ye=(r,i)=>(e,t)=>i(e,t,r),$;exports.UniverSheetsFilterPlugin=($=class extends d.Plugin{constructor(i,e){super(),this._injector=e}onStarting(i){[[exports.SheetsFilterService],[L]].forEach(e=>i.add(e))}},g($,"type",d.UniverInstanceType.UNIVER_SHEET),g($,"pluginName",H),$);exports.UniverSheetsFilterPlugin=Oe([ye(1,I.Inject(I.Injector))],exports.UniverSheetsFilterPlugin);exports.CustomFilterOperator=E;exports.FILTER_MUTATIONS=ee;exports.FilterColumn=k;exports.FilterModel=P;exports.ReCalcSheetsFilterMutation=z;exports.RemoveSheetsFilterMutation=T;exports.SHEET_FILTER_SNAPSHOT_ID=H;exports.SetSheetsFilterCriteriaMutation=F;exports.SetSheetsFilterRangeMutation=C;exports.equals=X;exports.getCustomFilterFn=B;exports.greaterThan=V;exports.greaterThanOrEqualTo=Q;exports.lessThan=G;exports.lessThanOrEqualTo=J;exports.notEquals=j;
|
package/lib/es/index.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var _ = (s, r, e) => (
|
|
4
|
-
import { isNumeric as
|
|
5
|
-
import { Inject as $, Injector as
|
|
6
|
-
import { BehaviorSubject as I, merge as
|
|
7
|
-
import { SheetInterceptorService as
|
|
1
|
+
var ce = Object.defineProperty;
|
|
2
|
+
var ue = (s, r, e) => r in s ? ce(s, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[r] = e;
|
|
3
|
+
var _ = (s, r, e) => (ue(s, typeof r != "symbol" ? r + "" : r, e), e);
|
|
4
|
+
import { isNumeric as de, Disposable as B, Rectangle as Y, mergeSets as U, Tools as V, extractPureTextFromCell as he, CellValueType as W, CommandType as N, UniverInstanceType as H, fromCallback as fe, OnLifecycle as K, LifecycleStages as Z, IResourceManagerService as me, IUniverInstanceService as k, ICommandService as ee, DisposableCollection as _e, moveMatrixArray as D, Plugin as ge } from "@univerjs/core";
|
|
5
|
+
import { Inject as $, Injector as Ce } from "@wendellhu/redi";
|
|
6
|
+
import { BehaviorSubject as I, merge as Re, filter as pe, switchMap as Fe, of as we } from "rxjs";
|
|
7
|
+
import { SheetInterceptorService as Se, RefRangeService as ve, SetWorksheetActivateCommand as Me, RemoveSheetCommand as Ee, INTERCEPTOR_POINT as Oe, RemoveRowMutation as ye, InsertRowMutation as Ae, MoveRangeCommand as Te, EffectRefRangId as Q, RemoveRowCommand as Ne, RemoveColCommand as xe, InsertColCommand as $e, InsertRowCommand as Pe } from "@univerjs/sheets";
|
|
8
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
|
|
9
|
+
const Ie = {
|
|
10
10
|
operator: E.GREATER_THAN,
|
|
11
11
|
fn: (s, r) => A(s) ? s > r : !1
|
|
12
|
-
},
|
|
12
|
+
}, Le = {
|
|
13
13
|
operator: E.GREATER_THAN_OR_EQUAL,
|
|
14
14
|
fn: (s, r) => A(s) ? s >= r : !1
|
|
15
|
-
},
|
|
15
|
+
}, Be = {
|
|
16
16
|
operator: E.LESS_THAN,
|
|
17
17
|
fn: (s, r) => A(s) ? s < r : !1
|
|
18
18
|
}, Ue = {
|
|
19
19
|
operator: E.LESS_THAN_OR_EQUAL,
|
|
20
20
|
fn: (s, r) => A(s) ? s <= r : !1
|
|
21
|
-
},
|
|
21
|
+
}, ze = {
|
|
22
22
|
operator: E.EQUAL,
|
|
23
23
|
fn: (s, r) => A(s) ? s === r : !1
|
|
24
24
|
}, te = {
|
|
@@ -28,15 +28,15 @@ const Pe = {
|
|
|
28
28
|
if (r === " ")
|
|
29
29
|
return s != null;
|
|
30
30
|
const e = se(s);
|
|
31
|
-
return e &&
|
|
31
|
+
return e && je(r) ? !ne(r).test(e) : e !== r;
|
|
32
32
|
}
|
|
33
33
|
return A(s) ? s !== r : !0;
|
|
34
34
|
}
|
|
35
|
-
}, re = /* @__PURE__ */ new Map([]),
|
|
36
|
-
|
|
35
|
+
}, re = /* @__PURE__ */ new Map([]), be = [Ie, Le, Be, Ue, ze, te];
|
|
36
|
+
be.forEach((s) => {
|
|
37
37
|
re.set(s.operator, s);
|
|
38
38
|
});
|
|
39
|
-
function
|
|
39
|
+
function We(s) {
|
|
40
40
|
return !!s;
|
|
41
41
|
}
|
|
42
42
|
const He = {
|
|
@@ -52,12 +52,12 @@ function A(s) {
|
|
|
52
52
|
return typeof s == "number";
|
|
53
53
|
}
|
|
54
54
|
function G(s) {
|
|
55
|
-
return !!(typeof s == "number" || typeof s == "string" &&
|
|
55
|
+
return !!(typeof s == "number" || typeof s == "string" && de(s));
|
|
56
56
|
}
|
|
57
57
|
function se(s) {
|
|
58
58
|
return typeof s == "boolean" || s == null ? null : typeof s == "string" ? s : s.toString();
|
|
59
59
|
}
|
|
60
|
-
function
|
|
60
|
+
function je(s) {
|
|
61
61
|
return typeof s == "number" ? !1 : s.indexOf("*") !== -1 || s.indexOf("?") !== -1;
|
|
62
62
|
}
|
|
63
63
|
function ne(s) {
|
|
@@ -65,7 +65,7 @@ function ne(s) {
|
|
|
65
65
|
return new RegExp(`^${r}$`);
|
|
66
66
|
}
|
|
67
67
|
const z = () => /* @__PURE__ */ new Set();
|
|
68
|
-
class L extends
|
|
68
|
+
class L extends B {
|
|
69
69
|
constructor(e, t, n) {
|
|
70
70
|
super();
|
|
71
71
|
_(this, "_filteredOutRows$", new I(z()));
|
|
@@ -129,7 +129,7 @@ class L extends U {
|
|
|
129
129
|
getFilteredOutRowsExceptCol(e) {
|
|
130
130
|
return this._getAllFilterColumns(!0).filter(([t]) => t !== e).reduce((t, [, n]) => {
|
|
131
131
|
const i = n.calc({ getAlreadyFilteredOutRows: () => t });
|
|
132
|
-
return i ?
|
|
132
|
+
return i ? U(t, i) : t;
|
|
133
133
|
}, /* @__PURE__ */ new Set());
|
|
134
134
|
}
|
|
135
135
|
/**
|
|
@@ -183,7 +183,7 @@ class L extends U {
|
|
|
183
183
|
if (e > o || e < i)
|
|
184
184
|
throw new Error(`[FilterModel] could not set criteria on column ${e} which is out of range!`);
|
|
185
185
|
let l;
|
|
186
|
-
this._filterColumnByIndex.has(e) ? l = this._filterColumnByIndex.get(e) : (l = new
|
|
186
|
+
this._filterColumnByIndex.has(e) ? l = this._filterColumnByIndex.get(e) : (l = new Ve(
|
|
187
187
|
this.unitId,
|
|
188
188
|
this.subUnitId,
|
|
189
189
|
this._worksheet,
|
|
@@ -202,18 +202,18 @@ class L extends U {
|
|
|
202
202
|
this._hasCriteria$.next(this._filterColumnByIndex.size > 0);
|
|
203
203
|
}
|
|
204
204
|
_rebuildAlreadyFilteredOutRowsWithCache() {
|
|
205
|
-
const e = this._getAllFilterColumns().filter((t) => t.hasCache()).reduce((t, n) =>
|
|
205
|
+
const e = this._getAllFilterColumns().filter((t) => t.hasCache()).reduce((t, n) => U(t, n.filteredOutRows), /* @__PURE__ */ new Set());
|
|
206
206
|
this._alreadyFilteredOutRows = e;
|
|
207
207
|
}
|
|
208
208
|
_reCalcWithNoCacheColumns() {
|
|
209
209
|
const e = this._getAllFilterColumns().filter((t) => !t.hasCache());
|
|
210
210
|
for (const t of e) {
|
|
211
211
|
const n = t.reCalc();
|
|
212
|
-
n && (this._alreadyFilteredOutRows =
|
|
212
|
+
n && (this._alreadyFilteredOutRows = U(this._alreadyFilteredOutRows, n));
|
|
213
213
|
}
|
|
214
214
|
}
|
|
215
215
|
}
|
|
216
|
-
class
|
|
216
|
+
class Ve extends B {
|
|
217
217
|
constructor(e, t, n, i, o) {
|
|
218
218
|
super();
|
|
219
219
|
_(this, "_filteredOutRows", null);
|
|
@@ -278,7 +278,7 @@ class je extends U {
|
|
|
278
278
|
const { row: a, rowSpan: u, col: c } = l;
|
|
279
279
|
if (o.has(a) && (!u || u === 1))
|
|
280
280
|
continue;
|
|
281
|
-
const h = this._filterByValues ?
|
|
281
|
+
const h = this._filterByValues ? he(this._worksheet.getCell(a, c)) : Ke(this._worksheet, a, c);
|
|
282
282
|
if (!this._filterFn(h) && (i.add(a), u))
|
|
283
283
|
for (let m = 1; m < u; m++)
|
|
284
284
|
i.add(a + m);
|
|
@@ -286,40 +286,40 @@ class je extends U {
|
|
|
286
286
|
return i;
|
|
287
287
|
}
|
|
288
288
|
_generateFilterFn() {
|
|
289
|
-
this._criteria && (this._filterFn =
|
|
289
|
+
this._criteria && (this._filterFn = De(this._criteria), this._filterByValues = !!this._criteria.filters);
|
|
290
290
|
}
|
|
291
291
|
}
|
|
292
|
-
function
|
|
292
|
+
function De(s) {
|
|
293
293
|
if (s.filters)
|
|
294
|
-
return
|
|
294
|
+
return Qe(s.filters);
|
|
295
295
|
if (s.customFilters)
|
|
296
|
-
return
|
|
296
|
+
return qe(s.customFilters);
|
|
297
297
|
throw new Error("[FilterModel]: other types of filters are not supported yet.");
|
|
298
298
|
}
|
|
299
|
-
function
|
|
299
|
+
function Qe(s) {
|
|
300
300
|
const r = !!s.blank, e = new Set(s.filters);
|
|
301
301
|
return (t) => t === void 0 || t === "" ? r : e.has(typeof t == "string" ? t : `${t}`);
|
|
302
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
303
|
function qe(s) {
|
|
304
|
+
const r = s.customFilters.map((e) => Ye(e));
|
|
305
|
+
return Xe(r) ? s.and ? Ge(r) : Je(r) : r[0];
|
|
306
|
+
}
|
|
307
|
+
function Ge(s) {
|
|
308
308
|
const [r, e] = s;
|
|
309
309
|
return (t) => r(t) && e(t);
|
|
310
310
|
}
|
|
311
|
-
function
|
|
311
|
+
function Je(s) {
|
|
312
312
|
const [r, e] = s;
|
|
313
313
|
return (t) => r(t) || e(t);
|
|
314
314
|
}
|
|
315
|
-
function
|
|
315
|
+
function Xe(s) {
|
|
316
316
|
return s.length === 2;
|
|
317
317
|
}
|
|
318
|
-
function
|
|
318
|
+
function Ye(s) {
|
|
319
319
|
const r = s.val;
|
|
320
320
|
if (s.operator === E.NOT_EQUALS && !G(r))
|
|
321
321
|
return (n) => te.fn(n, r);
|
|
322
|
-
if (
|
|
322
|
+
if (We(s.operator)) {
|
|
323
323
|
if (!G(r))
|
|
324
324
|
return () => !1;
|
|
325
325
|
const n = q(s.operator), i = Number(r);
|
|
@@ -328,12 +328,12 @@ function Xe(s) {
|
|
|
328
328
|
const e = q(s.operator);
|
|
329
329
|
return (t) => e.fn(t, r);
|
|
330
330
|
}
|
|
331
|
-
function
|
|
331
|
+
function Ke(s, r, e) {
|
|
332
332
|
const t = s.getCell(r, e);
|
|
333
333
|
if (!t)
|
|
334
334
|
return null;
|
|
335
335
|
const n = s.getCellRaw(r, e);
|
|
336
|
-
return t && !n ? J(t) : n ? t.t ===
|
|
336
|
+
return t && !n ? J(t) : n ? t.t === W.NUMBER && typeof t.v == "string" ? n.v : J(n) : null;
|
|
337
337
|
}
|
|
338
338
|
function J(s) {
|
|
339
339
|
var t, n;
|
|
@@ -341,49 +341,49 @@ function J(s) {
|
|
|
341
341
|
if (r)
|
|
342
342
|
return r.trimEnd();
|
|
343
343
|
const e = s.v;
|
|
344
|
-
return typeof e == "string" ? s.t ===
|
|
344
|
+
return typeof e == "string" ? s.t === W.BOOLEAN ? e.toUpperCase() : e : typeof e == "number" ? s.t === W.BOOLEAN ? e ? "TRUE" : "FALSE" : e : typeof e == "boolean" ? e ? "TRUE" : "FALSE" : "";
|
|
345
345
|
}
|
|
346
346
|
const C = {
|
|
347
347
|
id: "sheet.mutation.set-filter-range",
|
|
348
|
-
type:
|
|
348
|
+
type: N.MUTATION,
|
|
349
349
|
handler: (s, r) => {
|
|
350
350
|
const { subUnitId: e, unitId: t, range: n } = r;
|
|
351
351
|
return s.get(v).ensureFilterModel(t, e).setRange(n), !0;
|
|
352
352
|
}
|
|
353
353
|
}, F = {
|
|
354
354
|
id: "sheet.mutation.set-filter-criteria",
|
|
355
|
-
type:
|
|
355
|
+
type: N.MUTATION,
|
|
356
356
|
handler: (s, r) => {
|
|
357
357
|
const { subUnitId: e, unitId: t, criteria: n, col: i, reCalc: o = !0 } = r, a = s.get(v).getFilterModel(t, e);
|
|
358
358
|
return a ? (a.setCriteria(i, n, o), !0) : !1;
|
|
359
359
|
}
|
|
360
360
|
}, y = {
|
|
361
361
|
id: "sheet.mutation.remove-filter",
|
|
362
|
-
type:
|
|
362
|
+
type: N.MUTATION,
|
|
363
363
|
handler: (s, r) => {
|
|
364
364
|
const { unitId: e, subUnitId: t } = r;
|
|
365
365
|
return s.get(v).removeFilterModel(e, t);
|
|
366
366
|
}
|
|
367
367
|
}, ie = {
|
|
368
368
|
id: "sheet.mutation.re-calc-filter",
|
|
369
|
-
type:
|
|
369
|
+
type: N.MUTATION,
|
|
370
370
|
handler: (s, r) => {
|
|
371
371
|
const { unitId: e, subUnitId: t } = r, i = s.get(v).getFilterModel(e, t);
|
|
372
372
|
return i ? (i.reCalc(), !0) : !1;
|
|
373
373
|
}
|
|
374
374
|
};
|
|
375
|
-
var
|
|
376
|
-
for (var n = t > 1 ? void 0 : t ?
|
|
375
|
+
var Ze = Object.defineProperty, ke = Object.getOwnPropertyDescriptor, et = (s, r, e, t) => {
|
|
376
|
+
for (var n = t > 1 ? void 0 : t ? ke(r, e) : r, i = s.length - 1, o; i >= 0; i--)
|
|
377
377
|
(o = s[i]) && (n = (t ? o(r, e, n) : o(n)) || n);
|
|
378
|
-
return t && n &&
|
|
378
|
+
return t && n && Ze(r, e, n), n;
|
|
379
379
|
}, b = (s, r) => (e, t) => r(e, t, s);
|
|
380
|
-
const
|
|
380
|
+
const tt = /* @__PURE__ */ new Set([
|
|
381
381
|
C.id,
|
|
382
382
|
F.id,
|
|
383
383
|
y.id,
|
|
384
384
|
ie.id
|
|
385
|
-
]),
|
|
386
|
-
let v = class extends
|
|
385
|
+
]), oe = "SHEET_FILTER_PLUGIN";
|
|
386
|
+
let v = class extends B {
|
|
387
387
|
constructor(r, e, t) {
|
|
388
388
|
super();
|
|
389
389
|
_(this, "_filterModels", /* @__PURE__ */ new Map());
|
|
@@ -427,7 +427,7 @@ let v = class extends U {
|
|
|
427
427
|
_updateActiveFilterModel() {
|
|
428
428
|
let r;
|
|
429
429
|
try {
|
|
430
|
-
if (r = this._univerInstanceService.getCurrentUnitForType(
|
|
430
|
+
if (r = this._univerInstanceService.getCurrentUnitForType(H.UNIVER_SHEET), !r) {
|
|
431
431
|
this._activeFilterModel$.next(null);
|
|
432
432
|
return;
|
|
433
433
|
}
|
|
@@ -444,13 +444,13 @@ let v = class extends U {
|
|
|
444
444
|
}
|
|
445
445
|
_initActiveFilterModel() {
|
|
446
446
|
this.disposeWithMe(
|
|
447
|
-
|
|
447
|
+
Re(
|
|
448
448
|
// source1: executing filter related mutations
|
|
449
|
-
|
|
450
|
-
// source2:
|
|
451
|
-
this._univerInstanceService.getCurrentTypeOfUnit$(
|
|
449
|
+
fe(this._commandService.onCommandExecuted).pipe(pe(([r]) => r.type === N.MUTATION && tt.has(r.id))),
|
|
450
|
+
// source2: activate sheet changes
|
|
451
|
+
this._univerInstanceService.getCurrentTypeOfUnit$(H.UNIVER_SHEET).pipe(Fe((r) => {
|
|
452
452
|
var e;
|
|
453
|
-
return (e = r == null ? void 0 : r.activeSheet$) != null ? e :
|
|
453
|
+
return (e = r == null ? void 0 : r.activeSheet$) != null ? e : we(null);
|
|
454
454
|
}))
|
|
455
455
|
).subscribe(() => this._updateActiveFilterModel())
|
|
456
456
|
);
|
|
@@ -473,7 +473,7 @@ let v = class extends U {
|
|
|
473
473
|
}
|
|
474
474
|
_initModel() {
|
|
475
475
|
this._resourcesManagerService.registerPluginResource({
|
|
476
|
-
pluginName:
|
|
476
|
+
pluginName: oe,
|
|
477
477
|
businesses: [2],
|
|
478
478
|
toJson: (r) => this._serializeAutoFiltersForUnit(r),
|
|
479
479
|
parseJson: (r) => JSON.parse(r),
|
|
@@ -490,9 +490,9 @@ let v = class extends U {
|
|
|
490
490
|
this._filterModels.has(r) || this._filterModels.set(r, /* @__PURE__ */ new Map()), this._filterModels.get(r).set(e, t);
|
|
491
491
|
}
|
|
492
492
|
};
|
|
493
|
-
v =
|
|
493
|
+
v = et([
|
|
494
494
|
K(Z.Ready, v),
|
|
495
|
-
b(0,
|
|
495
|
+
b(0, me),
|
|
496
496
|
b(1, k),
|
|
497
497
|
b(2, ee)
|
|
498
498
|
], v);
|
|
@@ -512,8 +512,8 @@ var st = Object.defineProperty, nt = Object.getOwnPropertyDescriptor, it = (s, r
|
|
|
512
512
|
for (var n = t > 1 ? void 0 : t ? nt(r, e) : r, i = s.length - 1, o; i >= 0; i--)
|
|
513
513
|
(o = s[i]) && (n = (t ? o(r, e, n) : o(n)) || n);
|
|
514
514
|
return t && n && st(r, e, n), n;
|
|
515
|
-
},
|
|
516
|
-
let P = class extends
|
|
515
|
+
}, T = (s, r) => (e, t) => r(e, t, s);
|
|
516
|
+
let P = class extends B {
|
|
517
517
|
constructor(s, r, e, t, n) {
|
|
518
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
519
|
}
|
|
@@ -529,7 +529,7 @@ let P = class extends U {
|
|
|
529
529
|
this.disposeWithMe(this._sheetInterceptorService.interceptCommand({
|
|
530
530
|
getMutations: (e) => this._getUpdateFilter(e)
|
|
531
531
|
}));
|
|
532
|
-
const s = new
|
|
532
|
+
const s = new _e(), r = (e, t) => {
|
|
533
533
|
var a;
|
|
534
534
|
const n = this._univerInstanceService.getUniverSheetInstance(e);
|
|
535
535
|
if (!n || !(n == null ? void 0 : n.getSheetBySheetId(t)))
|
|
@@ -537,15 +537,15 @@ let P = class extends U {
|
|
|
537
537
|
s.dispose();
|
|
538
538
|
const o = (a = this._sheetsFilterService.getFilterModel(e, t)) == null ? void 0 : a.getRange(), l = (u) => {
|
|
539
539
|
switch (u.id) {
|
|
540
|
-
case
|
|
540
|
+
case Pe.id: {
|
|
541
541
|
const c = u.params, h = c.unitId || e, m = c.subUnitId || t;
|
|
542
542
|
return this._handleInsertRowCommand(c, h, m);
|
|
543
543
|
}
|
|
544
|
-
case
|
|
544
|
+
case $e.id: {
|
|
545
545
|
const c = u.params, h = c.unitId || e, m = c.subUnitId || t;
|
|
546
546
|
return this._handleInsertColCommand(c, h, m);
|
|
547
547
|
}
|
|
548
|
-
case
|
|
548
|
+
case xe.id: {
|
|
549
549
|
const c = u.params;
|
|
550
550
|
return this._handleRemoveColCommand(c, e, t);
|
|
551
551
|
}
|
|
@@ -561,7 +561,7 @@ let P = class extends U {
|
|
|
561
561
|
const c = u.params;
|
|
562
562
|
return this._handleMoveRowsCommand(c, e, t);
|
|
563
563
|
}
|
|
564
|
-
case
|
|
564
|
+
case Te.id: {
|
|
565
565
|
const c = u.params;
|
|
566
566
|
return this._handleMoveRangeCommand(c, e, t);
|
|
567
567
|
}
|
|
@@ -571,7 +571,7 @@ let P = class extends U {
|
|
|
571
571
|
o && s.add(this._refRangeService.registerRefRange(o, l, e, t));
|
|
572
572
|
};
|
|
573
573
|
this.disposeWithMe(this._commandService.onCommandExecuted((e) => {
|
|
574
|
-
if (e.id ===
|
|
574
|
+
if (e.id === Me.id) {
|
|
575
575
|
const t = e.params, n = t.subUnitId, i = t.unitId;
|
|
576
576
|
if (!n || !i)
|
|
577
577
|
return;
|
|
@@ -593,7 +593,7 @@ let P = class extends U {
|
|
|
593
593
|
_getUpdateFilter(s) {
|
|
594
594
|
const { id: r } = s;
|
|
595
595
|
switch (r) {
|
|
596
|
-
case
|
|
596
|
+
case Ee.id: {
|
|
597
597
|
const e = s.params;
|
|
598
598
|
return this._handleRemoveSheetCommand(e, e.unitId, e.subUnitId);
|
|
599
599
|
}
|
|
@@ -780,20 +780,20 @@ let P = class extends U {
|
|
|
780
780
|
var O, j;
|
|
781
781
|
const { colIndex: f, filter: g } = h[d], S = d;
|
|
782
782
|
if (g) {
|
|
783
|
-
const
|
|
783
|
+
const le = {
|
|
784
784
|
unitId: r,
|
|
785
785
|
subUnitId: e,
|
|
786
786
|
col: S,
|
|
787
787
|
criteria: { ...g.serialize(), colId: S }
|
|
788
788
|
};
|
|
789
|
-
if (u.push({ id: F.id, params:
|
|
790
|
-
const
|
|
789
|
+
if (u.push({ id: F.id, params: le }), 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 ae = {
|
|
791
791
|
unitId: r,
|
|
792
792
|
subUnitId: e,
|
|
793
793
|
col: f,
|
|
794
794
|
criteria: null
|
|
795
795
|
};
|
|
796
|
-
u.push({ id: F.id, params:
|
|
796
|
+
u.push({ id: F.id, params: ae }), c.push({ id: F.id, params: { unitId: r, subUnitId: e, col: f, criteria: { ...g.serialize(), colId: f } } });
|
|
797
797
|
}
|
|
798
798
|
}
|
|
799
799
|
}), i !== R || o !== p) {
|
|
@@ -898,7 +898,7 @@ let P = class extends U {
|
|
|
898
898
|
return { redos: [], undos: [] };
|
|
899
899
|
}
|
|
900
900
|
_initRowFilteredInterceptor() {
|
|
901
|
-
this.disposeWithMe(this._sheetInterceptorService.intercept(
|
|
901
|
+
this.disposeWithMe(this._sheetInterceptorService.intercept(Oe.ROW_FILTERED, {
|
|
902
902
|
handler: (s, r) => {
|
|
903
903
|
var e, t;
|
|
904
904
|
return s ? !0 : (t = (e = this._sheetsFilterService.getFilterModel(
|
|
@@ -960,7 +960,7 @@ let P = class extends U {
|
|
|
960
960
|
return;
|
|
961
961
|
const n = Array.from(t.filteredOutRows).sort((l, a) => l - a), i = [];
|
|
962
962
|
let o = !1;
|
|
963
|
-
if (s.id ===
|
|
963
|
+
if (s.id === ye.id) {
|
|
964
964
|
const { startRow: l, endRow: a } = s.params.range, u = n.filter((c) => c >= l && c <= a);
|
|
965
965
|
n.forEach((c) => {
|
|
966
966
|
if (c < l)
|
|
@@ -972,7 +972,7 @@ let P = class extends U {
|
|
|
972
972
|
i.push(c - (a - l + 1 - u.length));
|
|
973
973
|
});
|
|
974
974
|
}
|
|
975
|
-
if (s.id ===
|
|
975
|
+
if (s.id === Ae.id) {
|
|
976
976
|
const { startRow: l, endRow: a } = s.params.range;
|
|
977
977
|
n.forEach((u) => {
|
|
978
978
|
u >= l ? (o = !0, i.push(u + (a - l + 1))) : i.push(u);
|
|
@@ -984,20 +984,18 @@ let P = class extends U {
|
|
|
984
984
|
};
|
|
985
985
|
P = it([
|
|
986
986
|
K(Z.Ready, P),
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
987
|
+
T(0, ee),
|
|
988
|
+
T(1, $(Se)),
|
|
989
|
+
T(2, $(v)),
|
|
990
|
+
T(3, k),
|
|
991
|
+
T(4, $(ve))
|
|
992
992
|
], P);
|
|
993
993
|
var ot = Object.defineProperty, lt = Object.getOwnPropertyDescriptor, at = (s, r, e, t) => {
|
|
994
994
|
for (var n = t > 1 ? void 0 : t ? lt(r, e) : r, i = s.length - 1, o; i >= 0; i--)
|
|
995
995
|
(o = s[i]) && (n = (t ? o(r, e, n) : o(n)) || n);
|
|
996
996
|
return t && n && ot(r, e, n), n;
|
|
997
|
-
}, ct = (s, r) => (e, t) => r(e, t, s);
|
|
998
|
-
|
|
999
|
-
var x;
|
|
1000
|
-
let X = (x = class extends _e {
|
|
997
|
+
}, ct = (s, r) => (e, t) => r(e, t, s), x;
|
|
998
|
+
let X = (x = class extends ge {
|
|
1001
999
|
constructor(s, r) {
|
|
1002
1000
|
super(), this._injector = r;
|
|
1003
1001
|
}
|
|
@@ -1007,27 +1005,27 @@ let X = (x = class extends _e {
|
|
|
1007
1005
|
[P]
|
|
1008
1006
|
].forEach((r) => s.add(r));
|
|
1009
1007
|
}
|
|
1010
|
-
}, _(x, "type",
|
|
1008
|
+
}, _(x, "type", H.UNIVER_SHEET), _(x, "pluginName", oe), x);
|
|
1011
1009
|
X = at([
|
|
1012
|
-
ct(1, $(
|
|
1010
|
+
ct(1, $(Ce))
|
|
1013
1011
|
], X);
|
|
1014
1012
|
export {
|
|
1015
1013
|
E as CustomFilterOperator,
|
|
1016
|
-
|
|
1017
|
-
|
|
1014
|
+
tt as FILTER_MUTATIONS,
|
|
1015
|
+
Ve as FilterColumn,
|
|
1018
1016
|
L as FilterModel,
|
|
1019
1017
|
ie as ReCalcSheetsFilterMutation,
|
|
1020
1018
|
y as RemoveSheetsFilterMutation,
|
|
1021
|
-
|
|
1019
|
+
oe as SHEET_FILTER_SNAPSHOT_ID,
|
|
1022
1020
|
F as SetSheetsFilterCriteriaMutation,
|
|
1023
1021
|
C as SetSheetsFilterRangeMutation,
|
|
1024
1022
|
v as SheetsFilterService,
|
|
1025
1023
|
X as UniverSheetsFilterPlugin,
|
|
1026
|
-
|
|
1024
|
+
ze as equals,
|
|
1027
1025
|
q as getCustomFilterFn,
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1026
|
+
Ie as greaterThan,
|
|
1027
|
+
Le as greaterThanOrEqualTo,
|
|
1028
|
+
Be as lessThan,
|
|
1031
1029
|
Ue as lessThanOrEqualTo,
|
|
1032
1030
|
te as notEquals
|
|
1033
1031
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { IFilterColumn } from '../models/types';
|
|
2
|
-
import { ISheetCommandSharedParams } from '@univerjs/sheets';
|
|
3
1
|
import { IMutation, IRange, Nullable } from '@univerjs/core';
|
|
2
|
+
import { ISheetCommandSharedParams } from '@univerjs/sheets';
|
|
3
|
+
import { IFilterColumn } from '../models/types';
|
|
4
4
|
|
|
5
5
|
export interface ISetSheetsFilterRangeMutationParams extends ISheetCommandSharedParams {
|
|
6
6
|
range: IRange;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { IAutoFilter, IFilterColumn } from './types';
|
|
2
|
-
import { Observable } from 'rxjs';
|
|
3
1
|
import { Disposable, CellValue, IRange, Nullable, Worksheet } from '@univerjs/core';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import { IAutoFilter, IFilterColumn } from './types';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* This is the in-memory model of filter.
|
package/lib/types/plugin.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { IAutoFilter } from '../models/types';
|
|
2
|
-
import { FilterModel } from '../models/filter-model';
|
|
3
1
|
import { Nullable, Disposable, ICommandService, IResourceManagerService, IUniverInstanceService } from '@univerjs/core';
|
|
2
|
+
import { FilterModel } from '../models/filter-model';
|
|
3
|
+
import { IAutoFilter } from '../models/types';
|
|
4
4
|
|
|
5
5
|
export declare const FILTER_MUTATIONS: Set<string>;
|
|
6
6
|
type WorksheetID = string;
|
package/lib/umd/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(h,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("@univerjs/core"),require("@wendellhu/redi"),require("rxjs"),require("@univerjs/sheets")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","rxjs","@univerjs/sheets"],d):(h=typeof globalThis<"u"?globalThis:h||self,d(h.UniverSheetsFilter={},h.UniverCore,h["@wendellhu/redi"],h.rxjs,h.UniverSheets))})(this,function(h,d,O,T,E){"use strict";var ye=Object.defineProperty;var Te=(h,d,O)=>d in h?ye(h,d,{enumerable:!0,configurable:!0,writable:!0,value:O}):h[d]=O;var R=(h,d,O)=>(Te(h,typeof d!="symbol"?d+"":d,O),O);var B;var y=(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))(y||{});const W={operator:y.GREATER_THAN,fn:(i,n)=>P(i)?i>n:!1},D={operator:y.GREATER_THAN_OR_EQUAL,fn:(i,n)=>P(i)?i>=n:!1},V={operator:y.LESS_THAN,fn:(i,n)=>P(i)?i<n:!1},q={operator:y.LESS_THAN_OR_EQUAL,fn:(i,n)=>P(i)?i<=n:!1},Q={operator:y.EQUAL,fn:(i,n)=>P(i)?i===n:!1},b={operator:y.NOT_EQUALS,fn:(i,n)=>{if(typeof n=="string"){if(n===" ")return i!=null;const e=X(i);return e&&ne(n)?!Y(n).test(e):e!==n}return P(i)?i!==n:!0}},G=new Map([]);[W,D,V,q,Q,b].forEach(i=>{G.set(i.operator,i)});function re(i){return!!i}const ie={fn:(i,n)=>{const e=X(i);return e===null?n==="":Y(n).test(e)}};function j(i){return i?G.get(i):ie}function P(i){return typeof i=="number"}function J(i){return!!(typeof i=="number"||typeof i=="string"&&d.isNumeric(i))}function X(i){return typeof i=="boolean"||i==null?null:typeof i=="string"?i:i.toString()}function ne(i){return typeof i=="number"?!1:i.indexOf("*")!==-1||i.indexOf("?")!==-1}function Y(i){const n=i.replace(/[.+^${}()|[\]\\]/g,"\\$&").replaceAll("?",".").replace(/[*]/g,".$&");return new RegExp(`^${n}$`)}const x=()=>new Set;class $ extends d.Disposable{constructor(e,t,r){super();R(this,"_filteredOutRows$",new T.BehaviorSubject(x()));R(this,"filteredOutRows$",this._filteredOutRows$.asObservable());R(this,"_hasCriteria$",new T.BehaviorSubject(!1));R(this,"hasCriteria$",this._hasCriteria$.asObservable());R(this,"_filterColumnByIndex",new Map);R(this,"_alreadyFilteredOutRows",x());R(this,"_range");this.unitId=e,this.subUnitId=t,this._worksheet=r}get filteredOutRows(){return this._filteredOutRows$.getValue()}set filteredOutRows(e){this._alreadyFilteredOutRows=e,this._filteredOutRows$.next(e)}dispose(){super.dispose(),this._filteredOutRows$.complete(),this._hasCriteria$.complete()}serialize(){const e={ref:d.Rectangle.clone(this._range),filterColumns:this._getAllFilterColumns(!0).sort(([t],[r])=>t-r).map(([t,r])=>r.serialize())};return this._alreadyFilteredOutRows&&(e.cachedFilteredOut=Array.from(this._alreadyFilteredOutRows).sort()),e}static deserialize(e,t,r,s){const o=new $(e,t,r);return o._dump(s),o}_dump(e){var t;this.setRange(e.ref),(t=e.filterColumns)==null||t.forEach(r=>this._setCriteriaWithoutReCalc(r.colId,r)),e.cachedFilteredOut&&(this._alreadyFilteredOutRows=new Set(e.cachedFilteredOut),this._emit()),this._emitHasCriteria()}isRowFiltered(e){return this._alreadyFilteredOutRows.has(e)}getRange(){if(!this._range)throw new Error("[FilterModel] could not get range before a range is set!");return this._range}getFilteredOutRowsExceptCol(e){return this._getAllFilterColumns(!0).filter(([t])=>t!==e).reduce((t,[,r])=>{const s=r.calc({getAlreadyFilteredOutRows:()=>t});return s?d.mergeSets(t,s):t},new Set)}setRange(e){this._range=e,this._getAllFilterColumns(!0).forEach(([t,r])=>{r.setRangeAndColumn({startRow:e.startRow,endRow:e.endRow,startColumn:t,endColumn:t},t)})}setCriteria(e,t,r=!1){if(!this._range)throw new Error("[FilterModel] could not set criteria before a range is set!");if(!t){this._removeCriteria(e),this._rebuildAlreadyFilteredOutRowsWithCache(),r&&this._reCalcAllColumns(),this._emit(),this._emitHasCriteria();return}this._setCriteriaWithoutReCalc(e,t),r&&(this._rebuildAlreadyFilteredOutRowsWithCache(),this._reCalcWithNoCacheColumns(),this._emit(),this._emitHasCriteria())}getAllFilterColumns(){return this._getAllFilterColumns(!0)}getFilterColumn(e){var t;return(t=this._filterColumnByIndex.get(e))!=null?t:null}reCalc(){this._reCalcAllColumns(),this._emit()}_getAllFilterColumns(e=!1){const t=Array.from(this._filterColumnByIndex.entries());return e?t:t.map(([r,s])=>s)}_reCalcAllColumns(){this._alreadyFilteredOutRows=x(),this._getAllFilterColumns().forEach(e=>e.__clearCache()),this._reCalcWithNoCacheColumns()}_setCriteriaWithoutReCalc(e,t){const r=this._range;if(!r)throw new Error("[FilterModel] could not set criteria before a range is set!");const{startColumn:s,endColumn:o}=r;if(e>o||e<s)throw new Error(`[FilterModel] could not set criteria on column ${e} which is out of range!`);let l;this._filterColumnByIndex.has(e)?l=this._filterColumnByIndex.get(e):(l=new K(this.unitId,this.subUnitId,this._worksheet,t,{getAlreadyFilteredOutRows:()=>this._alreadyFilteredOutRows}),l.setRangeAndColumn(r,e),this._filterColumnByIndex.set(e,l)),l.setCriteria(t)}_removeCriteria(e){const t=this._filterColumnByIndex.get(e);t&&(t.dispose(),this._filterColumnByIndex.delete(e))}_emit(){this._filteredOutRows$.next(this._alreadyFilteredOutRows)}_emitHasCriteria(){this._hasCriteria$.next(this._filterColumnByIndex.size>0)}_rebuildAlreadyFilteredOutRowsWithCache(){const e=this._getAllFilterColumns().filter(t=>t.hasCache()).reduce((t,r)=>d.mergeSets(t,r.filteredOutRows),new Set);this._alreadyFilteredOutRows=e}_reCalcWithNoCacheColumns(){const e=this._getAllFilterColumns().filter(t=>!t.hasCache());for(const t of e){const r=t.reCalc();r&&(this._alreadyFilteredOutRows=d.mergeSets(this._alreadyFilteredOutRows,r))}}}class K extends d.Disposable{constructor(e,t,r,s,o){super();R(this,"_filteredOutRows",null);R(this,"_filterFn",null);R(this,"_range",null);R(this,"_column",0);R(this,"_filterByValues",!1);this.unitId=e,this.subUnitId=t,this._worksheet=r,this._criteria=s,this._filterColumnContext=o}get filteredOutRows(){return this._filteredOutRows}dispose(){super.dispose(),this._filteredOutRows=null}__clearCache(){this._filteredOutRows=null}serialize(){if(!this._criteria)throw new Error("[FilterColumn]: could not serialize without a filter column!");return d.Tools.deepClone({...this._criteria,colId:this._column})}hasCache(){return this._filteredOutRows!==null}setRangeAndColumn(e,t){this._range=e,this._column=t}setCriteria(e){this._criteria=e,this._generateFilterFn(),this._filteredOutRows=null}getColumnData(){return d.Tools.deepClone(this._criteria)}reCalc(){return this._filteredOutRows=this.calc(this._filterColumnContext),this._filteredOutRows}calc(e){if(!this._filterFn)throw new Error("[FilterColumn] cannot calculate without a filter fn!");if(!this._range)throw new Error("[FilterColumn] cannot calculate without a range!");if(typeof this._column!="number")throw new TypeError("[FilterColumn] cannot calculate without a column offset!");const t=this._column,r={startColumn:t,endColumn:t,startRow:this._range.startRow+1,endRow:this._range.endRow},s=new Set,o=e.getAlreadyFilteredOutRows();for(const l of this._worksheet.iterateByColumn(r,!1,!1)){const{row:a,rowSpan:c,col:u}=l;if(o.has(a)&&(!c||c===1))continue;const m=this._filterByValues?d.extractPureTextFromCell(this._worksheet.getCell(a,u)):he(this._worksheet,a,u);if(!this._filterFn(m)&&(s.add(a),c))for(let C=1;C<c;C++)s.add(a+C)}return s}_generateFilterFn(){this._criteria&&(this._filterFn=se(this._criteria),this._filterByValues=!!this._criteria.filters)}}function se(i){if(i.filters)return oe(i.filters);if(i.customFilters)return le(i.customFilters);throw new Error("[FilterModel]: other types of filters are not supported yet.")}function oe(i){const n=!!i.blank,e=new Set(i.filters);return t=>t===void 0||t===""?n:e.has(typeof t=="string"?t:`${t}`)}function le(i){const n=i.customFilters.map(e=>de(e));return ce(n)?i.and?ae(n):ue(n):n[0]}function ae(i){const[n,e]=i;return t=>n(t)&&e(t)}function ue(i){const[n,e]=i;return t=>n(t)||e(t)}function ce(i){return i.length===2}function de(i){const n=i.val;if(i.operator===y.NOT_EQUALS&&!J(n))return r=>b.fn(r,n);if(re(i.operator)){if(!J(n))return()=>!1;const r=j(i.operator),s=Number(n);return o=>r.fn(o,s)}const e=j(i.operator);return t=>e.fn(t,n)}function he(i,n,e){const t=i.getCell(n,e);if(!t)return null;const r=i.getCellRaw(n,e);return t&&!r?Z(t):r?t.t===d.CellValueType.NUMBER&&typeof t.v=="string"?r.v:Z(r):null}function Z(i){var t,r;const n=(r=(t=i.p)==null?void 0:t.body)==null?void 0:r.dataStream;if(n)return n.trimEnd();const e=i.v;return typeof e=="string"?i.t===d.CellValueType.BOOLEAN?e.toUpperCase():e:typeof e=="number"?i.t===d.CellValueType.BOOLEAN?e?"TRUE":"FALSE":e:typeof e=="boolean"?e?"TRUE":"FALSE":""}const g={id:"sheet.mutation.set-filter-range",type:d.CommandType.MUTATION,handler:(i,n)=>{const{subUnitId:e,unitId:t,range:r}=n;return i.get(h.SheetsFilterService).ensureFilterModel(t,e).setRange(r),!0}},p={id:"sheet.mutation.set-filter-criteria",type:d.CommandType.MUTATION,handler:(i,n)=>{const{subUnitId:e,unitId:t,criteria:r,col:s,reCalc:o=!0}=n,a=i.get(h.SheetsFilterService).getFilterModel(t,e);return a?(a.setCriteria(s,r,o),!0):!1}},A={id:"sheet.mutation.remove-filter",type:d.CommandType.MUTATION,handler:(i,n)=>{const{unitId:e,subUnitId:t}=n;return i.get(h.SheetsFilterService).removeFilterModel(e,t)}},z={id:"sheet.mutation.re-calc-filter",type:d.CommandType.MUTATION,handler:(i,n)=>{const{unitId:e,subUnitId:t}=n,s=i.get(h.SheetsFilterService).getFilterModel(e,t);return s?(s.reCalc(),!0):!1}};var fe=Object.defineProperty,me=Object.getOwnPropertyDescriptor,_e=(i,n,e,t)=>{for(var r=t>1?void 0:t?me(n,e):n,s=i.length-1,o;s>=0;s--)(o=i[s])&&(r=(t?o(n,e,r):o(r))||r);return t&&r&&fe(n,e,r),r},H=(i,n)=>(e,t)=>n(e,t,i);const k=new Set([g.id,p.id,A.id,z.id]),ee="SHEET_FILTER_PLUGIN";h.SheetsFilterService=class extends d.Disposable{constructor(e,t,r){super();R(this,"_filterModels",new Map);R(this,"_loadedUnitId$",new T.BehaviorSubject(null));R(this,"loadedUnitId$",this._loadedUnitId$.asObservable());R(this,"_activeFilterModel$",new T.BehaviorSubject(null));R(this,"activeFilterModel$",this._activeFilterModel$.asObservable());this._resourcesManagerService=e,this._univerInstanceService=t,this._commandService=r,this._initModel(),this._initActiveFilterModel()}get activeFilterModel(){return this._activeFilterModel$.getValue()}ensureFilterModel(e,t){const r=this.getFilterModel(e,t);if(r)return r;const s=this._univerInstanceService.getUniverSheetInstance(e);if(!s)throw new Error(`[SheetsFilterService]: could not create "FilterModel" on a non-existing workbook ${e}!`);const o=s.getSheetBySheetId(t);if(!o)throw new Error(`[SheetsFilterService]: could not create "FilterModel" on a non-existing worksheet ${t}!`);const l=new $(e,t,o);return this._cacheFilterModel(e,t,l),l}getFilterModel(e,t){var r,s;return(s=(r=this._filterModels.get(e))==null?void 0:r.get(t))!=null?s:null}removeFilterModel(e,t){const r=this.getFilterModel(e,t);return r?(r.dispose(),this._filterModels.get(e).delete(t),!0):!1}_updateActiveFilterModel(){let e;try{if(e=this._univerInstanceService.getCurrentUnitForType(d.UniverInstanceType.UNIVER_SHEET),!e){this._activeFilterModel$.next(null);return}}catch{return}const t=e.getActiveSheet();if(!t){this._activeFilterModel$.next(null);return}const r=t.getUnitId(),s=t.getSheetId(),o=this.getFilterModel(r,s);this._activeFilterModel$.next(o)}_initActiveFilterModel(){this.disposeWithMe(T.merge(d.fromCallback(this._commandService.onCommandExecuted).pipe(T.filter(([e])=>e.type===d.CommandType.MUTATION&&k.has(e.id))),this._univerInstanceService.getCurrentTypeOfUnit$(d.UniverInstanceType.UNIVER_SHEET).pipe(T.switchMap(e=>{var t;return(t=e==null?void 0:e.activeSheet$)!=null?t:T.of(null)}))).subscribe(()=>this._updateActiveFilterModel()))}_serializeAutoFiltersForUnit(e){const t=this._filterModels.get(e);if(!t)return"{}";const r={};return t.forEach((s,o)=>{r[o]=s.serialize()}),JSON.stringify(r)}_deserializeAutoFiltersForUnit(e,t){const r=this._univerInstanceService.getUniverSheetInstance(e);Object.keys(t).forEach(s=>{const o=t[s],l=$.deserialize(e,s,r.getSheetBySheetId(s),o);this._cacheFilterModel(e,s,l)})}_initModel(){this._resourcesManagerService.registerPluginResource({pluginName:ee,businesses:[2],toJson:e=>this._serializeAutoFiltersForUnit(e),parseJson:e=>JSON.parse(e),onLoad:(e,t)=>{this._deserializeAutoFiltersForUnit(e,t),this._loadedUnitId$.next(e),this._updateActiveFilterModel()},onUnLoad:e=>{const t=this._filterModels.get(e);t&&(t.forEach(r=>r.dispose()),this._filterModels.delete(e))}})}_cacheFilterModel(e,t,r){this._filterModels.has(e)||this._filterModels.set(e,new Map),this._filterModels.get(e).set(t,r)}},h.SheetsFilterService=_e([d.OnLifecycle(d.LifecycleStages.Ready,h.SheetsFilterService),H(0,d.IResourceManagerService),H(1,d.IUniverInstanceService),H(2,d.ICommandService)],h.SheetsFilterService);function Ce(i,n){for(let e=0;e<i.length;e++){let t=e;if(i[e])for(let r=e+1;r<i.length;r++)i[t]&&i[r]&&n(i[t],i[r])&&(i[t]=null,t=r)}return i.filter(e=>e!==null)}function N(i){return Ce(i,(n,e)=>n.id===p.id&&e.id===p.id&&n.params.unitId===e.params.unitId&&n.params.subUnitId===e.params.subUnitId&&n.params.col===e.params.col)}var ge=Object.defineProperty,Re=Object.getOwnPropertyDescriptor,Fe=(i,n,e,t)=>{for(var r=t>1?void 0:t?Re(n,e):n,s=i.length-1,o;s>=0;s--)(o=i[s])&&(r=(t?o(n,e,r):o(r))||r);return t&&r&&ge(n,e,r),r},L=(i,n)=>(e,t)=>n(e,t,i);let U=class extends d.Disposable{constructor(i,n,e,t,r){super(),this._commandService=i,this._sheetInterceptorService=n,this._sheetsFilterService=e,this._univerInstanceService=t,this._refRangeService=r,this._initCommands(),this._initRowFilteredInterceptor(),this._initInterceptors(),this._commandExecutedListener()}_initCommands(){[p,g,z,A].forEach(i=>this.disposeWithMe(this._commandService.registerCommand(i)))}_initInterceptors(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:e=>this._getUpdateFilter(e)}));const i=new d.DisposableCollection,n=(e,t)=>{var a;const r=this._univerInstanceService.getUniverSheetInstance(e);if(!r||!(r==null?void 0:r.getSheetBySheetId(t)))return;i.dispose();const o=(a=this._sheetsFilterService.getFilterModel(e,t))==null?void 0:a.getRange(),l=c=>{switch(c.id){case E.InsertRowCommand.id:{const u=c.params,m=u.unitId||e,C=u.subUnitId||t;return this._handleInsertRowCommand(u,m,C)}case E.InsertColCommand.id:{const u=c.params,m=u.unitId||e,C=u.subUnitId||t;return this._handleInsertColCommand(u,m,C)}case E.RemoveColCommand.id:{const u=c.params;return this._handleRemoveColCommand(u,e,t)}case E.RemoveRowCommand.id:{const u=c.params;return this._handleRemoveRowCommand(u,e,t)}case E.EffectRefRangId.MoveColsCommandId:{const u=c.params;return this._handleMoveColsCommand(u,e,t)}case E.EffectRefRangId.MoveRowsCommandId:{const u=c.params;return this._handleMoveRowsCommand(u,e,t)}case E.MoveRangeCommand.id:{const u=c.params;return this._handleMoveRangeCommand(u,e,t)}}return{redos:[],undos:[]}};o&&i.add(this._refRangeService.registerRefRange(o,l,e,t))};this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.id===E.SetWorksheetActivateCommand.id){const t=e.params,r=t.subUnitId,s=t.unitId;if(!r||!s)return;n(s,r)}if(e.id===g.id){const t=e.params,r=t.subUnitId,s=t.unitId;if(!r||!s)return;n(t.unitId,t.subUnitId)}})),this.disposeWithMe(this._sheetsFilterService.loadedUnitId$.subscribe(e=>{if(e){const t=this._univerInstanceService.getUniverSheetInstance(e),r=t==null?void 0:t.getActiveSheet();r&&n(e,r.getSheetId())}}))}_getUpdateFilter(i){const{id:n}=i;switch(n){case E.RemoveSheetCommand.id:{const e=i.params;return this._handleRemoveSheetCommand(e,e.unitId,e.subUnitId)}}return{redos:[],undos:[]}}_handleInsertColCommand(i,n,e){var _;const t=this._sheetsFilterService.getFilterModel(n,e),r=(_=t==null?void 0:t.getRange())!=null?_:null;if(!t||!r)return this._handleNull();const{startColumn:s,endColumn:o}=r,{startColumn:l,endColumn:a}=i.range,c=a-l+1;if(a>o)return this._handleNull();const u=[],m=[],C=l,w={unitId:n,subUnitId:e,range:{...r,startColumn:l<=s?s+c:s,endColumn:o+c}},S={unitId:n,subUnitId:e,range:r};u.push({id:g.id,params:w}),m.push({id:g.id,params:S});const f=t.getAllFilterColumns().filter(F=>F[0]>=C);if(f.length!==0){const{undos:F,redos:M}=this.moveCriteria(n,e,f,c);u.push(...M),m.push(...F)}return{redos:N(u),undos:N(m)}}_handleInsertRowCommand(i,n,e){var S;const t=this._sheetsFilterService.getFilterModel(n,e),r=(S=t==null?void 0:t.getRange())!=null?S:null;if(!t||!r)return this._handleNull();const{startRow:s,endRow:o}=r,{startRow:l,endRow:a}=i.range,c=a-l+1;if(a>o)return this._handleNull();const u=[],m=[],C={unitId:n,subUnitId:e,range:{...r,startRow:l<=s?s+c:s,endRow:o+c}},w={unitId:n,subUnitId:e,range:r};return u.push({id:g.id,params:C}),m.push({id:g.id,params:w}),{redos:N(u),undos:N(m)}}_handleRemoveColCommand(i,n,e){var v;const t=this._sheetsFilterService.getFilterModel(n,e),r=(v=t==null?void 0:t.getRange())!=null?v:null;if(!t||!r)return this._handleNull();const{startColumn:s,endColumn:o}=r,{startColumn:l,endColumn:a}=i.range;if(l>o)return this._handleNull();const c=[],u=[],m=a<s?0:Math.min(a,o)-Math.max(l,s)+1,C=a-l+1,w=t.getAllFilterColumns();w.forEach(f=>{const[_,F]=f;_<=a&&_>=l&&(c.push({id:p.id,params:{unitId:n,subUnitId:e,col:_,criteria:null}}),u.push({id:p.id,params:{unitId:n,subUnitId:e,col:_,criteria:{...F.serialize(),colId:_}}}))});const S=w.filter(f=>{const[_,F]=f;return _>a});if(S.length>0){const{undos:f,redos:_}=this.moveCriteria(n,e,S,-C);c.push(..._),u.push(...f)}if(m===o-s+1){const f={unitId:n,subUnitId:e};c.push({id:A.id,params:f})}else if(s<=l){const f=o-m,_={unitId:n,subUnitId:e,range:{...r,endColumn:f}};c.push({id:g.id,params:_})}else{const f={unitId:n,subUnitId:e,range:{...r,startColumn:l,endColumn:o-(a-l+1)}};c.push({id:g.id,params:f})}return u.push({id:g.id,params:{range:r,unitId:n,subUnitId:e}}),{undos:N(u),redos:N(c)}}_handleRemoveRowCommand(i,n,e){var S;const t=this._sheetsFilterService.getFilterModel(n,e);if(!t)return this._handleNull();const r=t.getRange(),{startRow:s,endRow:o}=r,{startRow:l,endRow:a}=i.range;if(l>o)return this._handleNull();const c=[],u=[],m=t.getAllFilterColumns(),C=s<=a&&s>=l;u.push({id:g.id,params:{range:r,unitId:n,subUnitId:e}});const w=Math.min(a,o)-Math.max(l,s)+1;if(w===o-s+1||C){const v={unitId:n,subUnitId:e};c.push({id:A.id,params:v}),m.forEach(f=>{const[_,F]=f,M={unitId:n,subUnitId:e,col:_,criteria:{...F.serialize(),colId:_}};u.push({id:p.id,params:M})})}else{const v=(S=this._univerInstanceService.getUniverSheetInstance(n))==null?void 0:S.getSheetBySheetId(e);if(!v)return this._handleNull();const f=[];for(let I=l;I<=a;I++)v.getRowFiltered(I)&&f.push(I);const _=Math.min(s,l),F=_+(o-s)-w+f.length,M={unitId:n,subUnitId:e,range:{...r,startRow:_,endRow:F}};c.push({id:g.id,params:M})}return{undos:N(u),redos:N(c)}}_handleMoveColsCommand(i,n,e){var v;const t=this._sheetsFilterService.getFilterModel(n,e),r=(v=t==null?void 0:t.getRange())!=null?v:null;if(!t||!r)return this._handleNull();const{startColumn:s,endColumn:o}=r,{fromRange:l,toRange:a}=i;if(l.endColumn<s&&a.startColumn<=s||l.startColumn>o&&a.endColumn>o)return this._handleNull();const c=[],u=[],m={};for(let f=s;f<=o;f++)m[f]={colIndex:f,filter:t.getFilterColumn(f)};d.moveMatrixArray(l.startColumn,l.endColumn-l.startColumn+1,a.startColumn,m);const C=Object.keys(m).map(f=>Number(f)),w=Math.max(...C),S=Math.min(...C);if(C.forEach(f=>{var I,te;const{colIndex:_,filter:F}=m[f],M=f;if(F){const Ee={unitId:n,subUnitId:e,col:M,criteria:{...F.serialize(),colId:M}};if(c.push({id:p.id,params:Ee}),u.push({id:A.id,params:{unitId:n,subUnitId:e,col:M,criteria:{...(I=t.getFilterColumn(M))==null?void 0:I.serialize(),colId:M}}}),!((te=m[_])!=null&&te.filter)){const Oe={unitId:n,subUnitId:e,col:_,criteria:null};c.push({id:p.id,params:Oe}),u.push({id:p.id,params:{unitId:n,subUnitId:e,col:_,criteria:{...F.serialize(),colId:_}}})}}}),s!==S||o!==w){const f={unitId:n,subUnitId:e,range:{...r,startColumn:S,endColumn:w}};c.unshift({id:g.id,params:f}),u.push({id:g.id,params:{range:r,unitId:n,subUnitId:e}})}return{undos:u,redos:c}}_handleMoveRowsCommand(i,n,e){var v;const t=this._sheetsFilterService.getFilterModel(n,e),r=(v=t==null?void 0:t.getRange())!=null?v:null;if(!t||!r)return this._handleNull();const{startRow:s,endRow:o}=r,{fromRange:l,toRange:a}=i;if(l.endRow<s&&a.startRow<=s||l.startRow>o&&a.endRow>o)return this._handleNull();const c=[],u=[],m={};for(let f=s;f<=o;f++)m[f]={offset:f-s};d.moveMatrixArray(l.startRow,l.endRow-l.startRow+1,a.startRow,m);const C=Object.keys(m).map(f=>Number(f)),w=Math.max(...C),S=Math.min(...C);if(s!==S||o!==w){const f={unitId:n,subUnitId:e,range:{...r,startRow:S,endRow:w}};c.unshift({id:g.id,params:f}),u.push({id:g.id,params:{range:r,unitId:n,subUnitId:e}})}return{redos:c,undos:u}}_handleMoveRangeCommand(i,n,e){const{fromRange:t,toRange:r}=i,s=this._sheetsFilterService.getFilterModel(n,e);if(!s)return this._handleNull();const o=s.getRange();if(!o)return this._handleNull();const l=[],a=[];if(d.Rectangle.contains(t,o)){const c=o.startRow-t.startRow,u=o.startColumn-t.startColumn,m={startRow:r.startRow+c,startColumn:r.startColumn+u,endRow:r.startRow+c+(o.endRow-o.startRow),endColumn:r.startColumn+u+(o.endColumn-o.startColumn)},C={id:A.id,params:{unitId:n,subUnitId:e}},w={id:g.id,params:{unitId:n,subUnitId:e,range:m}},S={id:g.id,params:{unitId:n,subUnitId:e,range:o}};l.push(C,w),a.push(C,S);const v=s.getAllFilterColumns(),f=r.startColumn-t.startColumn;v.forEach(_=>{const[F,M]=_;M&&(l.push({id:p.id,params:{unitId:n,subUnitId:e,col:F+f,criteria:{...M.serialize(),colId:F+f}}}),a.push({id:p.id,params:{unitId:n,subUnitId:e,col:F,criteria:{...M.serialize(),colId:F}}}))})}return{redos:l,undos:a}}_handleRemoveSheetCommand(i,n,e){const t=this._sheetsFilterService.getFilterModel(n,e);if(!t)return this._handleNull();const r=t.getRange();if(!r)return this._handleNull();const s=[],o=[];return t.getAllFilterColumns().forEach(a=>{const[c,u]=a;o.push({id:p.id,params:{unitId:n,subUnitId:e,col:a,criteria:{...u.serialize(),colId:a}}})}),s.push({id:A.id,params:{unitId:n,subUnitId:e,range:r}}),o.unshift({id:g.id,params:{range:r,unitId:n,subUnitId:e}}),{undos:o,redos:s}}_handleNull(){return{redos:[],undos:[]}}_initRowFilteredInterceptor(){this.disposeWithMe(this._sheetInterceptorService.intercept(E.INTERCEPTOR_POINT.ROW_FILTERED,{handler:(i,n)=>{var e,t;return i?!0:(t=(e=this._sheetsFilterService.getFilterModel(n.unitId,n.subUnitId))==null?void 0:e.isRowFiltered(n.row))!=null?t:!1}}))}moveCriteria(i,n,e,t){const r={unitId:i,subUnitId:n,criteria:null,col:-1},s=[],o=[];return e.forEach(l=>{const[a,c]=l;o.push({id:p.id,params:{...r,col:a}}),s.push({id:p.id,params:{...r,col:a,criteria:{...c.serialize(),colId:a}}})}),e.forEach(l=>{const[a,c]=l;o.push({id:p.id,params:{...r,col:a+t,criteria:{...c.serialize(),colId:a+t}}}),s.push({id:p.id,params:{...r,col:a+t,criteria:null}})}),{redos:o,undos:s}}_commandExecutedListener(){this.disposeWithMe(this._commandService.onCommandExecuted(i=>{const{unitId:n,subUnitId:e}=i.params||{},t=this._sheetsFilterService.getFilterModel(n,e);if(!t)return;const r=Array.from(t.filteredOutRows).sort((l,a)=>l-a),s=[];let o=!1;if(i.id===E.RemoveRowMutation.id){const{startRow:l,endRow:a}=i.params.range,c=r.filter(u=>u>=l&&u<=a);r.forEach(u=>{if(u<l)s.push(u);else if(o=!0,u<=a){const m=Math.max(l,s.length?s[s.length-1]+1:l);s.push(m)}else s.push(u-(a-l+1-c.length))})}if(i.id===E.InsertRowMutation.id){const{startRow:l,endRow:a}=i.params.range;r.forEach(c=>{c>=l?(o=!0,s.push(c+(a-l+1))):s.push(c)})}o&&(t.filteredOutRows=new Set(s))}))}};U=Fe([d.OnLifecycle(d.LifecycleStages.Ready,U),L(0,d.ICommandService),L(1,O.Inject(E.SheetInterceptorService)),L(2,O.Inject(h.SheetsFilterService)),L(3,d.IUniverInstanceService),L(4,O.Inject(E.RefRangeService))],U);var pe=Object.defineProperty,Se=Object.getOwnPropertyDescriptor,we=(i,n,e,t)=>{for(var r=t>1?void 0:t?Se(n,e):n,s=i.length-1,o;s>=0;s--)(o=i[s])&&(r=(t?o(n,e,r):o(r))||r);return t&&r&&pe(n,e,r),r},ve=(i,n)=>(e,t)=>n(e,t,i);const Me="UNIVER_SHEETS_FILTER_PLUGIN";h.UniverSheetsFilterPlugin=(B=class extends d.Plugin{constructor(n,e){super(),this._injector=e}onStarting(n){[[h.SheetsFilterService],[U]].forEach(e=>n.add(e))}},R(B,"type",d.UniverInstanceType.UNIVER_SHEET),R(B,"pluginName",Me),B),h.UniverSheetsFilterPlugin=we([ve(1,O.Inject(O.Injector))],h.UniverSheetsFilterPlugin),h.CustomFilterOperator=y,h.FILTER_MUTATIONS=k,h.FilterColumn=K,h.FilterModel=$,h.ReCalcSheetsFilterMutation=z,h.RemoveSheetsFilterMutation=A,h.SHEET_FILTER_SNAPSHOT_ID=ee,h.SetSheetsFilterCriteriaMutation=p,h.SetSheetsFilterRangeMutation=g,h.equals=Q,h.getCustomFilterFn=j,h.greaterThan=W,h.greaterThanOrEqualTo=D,h.lessThan=V,h.lessThanOrEqualTo=q,h.notEquals=b,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(h,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("@univerjs/core"),require("@wendellhu/redi"),require("rxjs"),require("@univerjs/sheets")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","rxjs","@univerjs/sheets"],d):(h=typeof globalThis<"u"?globalThis:h||self,d(h.UniverSheetsFilter={},h.UniverCore,h["@wendellhu/redi"],h.rxjs,h.UniverSheets))})(this,function(h,d,O,T,E){"use strict";var Oe=Object.defineProperty;var ye=(h,d,O)=>d in h?Oe(h,d,{enumerable:!0,configurable:!0,writable:!0,value:O}):h[d]=O;var R=(h,d,O)=>(ye(h,typeof d!="symbol"?d+"":d,O),O);var B;var y=(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))(y||{});const D={operator:y.GREATER_THAN,fn:(i,n)=>P(i)?i>n:!1},q={operator:y.GREATER_THAN_OR_EQUAL,fn:(i,n)=>P(i)?i>=n:!1},V={operator:y.LESS_THAN,fn:(i,n)=>P(i)?i<n:!1},Q={operator:y.LESS_THAN_OR_EQUAL,fn:(i,n)=>P(i)?i<=n:!1},G={operator:y.EQUAL,fn:(i,n)=>P(i)?i===n:!1},b={operator:y.NOT_EQUALS,fn:(i,n)=>{if(typeof n=="string"){if(n===" ")return i!=null;const e=Y(i);return e&&ne(n)?!K(n).test(e):e!==n}return P(i)?i!==n:!0}},J=new Map([]);[D,q,V,Q,G,b].forEach(i=>{J.set(i.operator,i)});function re(i){return!!i}const ie={fn:(i,n)=>{const e=Y(i);return e===null?n==="":K(n).test(e)}};function j(i){return i?J.get(i):ie}function P(i){return typeof i=="number"}function X(i){return!!(typeof i=="number"||typeof i=="string"&&d.isNumeric(i))}function Y(i){return typeof i=="boolean"||i==null?null:typeof i=="string"?i:i.toString()}function ne(i){return typeof i=="number"?!1:i.indexOf("*")!==-1||i.indexOf("?")!==-1}function K(i){const n=i.replace(/[.+^${}()|[\]\\]/g,"\\$&").replaceAll("?",".").replace(/[*]/g,".$&");return new RegExp(`^${n}$`)}const x=()=>new Set;class $ extends d.Disposable{constructor(e,t,r){super();R(this,"_filteredOutRows$",new T.BehaviorSubject(x()));R(this,"filteredOutRows$",this._filteredOutRows$.asObservable());R(this,"_hasCriteria$",new T.BehaviorSubject(!1));R(this,"hasCriteria$",this._hasCriteria$.asObservable());R(this,"_filterColumnByIndex",new Map);R(this,"_alreadyFilteredOutRows",x());R(this,"_range");this.unitId=e,this.subUnitId=t,this._worksheet=r}get filteredOutRows(){return this._filteredOutRows$.getValue()}set filteredOutRows(e){this._alreadyFilteredOutRows=e,this._filteredOutRows$.next(e)}dispose(){super.dispose(),this._filteredOutRows$.complete(),this._hasCriteria$.complete()}serialize(){const e={ref:d.Rectangle.clone(this._range),filterColumns:this._getAllFilterColumns(!0).sort(([t],[r])=>t-r).map(([t,r])=>r.serialize())};return this._alreadyFilteredOutRows&&(e.cachedFilteredOut=Array.from(this._alreadyFilteredOutRows).sort()),e}static deserialize(e,t,r,s){const o=new $(e,t,r);return o._dump(s),o}_dump(e){var t;this.setRange(e.ref),(t=e.filterColumns)==null||t.forEach(r=>this._setCriteriaWithoutReCalc(r.colId,r)),e.cachedFilteredOut&&(this._alreadyFilteredOutRows=new Set(e.cachedFilteredOut),this._emit()),this._emitHasCriteria()}isRowFiltered(e){return this._alreadyFilteredOutRows.has(e)}getRange(){if(!this._range)throw new Error("[FilterModel] could not get range before a range is set!");return this._range}getFilteredOutRowsExceptCol(e){return this._getAllFilterColumns(!0).filter(([t])=>t!==e).reduce((t,[,r])=>{const s=r.calc({getAlreadyFilteredOutRows:()=>t});return s?d.mergeSets(t,s):t},new Set)}setRange(e){this._range=e,this._getAllFilterColumns(!0).forEach(([t,r])=>{r.setRangeAndColumn({startRow:e.startRow,endRow:e.endRow,startColumn:t,endColumn:t},t)})}setCriteria(e,t,r=!1){if(!this._range)throw new Error("[FilterModel] could not set criteria before a range is set!");if(!t){this._removeCriteria(e),this._rebuildAlreadyFilteredOutRowsWithCache(),r&&this._reCalcAllColumns(),this._emit(),this._emitHasCriteria();return}this._setCriteriaWithoutReCalc(e,t),r&&(this._rebuildAlreadyFilteredOutRowsWithCache(),this._reCalcWithNoCacheColumns(),this._emit(),this._emitHasCriteria())}getAllFilterColumns(){return this._getAllFilterColumns(!0)}getFilterColumn(e){var t;return(t=this._filterColumnByIndex.get(e))!=null?t:null}reCalc(){this._reCalcAllColumns(),this._emit()}_getAllFilterColumns(e=!1){const t=Array.from(this._filterColumnByIndex.entries());return e?t:t.map(([r,s])=>s)}_reCalcAllColumns(){this._alreadyFilteredOutRows=x(),this._getAllFilterColumns().forEach(e=>e.__clearCache()),this._reCalcWithNoCacheColumns()}_setCriteriaWithoutReCalc(e,t){const r=this._range;if(!r)throw new Error("[FilterModel] could not set criteria before a range is set!");const{startColumn:s,endColumn:o}=r;if(e>o||e<s)throw new Error(`[FilterModel] could not set criteria on column ${e} which is out of range!`);let l;this._filterColumnByIndex.has(e)?l=this._filterColumnByIndex.get(e):(l=new Z(this.unitId,this.subUnitId,this._worksheet,t,{getAlreadyFilteredOutRows:()=>this._alreadyFilteredOutRows}),l.setRangeAndColumn(r,e),this._filterColumnByIndex.set(e,l)),l.setCriteria(t)}_removeCriteria(e){const t=this._filterColumnByIndex.get(e);t&&(t.dispose(),this._filterColumnByIndex.delete(e))}_emit(){this._filteredOutRows$.next(this._alreadyFilteredOutRows)}_emitHasCriteria(){this._hasCriteria$.next(this._filterColumnByIndex.size>0)}_rebuildAlreadyFilteredOutRowsWithCache(){const e=this._getAllFilterColumns().filter(t=>t.hasCache()).reduce((t,r)=>d.mergeSets(t,r.filteredOutRows),new Set);this._alreadyFilteredOutRows=e}_reCalcWithNoCacheColumns(){const e=this._getAllFilterColumns().filter(t=>!t.hasCache());for(const t of e){const r=t.reCalc();r&&(this._alreadyFilteredOutRows=d.mergeSets(this._alreadyFilteredOutRows,r))}}}class Z extends d.Disposable{constructor(e,t,r,s,o){super();R(this,"_filteredOutRows",null);R(this,"_filterFn",null);R(this,"_range",null);R(this,"_column",0);R(this,"_filterByValues",!1);this.unitId=e,this.subUnitId=t,this._worksheet=r,this._criteria=s,this._filterColumnContext=o}get filteredOutRows(){return this._filteredOutRows}dispose(){super.dispose(),this._filteredOutRows=null}__clearCache(){this._filteredOutRows=null}serialize(){if(!this._criteria)throw new Error("[FilterColumn]: could not serialize without a filter column!");return d.Tools.deepClone({...this._criteria,colId:this._column})}hasCache(){return this._filteredOutRows!==null}setRangeAndColumn(e,t){this._range=e,this._column=t}setCriteria(e){this._criteria=e,this._generateFilterFn(),this._filteredOutRows=null}getColumnData(){return d.Tools.deepClone(this._criteria)}reCalc(){return this._filteredOutRows=this.calc(this._filterColumnContext),this._filteredOutRows}calc(e){if(!this._filterFn)throw new Error("[FilterColumn] cannot calculate without a filter fn!");if(!this._range)throw new Error("[FilterColumn] cannot calculate without a range!");if(typeof this._column!="number")throw new TypeError("[FilterColumn] cannot calculate without a column offset!");const t=this._column,r={startColumn:t,endColumn:t,startRow:this._range.startRow+1,endRow:this._range.endRow},s=new Set,o=e.getAlreadyFilteredOutRows();for(const l of this._worksheet.iterateByColumn(r,!1,!1)){const{row:a,rowSpan:c,col:u}=l;if(o.has(a)&&(!c||c===1))continue;const m=this._filterByValues?d.extractPureTextFromCell(this._worksheet.getCell(a,u)):he(this._worksheet,a,u);if(!this._filterFn(m)&&(s.add(a),c))for(let C=1;C<c;C++)s.add(a+C)}return s}_generateFilterFn(){this._criteria&&(this._filterFn=se(this._criteria),this._filterByValues=!!this._criteria.filters)}}function se(i){if(i.filters)return oe(i.filters);if(i.customFilters)return le(i.customFilters);throw new Error("[FilterModel]: other types of filters are not supported yet.")}function oe(i){const n=!!i.blank,e=new Set(i.filters);return t=>t===void 0||t===""?n:e.has(typeof t=="string"?t:`${t}`)}function le(i){const n=i.customFilters.map(e=>de(e));return ce(n)?i.and?ae(n):ue(n):n[0]}function ae(i){const[n,e]=i;return t=>n(t)&&e(t)}function ue(i){const[n,e]=i;return t=>n(t)||e(t)}function ce(i){return i.length===2}function de(i){const n=i.val;if(i.operator===y.NOT_EQUALS&&!X(n))return r=>b.fn(r,n);if(re(i.operator)){if(!X(n))return()=>!1;const r=j(i.operator),s=Number(n);return o=>r.fn(o,s)}const e=j(i.operator);return t=>e.fn(t,n)}function he(i,n,e){const t=i.getCell(n,e);if(!t)return null;const r=i.getCellRaw(n,e);return t&&!r?k(t):r?t.t===d.CellValueType.NUMBER&&typeof t.v=="string"?r.v:k(r):null}function k(i){var t,r;const n=(r=(t=i.p)==null?void 0:t.body)==null?void 0:r.dataStream;if(n)return n.trimEnd();const e=i.v;return typeof e=="string"?i.t===d.CellValueType.BOOLEAN?e.toUpperCase():e:typeof e=="number"?i.t===d.CellValueType.BOOLEAN?e?"TRUE":"FALSE":e:typeof e=="boolean"?e?"TRUE":"FALSE":""}const g={id:"sheet.mutation.set-filter-range",type:d.CommandType.MUTATION,handler:(i,n)=>{const{subUnitId:e,unitId:t,range:r}=n;return i.get(h.SheetsFilterService).ensureFilterModel(t,e).setRange(r),!0}},p={id:"sheet.mutation.set-filter-criteria",type:d.CommandType.MUTATION,handler:(i,n)=>{const{subUnitId:e,unitId:t,criteria:r,col:s,reCalc:o=!0}=n,a=i.get(h.SheetsFilterService).getFilterModel(t,e);return a?(a.setCriteria(s,r,o),!0):!1}},A={id:"sheet.mutation.remove-filter",type:d.CommandType.MUTATION,handler:(i,n)=>{const{unitId:e,subUnitId:t}=n;return i.get(h.SheetsFilterService).removeFilterModel(e,t)}},z={id:"sheet.mutation.re-calc-filter",type:d.CommandType.MUTATION,handler:(i,n)=>{const{unitId:e,subUnitId:t}=n,s=i.get(h.SheetsFilterService).getFilterModel(e,t);return s?(s.reCalc(),!0):!1}};var fe=Object.defineProperty,me=Object.getOwnPropertyDescriptor,_e=(i,n,e,t)=>{for(var r=t>1?void 0:t?me(n,e):n,s=i.length-1,o;s>=0;s--)(o=i[s])&&(r=(t?o(n,e,r):o(r))||r);return t&&r&&fe(n,e,r),r},H=(i,n)=>(e,t)=>n(e,t,i);const ee=new Set([g.id,p.id,A.id,z.id]),W="SHEET_FILTER_PLUGIN";h.SheetsFilterService=class extends d.Disposable{constructor(e,t,r){super();R(this,"_filterModels",new Map);R(this,"_loadedUnitId$",new T.BehaviorSubject(null));R(this,"loadedUnitId$",this._loadedUnitId$.asObservable());R(this,"_activeFilterModel$",new T.BehaviorSubject(null));R(this,"activeFilterModel$",this._activeFilterModel$.asObservable());this._resourcesManagerService=e,this._univerInstanceService=t,this._commandService=r,this._initModel(),this._initActiveFilterModel()}get activeFilterModel(){return this._activeFilterModel$.getValue()}ensureFilterModel(e,t){const r=this.getFilterModel(e,t);if(r)return r;const s=this._univerInstanceService.getUniverSheetInstance(e);if(!s)throw new Error(`[SheetsFilterService]: could not create "FilterModel" on a non-existing workbook ${e}!`);const o=s.getSheetBySheetId(t);if(!o)throw new Error(`[SheetsFilterService]: could not create "FilterModel" on a non-existing worksheet ${t}!`);const l=new $(e,t,o);return this._cacheFilterModel(e,t,l),l}getFilterModel(e,t){var r,s;return(s=(r=this._filterModels.get(e))==null?void 0:r.get(t))!=null?s:null}removeFilterModel(e,t){const r=this.getFilterModel(e,t);return r?(r.dispose(),this._filterModels.get(e).delete(t),!0):!1}_updateActiveFilterModel(){let e;try{if(e=this._univerInstanceService.getCurrentUnitForType(d.UniverInstanceType.UNIVER_SHEET),!e){this._activeFilterModel$.next(null);return}}catch{return}const t=e.getActiveSheet();if(!t){this._activeFilterModel$.next(null);return}const r=t.getUnitId(),s=t.getSheetId(),o=this.getFilterModel(r,s);this._activeFilterModel$.next(o)}_initActiveFilterModel(){this.disposeWithMe(T.merge(d.fromCallback(this._commandService.onCommandExecuted).pipe(T.filter(([e])=>e.type===d.CommandType.MUTATION&&ee.has(e.id))),this._univerInstanceService.getCurrentTypeOfUnit$(d.UniverInstanceType.UNIVER_SHEET).pipe(T.switchMap(e=>{var t;return(t=e==null?void 0:e.activeSheet$)!=null?t:T.of(null)}))).subscribe(()=>this._updateActiveFilterModel()))}_serializeAutoFiltersForUnit(e){const t=this._filterModels.get(e);if(!t)return"{}";const r={};return t.forEach((s,o)=>{r[o]=s.serialize()}),JSON.stringify(r)}_deserializeAutoFiltersForUnit(e,t){const r=this._univerInstanceService.getUniverSheetInstance(e);Object.keys(t).forEach(s=>{const o=t[s],l=$.deserialize(e,s,r.getSheetBySheetId(s),o);this._cacheFilterModel(e,s,l)})}_initModel(){this._resourcesManagerService.registerPluginResource({pluginName:W,businesses:[2],toJson:e=>this._serializeAutoFiltersForUnit(e),parseJson:e=>JSON.parse(e),onLoad:(e,t)=>{this._deserializeAutoFiltersForUnit(e,t),this._loadedUnitId$.next(e),this._updateActiveFilterModel()},onUnLoad:e=>{const t=this._filterModels.get(e);t&&(t.forEach(r=>r.dispose()),this._filterModels.delete(e))}})}_cacheFilterModel(e,t,r){this._filterModels.has(e)||this._filterModels.set(e,new Map),this._filterModels.get(e).set(t,r)}},h.SheetsFilterService=_e([d.OnLifecycle(d.LifecycleStages.Ready,h.SheetsFilterService),H(0,d.IResourceManagerService),H(1,d.IUniverInstanceService),H(2,d.ICommandService)],h.SheetsFilterService);function Ce(i,n){for(let e=0;e<i.length;e++){let t=e;if(i[e])for(let r=e+1;r<i.length;r++)i[t]&&i[r]&&n(i[t],i[r])&&(i[t]=null,t=r)}return i.filter(e=>e!==null)}function N(i){return Ce(i,(n,e)=>n.id===p.id&&e.id===p.id&&n.params.unitId===e.params.unitId&&n.params.subUnitId===e.params.subUnitId&&n.params.col===e.params.col)}var ge=Object.defineProperty,Re=Object.getOwnPropertyDescriptor,Fe=(i,n,e,t)=>{for(var r=t>1?void 0:t?Re(n,e):n,s=i.length-1,o;s>=0;s--)(o=i[s])&&(r=(t?o(n,e,r):o(r))||r);return t&&r&&ge(n,e,r),r},L=(i,n)=>(e,t)=>n(e,t,i);let U=class extends d.Disposable{constructor(i,n,e,t,r){super(),this._commandService=i,this._sheetInterceptorService=n,this._sheetsFilterService=e,this._univerInstanceService=t,this._refRangeService=r,this._initCommands(),this._initRowFilteredInterceptor(),this._initInterceptors(),this._commandExecutedListener()}_initCommands(){[p,g,z,A].forEach(i=>this.disposeWithMe(this._commandService.registerCommand(i)))}_initInterceptors(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:e=>this._getUpdateFilter(e)}));const i=new d.DisposableCollection,n=(e,t)=>{var a;const r=this._univerInstanceService.getUniverSheetInstance(e);if(!r||!(r==null?void 0:r.getSheetBySheetId(t)))return;i.dispose();const o=(a=this._sheetsFilterService.getFilterModel(e,t))==null?void 0:a.getRange(),l=c=>{switch(c.id){case E.InsertRowCommand.id:{const u=c.params,m=u.unitId||e,C=u.subUnitId||t;return this._handleInsertRowCommand(u,m,C)}case E.InsertColCommand.id:{const u=c.params,m=u.unitId||e,C=u.subUnitId||t;return this._handleInsertColCommand(u,m,C)}case E.RemoveColCommand.id:{const u=c.params;return this._handleRemoveColCommand(u,e,t)}case E.RemoveRowCommand.id:{const u=c.params;return this._handleRemoveRowCommand(u,e,t)}case E.EffectRefRangId.MoveColsCommandId:{const u=c.params;return this._handleMoveColsCommand(u,e,t)}case E.EffectRefRangId.MoveRowsCommandId:{const u=c.params;return this._handleMoveRowsCommand(u,e,t)}case E.MoveRangeCommand.id:{const u=c.params;return this._handleMoveRangeCommand(u,e,t)}}return{redos:[],undos:[]}};o&&i.add(this._refRangeService.registerRefRange(o,l,e,t))};this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.id===E.SetWorksheetActivateCommand.id){const t=e.params,r=t.subUnitId,s=t.unitId;if(!r||!s)return;n(s,r)}if(e.id===g.id){const t=e.params,r=t.subUnitId,s=t.unitId;if(!r||!s)return;n(t.unitId,t.subUnitId)}})),this.disposeWithMe(this._sheetsFilterService.loadedUnitId$.subscribe(e=>{if(e){const t=this._univerInstanceService.getUniverSheetInstance(e),r=t==null?void 0:t.getActiveSheet();r&&n(e,r.getSheetId())}}))}_getUpdateFilter(i){const{id:n}=i;switch(n){case E.RemoveSheetCommand.id:{const e=i.params;return this._handleRemoveSheetCommand(e,e.unitId,e.subUnitId)}}return{redos:[],undos:[]}}_handleInsertColCommand(i,n,e){var _;const t=this._sheetsFilterService.getFilterModel(n,e),r=(_=t==null?void 0:t.getRange())!=null?_:null;if(!t||!r)return this._handleNull();const{startColumn:s,endColumn:o}=r,{startColumn:l,endColumn:a}=i.range,c=a-l+1;if(a>o)return this._handleNull();const u=[],m=[],C=l,w={unitId:n,subUnitId:e,range:{...r,startColumn:l<=s?s+c:s,endColumn:o+c}},S={unitId:n,subUnitId:e,range:r};u.push({id:g.id,params:w}),m.push({id:g.id,params:S});const f=t.getAllFilterColumns().filter(F=>F[0]>=C);if(f.length!==0){const{undos:F,redos:M}=this.moveCriteria(n,e,f,c);u.push(...M),m.push(...F)}return{redos:N(u),undos:N(m)}}_handleInsertRowCommand(i,n,e){var S;const t=this._sheetsFilterService.getFilterModel(n,e),r=(S=t==null?void 0:t.getRange())!=null?S:null;if(!t||!r)return this._handleNull();const{startRow:s,endRow:o}=r,{startRow:l,endRow:a}=i.range,c=a-l+1;if(a>o)return this._handleNull();const u=[],m=[],C={unitId:n,subUnitId:e,range:{...r,startRow:l<=s?s+c:s,endRow:o+c}},w={unitId:n,subUnitId:e,range:r};return u.push({id:g.id,params:C}),m.push({id:g.id,params:w}),{redos:N(u),undos:N(m)}}_handleRemoveColCommand(i,n,e){var v;const t=this._sheetsFilterService.getFilterModel(n,e),r=(v=t==null?void 0:t.getRange())!=null?v:null;if(!t||!r)return this._handleNull();const{startColumn:s,endColumn:o}=r,{startColumn:l,endColumn:a}=i.range;if(l>o)return this._handleNull();const c=[],u=[],m=a<s?0:Math.min(a,o)-Math.max(l,s)+1,C=a-l+1,w=t.getAllFilterColumns();w.forEach(f=>{const[_,F]=f;_<=a&&_>=l&&(c.push({id:p.id,params:{unitId:n,subUnitId:e,col:_,criteria:null}}),u.push({id:p.id,params:{unitId:n,subUnitId:e,col:_,criteria:{...F.serialize(),colId:_}}}))});const S=w.filter(f=>{const[_,F]=f;return _>a});if(S.length>0){const{undos:f,redos:_}=this.moveCriteria(n,e,S,-C);c.push(..._),u.push(...f)}if(m===o-s+1){const f={unitId:n,subUnitId:e};c.push({id:A.id,params:f})}else if(s<=l){const f=o-m,_={unitId:n,subUnitId:e,range:{...r,endColumn:f}};c.push({id:g.id,params:_})}else{const f={unitId:n,subUnitId:e,range:{...r,startColumn:l,endColumn:o-(a-l+1)}};c.push({id:g.id,params:f})}return u.push({id:g.id,params:{range:r,unitId:n,subUnitId:e}}),{undos:N(u),redos:N(c)}}_handleRemoveRowCommand(i,n,e){var S;const t=this._sheetsFilterService.getFilterModel(n,e);if(!t)return this._handleNull();const r=t.getRange(),{startRow:s,endRow:o}=r,{startRow:l,endRow:a}=i.range;if(l>o)return this._handleNull();const c=[],u=[],m=t.getAllFilterColumns(),C=s<=a&&s>=l;u.push({id:g.id,params:{range:r,unitId:n,subUnitId:e}});const w=Math.min(a,o)-Math.max(l,s)+1;if(w===o-s+1||C){const v={unitId:n,subUnitId:e};c.push({id:A.id,params:v}),m.forEach(f=>{const[_,F]=f,M={unitId:n,subUnitId:e,col:_,criteria:{...F.serialize(),colId:_}};u.push({id:p.id,params:M})})}else{const v=(S=this._univerInstanceService.getUniverSheetInstance(n))==null?void 0:S.getSheetBySheetId(e);if(!v)return this._handleNull();const f=[];for(let I=l;I<=a;I++)v.getRowFiltered(I)&&f.push(I);const _=Math.min(s,l),F=_+(o-s)-w+f.length,M={unitId:n,subUnitId:e,range:{...r,startRow:_,endRow:F}};c.push({id:g.id,params:M})}return{undos:N(u),redos:N(c)}}_handleMoveColsCommand(i,n,e){var v;const t=this._sheetsFilterService.getFilterModel(n,e),r=(v=t==null?void 0:t.getRange())!=null?v:null;if(!t||!r)return this._handleNull();const{startColumn:s,endColumn:o}=r,{fromRange:l,toRange:a}=i;if(l.endColumn<s&&a.startColumn<=s||l.startColumn>o&&a.endColumn>o)return this._handleNull();const c=[],u=[],m={};for(let f=s;f<=o;f++)m[f]={colIndex:f,filter:t.getFilterColumn(f)};d.moveMatrixArray(l.startColumn,l.endColumn-l.startColumn+1,a.startColumn,m);const C=Object.keys(m).map(f=>Number(f)),w=Math.max(...C),S=Math.min(...C);if(C.forEach(f=>{var I,te;const{colIndex:_,filter:F}=m[f],M=f;if(F){const Me={unitId:n,subUnitId:e,col:M,criteria:{...F.serialize(),colId:M}};if(c.push({id:p.id,params:Me}),u.push({id:A.id,params:{unitId:n,subUnitId:e,col:M,criteria:{...(I=t.getFilterColumn(M))==null?void 0:I.serialize(),colId:M}}}),!((te=m[_])!=null&&te.filter)){const Ee={unitId:n,subUnitId:e,col:_,criteria:null};c.push({id:p.id,params:Ee}),u.push({id:p.id,params:{unitId:n,subUnitId:e,col:_,criteria:{...F.serialize(),colId:_}}})}}}),s!==S||o!==w){const f={unitId:n,subUnitId:e,range:{...r,startColumn:S,endColumn:w}};c.unshift({id:g.id,params:f}),u.push({id:g.id,params:{range:r,unitId:n,subUnitId:e}})}return{undos:u,redos:c}}_handleMoveRowsCommand(i,n,e){var v;const t=this._sheetsFilterService.getFilterModel(n,e),r=(v=t==null?void 0:t.getRange())!=null?v:null;if(!t||!r)return this._handleNull();const{startRow:s,endRow:o}=r,{fromRange:l,toRange:a}=i;if(l.endRow<s&&a.startRow<=s||l.startRow>o&&a.endRow>o)return this._handleNull();const c=[],u=[],m={};for(let f=s;f<=o;f++)m[f]={offset:f-s};d.moveMatrixArray(l.startRow,l.endRow-l.startRow+1,a.startRow,m);const C=Object.keys(m).map(f=>Number(f)),w=Math.max(...C),S=Math.min(...C);if(s!==S||o!==w){const f={unitId:n,subUnitId:e,range:{...r,startRow:S,endRow:w}};c.unshift({id:g.id,params:f}),u.push({id:g.id,params:{range:r,unitId:n,subUnitId:e}})}return{redos:c,undos:u}}_handleMoveRangeCommand(i,n,e){const{fromRange:t,toRange:r}=i,s=this._sheetsFilterService.getFilterModel(n,e);if(!s)return this._handleNull();const o=s.getRange();if(!o)return this._handleNull();const l=[],a=[];if(d.Rectangle.contains(t,o)){const c=o.startRow-t.startRow,u=o.startColumn-t.startColumn,m={startRow:r.startRow+c,startColumn:r.startColumn+u,endRow:r.startRow+c+(o.endRow-o.startRow),endColumn:r.startColumn+u+(o.endColumn-o.startColumn)},C={id:A.id,params:{unitId:n,subUnitId:e}},w={id:g.id,params:{unitId:n,subUnitId:e,range:m}},S={id:g.id,params:{unitId:n,subUnitId:e,range:o}};l.push(C,w),a.push(C,S);const v=s.getAllFilterColumns(),f=r.startColumn-t.startColumn;v.forEach(_=>{const[F,M]=_;M&&(l.push({id:p.id,params:{unitId:n,subUnitId:e,col:F+f,criteria:{...M.serialize(),colId:F+f}}}),a.push({id:p.id,params:{unitId:n,subUnitId:e,col:F,criteria:{...M.serialize(),colId:F}}}))})}return{redos:l,undos:a}}_handleRemoveSheetCommand(i,n,e){const t=this._sheetsFilterService.getFilterModel(n,e);if(!t)return this._handleNull();const r=t.getRange();if(!r)return this._handleNull();const s=[],o=[];return t.getAllFilterColumns().forEach(a=>{const[c,u]=a;o.push({id:p.id,params:{unitId:n,subUnitId:e,col:a,criteria:{...u.serialize(),colId:a}}})}),s.push({id:A.id,params:{unitId:n,subUnitId:e,range:r}}),o.unshift({id:g.id,params:{range:r,unitId:n,subUnitId:e}}),{undos:o,redos:s}}_handleNull(){return{redos:[],undos:[]}}_initRowFilteredInterceptor(){this.disposeWithMe(this._sheetInterceptorService.intercept(E.INTERCEPTOR_POINT.ROW_FILTERED,{handler:(i,n)=>{var e,t;return i?!0:(t=(e=this._sheetsFilterService.getFilterModel(n.unitId,n.subUnitId))==null?void 0:e.isRowFiltered(n.row))!=null?t:!1}}))}moveCriteria(i,n,e,t){const r={unitId:i,subUnitId:n,criteria:null,col:-1},s=[],o=[];return e.forEach(l=>{const[a,c]=l;o.push({id:p.id,params:{...r,col:a}}),s.push({id:p.id,params:{...r,col:a,criteria:{...c.serialize(),colId:a}}})}),e.forEach(l=>{const[a,c]=l;o.push({id:p.id,params:{...r,col:a+t,criteria:{...c.serialize(),colId:a+t}}}),s.push({id:p.id,params:{...r,col:a+t,criteria:null}})}),{redos:o,undos:s}}_commandExecutedListener(){this.disposeWithMe(this._commandService.onCommandExecuted(i=>{const{unitId:n,subUnitId:e}=i.params||{},t=this._sheetsFilterService.getFilterModel(n,e);if(!t)return;const r=Array.from(t.filteredOutRows).sort((l,a)=>l-a),s=[];let o=!1;if(i.id===E.RemoveRowMutation.id){const{startRow:l,endRow:a}=i.params.range,c=r.filter(u=>u>=l&&u<=a);r.forEach(u=>{if(u<l)s.push(u);else if(o=!0,u<=a){const m=Math.max(l,s.length?s[s.length-1]+1:l);s.push(m)}else s.push(u-(a-l+1-c.length))})}if(i.id===E.InsertRowMutation.id){const{startRow:l,endRow:a}=i.params.range;r.forEach(c=>{c>=l?(o=!0,s.push(c+(a-l+1))):s.push(c)})}o&&(t.filteredOutRows=new Set(s))}))}};U=Fe([d.OnLifecycle(d.LifecycleStages.Ready,U),L(0,d.ICommandService),L(1,O.Inject(E.SheetInterceptorService)),L(2,O.Inject(h.SheetsFilterService)),L(3,d.IUniverInstanceService),L(4,O.Inject(E.RefRangeService))],U);var pe=Object.defineProperty,Se=Object.getOwnPropertyDescriptor,we=(i,n,e,t)=>{for(var r=t>1?void 0:t?Se(n,e):n,s=i.length-1,o;s>=0;s--)(o=i[s])&&(r=(t?o(n,e,r):o(r))||r);return t&&r&&pe(n,e,r),r},ve=(i,n)=>(e,t)=>n(e,t,i);h.UniverSheetsFilterPlugin=(B=class extends d.Plugin{constructor(n,e){super(),this._injector=e}onStarting(n){[[h.SheetsFilterService],[U]].forEach(e=>n.add(e))}},R(B,"type",d.UniverInstanceType.UNIVER_SHEET),R(B,"pluginName",W),B),h.UniverSheetsFilterPlugin=we([ve(1,O.Inject(O.Injector))],h.UniverSheetsFilterPlugin),h.CustomFilterOperator=y,h.FILTER_MUTATIONS=ee,h.FilterColumn=Z,h.FilterModel=$,h.ReCalcSheetsFilterMutation=z,h.RemoveSheetsFilterMutation=A,h.SHEET_FILTER_SNAPSHOT_ID=W,h.SetSheetsFilterCriteriaMutation=p,h.SetSheetsFilterRangeMutation=g,h.equals=G,h.getCustomFilterFn=j,h.greaterThan=D,h.greaterThanOrEqualTo=q,h.lessThan=V,h.lessThanOrEqualTo=Q,h.notEquals=b,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs/sheets-filter",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "A library for filtering data in Univer Sheet",
|
|
6
6
|
"author": "DreamNum <developer@univer.ai>",
|
|
@@ -44,21 +44,21 @@
|
|
|
44
44
|
"lib"
|
|
45
45
|
],
|
|
46
46
|
"peerDependencies": {
|
|
47
|
-
"@wendellhu/redi": "
|
|
47
|
+
"@wendellhu/redi": "0.15.2",
|
|
48
48
|
"rxjs": ">=7.0.0",
|
|
49
|
-
"@univerjs/
|
|
50
|
-
"@univerjs/
|
|
49
|
+
"@univerjs/sheets": "0.1.12",
|
|
50
|
+
"@univerjs/core": "0.1.12"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@wendellhu/redi": "
|
|
54
|
+
"@wendellhu/redi": "0.15.2",
|
|
55
55
|
"rxjs": "^7.8.1",
|
|
56
56
|
"typescript": "^5.4.5",
|
|
57
|
-
"vite": "^5.2.
|
|
58
|
-
"vitest": "^1.
|
|
59
|
-
"@univerjs/core": "0.1.
|
|
60
|
-
"@univerjs/shared": "0.1.
|
|
61
|
-
"@univerjs/sheets": "0.1.
|
|
57
|
+
"vite": "^5.2.11",
|
|
58
|
+
"vitest": "^1.6.0",
|
|
59
|
+
"@univerjs/core": "0.1.12",
|
|
60
|
+
"@univerjs/shared": "0.1.12",
|
|
61
|
+
"@univerjs/sheets": "0.1.12"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
64
|
"test": "vitest run",
|
package/lib/types/controllers/{sheets-fiter.controller.d.ts → sheets-filter.controller.d.ts}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { SheetsFilterService } from '../services/sheet-filter.service';
|
|
2
|
-
import { RefRangeService, SheetInterceptorService } from '@univerjs/sheets';
|
|
3
1
|
import { Disposable, ICommandService, IUniverInstanceService } from '@univerjs/core';
|
|
2
|
+
import { RefRangeService, SheetInterceptorService } from '@univerjs/sheets';
|
|
3
|
+
import { SheetsFilterService } from '../services/sheet-filter.service';
|
|
4
4
|
|
|
5
5
|
export declare class SheetsFilterController extends Disposable {
|
|
6
6
|
private readonly _commandService;
|