-
-
Notifications
You must be signed in to change notification settings - Fork 458
/
formula-clipboard.controller.ts
234 lines (205 loc) · 8.52 KB
/
formula-clipboard.controller.ts
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* 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 { ICellData, IMutationInfo, Workbook } from '@univerjs/core';
import {
Disposable,
isFormulaId,
isFormulaString,
IUniverInstanceService,
LifecycleStages,
ObjectMatrix,
OnLifecycle,
Tools,
UniverInstanceType,
} from '@univerjs/core';
import { FormulaDataModel, LexerTreeBuilder } from '@univerjs/engine-formula';
import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '@univerjs/sheets';
import { COPY_TYPE, ISheetClipboardService, PREDEFINED_HOOK_NAME } from '@univerjs/sheets-ui';
import type { ICellDataWithSpanInfo, ICopyPastePayload, IDiscreteRange, ISheetClipboardHook, ISheetDiscreteRangeLocation } from '@univerjs/sheets-ui';
import type { IAccessor } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';
import { SPECIAL_PASTE_FORMULA } from '../commands/commands/formula-clipboard.command';
export const DEFAULT_PASTE_FORMULA = 'default-paste-formula';
@OnLifecycle(LifecycleStages.Ready, FormulaClipboardController)
export class FormulaClipboardController extends Disposable {
constructor(
@IUniverInstanceService private readonly _currentUniverSheet: IUniverInstanceService,
@Inject(LexerTreeBuilder) private readonly _lexerTreeBuilder: LexerTreeBuilder,
@ISheetClipboardService private readonly _sheetClipboardService: ISheetClipboardService,
@Inject(Injector) private readonly _injector: Injector,
@Inject(FormulaDataModel) private readonly _formulaDataModel: FormulaDataModel
) {
super();
this._initialize();
}
private _initialize(): void {
this._registerClipboardHook();
}
private _registerClipboardHook() {
this.disposeWithMe(this._sheetClipboardService.addClipboardHook(this._pasteFormulaHook()));
this.disposeWithMe(this._sheetClipboardService.addClipboardHook(this._pasteWithFormulaHook()));
}
private _pasteFormulaHook(): ISheetClipboardHook {
return {
id: SPECIAL_PASTE_FORMULA,
priority: 10,
specialPasteInfo: { label: 'specialPaste.formula' },
onPasteCells: (pasteFrom, pasteTo, data, payload) => this._onPasteCells(pasteFrom, pasteTo, data, payload, true),
};
}
private _pasteWithFormulaHook(): ISheetClipboardHook {
return {
id: DEFAULT_PASTE_FORMULA,
priority: 10,
onPasteCells: (pasteFrom, pasteTo, data, payload) => this._onPasteCells(pasteFrom, pasteTo, data, payload, false),
};
}
private _onPasteCells(
pasteFrom: ISheetDiscreteRangeLocation | null,
pasteTo: ISheetDiscreteRangeLocation,
data: ObjectMatrix<ICellDataWithSpanInfo>,
payload: ICopyPastePayload,
isSpecialPaste: boolean
) {
const copyInfo = {
copyType: payload.copyType || COPY_TYPE.COPY,
copyRange: pasteFrom?.range,
pasteType: payload.pasteType,
};
const pastedRange = pasteTo.range;
const matrix = data;
const workbook = this._currentUniverSheet.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
const unitId = pasteTo.unitId || workbook.getUnitId();
const subUnitId = pasteTo.subUnitId || workbook.getActiveSheet().getSheetId();
return this._injector.invoke((accessor) => getSetCellFormulaMutations(
unitId,
subUnitId,
pastedRange,
matrix,
accessor,
copyInfo,
this._lexerTreeBuilder,
this._formulaDataModel,
isSpecialPaste,
pasteFrom
));
}
}
// eslint-disable-next-line max-lines-per-function
export function getSetCellFormulaMutations(
unitId: string,
subUnitId: string,
range: IDiscreteRange,
matrix: ObjectMatrix<ICellDataWithSpanInfo>,
accessor: IAccessor,
copyInfo: {
copyType: COPY_TYPE;
copyRange?: IDiscreteRange;
pasteType: string;
},
lexerTreeBuilder: LexerTreeBuilder,
formulaDataModel: FormulaDataModel,
isSpecialPaste = false,
pasteFrom: ISheetDiscreteRangeLocation | null
) {
const redoMutationsInfo: IMutationInfo[] = [];
const undoMutationsInfo: IMutationInfo[] = [];
const valueMatrix = new ObjectMatrix<ICellData>();
const formulaIdMap = new Map<string, string>();
let copyRowLength = 0;
let copyColumnLength = 0;
if (copyInfo && copyInfo.copyRange) {
const { copyType, copyRange } = copyInfo;
if (copyType === COPY_TYPE.COPY && copyRange) {
copyRowLength = copyRange.rows.length;
copyColumnLength = copyRange.cols.length;
}
}
matrix.forValue((row, col, value) => {
const originalFormula = value.f || '';
const originalFormulaId = value.si || '';
let valueObject: ICellDataWithSpanInfo = {};
// Paste the formula only, you also need to process some regular values
if (isSpecialPaste) {
valueObject = Tools.deepClone(value);
}
if (!copyRowLength || !copyColumnLength) {
return;
}
// Directly reuse when there is a formula id
if (isFormulaId(originalFormulaId)) {
const { unitId: pasteFromUnitId = '', subUnitId: pasteFromSubUnitId = '', range } = pasteFrom || {};
if (((pasteFromUnitId && unitId !== pasteFromUnitId) || (pasteFromSubUnitId && subUnitId !== pasteFromSubUnitId)) && range?.rows && range?.cols) {
const formulaString = formulaDataModel.getFormulaStringByCell(range.rows[row], range.cols[col], pasteFromSubUnitId, pasteFromUnitId);
// TODO handle as normal formula string
} else {
valueObject.si = originalFormulaId;
valueObject.f = null;
valueObject.v = null;
valueObject.p = null;
}
} else if (isFormulaString(originalFormula) && copyInfo.pasteType === PREDEFINED_HOOK_NAME.DEFAULT_PASTE) {
const rowIndex = row % copyRowLength;
const colIndex = col % copyColumnLength;
const index = `${rowIndex}_${colIndex}`;
let formulaId = formulaIdMap.get(index);
if (!formulaId) {
formulaId = Tools.generateRandomId(6);
formulaIdMap.set(index, formulaId);
const copyX = copyInfo?.copyRange ? copyInfo?.copyRange?.cols[colIndex] : colIndex;
const copyY = copyInfo?.copyRange ? copyInfo?.copyRange?.rows[rowIndex] : rowIndex;
const offsetX = range.cols[col] - copyX;
const offsetY = range.rows[row] - copyY;
const shiftedFormula = lexerTreeBuilder.moveFormulaRefOffset(originalFormula, offsetX, offsetY);
valueObject.si = formulaId;
valueObject.f = shiftedFormula;
valueObject.v = null;
valueObject.p = null;
} else {
// At the beginning of the second formula, set formulaId only
valueObject.si = formulaId;
valueObject.f = null;
valueObject.v = null;
valueObject.p = null;
}
}
valueMatrix.setValue(range.rows[row], range.cols[col], valueObject);
});
// set cell value and style
const setValuesMutation: ISetRangeValuesMutationParams = {
unitId,
subUnitId,
cellValue: valueMatrix.getData(),
};
redoMutationsInfo.push({
id: SetRangeValuesMutation.id,
params: setValuesMutation,
});
// undo
const undoSetValuesMutation: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(
accessor,
setValuesMutation
);
undoMutationsInfo.push({
id: SetRangeValuesMutation.id,
params: undoSetValuesMutation,
});
return {
undos: undoMutationsInfo,
redos: redoMutationsInfo,
};
}