-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
sheets-sort-ui.service.tsx
197 lines (177 loc) · 6.71 KB
/
sheets-sort-ui.service.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { IRange, Nullable, Workbook } from '@univerjs/core';
import { Disposable, ICommandService,
ILogService,
IUniverInstanceService,
LifecycleStages,
LocaleService,
OnLifecycle,
UniverInstanceType,
} from '@univerjs/core';
import { SelectionManagerService } from '@univerjs/sheets';
import { Inject } from '@wendellhu/redi';
import type { ISheetRangeLocation } from '@univerjs/sheets-ui';
import { expandToContinuousRange } from '@univerjs/sheets-ui';
import type { ISortOption } from '@univerjs/sheets-sort';
import { SheetsSortService, SortType } from '@univerjs/sheets-sort';
import React from 'react';
import { IConfirmService } from '@univerjs/ui';
import { BehaviorSubject } from 'rxjs';
import { ExtendConfirm } from '../views/ExtendConfirm';
export enum EXTEND_TYPE {
KEEP = 'keep',
EXTEND = 'extend',
CANCEL = 'cancel',
}
export interface ICustomSortState {
range?: IRange;
show: boolean;
}
export interface ISheetSortLocation extends ISheetRangeLocation {
colIndex: number;
}
@OnLifecycle(LifecycleStages.Ready, SheetsSortService)
export class SheetsSortUIService extends Disposable {
private readonly _customSortState$ = new BehaviorSubject<Nullable<ICustomSortState>>(null);
readonly customSortState$ = this._customSortState$.asObservable();
constructor(
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@IConfirmService private readonly _confirmService: IConfirmService,
@ILogService private readonly _logService: ILogService,
@Inject(SelectionManagerService) private readonly _selectionManagerService: SelectionManagerService,
@Inject(SheetsSortService) private readonly _sheetsSortService: SheetsSortService,
@Inject(LocaleService) private readonly _localeService: LocaleService,
@ICommandService private readonly _commandService: ICommandService) {
super();
}
async triggerSortDirectly(asc: boolean, extend: boolean, sheetRangeLocation?: ISheetSortLocation): Promise<boolean> {
const location = sheetRangeLocation || await this._detectSortLocation(extend);
if (!location) {
return false;
}
const mergeCheck = this._sheetsSortService.mergeCheck(location);
if (!mergeCheck) {
this.showMergeError();
return false;
}
const sortOption: ISortOption = {
orderRules: [{
type: asc ? SortType.ASC : SortType.DESC,
colIndex: location.colIndex,
}],
range: location.range,
};
this._sheetsSortService.applySort(sortOption, location.unitId, location.subUnitId);
return true;
}
async triggerSortCustomize() {
const location = await this._detectSortLocation();
if (!location) {
return false;
}
const mergeCheck = this._sheetsSortService.mergeCheck(location);
if (!mergeCheck) {
this.showMergeError();
return false;
}
// open customize dialog
this.showCustomSortPanel(location);
// if (!sortOption) {
// return false;
// }
// this._sheetsSortService.applySort(sortOption, location.unitId, location.subUnitId);
// return true;
return true;
}
customSortState() {
return this._customSortState$.getValue();
}
private async _detectSortLocation(extend?: boolean): Promise<Nullable<ISheetSortLocation >> {
const workbook = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SHEET) as Workbook;
const worksheet = workbook.getActiveSheet();
const selection = this._selectionManagerService.getLast();
if (!selection) {
return null;
}
let range;
if (extend === true) {
range = expandToContinuousRange(selection.range, { up: true, down: true, left: true, right: true }, worksheet);
} else if (extend === false) {
range = selection.range;
} else {
const confirmRes = await this.showExtendConfirm();
if (confirmRes === EXTEND_TYPE.CANCEL) {
return null;
}
if (confirmRes === EXTEND_TYPE.KEEP) {
range = selection.range;
} else {
range = expandToContinuousRange(selection.range, { up: true, down: true, left: true, right: true }, worksheet);
}
}
const primary = this._selectionManagerService.getLast()?.primary;
if (!primary) {
return null;
}
return {
range,
unitId: workbook.getUnitId(),
subUnitId: worksheet.getSheetId(),
colIndex: primary.actualColumn,
};
}
async showMergeError(): Promise<boolean> {
return await this._confirmService.confirm({
id: 'sort-range-merge-error',
title: {
title: this._localeService.t('info.tooltip'),
},
children: {
title: <div>{this._localeService.t('sheets-sort.error.merge-size')}</div>,
},
});
}
async showExtendConfirm(): Promise<EXTEND_TYPE> {
let shouldExtend = false;
const confirm = await this._confirmService.confirm({
id: 'extend-sort-range-dialog',
title: {
title: this._localeService.t('sheets-sort.dialog.sort-reminder'),
},
children: {
title: (
<ExtendConfirm
onChange={(value: string) => {
shouldExtend = value === '1';
}}
/>
),
},
width: 400,
});
if (confirm) {
return shouldExtend ? EXTEND_TYPE.EXTEND : EXTEND_TYPE.KEEP;
}
return EXTEND_TYPE.CANCEL;
}
showCustomSortPanel(location: ISheetSortLocation) {
this._customSortState$.next({ range: location.range, show: true });
}
closeCustomSortPanel() {
this._customSortState$.next({ show: false });
}
}