@univerjs/sheets-sort 0.5.2 → 0.5.3
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 -1
- package/lib/es/facade.js +69 -22
- package/lib/types/facade/f-event.d.ts +59 -0
- package/lib/types/facade/f-range.d.ts +15 -5
- package/lib/types/facade/f-worksheet.d.ts +12 -6
- package/lib/types/facade/index.d.ts +2 -0
- package/lib/umd/facade.js +1 -1
- package/package.json +6 -29
package/lib/cjs/facade.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const
|
|
1
|
+
"use strict";const r=require("@univerjs/sheets-sort"),h=require("@univerjs/sheets/facade"),c=require("@univerjs/core");class l extends h.FRange{sort(o){const e=this._range.startColumn,s=(Array.isArray(o)?o:[o]).map(n=>typeof n=="number"?{colIndex:n+e,type:r.SortType.ASC}:{colIndex:n.column+e,type:n.ascending?r.SortType.ASC:r.SortType.DESC});return this._commandService.syncExecuteCommand(r.SortRangeCommand.id,{orderRules:s,range:this._range,hasTitle:!1,unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId()}),this}}h.FRange.extend(l);class R extends h.FWorksheet{sort(o,e=!0){const t=[{colIndex:o,type:e?r.SortType.ASC:r.SortType.DESC}],s={startRow:0,startColumn:0,endRow:this._worksheet.getRowCount()-1,endColumn:this._worksheet.getColumnCount()-1,rangeType:c.RANGE_TYPE.ALL};return this._commandService.syncExecuteCommand(r.SortRangeCommand.id,{orderRules:t,range:s,hasTitle:!1,unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId()}),this}}h.FWorksheet.extend(R);c.FEventName.extend(h.FSheetEventName);class C extends c.FUniver{_initialize(o){const e=o.get(c.ICommandService);this.disposeWithMe(e.beforeCommandExecuted(t=>{switch(t.id){case r.SortRangeCommand.id:this._beforeRangeSort(t);break}})),this.disposeWithMe(e.onCommandExecuted(t=>{switch(t.id){case r.SortRangeCommand.id:this._onRangeSorted(t);break}}))}_beforeRangeSort(o){const e=o.params,t=this.getUniverSheet(e.unitId),s=t.getSheetBySheetId(e.subUnitId),{startColumn:n,endColumn:m,startRow:a,endRow:S}=e.range,g=s.getRange(a,n,S-a+1,m-n+1),i={workbook:t,worksheet:s,range:g,sortColumn:e.orderRules.map(d=>({column:d.colIndex-n,ascending:d.type===r.SortType.ASC}))};if(this.fireEvent(this.Event.SheetBeforeRangeSort,i),i.cancel)throw new Error("SortRangeCommand canceled.")}_onRangeSorted(o){const e=o.params,t=this.getUniverSheet(e.unitId),s=t.getSheetBySheetId(e.subUnitId),{startColumn:n,endColumn:m,startRow:a,endRow:S}=e.range,g=s.getRange(a,n,S-a+1,m-n+1),i={workbook:t,worksheet:s,range:g,sortColumn:e.orderRules.map(d=>({column:d.colIndex-n,ascending:d.type===r.SortType.ASC}))};if(this.fireEvent(this.Event.SheetRangeSorted,i),i.cancel)throw new Error("SortRangeCommand canceled.")}}c.FUniver.extend(C);
|
package/lib/es/facade.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { SortType as
|
|
2
|
-
import { FRange as
|
|
3
|
-
import { RANGE_TYPE as
|
|
4
|
-
class
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
colIndex:
|
|
8
|
-
type:
|
|
1
|
+
import { SortType as r, SortRangeCommand as c } from "@univerjs/sheets-sort";
|
|
2
|
+
import { FRange as S, FWorksheet as l, FSheetEventName as R } from "@univerjs/sheets/facade";
|
|
3
|
+
import { RANGE_TYPE as k, FEventName as w, FUniver as C, ICommandService as p } from "@univerjs/core";
|
|
4
|
+
class f extends S {
|
|
5
|
+
sort(o) {
|
|
6
|
+
const e = this._range.startColumn, s = (Array.isArray(o) ? o : [o]).map((n) => typeof n == "number" ? { colIndex: n + e, type: r.ASC } : {
|
|
7
|
+
colIndex: n.column + e,
|
|
8
|
+
type: n.ascending ? r.ASC : r.DESC
|
|
9
9
|
});
|
|
10
|
-
return
|
|
11
|
-
orderRules:
|
|
10
|
+
return this._commandService.syncExecuteCommand(c.id, {
|
|
11
|
+
orderRules: s,
|
|
12
12
|
range: this._range,
|
|
13
13
|
hasTitle: !1,
|
|
14
14
|
unitId: this._workbook.getUnitId(),
|
|
@@ -16,26 +16,73 @@ class h extends i {
|
|
|
16
16
|
}), this;
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
-
|
|
20
|
-
class
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
colIndex
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
S.extend(f);
|
|
20
|
+
class v extends l {
|
|
21
|
+
sort(o, e = !0) {
|
|
22
|
+
const t = [{
|
|
23
|
+
// real column index should be colIndex - 1.
|
|
24
|
+
colIndex: o,
|
|
25
|
+
type: e ? r.ASC : r.DESC
|
|
26
|
+
}], s = {
|
|
26
27
|
startRow: 0,
|
|
27
28
|
startColumn: 0,
|
|
28
29
|
endRow: this._worksheet.getRowCount() - 1,
|
|
29
30
|
endColumn: this._worksheet.getColumnCount() - 1,
|
|
30
|
-
rangeType:
|
|
31
|
+
rangeType: k.ALL
|
|
31
32
|
};
|
|
32
|
-
return
|
|
33
|
-
orderRules:
|
|
34
|
-
range:
|
|
33
|
+
return this._commandService.syncExecuteCommand(c.id, {
|
|
34
|
+
orderRules: t,
|
|
35
|
+
range: s,
|
|
35
36
|
hasTitle: !1,
|
|
36
37
|
unitId: this._workbook.getUnitId(),
|
|
37
38
|
subUnitId: this._worksheet.getSheetId()
|
|
38
39
|
}), this;
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
|
-
|
|
42
|
+
l.extend(v);
|
|
43
|
+
w.extend(R);
|
|
44
|
+
class E extends C {
|
|
45
|
+
_initialize(o) {
|
|
46
|
+
const e = o.get(p);
|
|
47
|
+
this.disposeWithMe(e.beforeCommandExecuted((t) => {
|
|
48
|
+
switch (t.id) {
|
|
49
|
+
case c.id:
|
|
50
|
+
this._beforeRangeSort(t);
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
})), this.disposeWithMe(e.onCommandExecuted((t) => {
|
|
54
|
+
switch (t.id) {
|
|
55
|
+
case c.id:
|
|
56
|
+
this._onRangeSorted(t);
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
_beforeRangeSort(o) {
|
|
62
|
+
const e = o.params, t = this.getUniverSheet(e.unitId), s = t.getSheetBySheetId(e.subUnitId), { startColumn: n, endColumn: h, startRow: a, endRow: m } = e.range, g = s.getRange(a, n, m - a + 1, h - n + 1), i = {
|
|
63
|
+
workbook: t,
|
|
64
|
+
worksheet: s,
|
|
65
|
+
range: g,
|
|
66
|
+
sortColumn: e.orderRules.map((d) => ({
|
|
67
|
+
column: d.colIndex - n,
|
|
68
|
+
ascending: d.type === r.ASC
|
|
69
|
+
}))
|
|
70
|
+
};
|
|
71
|
+
if (this.fireEvent(this.Event.SheetBeforeRangeSort, i), i.cancel)
|
|
72
|
+
throw new Error("SortRangeCommand canceled.");
|
|
73
|
+
}
|
|
74
|
+
_onRangeSorted(o) {
|
|
75
|
+
const e = o.params, t = this.getUniverSheet(e.unitId), s = t.getSheetBySheetId(e.subUnitId), { startColumn: n, endColumn: h, startRow: a, endRow: m } = e.range, g = s.getRange(a, n, m - a + 1, h - n + 1), i = {
|
|
76
|
+
workbook: t,
|
|
77
|
+
worksheet: s,
|
|
78
|
+
range: g,
|
|
79
|
+
sortColumn: e.orderRules.map((d) => ({
|
|
80
|
+
column: d.colIndex - n,
|
|
81
|
+
ascending: d.type === r.ASC
|
|
82
|
+
}))
|
|
83
|
+
};
|
|
84
|
+
if (this.fireEvent(this.Event.SheetRangeSorted, i), i.cancel)
|
|
85
|
+
throw new Error("SortRangeCommand canceled.");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
C.extend(E);
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { IEventBase, FEventName } from '@univerjs/core';
|
|
2
|
+
import { FRange, FWorkbook, FWorksheet } from '@univerjs/sheets/facade';
|
|
3
|
+
export interface IFSheetSortEventMixin {
|
|
4
|
+
/**
|
|
5
|
+
* This event will be emitted when a range on a worksheet is sorted.
|
|
6
|
+
* Type of the event is {@link ISheetRangeSortedParams}.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const callbackDisposable = univerAPI.addEvent(univerAPI.Event.SheetRangeSorted, (params) => {
|
|
11
|
+
* const { workbook, worksheet, sortColumn } = params;
|
|
12
|
+
* sortColumn.forEach((col) => {
|
|
13
|
+
* console.log(col.column, col.ascending);
|
|
14
|
+
* });
|
|
15
|
+
* });
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
SheetRangeSorted: 'SheetRangeSorted';
|
|
19
|
+
/**
|
|
20
|
+
* This event will be emitted before sorting a range on a worksheet.
|
|
21
|
+
* Type of the event is {@link ISheetRangeSortParams}.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const callbackDisposable = univerAPI.addEvent(univerAPI.Event.SheetBeforeRangeSort, (params) => {
|
|
26
|
+
* const { workbook, worksheet, sortColumn } = params;
|
|
27
|
+
* sortColumn.forEach((col) => {
|
|
28
|
+
* console.log(col.column, col.ascending);
|
|
29
|
+
* });
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
SheetBeforeRangeSort: 'SheetBeforeRangeSort';
|
|
34
|
+
}
|
|
35
|
+
export declare class FSheetSortEventName extends FEventName implements IFSheetSortEventMixin {
|
|
36
|
+
readonly SheetRangeSorted: "SheetRangeSorted";
|
|
37
|
+
readonly SheetBeforeRangeSort: "SheetBeforeRangeSort";
|
|
38
|
+
}
|
|
39
|
+
interface ISortColumn {
|
|
40
|
+
column: number;
|
|
41
|
+
ascending: boolean;
|
|
42
|
+
}
|
|
43
|
+
export interface ISheetRangeSortParams extends IEventBase {
|
|
44
|
+
workbook: FWorkbook;
|
|
45
|
+
worksheet: FWorksheet;
|
|
46
|
+
range: FRange;
|
|
47
|
+
sortColumn: ISortColumn[];
|
|
48
|
+
}
|
|
49
|
+
export interface ISheetRangeSortEventParamConfig {
|
|
50
|
+
SheetBeforeRangeSort: ISheetRangeSortParams;
|
|
51
|
+
SheetRangeSorted: ISheetRangeSortParams;
|
|
52
|
+
}
|
|
53
|
+
declare module '@univerjs/core' {
|
|
54
|
+
interface FEventName extends IFSheetSortEventMixin {
|
|
55
|
+
}
|
|
56
|
+
interface IEventParamConfig extends ISheetRangeSortEventParamConfig {
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export {};
|
|
@@ -5,16 +5,26 @@ export type SortColumnSpec = {
|
|
|
5
5
|
} | number;
|
|
6
6
|
export interface IFRangeSort {
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* Sorts the cells in the given range, by column(s) and order specified.
|
|
9
9
|
*
|
|
10
|
-
* @
|
|
10
|
+
* @param {SortColumnSpec | SortColumnSpec[]} column The column index with order or an array of column indexes with order. The column index starts from 1.
|
|
11
11
|
*
|
|
12
|
-
* @
|
|
12
|
+
* @returns The range itself for chaining.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const activeSpreadsheet = univerAPI.getActiveWorkbook();
|
|
17
|
+
* const activeSheet = activeSpreadsheet.getActiveSheet();
|
|
18
|
+
* const range = activeSheet.getRange(0, 0, 10, 10);
|
|
19
|
+
* range.sort(1); // Sorts the range by the first column in ascending order.
|
|
20
|
+
* range.sort({ column: 1, ascending: false }); // Sorts the range by the first column in descending order.
|
|
21
|
+
* range.sort([{ column: 1, ascending: false }, 2]); // Sorts the range by the first column in descending order and the second column in ascending order.
|
|
22
|
+
* ```
|
|
13
23
|
*/
|
|
14
|
-
sort(column: SortColumnSpec | SortColumnSpec[]):
|
|
24
|
+
sort(column: SortColumnSpec | SortColumnSpec[]): FRange;
|
|
15
25
|
}
|
|
16
26
|
export declare class FRangeSort extends FRange implements IFRangeSort {
|
|
17
|
-
sort(column: SortColumnSpec | SortColumnSpec[]):
|
|
27
|
+
sort(column: SortColumnSpec | SortColumnSpec[]): FRange;
|
|
18
28
|
}
|
|
19
29
|
declare module '@univerjs/sheets/facade' {
|
|
20
30
|
interface FRange extends IFRangeSort {
|
|
@@ -3,16 +3,22 @@ export interface IFWorksheetSort {
|
|
|
3
3
|
/**
|
|
4
4
|
* Sort the worksheet by the specified column.
|
|
5
5
|
*
|
|
6
|
-
* @
|
|
6
|
+
* @param {number} colIndex The column index to sort by. which starts from 1.
|
|
7
|
+
* @param {boolean} [asc=true] The sort order. `true` for ascending, `false` for descending.
|
|
8
|
+
* @returns The worksheet itself for chaining.
|
|
7
9
|
*
|
|
8
|
-
* @
|
|
9
|
-
*
|
|
10
|
-
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const activeSpreadsheet = univerAPI.getActiveWorkbook();
|
|
13
|
+
* const activeSheet = activeSpreadsheet.getActiveSheet();
|
|
14
|
+
* activeSheet.sort(1); // Sorts the worksheet by the first column in ascending order.
|
|
15
|
+
* activeSheet.sort(1, false); // Sorts the worksheet by the first column in descending order.
|
|
16
|
+
* ```
|
|
11
17
|
*/
|
|
12
|
-
sort(colIndex: number, asc?: boolean):
|
|
18
|
+
sort(colIndex: number, asc?: boolean): FWorksheet;
|
|
13
19
|
}
|
|
14
20
|
export declare class FWorksheetSort extends FWorksheet implements IFWorksheetSort {
|
|
15
|
-
sort(colIndex: number, asc?: boolean):
|
|
21
|
+
sort(colIndex: number, asc?: boolean): FWorksheet;
|
|
16
22
|
}
|
|
17
23
|
declare module '@univerjs/sheets/facade' {
|
|
18
24
|
interface FWorksheet extends IFWorksheetSort {
|
package/lib/umd/facade.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(e,
|
|
1
|
+
(function(e,s){typeof exports=="object"&&typeof module<"u"?s(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"],s):(e=typeof globalThis<"u"?globalThis:e||self,s(e.UniverSheetsSort,e.UniverSheetsFacade,e.UniverCore))})(this,function(e,s,a){"use strict";class C extends s.FRange{sort(r){const t=this._range.startColumn,i=(Array.isArray(r)?r:[r]).map(o=>typeof o=="number"?{colIndex:o+t,type:e.SortType.ASC}:{colIndex:o.column+t,type:o.ascending?e.SortType.ASC:e.SortType.DESC});return this._commandService.syncExecuteCommand(e.SortRangeCommand.id,{orderRules:i,range:this._range,hasTitle:!1,unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId()}),this}}s.FRange.extend(C);class R extends s.FWorksheet{sort(r,t=!0){const n=[{colIndex:r,type:t?e.SortType.ASC:e.SortType.DESC}],i={startRow:0,startColumn:0,endRow:this._worksheet.getRowCount()-1,endColumn:this._worksheet.getColumnCount()-1,rangeType:a.RANGE_TYPE.ALL};return this._commandService.syncExecuteCommand(e.SortRangeCommand.id,{orderRules:n,range:i,hasTitle:!1,unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId()}),this}}s.FWorksheet.extend(R),a.FEventName.extend(s.FSheetEventName);class l extends a.FUniver{_initialize(r){const t=r.get(a.ICommandService);this.disposeWithMe(t.beforeCommandExecuted(n=>{switch(n.id){case e.SortRangeCommand.id:this._beforeRangeSort(n);break}})),this.disposeWithMe(t.onCommandExecuted(n=>{switch(n.id){case e.SortRangeCommand.id:this._onRangeSorted(n);break}}))}_beforeRangeSort(r){const t=r.params,n=this.getUniverSheet(t.unitId),i=n.getSheetBySheetId(t.subUnitId),{startColumn:o,endColumn:m,startRow:d,endRow:u}=t.range,g=i.getRange(d,o,u-d+1,m-o+1),c={workbook:n,worksheet:i,range:g,sortColumn:t.orderRules.map(h=>({column:h.colIndex-o,ascending:h.type===e.SortType.ASC}))};if(this.fireEvent(this.Event.SheetBeforeRangeSort,c),c.cancel)throw new Error("SortRangeCommand canceled.")}_onRangeSorted(r){const t=r.params,n=this.getUniverSheet(t.unitId),i=n.getSheetBySheetId(t.subUnitId),{startColumn:o,endColumn:m,startRow:d,endRow:u}=t.range,g=i.getRange(d,o,u-d+1,m-o+1),c={workbook:n,worksheet:i,range:g,sortColumn:t.orderRules.map(h=>({column:h.colIndex-o,ascending:h.type===e.SortType.ASC}))};if(this.fireEvent(this.Event.SheetRangeSorted,c),c.cancel)throw new Error("SortRangeCommand canceled.")}}a.FUniver.extend(l)});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs/sheets-sort",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "A library for sorting data in Univer Sheet",
|
|
6
6
|
"author": "DreamNum <developer@univer.ai>",
|
|
@@ -53,38 +53,15 @@
|
|
|
53
53
|
"lib"
|
|
54
54
|
],
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@univerjs/core": "0.5.
|
|
57
|
-
"@univerjs/
|
|
58
|
-
"@univerjs/
|
|
56
|
+
"@univerjs/core": "0.5.3",
|
|
57
|
+
"@univerjs/sheets": "0.5.3",
|
|
58
|
+
"@univerjs/engine-formula": "0.5.3"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
61
|
"typescript": "^5.7.2",
|
|
62
|
-
"vite": "^6.0.
|
|
62
|
+
"vite": "^6.0.6",
|
|
63
63
|
"vitest": "^2.1.8",
|
|
64
|
-
"@univerjs-infra/shared": "0.5.
|
|
65
|
-
},
|
|
66
|
-
"space": {
|
|
67
|
-
".": {
|
|
68
|
-
"import": "./lib/es/index.js",
|
|
69
|
-
"require": "./lib/cjs/index.js",
|
|
70
|
-
"types": "./lib/types/index.d.ts"
|
|
71
|
-
},
|
|
72
|
-
"./*": {
|
|
73
|
-
"import": "./lib/es/*",
|
|
74
|
-
"require": "./lib/cjs/*",
|
|
75
|
-
"types": "./lib/types/index.d.ts"
|
|
76
|
-
},
|
|
77
|
-
"./locale/*": {
|
|
78
|
-
"import": "./lib/es/locale/*.js",
|
|
79
|
-
"require": "./lib/cjs/locale/*.js",
|
|
80
|
-
"types": "./lib/types/locale/*.d.ts"
|
|
81
|
-
},
|
|
82
|
-
"./facade": {
|
|
83
|
-
"import": "./lib/es/facade.js",
|
|
84
|
-
"require": "./lib/cjs/facade.js",
|
|
85
|
-
"types": "./lib/types/facade/index.d.ts"
|
|
86
|
-
},
|
|
87
|
-
"./lib/*": "./lib/*"
|
|
64
|
+
"@univerjs-infra/shared": "0.5.3"
|
|
88
65
|
},
|
|
89
66
|
"scripts": {
|
|
90
67
|
"test": "vitest run",
|