Skip to content

Commit

Permalink
refactor: extract almost all packages
Browse files Browse the repository at this point in the history
  • Loading branch information
wzhudev committed Oct 14, 2024
1 parent 85fb2d5 commit 1f08ad8
Show file tree
Hide file tree
Showing 35 changed files with 822 additions and 903 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@
* limitations under the License.
*/

import type { IDisposable } from '@univerjs/core';
import { Inject, Injector, LifecycleService, LifecycleStages, toDisposable } from '@univerjs/core';
import type { IDisposable } from '../common/di';
import type { IUndoRedoItem } from '../services/undoredo/undoredo.service';
import { filter } from 'rxjs';
import { FClipboardHooks } from './hooks/f-clipboard-hooks';
import { FUndoRedoHooks } from './hooks/f-undoredo-hooks';
import { Inject, Injector } from '../common/di';
import { ICommandService } from '../services/command/command.service';
import { LifecycleStages } from '../services/lifecycle/lifecycle';
import { LifecycleService } from '../services/lifecycle/lifecycle.service';
import { IUndoRedoService, RedoCommand, UndoCommand } from '../services/undoredo/undoredo.service';
import { toDisposable } from '../shared/lifecycle';
import { FBase } from './f-univer';

export class FHooks {
export class FHooks extends FBase {
constructor(
@Inject(Injector) protected readonly _injector: Injector,
@Inject(LifecycleService) private readonly _lifecycleService: LifecycleService
) {
// empty
super();
}

/**
Expand Down Expand Up @@ -64,59 +69,79 @@ export class FHooks {
return toDisposable(this._lifecycleService.lifecycle$.pipe(filter((lifecycle) => lifecycle === LifecycleStages.Steady)).subscribe(callback));
}

/**
* Hook that fires before an undo operation is executed.
* @param callback Function to be called when the event is triggered
* @returns A disposable object that can be used to unsubscribe from the event
*/
onBeforeUndo = FUndoRedoHooks.beforeUndo.bind(this);
/**
* Hook that fires before an undo operation is executed.
* @param callback Function to be called when the event is triggered
* @returns A disposable object that can be used to unsubscribe from the event
*/
onBeforeUndo(callback: (action: IUndoRedoItem) => void): IDisposable {
const commandService = this._injector.get(ICommandService);

return commandService.beforeCommandExecuted((command) => {
if (command.id === UndoCommand.id) {
const undoredoService = this._injector.get(IUndoRedoService);
const action = undoredoService.pitchTopUndoElement();
if (action) {
callback(action);
}
}
});
}

/**
* Hook that fires after an undo operation is executed.
* @param callback Function to be called when the event is triggered
* @returns A disposable object that can be used to unsubscribe from the event
*/
onUndo = FUndoRedoHooks.afterUndo.bind(this);
onUndo(callback: (action: IUndoRedoItem) => void): IDisposable {
const commandService = this._injector.get(ICommandService);

return commandService.onCommandExecuted((command) => {
if (command.id === UndoCommand.id) {
const undoredoService = this._injector.get(IUndoRedoService);
const action = undoredoService.pitchTopUndoElement();
if (action) {
callback(action);
}
}
});
}

/**
* Hook that fires before a redo operation is executed.
* @param callback Function to be called when the event is triggered
* @returns A disposable object that can be used to unsubscribe from the event
*/
onBeforeRedo = FUndoRedoHooks.beforeRedo.bind(this);
onBeforeRedo(callback: (action: IUndoRedoItem) => void): IDisposable {
const commandService = this._injector.get(ICommandService);

return commandService.beforeCommandExecuted((command) => {
if (command.id === RedoCommand.id) {
const undoredoService = this._injector.get(IUndoRedoService);
const action = undoredoService.pitchTopRedoElement();
if (action) {
callback(action);
}
}
});
}

/**
* Hook that fires after a redo operation is executed.
* @param callback Function to be called when the event is triggered
* @returns A disposable object that can be used to unsubscribe from the event
*/
onRedo = FUndoRedoHooks.afterRedo.bind(this);

/**
* The onBeforeCopy event is fired before a copy operation is performed.
* @param callback Callback function that will be called when the event is fired
* @returns A disposable object that can be used to unsubscribe from the event
*/
onBeforeCopy = FClipboardHooks.onBeforeCopy.bind(this);
onRedo(callback: (action: IUndoRedoItem) => void): IDisposable {
const commandService = this._injector.get(ICommandService);

/**
* The onCopy event is fired after a copy operation is performed.
* @param callback Callback function that will be called when the event is fired
* @returns A disposable object that can be used to unsubscribe from the event
*/
onCopy = FClipboardHooks.onCopy.bind(this);

/**
* The onBeforePaste event is fired before a paste operation is performed.
* @param callback Callback function that will be called when the event is fired
* @returns A disposable object that can be used to unsubscribe from the event
*/
onBeforePaste = FClipboardHooks.onBeforePaste.bind(this);

/**
* The onPaste event is fired after a paste operation is performed.
* @param callback Callback function that will be called when the event is fired
* @returns A disposable object that can be used to unsubscribe from the event
*/
onPaste = FClipboardHooks.onPaste.bind(this);
return commandService.onCommandExecuted((command) => {
if (command.id === RedoCommand.id) {
const undoredoService = this._injector.get(IUndoRedoService);
const action = undoredoService.pitchTopRedoElement();
if (action) {
callback(action);
}
}
});
}
}
10 changes: 10 additions & 0 deletions packages/core/src/facade/f-univer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { IUniverInstanceService } from '../services/instance/instance.service';
import { LifecycleService } from '../services/lifecycle/lifecycle.service';
import { RedoCommand, UndoCommand } from '../services/undoredo/undoredo.service';
import { Univer } from '../univer';
import { FHooks } from './f-hooks';

/**
* `FBase` is a base class for all facade classes.
Expand Down Expand Up @@ -160,4 +161,13 @@ export class FUniver extends FBase {
): Promise<R> {
return this._commandService.executeCommand(id, params, options);
}

/**
* Get hooks.
*
* @returns {FHooks} FHooks instance
*/
getHooks(): FHooks {
return this._injector.createInstance(FHooks);
}
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type { Serializable } from './common/json';
export { MemoryCursor } from './common/memory-cursor';
export { mixinClass } from './common/mixin';
export { FBase, FUniver } from './facade/f-univer';
export { FHooks } from './facade/f-hooks';
export { isNumeric, isSafeNumeric } from './common/number';
export { Registry, RegistryAsMap } from './common/registry';
export { requestImmediateMacroTask } from './common/request-immediate-macro-task';
Expand Down
3 changes: 2 additions & 1 deletion packages/docs-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"exports": {
".": "./src/index.ts",
"./*": "./src/*",
"./locale/*": "./src/locale/*.ts"
"./locale/*": "./src/locale/*.ts",
"./facade": "./src/facade/index.ts"
},
"main": "./lib/cjs/index.js",
"module": "./lib/es/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import type { DocumentDataModel, IDocumentData } from '@univerjs/core';
import { DOC_RANGE_TYPE, ICommandService,
Inject,
Injector,
Expand All @@ -23,9 +24,9 @@ import { DOC_RANGE_TYPE, ICommandService,
UndoCommand,
UniverInstanceType,
} from '@univerjs/core';
import { DocSelectionRenderService, InsertCommand } from '@univerjs/docs-ui';
import { IRenderManagerService } from '@univerjs/engine-render';
import type { DocumentDataModel, IDocumentData } from '@univerjs/core';
import { InsertCommand } from '../commands/commands/core-editing.command';
import { DocSelectionRenderService } from '../services/selection/doc-selection-render.service';

export class FDocument {
readonly id: string;
Expand Down Expand Up @@ -126,11 +127,4 @@ export class FDocument {
, true
);
}

// setHyperLink(linkUrl: string): FDocument;
// setHyperLink(startOffset: number, endOffset: number, linkUrl: string | null): FDocument {
// this._commandService.executeCommand(
// AddDocHyperLinkCommand.id,
// )
// }
}
73 changes: 73 additions & 0 deletions packages/docs-ui/src/facade/f-univer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 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 { FUniver, UniverInstanceType } from '@univerjs/core';
import { FDocument } from './f-document';

interface IFUniverDocsUIMixin {
/**
* Create a new document and get the API handler of that document.
*
* @param {Partial<IDocumentData>} data The snapshot of the document.
* @returns {FDocument} FDocument API instance.
*/
createUniverDoc(data: Partial<IDocumentData>): FDocument;
/**
* Get the document API handler by the document id.
*
* @param {string} id The document id.
* @returns {FDocument | null} The document API instance.
*/
getUniverDoc(id: string): FDocument | null;
/**
* Get the currently focused Univer document.
*
* @returns {FDocument | null} The currently focused Univer document.
*/
getActiveDocument(): FDocument | null;
}

export class FUniverDocsMixin extends FUniver implements IFUniverDocsUIMixin {
override createUniverDoc(data: Partial<IDocumentData>): FDocument {
const document = this._univerInstanceService.createUnit<IDocumentData, DocumentDataModel>(UniverInstanceType.UNIVER_DOC, data);
return this._injector.createInstance(FDocument, document);
}

override getActiveDocument(): FDocument | null {
const document = this._univerInstanceService.getCurrentUnitForType<DocumentDataModel>(UniverInstanceType.UNIVER_DOC);
if (!document) {
return null;
}

return this._injector.createInstance(FDocument, document);
}

override getUniverDoc(id: string): FDocument | null {
const document = this._univerInstanceService.getUniverDocInstance(id);
if (!document) {
return null;
}

return this._injector.createInstance(FDocument, document);
}
}

FUniver.extend(FUniverDocsMixin);
declare module '@univerjs/core' {
// eslint-disable-next-line ts/naming-convention
interface FUniver extends IFUniverDocsUIMixin {}
}
19 changes: 19 additions & 0 deletions packages/docs-ui/src/facade/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 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 './f-univer';

export { FDocument } from './f-document';
3 changes: 2 additions & 1 deletion packages/engine-formula/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
],
"exports": {
".": "./src/index.ts",
"./*": "./src/*"
"./*": "./src/*",
"./facade": "./src/facade/index.ts"
},
"main": "./lib/cjs/index.js",
"module": "./lib/es/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/

