Skip to content

Commit

Permalink
feat(facade): add more Facade API (#1029)
Browse files Browse the repository at this point in the history
* feat(formula): register function (#928)

* feat(formula): register function

* feat(formula): register function in worker

* feat(formula): register function, send to worker

* feat(formula): register function in uniscript

* feat(formula): use simple description when register function

* fix(facade): conflict

* fix(formula): register description

* fix(formula): register use service

* fix(formula): custom function service name

docs: add how to contribute to facade API

fix: fix type errors

fix: fix unintented change

fix: tsconfig format

chore: move spec file

feat: init find-replace plugin

feat(facade): add more apis

feat(facade): add API of changing number format

feat(facade): add callback API

feat: add set number format apis

feat(facade): change wrap and alignment API

* fix: resolve conflict

* fix: change naming

* fix: fix type error

* fix: fix type error

* fix: lint problems

---------

Co-authored-by: Dushusir <[email protected]>
  • Loading branch information
wzhudev and Dushusir committed Jan 8, 2024
1 parent cd7b740 commit 6224b4f
Show file tree
Hide file tree
Showing 39 changed files with 913 additions and 68 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ packages/design @jikkai @wzhudev
# common
packages/image @DR-Univer
packages/uniscript @wzhudev
packages/find-replace @wzhudev

# docs
packages/docs @DR-Univer @Jocs
Expand Down
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@univerjs/docs": "workspace:*",
"@univerjs/docs-ui": "workspace:*",
"@univerjs/engine-formula": "workspace:*",
"@univerjs/find-replace": "workspace:*",
"@univerjs/engine-render": "workspace:*",
"@univerjs/icons": "^0.1.24",
"@univerjs/rpc": "workspace:*",
Expand Down
7 changes: 6 additions & 1 deletion examples/src/sheets/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { UniverDocsPlugin } from '@univerjs/docs';
import { UniverDocsUIPlugin } from '@univerjs/docs-ui';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { UniverFindReplacePlugin } from '@univerjs/find-replace';
import type { IUniverRPCMainThreadConfig } from '@univerjs/rpc';
import { UniverRPCMainThreadPlugin } from '@univerjs/rpc';
import { UniverSheetsPlugin } from '@univerjs/sheets';
Expand Down Expand Up @@ -53,11 +54,14 @@ univer.registerPlugin(UniverUIPlugin, {
toolbar: true,
footer: true,
});
univer.registerPlugin(UniverFindReplacePlugin);

univer.registerPlugin(UniverDocsUIPlugin);

univer.registerPlugin(UniverSheetsPlugin, {
notExecuteFormula: true,
});
univer.registerPlugin(UniverSheetsUIPlugin);
univer.registerPlugin(UniverDocsUIPlugin);

// sheet feature plugins

Expand All @@ -76,6 +80,7 @@ univer.registerPlugin(UniverRPCMainThreadPlugin, {
univer.createUniverSheet(DEFAULT_WORKBOOK_DATA_DEMO);

declare global {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface Window {
univer?: Univer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
import { Injector } from '@wendellhu/redi';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { ContextService, IContextService } from '../context/context.service';
import { DesktopLogService, ILogService } from '../log/log.service';
import type { IMultiCommand } from './command.service';
import { CommandService, CommandType, ICommandService, sequenceExecute, sequenceExecuteAsync } from './command.service';
import { ContextService, IContextService } from '../../context/context.service';
import { DesktopLogService, ILogService } from '../../log/log.service';
import type { IMultiCommand } from '../command.service';
import {
CommandService,
CommandType,
ICommandService,
sequenceExecute,
sequenceExecuteAsync,
} from '../command.service';

const commandID = 'emit-plural-error-command';
const anotherCommandID = 'another-command';
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/sheets/workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export class Workbook extends Disposable {
return this._snapshot;
}

getName(): string {
return this._snapshot.name;
}

getUnitId() {
return this._unitId;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/facade/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@univerjs/sheets": "workspace:*",
"@univerjs/engine-formula": "workspace:*",
"@univerjs/sheets-formula": "workspace:*",
"@univerjs/sheets-numfmt": "workspace:*",
"@wendellhu/redi": "^0.12.13",
"rxjs": "^7.8.1"
},
Expand All @@ -62,6 +63,8 @@
"peerDependencies": {
"@univerjs/core": "workspace:*",
"@univerjs/sheets": "workspace:*",
"@univerjs/sheets-formula": "workspace:*",
"@univerjs/sheets-numfmt": "workspace:*",
"@wendellhu/redi": ">=0.12.13",
"rxjs": ">=7.0.0"
}
Expand Down
79 changes: 76 additions & 3 deletions packages/facade/src/apis/facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@
* limitations under the License.
*/

import type { IWorkbookData } from '@univerjs/core';
import { ICommandService, IUniverInstanceService, Univer } from '@univerjs/core';
import type { CommandListener, IWorkbookData } from '@univerjs/core';
import {
BorderStyleTypes,
ICommandService,
IUniverInstanceService,
UndoCommand,
Univer,
WrapStrategy,
} from '@univerjs/core';
import type { IRegisterFunctionParams, IUnregisterFunctionParams } from '@univerjs/sheets-formula';
import { IRegisterFunctionService } from '@univerjs/sheets-formula';
import type { IDisposable } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';

import { FWorkbook } from './sheet/f-workbook';
Expand All @@ -31,19 +39,45 @@ export class FUniver {
return injector.createInstance(FUniver);
}

static BorderStyle = BorderStyleTypes;
static WrapStrategy = WrapStrategy;

constructor(
@Inject(Injector) private readonly _injector: Injector,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@ICommandService private readonly _commandService: ICommandService,
@IRegisterFunctionService private readonly _registerFunctionService: IRegisterFunctionService
) {}

/**
* 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: IWorkbookData): FWorkbook {
const workbook = this._univerInstanceService.createSheet(data);
return this._injector.createInstance(FWorkbook, workbook);
}

getCurrentSheet(): FWorkbook | null {
/**
* 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 currently focused Univer spreadsheet.
* @returns the currently focused Univer spreadsheet.
*/
getActiveSheet(): FWorkbook | null {
const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();
if (!workbook) {
return null;
Expand All @@ -59,4 +93,43 @@ export class FUniver {
unregisterFunction(config: IUnregisterFunctionParams) {
this._registerFunctionService.unregisterFunctions(config);
}

// #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 editing

// #callback

// #region listeners

/**
* 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) => {
callback(command);
});
}

// @endregion
}
12 changes: 6 additions & 6 deletions packages/facade/src/apis/sheet/__tests__/f-range.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
* limitations under the License.
*/

import type { ICellData, IStyleData, Nullable, Univer } from '@univerjs/core';
/* eslint-disable no-magic-numbers */

import type { ICellData, IStyleData, Nullable } from '@univerjs/core';
import { ICommandService, IUniverInstanceService } from '@univerjs/core';
import { SetRangeValuesCommand, SetRangeValuesMutation, SetStyleCommand } from '@univerjs/sheets';
import type { Injector } from '@wendellhu/redi';
Expand All @@ -24,7 +26,6 @@ import type { FUniver } from '../../facade';
import { createTestBed } from './create-test-bed';

describe('Test FRange', () => {
let univer: Univer;
let get: Injector['get'];
let commandService: ICommandService;
let univerAPI: FUniver;
Expand All @@ -43,7 +44,6 @@ describe('Test FRange', () => {

beforeEach(() => {
const testBed = createTestBed();
univer = testBed.univer;
get = testBed.get;
univerAPI = testBed.univerAPI;

Expand Down Expand Up @@ -79,7 +79,7 @@ describe('Test FRange', () => {
});

it('Range setValue', () => {
const activeSheet = univerAPI.getCurrentSheet()?.getActiveSheet();
const activeSheet = univerAPI.getActiveSheet()?.getActiveSheet();

// A1 sets the number
const range1 = activeSheet?.getRange(0, 0, 1, 1);
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('Test FRange', () => {
});

it('Range setValues', () => {
const activeSheet = univerAPI.getCurrentSheet()?.getActiveSheet();
const activeSheet = univerAPI.getActiveSheet()?.getActiveSheet();

// B3:C4 sets value
const range1 = activeSheet?.getRange(2, 1, 2, 2);
Expand Down Expand Up @@ -176,7 +176,7 @@ describe('Test FRange', () => {
});

it('Range setFontWeight', () => {
const activeSheet = univerAPI.getCurrentSheet()?.getActiveSheet();
const activeSheet = univerAPI.getActiveSheet()?.getActiveSheet();

// change A1 font weight
const range = activeSheet?.getRange(0, 0, 1, 1);
Expand Down
Loading

0 comments on commit 6224b4f

Please sign in to comment.