Skip to content

Commit

Permalink
feat(facade): add appendText api
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed Mar 5, 2024
1 parent 33c6890 commit 641152b
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 16 deletions.
3 changes: 0 additions & 3 deletions examples/src/docs-uniscript/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { UniverUniscriptPlugin } from '@univerjs/uniscript';

import { UniverSheetsUIPlugin } from '@univerjs/sheets-ui';
import { UniverSheetsPlugin } from '@univerjs/sheets';
import { FUniver } from '@univerjs/facade';
import { DEFAULT_DOCUMENT_DATA_CN } from '../data';

// univer
Expand Down Expand Up @@ -66,8 +65,6 @@ univer.registerPlugin(UniverUniscriptPlugin, {
// create univer doc instance
univer.createUniverDoc(DEFAULT_DOCUMENT_DATA_CN);

const univerAPI = FUniver.newAPI(univer);

declare global {
interface Window {

Check warning on line 69 in examples/src/docs-uniscript/main.ts

View workflow job for this annotation

GitHub Actions / eslint

Interface name `Window` must match the RegExp: /^I[A-Z0-9]/u
univer?: Univer;
Expand Down
2 changes: 2 additions & 0 deletions packages/facade/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
},
"peerDependencies": {
"@univerjs/core": "workspace:*",
"@univerjs/docs": "workspace:*",
"@univerjs/network": "workspace:*",
"@univerjs/sheets": "workspace:*",
"@univerjs/sheets-formula": "workspace:*",
Expand All @@ -72,6 +73,7 @@
"dependencies": {},
"devDependencies": {
"@univerjs/core": "workspace:*",
"@univerjs/docs": "workspace:*",
"@univerjs/engine-formula": "workspace:*",
"@univerjs/network": "workspace:*",
"@univerjs/shared": "workspace:*",
Expand Down
102 changes: 102 additions & 0 deletions packages/facade/src/apis/docs/f-document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* 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://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 { DocumentDataModel, IDocumentData } from '@univerjs/core';
import {
ICommandService,
IUniverInstanceService,
RedoCommand,
UndoCommand,
} from '@univerjs/core';
import { InsertCommand } from '@univerjs/docs';

export class FDocument {
readonly id: string;

constructor(
private readonly _documentDataModel: DocumentDataModel,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@ICommandService private readonly _commandService: ICommandService
) {
this.id = this._documentDataModel.getUnitId();
}

getId(): string {
return this._documentDataModel.getUnitId();
}

getName(): string {
return this.getSnapshot().title || '';
}

getSnapshot(): IDocumentData {
return this._documentDataModel.getSnapshot();
}

undo(): Promise<boolean> {
this._univerInstanceService.focusUniverInstance(this.id);
return this._commandService.executeCommand(UndoCommand.id);
}

redo(): Promise<boolean> {
this._univerInstanceService.focusUniverInstance(this.id);
return this._commandService.executeCommand(RedoCommand.id);
}

/**
* Adds the specified text to the end of this text region.
* @param text
*/
appendText(text: string): void {
const unitId = this.id;

const { body } = this.getSnapshot();

if (!body) {
throw new Error('The document body is empty');
}

const lastPosition = body.dataStream.length - 2;

const activeRange = {
startOffset: lastPosition,
endOffset: lastPosition,
collapsed: true,
segmentId: '',
};

const { startOffset, segmentId } = activeRange;

const len = text.length;

const textRanges = [
{
startOffset: startOffset + len,
endOffset: startOffset + len,
},
];

this._commandService.executeCommand(InsertCommand.id, {
unitId,
body: {
dataStream: text,
},
range: activeRange,
textRanges,
segmentId,
});
}
}
66 changes: 60 additions & 6 deletions packages/facade/src/apis/facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { CommandListener, IExecutionOptions, IWorkbookData } from '@univerjs/core';
import type { CommandListener, IDocumentData, IExecutionOptions, IWorkbookData } from '@univerjs/core';
import {
BorderStyleTypes,
ICommandService,
Expand All @@ -25,11 +25,12 @@ import {
} from '@univerjs/core';
import { ISocketService, WebSocketService } from '@univerjs/network';
import type { IRegisterFunctionParams, IUnregisterFunctionParams } from '@univerjs/sheets-formula';
import { IRegisterFunctionService } from '@univerjs/sheets-formula';
import { IRegisterFunctionService, RegisterFunctionService } from '@univerjs/sheets-formula';
import type { IDisposable } from '@wendellhu/redi';
import { Inject, Injector, Quantity } from '@wendellhu/redi';

import { FWorkbook } from './sheet/f-workbook';
import { FWorkbook } from './sheets/f-workbook';
import { FDocument } from './docs/f-document';

export class FUniver {
/**
Expand All @@ -53,7 +54,7 @@ export class FUniver {
@Inject(Injector) private readonly _injector: Injector,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@ICommandService private readonly _commandService: ICommandService,
@IRegisterFunctionService private readonly _registerFunctionService: IRegisterFunctionService,
// @IRegisterFunctionService private readonly _registerFunctionService: IRegisterFunctionService,
@ISocketService private readonly _ws: ISocketService
) {}

Expand All @@ -67,6 +68,16 @@ export class FUniver {
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.createDoc(data);
return this._injector.createInstance(FDocument, document);
}

/**
* Get the spreadsheet API handler by the spreadsheet id.
* @param id the spreadsheet id.
Expand All @@ -81,6 +92,20 @@ export class FUniver {
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.
Expand All @@ -94,20 +119,49 @@ export class FUniver {
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) {
this._registerFunctionService.registerFunctions(config);
let registerFunctionService = this._injector.get(IRegisterFunctionService);

if (!registerFunctionService) {
this._injector.add([IRegisterFunctionService, { useClass: RegisterFunctionService }]);
registerFunctionService = this._injector.get(IRegisterFunctionService);
}

registerFunctionService.registerFunctions(config);
}

/**
* Unregister a function from the spreadsheet.
*
* TODO@Dushusir: remove unregister,use IDisposable
* @param config
*/
unregisterFunction(config: IUnregisterFunctionParams) {
this._registerFunctionService.unregisterFunctions(config);
let registerFunctionService = this._injector.get(IRegisterFunctionService);

if (!registerFunctionService) {
this._injector.add([IRegisterFunctionService, { useClass: RegisterFunctionService }]);
registerFunctionService = this._injector.get(IRegisterFunctionService);
}

registerFunctionService.unregisterFunctions(config);
}

// #region
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions packages/facade/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

export type { FontLine, FontStyle, FontWeight } from './apis/sheet/f-range';
export type { FontLine, FontStyle, FontWeight } from './apis/sheets/f-range';
export { FUniver } from './apis/facade';
export { FRange } from './apis/sheet/f-range';
export { FSelection } from './apis/sheet/f-selection';
export { FWorkbook } from './apis/sheet/f-workbook';
export { FWorksheet } from './apis/sheet/f-worksheet';
export { FRange } from './apis/sheets/f-range';
export { FSelection } from './apis/sheets/f-selection';
export { FWorkbook } from './apis/sheets/f-workbook';
export { FWorksheet } from './apis/sheets/f-worksheet';
2 changes: 1 addition & 1 deletion packages/uniscript/src/controllers/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function UniscriptMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
icon: 'CodeSingle',
type: MenuItemType.BUTTON,
positions: [MenuPosition.TOOLBAR_START],
// FIXME@wendellhu: hidden$ and disabled$ are not correctly in doc
// FIXME hidden$ and disabled$ are not correctly in doc
// hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.SHEET),
// disabled$: getCurrentSheetDisabled$(accessor),
};
Expand Down
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 641152b

Please sign in to comment.