import type { ICommandInfo, IDisposable } from '@univerjs/core';
import type { FormulaExecutedStateType, IExecutionInProgressParams, ISetFormulaCalculationNotificationMutation, ISetFormulaCalculationStartMutation } from '@univerjs/engine-formula';
import type { ISetFormulaCalculationNotificationMutation, ISetFormulaCalculationStartMutation } from '../commands/mutations/set-formula-calculation.mutation';
import type { FormulaExecutedStateType, IExecutionInProgressParams } from '../services/runtime.service';
import { ICommandService } from '@univerjs/core';
import { SetFormulaCalculationNotificationMutation, SetFormulaCalculationStartMutation, SetFormulaCalculationStopMutation } from '@univerjs/engine-formula';
import { SetFormulaCalculationNotificationMutation, SetFormulaCalculationStartMutation, SetFormulaCalculationStopMutation } from '../commands/mutations/set-formula-calculation.mutation';

/**
* This interface class provides methods to modify the behavior of the operation formula.
Expand All @@ -26,22 +27,14 @@ export class FFormula {
constructor(
@ICommandService private readonly _commandService: ICommandService
) {
// empty
}

/**
* Start the calculation of the formula.
*/
executeCalculation(): void {
this._commandService.executeCommand(
SetFormulaCalculationStartMutation.id,
{
commands: [],
forceCalculation: true,
},
{
onlyLocal: true,
}
);
this._commandService.executeCommand(SetFormulaCalculationStartMutation.id, { commands: [], forceCalculation: true }, { onlyLocal: true });
}

/**
Expand Down
34 changes: 34 additions & 0 deletions packages/engine-formula/src/facade/f-univer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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 { FUniver } from '@univerjs/core';
import { FFormula } from './f-formula';

interface IFUniverEngineFormulaMixin {
getFormula(): FFormula;
}

class FUniverEngineFormulaMixin extends FUniver implements IFUniverEngineFormulaMixin {
override getFormula(): FFormula {
return this._injector.createInstance(FFormula);
}
}

FUniver.extend(FUniverEngineFormulaMixin);
declare module '@univerjs/core' {
// eslint-disable-next-line ts/naming-convention
interface FUniver extends IFUniverEngineFormulaMixin {}
}
Loading

0 comments on commit 1f08ad8

Please sign in to comment.