-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
facade.ts
408 lines (358 loc) · 13.6 KB
/
facade.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* 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 { CommandListener, DocumentDataModel, IDocumentData, IExecutionOptions, IWorkbookData,
Nullable,
Workbook } from '@univerjs/core';
import {
BorderStyleTypes,
debounce,
ICommandService,
IUniverInstanceService,
toDisposable,
UndoCommand,
Univer,
UniverInstanceType,
WrapStrategy,
} from '@univerjs/core';
import { ISocketService, WebSocketService } from '@univerjs/network';
import type { IRegisterFunctionParams } from '@univerjs/sheets-formula';
import { IRegisterFunctionService, RegisterFunctionService } from '@univerjs/sheets-formula';
import type { Dependency, IDisposable } from '@wendellhu/redi';
import { Inject, Injector, Quantity } from '@wendellhu/redi';
import type {
IColumnsHeaderCfgParam,
IRowsHeaderCfgParam,
RenderComponentType,
SheetComponent,
SheetExtension, SpreadsheetColumnHeader,
SpreadsheetRowHeader } from '@univerjs/engine-render';
import {
IRenderManagerService } from '@univerjs/engine-render';
import { SHEET_VIEW_KEY } from '@univerjs/sheets-ui';
import { SetFormulaCalculationStartMutation } from '@univerjs/engine-formula';
import { FDocument } from './docs/f-document';
import { FWorkbook } from './sheets/f-workbook';
import { FSheetHooks } from './sheets/f-sheet-hooks';
import { FHooks } from './f-hooks';
export class FUniver {
/**
* Get dependencies for FUniver, you can override newAPI to add more dependencies.
* @param injector
* @param derivedDependencies
* @returns
*/
protected static getDependencies(injector: Injector, derivedDependencies?: []): Dependency[] {
const dependencies: Dependency[] = derivedDependencies || [];
// Is unified registration required?
const socketService = injector.get(ISocketService, Quantity.OPTIONAL);
if (!socketService) {
dependencies.push([ISocketService, { useClass: WebSocketService }]);
}
return dependencies;
}
/**
* Create a FUniver instance, if the injector is not provided, it will create a new Univer instance.
* @param wrapped The Univer instance or injector.
* @returns FUniver instance.
*
* @zh 创建一个 FUniver 实例,如果未提供注入器,将创建一个新的 Univer 实例。
* @param_zh wrapped Univer 实例或注入器。
* @returns_zh FUniver 实例。
*/
static newAPI(wrapped: Univer | Injector): FUniver {
const injector = wrapped instanceof Univer ? wrapped.__getInjector() : wrapped;
const dependencies = FUniver.getDependencies(injector);
dependencies.forEach((dependency) => injector.add(dependency));
return injector.createInstance(FUniver);
}
static BorderStyle = BorderStyleTypes;
static WrapStrategy = WrapStrategy;
/**
* registerFunction may be executed multiple times, triggering multiple formula forced refreshes
*/
private _debouncedFormulaCalculation: () => void;
constructor(
@Inject(Injector) protected readonly _injector: Injector,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@ICommandService private readonly _commandService: ICommandService,
@ISocketService private readonly _ws: ISocketService,
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService
) {
this._initialize();
}
/**
* Create a new spreadsheet and get the API handler of that spreadsheet.
* @param data The snapshot of the spreadsheet.
* @returns Spreadsheet API instance.
*/
createUniverSheet(data: Partial<IWorkbookData>): FWorkbook {
const workbook = this._univerInstanceService.createUnit<IWorkbookData, Workbook>(UniverInstanceType.UNIVER_SHEET, data);
return this._injector.createInstance(FWorkbook, workbook);
}
/**
* Create a new document and get the API handler of that document.
* @param data The snapshot of the document.
* @returns Document API instance.
*/
createUniverDoc(data: Partial<IDocumentData>): FDocument {
const document = this._univerInstanceService.createUnit<IDocumentData, DocumentDataModel>(UniverInstanceType.UNIVER_DOC, data);
return this._injector.createInstance(FDocument, document);
}
/**
* Dispose the UniverSheet by the `unitId`. The UniverSheet would be unload from the application.
* @param unitId The `unitId` of the UniverSheet.
* @returns If the UniverSheet is disposed successfully, return `true`, otherwise return `false`.
*/
disposeUnit(unitId: string): boolean {
return this._univerInstanceService.disposeUnit(unitId);
}
/**
* Get the spreadsheet API handler by the spreadsheet id.
* @param id The spreadsheet id.
* @returns Spreadsheet API instance.
*/
getUniverSheet(id: string): FWorkbook | null {
const workbook = this._univerInstanceService.getUniverSheetInstance(id);
if (!workbook) {
return null;
}
return this._injector.createInstance(FWorkbook, workbook);
}
/**
* Get the document API handler by the document id.
* @param id The document id.
* @returns Document API instance.
*/
getUniverDoc(id: string): FDocument | null {
const document = this._univerInstanceService.getUniverDocInstance(id);
if (!document) {
return null;
}
return this._injector.createInstance(FDocument, document);
}
/**
* Get the currently focused Univer spreadsheet.
* @returns the currently focused Univer spreadsheet.
*/
getActiveWorkbook(): FWorkbook | null {
const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
if (!workbook) {
return null;
}
return this._injector.createInstance(FWorkbook, workbook);
}
/**
* Get the currently focused Univer document.
* @returns the currently focused Univer document.
*/
getActiveDocument(): FDocument | null {
const document = this._univerInstanceService.getCurrentUniverDocInstance();
if (!document) {
return null;
}
return this._injector.createInstance(FDocument, document);
}
/**
* Register a function to the spreadsheet.
* @param config
*/
registerFunction(config: IRegisterFunctionParams): IDisposable {
let registerFunctionService = this._injector.get(IRegisterFunctionService);
if (!registerFunctionService) {
this._injector.add([IRegisterFunctionService, { useClass: RegisterFunctionService }]);
registerFunctionService = this._injector.get(IRegisterFunctionService);
}
const functionsDisposable = registerFunctionService.registerFunctions(config);
// When the initialization workbook data already contains custom formulas, and then register the formula, you need to trigger a forced calculation to refresh the calculation results
this._debouncedFormulaCalculation();
return toDisposable(() => {
functionsDisposable.dispose();
});
}
/**
* Register sheet row header render extensions.
* @param unitId
* @param extensions
*/
registerSheetRowHeaderExtension(unitId: string, ...extensions: SheetExtension[]): IDisposable {
const sheetComponent = this._getSheetRenderComponent(unitId, SHEET_VIEW_KEY.ROW) as SheetComponent;
const registerDisposable = sheetComponent.register(...extensions);
return toDisposable(() => {
registerDisposable.dispose();
sheetComponent.makeDirty(true);
});
}
/**
* Register sheet column header render extensions.
* @param unitId
* @param extensions
*/
registerSheetColumnHeaderExtension(unitId: string, ...extensions: SheetExtension[]): IDisposable {
const sheetComponent = this._getSheetRenderComponent(unitId, SHEET_VIEW_KEY.COLUMN) as SheetComponent;
const registerDisposable = sheetComponent.register(...extensions);
return toDisposable(() => {
registerDisposable.dispose();
sheetComponent.makeDirty(true);
});
}
/**
* Register sheet main render extensions.
* @param unitId
* @param uKeys
*/
registerSheetMainExtension(unitId: string, ...extensions: SheetExtension[]): IDisposable {
const sheetComponent = this._getSheetRenderComponent(unitId, SHEET_VIEW_KEY.MAIN) as SheetComponent;
const registerDisposable = sheetComponent.register(...extensions);
return toDisposable(() => {
registerDisposable.dispose();
sheetComponent.makeDirty(true);
});
}
// #region
/**
* Undo an editing on the currently focused document.
* @returns redo result
*/
undo(): Promise<boolean> {
return this._commandService.executeCommand(UndoCommand.id);
}
/**
* Redo an editing on the currently focused document.
* @returns redo result
*/
redo(): Promise<boolean> {
return this._commandService.executeCommand(UndoCommand.id);
}
// #endregion
// #region listeners
/**
* Register a callback that will be triggered before invoking a command.
* @param callback the callback.
* @returns A function to dispose the listening.
*/
onBeforeCommandExecute(callback: CommandListener): IDisposable {
return this._commandService.beforeCommandExecuted((command, options?: IExecutionOptions) => {
callback(command, options);
});
}
/**
* Register a callback that will be triggered when a command is invoked.
* @param callback the callback.
* @returns A function to dispose the listening.
*/
onCommandExecuted(callback: CommandListener): IDisposable {
return this._commandService.onCommandExecuted((command, options?: IExecutionOptions) => {
callback(command, options);
});
}
// #endregion
/**
* Execute command
* @param id Command id
* @param params Command params
* @param options Command options
* @returns Command Promise
*/
executeCommand<P extends object = object, R = boolean>(
id: string,
params?: P,
options?: IExecutionOptions
): Promise<R> {
return this._commandService.executeCommand(id, params, options);
}
/**
* Set WebSocket URL for WebSocketService
* @param url WebSocketService URL
* @returns WebSocket info and callback
*/
createSocket(url: string) {
const ws = this._ws.createSocket(url);
if (!ws) {
throw new Error('[WebSocketService]: failed to create socket!');
}
return ws;
}
/**
* Get sheet hooks
* @returns
*/
getSheetHooks() {
return this._injector.createInstance(FSheetHooks);
}
/**
* Get hooks
*/
getHooks() {
return this._injector.createInstance(FHooks);
}
/**
* Get sheet render component from render by unitId and view key.
* @param unitId
* @returns
*/
private _getSheetRenderComponent(unitId: string, viewKey: SHEET_VIEW_KEY): Nullable<RenderComponentType> {
const render = this._renderManagerService.getRenderById(unitId);
if (!render) {
throw new Error('Render not found');
}
const { components } = render;
const renderComponent = components.get(viewKey);
if (!renderComponent) {
throw new Error('Render component not found');
}
return renderComponent;
}
/**
* customizeColumnHeader
* @param cfg
* cfg example
({ headerStyle:{backgroundColor: 'pink', fontSize: 9}, columnsCfg: ['MokaII', undefined, null, {text: 'Size', textAlign: 'left'}]})
*/
customizeColumnHeader(cfg: IColumnsHeaderCfgParam) {
const wb = this.getActiveWorkbook();
if (!wb) {
console.error('WorkBook not exist');
return;
}
const unitId = wb?.getId();
const sheetColumn = this._getSheetRenderComponent(unitId, SHEET_VIEW_KEY.COLUMN) as SpreadsheetColumnHeader;
sheetColumn.setCustomHeader(cfg);
}
customizeRowHeader(cfg: IRowsHeaderCfgParam) {
const wb = this.getActiveWorkbook();
if (!wb) {
console.error('WorkBook not exist');
return;
}
const unitId = wb?.getId();
const sheetRow = this._getSheetRenderComponent(unitId, SHEET_VIEW_KEY.ROW) as SpreadsheetRowHeader;
sheetRow.setCustomHeader(cfg);
}
private _initialize(): void {
this._debouncedFormulaCalculation = debounce(() => {
this._commandService.executeCommand(
SetFormulaCalculationStartMutation.id,
{
commands: [],
forceCalculation: true,
},
{
onlyLocal: true,
}
);
}, 10);
}
}