@univerjs/sheets-sort 0.5.1 → 0.5.2
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/facade.js +1 -0
- package/lib/cjs/index.js +1 -1
- package/lib/es/facade.js +41 -0
- package/lib/es/index.js +74 -70
- package/lib/types/facade/f-range.d.ts +22 -0
- package/lib/types/facade/f-worksheet.d.ts +20 -0
- package/lib/types/facade/index.d.ts +19 -0
- package/lib/umd/facade.js +1 -0
- package/lib/umd/index.js +1 -1
- package/package.json +17 -7
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const e=require("@univerjs/sheets-sort"),o=require("@univerjs/sheets/facade"),i=require("@univerjs/core");class d extends o.FRange{async sort(r){const s=(Array.isArray(r)?r:[r]).map(t=>typeof t=="number"?{colIndex:t,type:e.SortType.ASC}:{colIndex:t.column,type:t.ascending?e.SortType.ASC:e.SortType.DESC});return await this._commandService.executeCommand(e.SortRangeCommand.id,{orderRules:s,range:this._range,hasTitle:!1,unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId()}),this}}o.FRange.extend(d);class u extends o.FWorksheet{async sort(r,n=!0){const s=[{colIndex:r,type:n?e.SortType.ASC:e.SortType.DESC}],t={startRow:0,startColumn:0,endRow:this._worksheet.getRowCount()-1,endColumn:this._worksheet.getColumnCount()-1,rangeType:i.RANGE_TYPE.ALL};return await this._commandService.executeCommand(e.SortRangeCommand.id,{orderRules:s,range:t,hasTitle:!1,unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId()}),this}}o.FWorksheet.extend(u);
|
package/lib/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var M=Object.defineProperty;var $=(e,r,t)=>r in e?M(e,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[r]=t;var d=(e,r,t)=>$(e,typeof r!="symbol"?r+"":r,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@univerjs/core"),R=require("@univerjs/sheets"),V=require("@univerjs/engine-formula"),x="sheets-sort.config",A={};var f=(e=>(e.DESC="desc",e.ASC="asc",e))(f||{});const P=e=>e.replace(/-/gi,"").replace(/'/gi,""),L=(e,r)=>{const t=e===null||e==="",o=r===null||r==="";return t&&o?0:t?1:o?-1:null},G=(e,r,t)=>{const o=typeof e=="number",n=typeof r=="number";return o&&n?e<r?t===f.ASC?-1:1:e>r?t===f.ASC?1:-1:0:o?t===f.ASC?1:-1:n?t===f.ASC?-1:1:null},q=(e,r,t)=>{const o=typeof e=="string",n=typeof r=="string";if(o&&(e=P(e.toLocaleLowerCase())),n&&(r=P(r.toLocaleLowerCase())),!o&&!n)return null;if(o&&n){const s=e,i=r;return s<i?t===f.ASC?-1:1:s>i?t===f.ASC?1:-1:0}return o?t===f.ASC?1:-1:n?t===f.ASC?-1:1:null},D=e=>!e||Object.keys(e).length===0||(e==null?void 0:e.v)==null&&(e==null?void 0:e.p)==null;var B=Object.defineProperty,H=Object.getOwnPropertyDescriptor,K=(e,r,t,o)=>{for(var n=o>1?void 0:o?H(r,t):r,s=e.length-1,i;s>=0;s--)(i=e[s])&&(n=(o?i(r,t,n):i(n))||n);return o&&n&&B(r,t,n),n},p=(e,r)=>(t,o)=>r(t,o,e);exports.SheetsSortService=class extends u.Disposable{constructor(t,o,n){super();d(this,"_compareFns",[]);this._univerInstanceService=t,this._commandService=o,this._formulaDataModel=n}mergeCheck(t){var c;const{unitId:o,subUnitId:n,range:s}=t,i=(c=this._univerInstanceService.getUnit(o))==null?void 0:c.getSheetBySheetId(n);if(!i)return!1;const l=i.getMergeData().filter(a=>u.Rectangle.contains(s,a));return l.length===0?!0:W(s,l)}emptyCheck(t){var l;const{unitId:o,subUnitId:n,range:s}=t,i=(l=this._univerInstanceService.getUnit(o))==null?void 0:l.getSheetBySheetId(n);if(!i)return!1;for(let c=s.startRow;c<=s.endRow;c++)for(let a=s.startColumn;a<=s.endColumn;a++)if(!D(i.getCellRaw(c,a)))return!0;return!1}singleCheck(t){return t.range.startRow!==t.range.endRow}formulaCheck(t){var l,c;const{unitId:o,subUnitId:n,range:s}=t,i=(c=(l=this._formulaDataModel.getArrayFormulaRange())==null?void 0:l[o])==null?void 0:c[n];for(const a in i){const g=i[Number(a)];for(const C in g){const S=g[Number(C)];if(S&&u.Rectangle.intersects(s,S))return!1}}return!0}registerCompareFn(t){this._compareFns.unshift(t)}getAllCompareFns(){return this._compareFns}applySort(t,o,n){var l;const{unitId:s,subUnitId:i}=R.getSheetCommandTarget(this._univerInstanceService)||{};this._commandService.executeCommand(I.id,{orderRules:t.orderRules,range:t.range,hasTitle:(l=t.hasTitle)!=null?l:!1,unitId:o||s,subUnitId:n||i})}};exports.SheetsSortService=K([p(0,u.IUniverInstanceService),p(1,u.ICommandService),p(2,u.Inject(V.FormulaDataModel))],exports.SheetsSortService);function W(e,r){const t=e.endRow-e.startRow+1,o=e.endColumn-e.startColumn+1;let n=null,s=null;const i=t*o;let l=0;for(const c of r)if(c.startRow>=e.startRow&&c.endRow<=e.endRow&&c.startColumn>=e.startColumn&&c.endColumn<=e.endColumn){const a=c.endRow-c.startRow+1,g=c.endColumn-c.startColumn+1;if(n===null&&s===null)n=a,s=g;else if(a!==n||g!==s)return!1;l+=a*g}return l===i}const I={id:"sheet.command.sort-range",type:u.CommandType.COMMAND,handler:(e,r)=>{const{range:t,orderRules:o,hasTitle:n,unitId:s,subUnitId:i}=r,l=e.get(exports.SheetsSortService),c=e.get(u.IUniverInstanceService),{worksheet:a}=R.getSheetCommandTarget(c,r)||{};if(!a)return!1;const g=a.getMergeData().filter(m=>u.Rectangle.contains(t,m)),C=g.map(m=>m.startRow),{startRow:S,endRow:N}=t,O=n?S+1:S,_=[],w=[];for(let m=O;m<=N;m++)a.getRowFiltered(m)||a.getRowRawVisible(m)!==!1&&(g.length&&!C.includes(m)||(_.push({index:m,value:Y(a,m,o)}),w.push(m)));const T=l.getAllCompareFns();_.sort(J(o,z(T)));const b={};_.forEach(({index:m,value:oe},j)=>{b[w[j]]=m});const U={id:R.ReorderRangeCommand.id,params:{unitId:s,subUnitId:i,range:t,order:b}},E=e.get(u.ICommandService);return u.sequenceExecute([U],E).result}};function Y(e,r,t){const o=[];return t.forEach(({colIndex:n})=>{o.push(e.getCellRaw(r,n))}),o}function z(e){return(r,t,o)=>{for(let n=0;n<e.length;n++){const s=e[n](r,t,o);if(s!=null)return s}return 0}}function J(e,r){return function(t,o){let n=null;for(let s=0;s<e.length;s++){const i=t.value[s],l=o.value[s];if(n=r(e[s].type,i,l),n!==0&&n!==null&&n!==void 0)return n}return 0}}var Q=Object.defineProperty,X=Object.getOwnPropertyDescriptor,Z=(e,r,t,o)=>{for(var n=o>1?void 0:o?X(r,t):r,s=e.length-1,i;s>=0;s--)(i=e[s])&&(n=(o?i(r,t,n):i(n))||n);return o&&n&&Q(r,t,n),n},y=(e,r)=>(t,o)=>r(t,o,e);let h=class extends u.Disposable{constructor(e,r){super(),this._commandService=e,this._sortService=r,this._initCommands(),this._registerCompareFns()}_initCommands(){[I].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}_registerCompareFns(){const e=(r,t,o)=>{const n=this._getCommonValue(t),s=this._getCommonValue(o),i=[L,q,G];for(let l=0;l<i.length;l++){const c=i[l](n,s,r);if(c!==null)return c}return null};this._sortService.registerCompareFn(e)}_getCommonValue(e){var t,o;if(D(e))return null;const r=(o=(t=e==null?void 0:e.p)==null?void 0:t.body)==null?void 0:o.dataStream;return r||((e==null?void 0:e.t)===u.CellValueType.NUMBER?Number.parseFloat(`${e.v}`):(e==null?void 0:e.t)===u.CellValueType.STRING?typeof e.v=="number"?e.v:`${e.v}`:(e==null?void 0:e.t)===u.CellValueType.BOOLEAN?`${e.v}`:(e==null?void 0:e.t)===u.CellValueType.FORCE_STRING?Number.parseFloat(`${e.v}`):`${e==null?void 0:e.v}`)}};h=Z([y(0,u.ICommandService),y(1,u.Inject(exports.SheetsSortService))],h);var k=Object.defineProperty,ee=Object.getOwnPropertyDescriptor,te=(e,r,t,o)=>{for(var n=o>1?void 0:o?ee(r,t):r,s=e.length-1,i;s>=0;s--)(i=e[s])&&(n=(o?i(r,t,n):i(n))||n);return o&&n&&k(r,t,n),n},F=(e,r)=>(t,o)=>r(t,o,e);const re="SHEET_SORT_PLUGIN";var v;exports.UniverSheetsSortPlugin=(v=class extends u.Plugin{constructor(r=A,t,o){super(),this._config=r,this._injector=t,this._configService=o;const{...n}=u.merge({},A,this._config);this._configService.setConfig(x,n)}onStarting(){[[h],[exports.SheetsSortService]].forEach(r=>this._injector.add(r))}onReady(){this._injector.get(h)}},d(v,"type",u.UniverInstanceType.UNIVER_SHEET),d(v,"pluginName",re),v);exports.UniverSheetsSortPlugin=te([F(1,u.Inject(u.Injector)),F(2,u.IConfigService)],exports.UniverSheetsSortPlugin);exports.SortRangeCommand=I;exports.SortType=f;
|
package/lib/es/facade.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { SortType as o, SortRangeCommand as s } from "@univerjs/sheets-sort";
|
|
2
|
+
import { FRange as i, FWorksheet as a } from "@univerjs/sheets/facade";
|
|
3
|
+
import { RANGE_TYPE as m } from "@univerjs/core";
|
|
4
|
+
class h extends i {
|
|
5
|
+
async sort(t) {
|
|
6
|
+
const r = (Array.isArray(t) ? t : [t]).map((e) => typeof e == "number" ? { colIndex: e, type: o.ASC } : {
|
|
7
|
+
colIndex: e.column,
|
|
8
|
+
type: e.ascending ? o.ASC : o.DESC
|
|
9
|
+
});
|
|
10
|
+
return await this._commandService.executeCommand(s.id, {
|
|
11
|
+
orderRules: r,
|
|
12
|
+
range: this._range,
|
|
13
|
+
hasTitle: !1,
|
|
14
|
+
unitId: this._workbook.getUnitId(),
|
|
15
|
+
subUnitId: this._worksheet.getSheetId()
|
|
16
|
+
}), this;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
i.extend(h);
|
|
20
|
+
class u extends a {
|
|
21
|
+
async sort(t, n = !0) {
|
|
22
|
+
const r = [{
|
|
23
|
+
colIndex: t,
|
|
24
|
+
type: n ? o.ASC : o.DESC
|
|
25
|
+
}], e = {
|
|
26
|
+
startRow: 0,
|
|
27
|
+
startColumn: 0,
|
|
28
|
+
endRow: this._worksheet.getRowCount() - 1,
|
|
29
|
+
endColumn: this._worksheet.getColumnCount() - 1,
|
|
30
|
+
rangeType: m.ALL
|
|
31
|
+
};
|
|
32
|
+
return await this._commandService.executeCommand(s.id, {
|
|
33
|
+
orderRules: r,
|
|
34
|
+
range: e,
|
|
35
|
+
hasTitle: !1,
|
|
36
|
+
unitId: this._workbook.getUnitId(),
|
|
37
|
+
subUnitId: this._worksheet.getSheetId()
|
|
38
|
+
}), this;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
a.extend(u);
|
package/lib/es/index.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var h = (e, t, r) =>
|
|
4
|
-
import { Inject as w, Disposable as
|
|
5
|
-
import { getSheetCommandTarget as
|
|
6
|
-
import { FormulaDataModel as
|
|
7
|
-
const
|
|
1
|
+
var H = Object.defineProperty;
|
|
2
|
+
var q = (e, t, r) => t in e ? H(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
|
|
3
|
+
var h = (e, t, r) => q(e, typeof t != "symbol" ? t + "" : t, r);
|
|
4
|
+
import { Inject as w, Disposable as y, Rectangle as R, IUniverInstanceService as U, ICommandService as I, CommandType as K, sequenceExecute as W, CellValueType as v, Plugin as Y, merge as z, UniverInstanceType as J, Injector as Q, IConfigService as X } from "@univerjs/core";
|
|
5
|
+
import { getSheetCommandTarget as T, ReorderRangeCommand as Z } from "@univerjs/sheets";
|
|
6
|
+
import { FormulaDataModel as k } from "@univerjs/engine-formula";
|
|
7
|
+
const ee = "sheets-sort.config", N = {};
|
|
8
8
|
var f = /* @__PURE__ */ ((e) => (e.DESC = "desc", e.ASC = "asc", e))(f || {});
|
|
9
|
-
const
|
|
9
|
+
const D = (e) => e.replace(/-/gi, "").replace(/'/gi, ""), te = (e, t) => {
|
|
10
10
|
const r = e === null || e === "", o = t === null || t === "";
|
|
11
11
|
return r && o ? 0 : r ? 1 : o ? -1 : null;
|
|
12
|
-
},
|
|
12
|
+
}, re = (e, t, r) => {
|
|
13
13
|
const o = typeof e == "number", n = typeof t == "number";
|
|
14
14
|
return o && n ? e < t ? r === f.ASC ? -1 : 1 : e > t ? r === f.ASC ? 1 : -1 : 0 : o ? r === f.ASC ? 1 : -1 : n ? r === f.ASC ? -1 : 1 : null;
|
|
15
|
-
},
|
|
15
|
+
}, ne = (e, t, r) => {
|
|
16
16
|
const o = typeof e == "string", n = typeof t == "string";
|
|
17
|
-
if (o && (e =
|
|
17
|
+
if (o && (e = D(e.toLocaleLowerCase())), n && (t = D(t.toLocaleLowerCase())), !o && !n)
|
|
18
18
|
return null;
|
|
19
19
|
if (o && n) {
|
|
20
20
|
const s = e, i = t;
|
|
21
21
|
return s < i ? r === f.ASC ? -1 : 1 : s > i ? r === f.ASC ? 1 : -1 : 0;
|
|
22
22
|
}
|
|
23
23
|
return o ? r === f.ASC ? 1 : -1 : n ? r === f.ASC ? -1 : 1 : null;
|
|
24
|
-
},
|
|
25
|
-
var
|
|
26
|
-
for (var n = o > 1 ? void 0 : o ?
|
|
24
|
+
}, M = (e) => !e || Object.keys(e).length === 0 || (e == null ? void 0 : e.v) == null && (e == null ? void 0 : e.p) == null;
|
|
25
|
+
var oe = Object.defineProperty, se = Object.getOwnPropertyDescriptor, ie = (e, t, r, o) => {
|
|
26
|
+
for (var n = o > 1 ? void 0 : o ? se(t, r) : t, s = e.length - 1, i; s >= 0; s--)
|
|
27
27
|
(i = e[s]) && (n = (o ? i(t, r, n) : i(n)) || n);
|
|
28
|
-
return o && n &&
|
|
28
|
+
return o && n && oe(t, r, n), n;
|
|
29
29
|
}, p = (e, t) => (r, o) => t(r, o, e);
|
|
30
|
-
let g = class extends
|
|
30
|
+
let g = class extends y {
|
|
31
31
|
constructor(t, r, o) {
|
|
32
32
|
super();
|
|
33
33
|
h(this, "_compareFns", []);
|
|
@@ -39,7 +39,7 @@ let g = class extends E {
|
|
|
39
39
|
if (!s)
|
|
40
40
|
return !1;
|
|
41
41
|
const i = s.getMergeData().filter((u) => R.contains(n, u));
|
|
42
|
-
return i.length === 0 ? !0 :
|
|
42
|
+
return i.length === 0 ? !0 : ue(n, i);
|
|
43
43
|
}
|
|
44
44
|
emptyCheck(t) {
|
|
45
45
|
var i;
|
|
@@ -48,7 +48,7 @@ let g = class extends E {
|
|
|
48
48
|
return !1;
|
|
49
49
|
for (let c = n.startRow; c <= n.endRow; c++)
|
|
50
50
|
for (let u = n.startColumn; u <= n.endColumn; u++)
|
|
51
|
-
if (!
|
|
51
|
+
if (!M(s.getCellRaw(c, u)))
|
|
52
52
|
return !0;
|
|
53
53
|
return !1;
|
|
54
54
|
}
|
|
@@ -76,8 +76,8 @@ let g = class extends E {
|
|
|
76
76
|
}
|
|
77
77
|
applySort(t, r, o) {
|
|
78
78
|
var i;
|
|
79
|
-
const { unitId: n, subUnitId: s } =
|
|
80
|
-
this._commandService.executeCommand(
|
|
79
|
+
const { unitId: n, subUnitId: s } = T(this._univerInstanceService) || {};
|
|
80
|
+
this._commandService.executeCommand($.id, {
|
|
81
81
|
orderRules: t.orderRules,
|
|
82
82
|
range: t.range,
|
|
83
83
|
hasTitle: (i = t.hasTitle) != null ? i : !1,
|
|
@@ -86,12 +86,12 @@ let g = class extends E {
|
|
|
86
86
|
});
|
|
87
87
|
}
|
|
88
88
|
};
|
|
89
|
-
g =
|
|
90
|
-
p(0,
|
|
89
|
+
g = ie([
|
|
90
|
+
p(0, U),
|
|
91
91
|
p(1, I),
|
|
92
|
-
p(2, w(
|
|
92
|
+
p(2, w(k))
|
|
93
93
|
], g);
|
|
94
|
-
function
|
|
94
|
+
function ue(e, t) {
|
|
95
95
|
const r = e.endRow - e.startRow + 1, o = e.endColumn - e.startColumn + 1;
|
|
96
96
|
let n = null, s = null;
|
|
97
97
|
const i = r * o;
|
|
@@ -107,44 +107,44 @@ function ie(e, t) {
|
|
|
107
107
|
}
|
|
108
108
|
return c === i;
|
|
109
109
|
}
|
|
110
|
-
const
|
|
110
|
+
const $ = {
|
|
111
111
|
id: "sheet.command.sort-range",
|
|
112
|
-
type:
|
|
112
|
+
type: K.COMMAND,
|
|
113
113
|
handler: (e, t) => {
|
|
114
|
-
const { range: r, orderRules: o, hasTitle: n, unitId: s, subUnitId: i } = t, c = e.get(g), u = e.get(
|
|
114
|
+
const { range: r, orderRules: o, hasTitle: n, unitId: s, subUnitId: i } = t, c = e.get(g), u = e.get(U), { worksheet: a } = T(u, t) || {};
|
|
115
115
|
if (!a)
|
|
116
116
|
return !1;
|
|
117
|
-
const m = a.getMergeData().filter((l) => R.contains(r, l)), d = m.map((l) => l.startRow), { startRow: b, endRow:
|
|
118
|
-
for (let l =
|
|
117
|
+
const m = a.getMergeData().filter((l) => R.contains(r, l)), d = m.map((l) => l.startRow), { startRow: b, endRow: j } = r, x = n ? b + 1 : b, S = [], A = [];
|
|
118
|
+
for (let l = x; l <= j; l++)
|
|
119
119
|
a.getRowFiltered(l) || a.getRowRawVisible(l) !== !1 && (m.length && !d.includes(l) || (S.push({
|
|
120
120
|
index: l,
|
|
121
|
-
value:
|
|
121
|
+
value: ce(a, l, o)
|
|
122
122
|
}), A.push(l)));
|
|
123
|
-
const
|
|
124
|
-
S.sort(
|
|
123
|
+
const V = c.getAllCompareFns();
|
|
124
|
+
S.sort(ae(o, le(V)));
|
|
125
125
|
const F = {};
|
|
126
|
-
S.forEach(({ index: l, value:
|
|
127
|
-
F[A[
|
|
126
|
+
S.forEach(({ index: l, value: Se }, B) => {
|
|
127
|
+
F[A[B]] = l;
|
|
128
128
|
});
|
|
129
|
-
const
|
|
130
|
-
id:
|
|
129
|
+
const L = {
|
|
130
|
+
id: Z.id,
|
|
131
131
|
params: {
|
|
132
132
|
unitId: s,
|
|
133
133
|
subUnitId: i,
|
|
134
134
|
range: r,
|
|
135
135
|
order: F
|
|
136
136
|
}
|
|
137
|
-
},
|
|
138
|
-
return
|
|
137
|
+
}, G = e.get(I);
|
|
138
|
+
return W([L], G).result;
|
|
139
139
|
}
|
|
140
140
|
};
|
|
141
|
-
function
|
|
141
|
+
function ce(e, t, r) {
|
|
142
142
|
const o = [];
|
|
143
143
|
return r.forEach(({ colIndex: n }) => {
|
|
144
144
|
o.push(e.getCellRaw(t, n));
|
|
145
145
|
}), o;
|
|
146
146
|
}
|
|
147
|
-
function
|
|
147
|
+
function le(e) {
|
|
148
148
|
return (t, r, o) => {
|
|
149
149
|
for (let n = 0; n < e.length; n++) {
|
|
150
150
|
const s = e[n](t, r, o);
|
|
@@ -154,7 +154,7 @@ function ce(e) {
|
|
|
154
154
|
return 0;
|
|
155
155
|
};
|
|
156
156
|
}
|
|
157
|
-
function
|
|
157
|
+
function ae(e, t) {
|
|
158
158
|
return function(r, o) {
|
|
159
159
|
let n = null;
|
|
160
160
|
for (let s = 0; s < e.length; s++) {
|
|
@@ -165,26 +165,26 @@ function le(e, t) {
|
|
|
165
165
|
return 0;
|
|
166
166
|
};
|
|
167
167
|
}
|
|
168
|
-
var
|
|
169
|
-
for (var n = o > 1 ? void 0 : o ?
|
|
168
|
+
var fe = Object.defineProperty, me = Object.getOwnPropertyDescriptor, ge = (e, t, r, o) => {
|
|
169
|
+
for (var n = o > 1 ? void 0 : o ? me(t, r) : t, s = e.length - 1, i; s >= 0; s--)
|
|
170
170
|
(i = e[s]) && (n = (o ? i(t, r, n) : i(n)) || n);
|
|
171
|
-
return o && n &&
|
|
172
|
-
},
|
|
173
|
-
let _ = class extends
|
|
171
|
+
return o && n && fe(t, r, n), n;
|
|
172
|
+
}, O = (e, t) => (r, o) => t(r, o, e);
|
|
173
|
+
let _ = class extends y {
|
|
174
174
|
constructor(e, t) {
|
|
175
175
|
super(), this._commandService = e, this._sortService = t, this._initCommands(), this._registerCompareFns();
|
|
176
176
|
}
|
|
177
177
|
_initCommands() {
|
|
178
178
|
[
|
|
179
|
-
|
|
179
|
+
$
|
|
180
180
|
].forEach((e) => this.disposeWithMe(this._commandService.registerCommand(e)));
|
|
181
181
|
}
|
|
182
182
|
_registerCompareFns() {
|
|
183
183
|
const e = (t, r, o) => {
|
|
184
184
|
const n = this._getCommonValue(r), s = this._getCommonValue(o), i = [
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
185
|
+
te,
|
|
186
|
+
ne,
|
|
187
|
+
re
|
|
188
188
|
];
|
|
189
189
|
for (let c = 0; c < i.length; c++) {
|
|
190
190
|
const u = i[c](n, s, t);
|
|
@@ -197,28 +197,32 @@ let _ = class extends E {
|
|
|
197
197
|
}
|
|
198
198
|
_getCommonValue(e) {
|
|
199
199
|
var r, o;
|
|
200
|
-
if (
|
|
200
|
+
if (M(e))
|
|
201
201
|
return null;
|
|
202
202
|
const t = (o = (r = e == null ? void 0 : e.p) == null ? void 0 : r.body) == null ? void 0 : o.dataStream;
|
|
203
203
|
return t || ((e == null ? void 0 : e.t) === v.NUMBER ? Number.parseFloat(`${e.v}`) : (e == null ? void 0 : e.t) === v.STRING ? typeof e.v == "number" ? e.v : `${e.v}` : (e == null ? void 0 : e.t) === v.BOOLEAN ? `${e.v}` : (e == null ? void 0 : e.t) === v.FORCE_STRING ? Number.parseFloat(`${e.v}`) : `${e == null ? void 0 : e.v}`);
|
|
204
204
|
}
|
|
205
205
|
};
|
|
206
|
-
_ =
|
|
207
|
-
|
|
208
|
-
|
|
206
|
+
_ = ge([
|
|
207
|
+
O(0, I),
|
|
208
|
+
O(1, w(g))
|
|
209
209
|
], _);
|
|
210
|
-
var
|
|
211
|
-
for (var n = o > 1 ? void 0 : o ?
|
|
210
|
+
var de = Object.defineProperty, he = Object.getOwnPropertyDescriptor, ve = (e, t, r, o) => {
|
|
211
|
+
for (var n = o > 1 ? void 0 : o ? he(t, r) : t, s = e.length - 1, i; s >= 0; s--)
|
|
212
212
|
(i = e[s]) && (n = (o ? i(t, r, n) : i(n)) || n);
|
|
213
|
-
return o && n &&
|
|
214
|
-
},
|
|
215
|
-
const
|
|
213
|
+
return o && n && de(t, r, n), n;
|
|
214
|
+
}, P = (e, t) => (r, o) => t(r, o, e);
|
|
215
|
+
const Ce = "SHEET_SORT_PLUGIN";
|
|
216
216
|
var C;
|
|
217
|
-
let
|
|
218
|
-
constructor(e =
|
|
217
|
+
let E = (C = class extends Y {
|
|
218
|
+
constructor(e = N, t, r) {
|
|
219
219
|
super(), this._config = e, this._injector = t, this._configService = r;
|
|
220
|
-
const { ...o } =
|
|
221
|
-
|
|
220
|
+
const { ...o } = z(
|
|
221
|
+
{},
|
|
222
|
+
N,
|
|
223
|
+
this._config
|
|
224
|
+
);
|
|
225
|
+
this._configService.setConfig(ee, o);
|
|
222
226
|
}
|
|
223
227
|
onStarting() {
|
|
224
228
|
[
|
|
@@ -229,14 +233,14 @@ let P = (C = class extends W {
|
|
|
229
233
|
onReady() {
|
|
230
234
|
this._injector.get(_);
|
|
231
235
|
}
|
|
232
|
-
}, h(C, "type",
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
],
|
|
236
|
+
}, h(C, "type", J.UNIVER_SHEET), h(C, "pluginName", Ce), C);
|
|
237
|
+
E = ve([
|
|
238
|
+
P(1, w(Q)),
|
|
239
|
+
P(2, X)
|
|
240
|
+
], E);
|
|
237
241
|
export {
|
|
238
242
|
g as SheetsSortService,
|
|
239
|
-
|
|
243
|
+
$ as SortRangeCommand,
|
|
240
244
|
f as SortType,
|
|
241
|
-
|
|
245
|
+
E as UniverSheetsSortPlugin
|
|
242
246
|
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { FRange } from '@univerjs/sheets/facade';
|
|
2
|
+
export type SortColumnSpec = {
|
|
3
|
+
column: number;
|
|
4
|
+
ascending: boolean;
|
|
5
|
+
} | number;
|
|
6
|
+
export interface IFRangeSort {
|
|
7
|
+
/**
|
|
8
|
+
* Sort the range by the specified column(s) with specified order.
|
|
9
|
+
*
|
|
10
|
+
* @async
|
|
11
|
+
*
|
|
12
|
+
* @return The range itself to chain other operations.
|
|
13
|
+
*/
|
|
14
|
+
sort(column: SortColumnSpec | SortColumnSpec[]): Promise<FRange>;
|
|
15
|
+
}
|
|
16
|
+
export declare class FRangeSort extends FRange implements IFRangeSort {
|
|
17
|
+
sort(column: SortColumnSpec | SortColumnSpec[]): Promise<FRange>;
|
|
18
|
+
}
|
|
19
|
+
declare module '@univerjs/sheets/facade' {
|
|
20
|
+
interface FRange extends IFRangeSort {
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { FWorksheet } from '@univerjs/sheets/facade';
|
|
2
|
+
export interface IFWorksheetSort {
|
|
3
|
+
/**
|
|
4
|
+
* Sort the worksheet by the specified column.
|
|
5
|
+
*
|
|
6
|
+
* @async
|
|
7
|
+
*
|
|
8
|
+
* @param colIndex The column index to sort by.
|
|
9
|
+
* @param asc Whether to sort in ascending order. Default is `true`.
|
|
10
|
+
* @returns The worksheet itself to chain calls.
|
|
11
|
+
*/
|
|
12
|
+
sort(colIndex: number, asc?: boolean): Promise<FWorksheet>;
|
|
13
|
+
}
|
|
14
|
+
export declare class FWorksheetSort extends FWorksheet implements IFWorksheetSort {
|
|
15
|
+
sort(colIndex: number, asc?: boolean): Promise<FWorksheet>;
|
|
16
|
+
}
|
|
17
|
+
declare module '@univerjs/sheets/facade' {
|
|
18
|
+
interface FWorksheet extends IFWorksheetSort {
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import './f-range';
|
|
17
|
+
import './f-worksheet';
|
|
18
|
+
export type * from './f-range';
|
|
19
|
+
export type * from './f-worksheet';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,t){typeof exports=="object"&&typeof module<"u"?t(require("@univerjs/sheets-sort"),require("@univerjs/sheets/facade"),require("@univerjs/core")):typeof define=="function"&&define.amd?define(["@univerjs/sheets-sort","@univerjs/sheets/facade","@univerjs/core"],t):(e=typeof globalThis<"u"?globalThis:e||self,t(e.UniverSheetsSort,e.UniverSheetsFacade,e.UniverCore))})(this,function(e,t,i){"use strict";class d extends t.FRange{async sort(r){const s=(Array.isArray(r)?r:[r]).map(n=>typeof n=="number"?{colIndex:n,type:e.SortType.ASC}:{colIndex:n.column,type:n.ascending?e.SortType.ASC:e.SortType.DESC});return await this._commandService.executeCommand(e.SortRangeCommand.id,{orderRules:s,range:this._range,hasTitle:!1,unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId()}),this}}t.FRange.extend(d);class u extends t.FWorksheet{async sort(r,o=!0){const s=[{colIndex:r,type:o?e.SortType.ASC:e.SortType.DESC}],n={startRow:0,startColumn:0,endRow:this._worksheet.getRowCount()-1,endColumn:this._worksheet.getColumnCount()-1,rangeType:i.RANGE_TYPE.ALL};return await this._commandService.executeCommand(e.SortRangeCommand.id,{orderRules:s,range:n,hasTitle:!1,unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId()}),this}}t.FWorksheet.extend(u)});
|
package/lib/umd/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(l,i){typeof exports=="object"&&typeof module<"u"?i(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"],i):(l=typeof globalThis<"u"?globalThis:l||self,i(l.UniverSheetsSort={},l.UniverCore,l.UniverSheets,l.UniverEngineFormula))})(this,function(l,i,g,
|
|
1
|
+
(function(l,i){typeof exports=="object"&&typeof module<"u"?i(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"],i):(l=typeof globalThis<"u"?globalThis:l||self,i(l.UniverSheetsSort={},l.UniverCore,l.UniverSheets,l.UniverEngineFormula))})(this,function(l,i,g,j){"use strict";var ne=Object.defineProperty;var re=(l,i,g)=>i in l?ne(l,i,{enumerable:!0,configurable:!0,writable:!0,value:g}):l[i]=g;var _=(l,i,g)=>re(l,typeof i!="symbol"?i+"":i,g);var C;const D="sheets-sort.config",b={};var S=(e=>(e.DESC="desc",e.ASC="asc",e))(S||{});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===S.ASC?-1:1:e>s?t===S.ASC?1:-1:0:r?t===S.ASC?1:-1:n?t===S.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 o=e,u=s;return o<u?t===S.ASC?-1:1:o>u?t===S.ASC?1:-1:0}return r?t===S.ASC?1:-1:n?t===S.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,o=e.length-1,u;o>=0;o--)(u=e[o])&&(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 i.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:o}=t,u=(a=this._univerInstanceService.getUnit(r))==null?void 0:a.getSheetBySheetId(n);if(!u)return!1;const c=u.getMergeData().filter(f=>i.Rectangle.contains(o,f));return c.length===0?!0:L(o,c)}emptyCheck(t){var c;const{unitId:r,subUnitId:n,range:o}=t,u=(c=this._univerInstanceService.getUnit(r))==null?void 0:c.getSheetBySheetId(n);if(!u)return!1;for(let a=o.startRow;a<=o.endRow;a++)for(let f=o.startColumn;f<=o.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:o}=t,u=(a=(c=this._formulaDataModel.getArrayFormulaRange())==null?void 0:c[r])==null?void 0:a[n];for(const f in u){const d=u[Number(f)];for(const I in d){const v=d[Number(I)];if(v&&i.Rectangle.intersects(o,v))return!1}}return!0}registerCompareFn(t){this._compareFns.unshift(t)}getAllCompareFns(){return this._compareFns}applySort(t,r,n){var c;const{unitId:o,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||o,subUnitId:n||u})}},l.SheetsSortService=V([p(0,i.IUniverInstanceService),p(1,i.ICommandService),p(2,i.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,o=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,d=a.endColumn-a.startColumn+1;if(n===null&&o===null)n=f,o=d;else if(f!==n||d!==o)return!1;c+=f*d}return c===u}const R={id:"sheet.command.sort-range",type:i.CommandType.COMMAND,handler:(e,s)=>{const{range:t,orderRules:r,hasTitle:n,unitId:o,subUnitId:u}=s,c=e.get(l.SheetsSortService),a=e.get(i.IUniverInstanceService),{worksheet:f}=g.getSheetCommandTarget(a,s)||{};if(!f)return!1;const d=f.getMergeData().filter(m=>i.Rectangle.contains(t,m)),I=d.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&&(d.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:oe},te)=>{T[F[te]]=m});const k={id:g.ReorderRangeCommand.id,params:{unitId:o,subUnitId:u,range:t,order:T}},ee=e.get(i.ICommandService);return i.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 o=e[n](s,t,r);if(o!=null)return o}return 0}}function B(e,s){return function(t,r){let n=null;for(let o=0;o<e.length;o++){const u=t.value[o],c=r.value[o];if(n=s(e[o].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,o=e.length-1,u;o>=0;o--)(u=e[o])&&(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 i.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),o=this._getCommonValue(r),u=[N,E,O];for(let c=0;c<u.length;c++){const a=u[c](n,o,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)===i.CellValueType.NUMBER?Number.parseFloat(`${e.v}`):(e==null?void 0:e.t)===i.CellValueType.STRING?typeof e.v=="number"?e.v:`${e.v}`:(e==null?void 0:e.t)===i.CellValueType.BOOLEAN?`${e.v}`:(e==null?void 0:e.t)===i.CellValueType.FORCE_STRING?Number.parseFloat(`${e.v}`):`${e==null?void 0:e.v}`)}};h=K([P(0,i.ICommandService),P(1,i.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,o=e.length-1,u;o>=0;o--)(u=e[o])&&(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 i.Plugin{constructor(s=b,t,r){super(),this._config=s,this._injector=t,this._configService=r;const{...n}=i.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",i.UniverInstanceType.UNIVER_SHEET),_(C,"pluginName",J),C),l.UniverSheetsSortPlugin=z([U(1,i.Inject(i.Injector)),U(2,i.IConfigService)],l.UniverSheetsSortPlugin),l.SortRangeCommand=R,l.SortType=S,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs/sheets-sort",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "A library for sorting data in Univer Sheet",
|
|
6
6
|
"author": "DreamNum <developer@univer.ai>",
|
|
@@ -34,6 +34,11 @@
|
|
|
34
34
|
"require": "./lib/cjs/locale/*.js",
|
|
35
35
|
"types": "./lib/types/locale/*.d.ts"
|
|
36
36
|
},
|
|
37
|
+
"./facade": {
|
|
38
|
+
"import": "./lib/es/facade.js",
|
|
39
|
+
"require": "./lib/cjs/facade.js",
|
|
40
|
+
"types": "./lib/types/facade/index.d.ts"
|
|
41
|
+
},
|
|
37
42
|
"./lib/*": "./lib/*"
|
|
38
43
|
},
|
|
39
44
|
"main": "./lib/es/index.js",
|
|
@@ -48,15 +53,15 @@
|
|
|
48
53
|
"lib"
|
|
49
54
|
],
|
|
50
55
|
"dependencies": {
|
|
51
|
-
"@univerjs/core": "0.5.
|
|
52
|
-
"@univerjs/
|
|
53
|
-
"@univerjs/
|
|
56
|
+
"@univerjs/core": "0.5.2",
|
|
57
|
+
"@univerjs/engine-formula": "0.5.2",
|
|
58
|
+
"@univerjs/sheets": "0.5.2"
|
|
54
59
|
},
|
|
55
60
|
"devDependencies": {
|
|
56
61
|
"typescript": "^5.7.2",
|
|
57
|
-
"vite": "^6.0.
|
|
58
|
-
"vitest": "^2.1.
|
|
59
|
-
"@univerjs-infra/shared": "0.5.
|
|
62
|
+
"vite": "^6.0.3",
|
|
63
|
+
"vitest": "^2.1.8",
|
|
64
|
+
"@univerjs-infra/shared": "0.5.2"
|
|
60
65
|
},
|
|
61
66
|
"space": {
|
|
62
67
|
".": {
|
|
@@ -74,6 +79,11 @@
|
|
|
74
79
|
"require": "./lib/cjs/locale/*.js",
|
|
75
80
|
"types": "./lib/types/locale/*.d.ts"
|
|
76
81
|
},
|
|
82
|
+
"./facade": {
|
|
83
|
+
"import": "./lib/es/facade.js",
|
|
84
|
+
"require": "./lib/cjs/facade.js",
|
|
85
|
+
"types": "./lib/types/facade/index.d.ts"
|
|
86
|
+
},
|
|
77
87
|
"./lib/*": "./lib/*"
|
|
78
88
|
},
|
|
79
89
|
"scripts": {
|