| 1 |
- (function(l,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("@univerjs/core"),require("@univerjs/sheets"),require("@univerjs/engine-formula")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@univerjs/sheets","@univerjs/engine-formula"],o):(l=typeof globalThis<"u"?globalThis:l||self,o(l.UniverSheetsSort={},l.UniverCore,l.UniverSheets,l.UniverEngineFormula))})(this,function(l,o,g,j){"use strict";var ne=Object.defineProperty;var re=(l,o,g)=>o in l?ne(l,o,{enumerable:!0,configurable:!0,writable:!0,value:g}):l[o]=g;var _=(l,o,g)=>re(l,typeof o!="symbol"?o+"":o,g);var C;const D="sheets-sort.config",b={};var d=(e=>(e.DESC="desc",e.ASC="asc",e))(d||{});const A=e=>e.replace(/-/gi,"").replace(/'/gi,""),N=(e,s)=>{const t=e===null||e==="",r=s===null||s==="";return t&&r?0:t?1:r?-1:null},O=(e,s,t)=>{const r=typeof e=="number",n=typeof s=="number";return r&&n?e<s?t===d.ASC?-1:1:e>s?t===d.ASC?1:-1:0:r?t===d.ASC?1:-1:n?t===d.ASC?-1:1:null},E=(e,s,t)=>{const r=typeof e=="string",n=typeof s=="string";if(r&&(e=A(e.toLocaleLowerCase())),n&&(s=A(s.toLocaleLowerCase())),!r&&!n)return null;if(r&&n){const i=e,u=s;return i<u?t===d.ASC?-1:1:i>u?t===d.ASC?1:-1:0}return r?t===d.ASC?1:-1:n?t===d.ASC?-1:1:null},y=e=>!e||Object.keys(e).length===0||(e==null?void 0:e.v)==null&&(e==null?void 0:e.p)==null;var M=Object.defineProperty,$=Object.getOwnPropertyDescriptor,V=(e,s,t,r)=>{for(var n=r>1?void 0:r?$(s,t):s,i=e.length-1,u;i>=0;i--)(u=e[i])&&(n=(r?u(s,t,n):u(n))||n);return r&&n&&M(s,t,n),n},p=(e,s)=>(t,r)=>s(t,r,e);l.SheetsSortService=class extends o.Disposable{constructor(t,r,n){super();_(this,"_compareFns",[]);this._univerInstanceService=t,this._commandService=r,this._formulaDataModel=n}mergeCheck(t){var a;const{unitId:r,subUnitId:n,range:i}=t,u=(a=this._univerInstanceService.getUnit(r))==null?void 0:a.getSheetBySheetId(n);if(!u)return!1;const c=u.getMergeData().filter(f=>o.Rectangle.contains(i,f));return c.length===0?!0:L(i,c)}emptyCheck(t){var c;const{unitId:r,subUnitId:n,range:i}=t,u=(c=this._univerInstanceService.getUnit(r))==null?void 0:c.getSheetBySheetId(n);if(!u)return!1;for(let a=i.startRow;a<=i.endRow;a++)for(let f=i.startColumn;f<=i.endColumn;f++)if(!y(u.getCellRaw(a,f)))return!0;return!1}singleCheck(t){return t.range.startRow!==t.range.endRow}formulaCheck(t){var c,a;const{unitId:r,subUnitId:n,range:i}=t,u=(a=(c=this._formulaDataModel.getArrayFormulaRange())==null?void 0:c[r])==null?void 0:a[n];for(const f in u){const S=u[Number(f)];for(const I in S){const v=S[Number(I)];if(v&&o.Rectangle.intersects(i,v))return!1}}return!0}registerCompareFn(t){this._compareFns.unshift(t)}getAllCompareFns(){return this._compareFns}applySort(t,r,n){var c;const{unitId:i,subUnitId:u}=g.getSheetCommandTarget(this._univerInstanceService)||{};this._commandService.executeCommand(R.id,{orderRules:t.orderRules,range:t.range,hasTitle:(c=t.hasTitle)!=null?c:!1,unitId:r||i,subUnitId:n||u})}},l.SheetsSortService=V([p(0,o.IUniverInstanceService),p(1,o.ICommandService),p(2,o.Inject(j.FormulaDataModel))],l.SheetsSortService);function L(e,s){const t=e.endRow-e.startRow+1,r=e.endColumn-e.startColumn+1;let n=null,i=null;const u=t*r;let c=0;for(const a of s)if(a.startRow>=e.startRow&&a.endRow<=e.endRow&&a.startColumn>=e.startColumn&&a.endColumn<=e.endColumn){const f=a.endRow-a.startRow+1,S=a.endColumn-a.startColumn+1;if(n===null&&i===null)n=f,i=S;else if(f!==n||S!==i)return!1;c+=f*S}return c===u}const R={id:"sheet.command.sort-range",type:o.CommandType.COMMAND,handler:(e,s)=>{const{range:t,orderRules:r,hasTitle:n,unitId:i,subUnitId:u}=s,c=e.get(l.SheetsSortService),a=e.get(o.IUniverInstanceService),{worksheet:f}=g.getSheetCommandTarget(a,s)||{};if(!f)return!1;const S=f.getMergeData().filter(m=>o.Rectangle.contains(t,m)),I=S.map(m=>m.startRow),{startRow:v,endRow:Q}=t,X=n?v+1:v,w=[],F=[];for(let m=X;m<=Q;m++)f.getRowFiltered(m)||f.getRowRawVisible(m)!==!1&&(S.length&&!I.includes(m)||(w.push({index:m,value:G(f,m,r)}),F.push(m)));const Z=c.getAllCompareFns();w.sort(B(r,q(Z)));const T={};w.forEach(({index:m,value:ie},te)=>{T[F[te]]=m});const k={id:g.ReorderRangeCommand.id,params:{unitId:i,subUnitId:u,range:t,order:T}},ee=e.get(o.ICommandService);return o.sequenceExecute([k],ee).result}};function G(e,s,t){const r=[];return t.forEach(({colIndex:n})=>{r.push(e.getCellRaw(s,n))}),r}function q(e){return(s,t,r)=>{for(let n=0;n<e.length;n++){const i=e[n](s,t,r);if(i!=null)return i}return 0}}function B(e,s){return function(t,r){let n=null;for(let i=0;i<e.length;i++){const u=t.value[i],c=r.value[i];if(n=s(e[i].type,u,c),n!==0&&n!==null&&n!==void 0)return n}return 0}}var H=Object.defineProperty,x=Object.getOwnPropertyDescriptor,K=(e,s,t,r)=>{for(var n=r>1?void 0:r?x(s,t):s,i=e.length-1,u;i>=0;i--)(u=e[i])&&(n=(r?u(s,t,n):u(n))||n);return r&&n&&H(s,t,n),n},P=(e,s)=>(t,r)=>s(t,r,e);let h=class extends o.Disposable{constructor(e,s){super(),this._commandService=e,this._sortService=s,this._initCommands(),this._registerCompareFns()}_initCommands(){[R].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}_registerCompareFns(){const e=(s,t,r)=>{const n=this._getCommonValue(t),i=this._getCommonValue(r),u=[N,E,O];for(let c=0;c<u.length;c++){const a=u[c](n,i,s);if(a!==null)return a}return null};this._sortService.registerCompareFn(e)}_getCommonValue(e){var t,r;if(y(e))return null;const s=(r=(t=e==null?void 0:e.p)==null?void 0:t.body)==null?void 0:r.dataStream;return s||((e==null?void 0:e.t)===o.CellValueType.NUMBER?Number.parseFloat(`${e.v}`):(e==null?void 0:e.t)===o.CellValueType.STRING?typeof e.v=="number"?e.v:`${e.v}`:(e==null?void 0:e.t)===o.CellValueType.BOOLEAN?`${e.v}`:(e==null?void 0:e.t)===o.CellValueType.FORCE_STRING?Number.parseFloat(`${e.v}`):`${e==null?void 0:e.v}`)}};h=K([P(0,o.ICommandService),P(1,o.Inject(l.SheetsSortService))],h);var W=Object.defineProperty,Y=Object.getOwnPropertyDescriptor,z=(e,s,t,r)=>{for(var n=r>1?void 0:r?Y(s,t):s,i=e.length-1,u;i>=0;i--)(u=e[i])&&(n=(r?u(s,t,n):u(n))||n);return r&&n&&W(s,t,n),n},U=(e,s)=>(t,r)=>s(t,r,e);const J="SHEET_SORT_PLUGIN";l.UniverSheetsSortPlugin=(C=class extends o.Plugin{constructor(s=b,t,r){super(),this._config=s,this._injector=t,this._configService=r;const{...n}=o.merge({},b,this._config);this._configService.setConfig(D,n)}onStarting(){[[h],[l.SheetsSortService]].forEach(s=>this._injector.add(s))}onReady(){this._injector.get(h)}},_(C,"type",o.UniverInstanceType.UNIVER_SHEET),_(C,"pluginName",J),C),l.UniverSheetsSortPlugin=z([U(1,o.Inject(o.Injector)),U(2,o.IConfigService)],l.UniverSheetsSortPlugin),l.SortRangeCommand=R,l.SortType=d,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
|