Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(docs-ui): refactor to RenderUnit #2539

Open
wants to merge 22 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: fix docs test
  • Loading branch information
wzhudev committed Jun 18, 2024
commit 44d59f1a65732fb2a5ab4f0e7fdfeeead364afbe
3 changes: 2 additions & 1 deletion examples/src/sheets/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { UniverSheetsNumfmtPlugin } from '@univerjs/sheets-numfmt';
import { UniverSheetsDataValidationPlugin } from '@univerjs/sheets-data-validation';
import { UniverSheetsDrawingUIPlugin } from '@univerjs/sheets-drawing-ui';
import { FUniver } from '@univerjs/facade';
import { UniverSheetsZenEditorPlugin } from '@univerjs/sheets-zen-editor';
import { enUS, ruRU, zhCN } from '../locales';
import { DEFAULT_WORKBOOK_DATA_DEMO } from '../data/sheets/demo/default-workbook-data-demo';

Expand Down Expand Up @@ -75,7 +76,7 @@ univer.registerPlugin(UniverSheetsUIPlugin);
// sheet feature plugins

univer.registerPlugin(UniverSheetsNumfmtPlugin);
// univer.registerPlugin(UniverSheetsZenEditorPlugin);
univer.registerPlugin(UniverSheetsZenEditorPlugin);
univer.registerPlugin(UniverFormulaEnginePlugin, {
notExecuteFormula: true,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/docs-drawing-ui/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ export class UniverDocsDrawingUIPlugin extends Plugin {
override onReady(): void {
([
DocDrawingUpdateRenderController,
]).forEach((m) => this._renderManagerSrv.registerRenderController(UniverInstanceType.UNIVER_DOC, m));
]).forEach((m) => this._renderManagerSrv.registerRenderModule(UniverInstanceType.UNIVER_DOC, m));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ export class DocZoomRenderController extends Disposable implements IRenderModule
});

e.preventDefault();
})
);
}));
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/docs-ui/src/docs-ui-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class UniverDocsUIPlugin extends Plugin {
DocFloatingObjectRenderController,
DocTextSelectionRenderController,
]).forEach((m) => {
this._renderManagerSrv.registerRenderController(UniverInstanceType.UNIVER_DOC, m);
this._renderManagerSrv.registerRenderModule(UniverInstanceType.UNIVER_DOC, m);
});
}
}
1 change: 0 additions & 1 deletion packages/docs-ui/src/services/docs-render.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export class DocsRenderService extends RxDisposable {
this._instanceSrv.getAllUnitsForType<DocumentDataModel>(UniverInstanceType.UNIVER_DOC)
.forEach((documentModel) => this._createRenderer(documentModel));

// TODO@wzhudev: maybe we should rollback here later
this._instanceSrv.getTypeOfUnitDisposed$<DocumentDataModel>(UniverInstanceType.UNIVER_DOC)
.pipe(takeUntil(this.dispose$))
.subscribe((doc) => this._disposeRenderer(doc));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,30 @@
* limitations under the License.
*/

import type { IDocumentData } from '@univerjs/core';
/* eslint-disable ts/no-explicit-any */

import type { DocumentDataModel, IDocumentData, Nullable } from '@univerjs/core';
import {
BooleanNumber,
DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
ILogService,
IUniverInstanceService,
LogLevel,
Plugin,
RxDisposable,
Univer,
UniverInstanceType,
} from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import type { Ctor, Dependency, DependencyIdentifier } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';
import type { DocumentSkeleton, IRender, IRenderContext, IRenderModule } from '@univerjs/engine-render';
import { DocumentViewModel, IRenderManagerService } from '@univerjs/engine-render';

import { BehaviorSubject, takeUntil } from 'rxjs';
import { TextSelectionManagerService } from '../../../services/text-selection-manager.service';
import { DocStateChangeManagerService } from '../../../services/doc-state-change-manager.service';
import { IMEInputManagerService } from '../../../services/ime-input-manager.service';
import { DocSkeletonManagerService } from '../../../services/doc-skeleton-manager.service';
import { ITextSelectionRenderManager, TextSelectionRenderManager } from './mock-text-selection-render-manager';

const TEST_DOCUMENT_DATA_EN: IDocumentData = {
Expand Down Expand Up @@ -91,26 +100,21 @@ export function createCommandTestBed(workbookData?: IDocumentData, dependencies?
const injector = univer.__getInjector();
const get = injector.get.bind(injector);

/**
* This plugin hooks into Doc's DI system to expose API to test scripts
*/
class TestPlugin extends Plugin {
static override pluginName = 'test-plugin';

constructor(_config: undefined, @Inject(Injector) override readonly _injector: Injector) {
constructor(
_config: undefined,
@Inject(Injector) override readonly _injector: Injector
) {
super();
}

override onStarting(injector: Injector): void {
injector.add([TextSelectionManagerService]);
injector.add([DocStateChangeManagerService]);
injector.add([IMEInputManagerService]);
injector.add([
ITextSelectionRenderManager,
{
useClass: TextSelectionRenderManager,
},
]);
injector.add([ITextSelectionRenderManager, { useClass: TextSelectionRenderManager }]);

dependencies?.forEach((d) => injector.add(d));
}
Expand All @@ -119,15 +123,112 @@ export function createCommandTestBed(workbookData?: IDocumentData, dependencies?
univer.registerPlugin(TestPlugin);

const doc = univer.createUniverDoc(workbookData || TEST_DOCUMENT_DATA_EN);

const univerInstanceService = get(IUniverInstanceService);

// NOTE: This is pretty hack for the test. But with these hacks we can avoid to create
// real canvas-environment in univerjs/docs. If some we have to do that, this hack could be removed.
// Refer to packages/sheets-ui/src/services/clipboard/__tests__/clipboard-test-bed.ts
const fakeDocSkeletonManager = new MockDocSkeletonManagerService({
unit: doc,
unitId: 'test-doc',
type: UniverInstanceType.UNIVER_DOC,
engine: null as any,
scene: null as any,
mainComponent: null as any,
components: null as any,
isMainScene: true,
}, univerInstanceService);

injector.add([DocSkeletonManagerService, { useValue: fakeDocSkeletonManager as unknown as DocSkeletonManagerService }]);
injector.add([IRenderManagerService, { useClass: MockRenderManagerService as unknown as Ctor<IRenderManagerService> }]);

univerInstanceService.focusUnit('test-doc');

const logService = get(ILogService);
logService.setLogLevel(LogLevel.SILENT); // change this to `LogLevel.VERBOSE` to debug tests via logs
logService.setLogLevel(LogLevel.SILENT);

return {
univer,
get,
doc,
};
}

// These services are for document build and manage doc skeletons.

export class MockRenderManagerService implements Pick<IRenderManagerService, 'getRenderById'> {
constructor(
@Inject(Injector) private readonly _injector: Injector
) {}

getRenderById(_unitId: string): Nullable<IRender> {
return {
with: <T>(identifier: DependencyIdentifier<T>) => this._injector.get(identifier),
} as unknown as IRender;
}
}

export class MockDocSkeletonManagerService extends RxDisposable implements IRenderModule {
private _docViewModel: DocumentViewModel;

private readonly _currentSkeleton$ = new BehaviorSubject<Nullable<DocumentSkeleton>>(null);
readonly currentSkeleton$ = this._currentSkeleton$.asObservable();

// CurrentSkeletonBefore for pre-triggered logic during registration
private readonly _currentSkeletonBefore$ = new BehaviorSubject<Nullable<DocumentSkeleton>>(null);
readonly currentSkeletonBefore$ = this._currentSkeletonBefore$.asObservable();

constructor(
private readonly _context: IRenderContext<DocumentDataModel>,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService
) {
super();

this._update();

this._univerInstanceService.getCurrentTypeOfUnit$<DocumentDataModel>(UniverInstanceType.UNIVER_DOC)
.pipe(takeUntil(this.dispose$))
.subscribe((documentModel) => {
if (documentModel?.getUnitId() === this._context.unitId) {
this._update();
}
});
}

override dispose(): void {
super.dispose();

this._currentSkeletonBefore$.complete();
this._currentSkeleton$.complete();
}

private _update() {
const documentDataModel = this._context.unit;
const unitId = this._context.unitId;

// No need to build view model, if data model has no body.
if (documentDataModel.getBody() == null) {
return;
}

// Always need to reset document data model, because cell editor change doc instance every time.
if (this._docViewModel && unitId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY) {
this._docViewModel.reset(documentDataModel);
} else if (!this._docViewModel) {
this._docViewModel = this._buildDocViewModel(documentDataModel);
}
}

getSkeleton(): DocumentSkeleton {
throw new Error('[MockDocSkeletonManagerService]: cannot access to doc skeleton in unit tests!');
}

getViewModel(): DocumentViewModel {
return this._docViewModel;
}

private _buildDocViewModel(documentDataModel: DocumentDataModel) {
return new DocumentViewModel(documentDataModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ describe('replace or cover content of document', () => {
]);
});

afterEach(() => univer.dispose());
afterEach(() => {
univer.dispose();
});

describe('replace content of document and reserve undo and redo stack', () => {
it('Should pass the test case when replace content', async () => {
Expand Down Expand Up @@ -155,3 +157,4 @@ describe('replace or cover content of document', () => {
});
});
});

Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface IRenderManagerService extends IDisposable {

has(unitId: string): boolean;
withCurrentTypeOfUnit<T>(type: UniverInstanceType, id: DependencyIdentifier<T>): Nullable<T>;
registerRenderController<T extends UnitModel>(type: UnitType, ctor: IRenderModuleCtor<T>): IDisposable;
registerRenderModule<T extends UnitModel>(type: UnitType, ctor: IRenderModuleCtor<T>): IDisposable;
}

const DEFAULT_SCENE_SIZE = { width: 1500, height: 1000 };
Expand Down Expand Up @@ -105,7 +105,7 @@ export class RenderManagerService extends Disposable implements IRenderManagerSe
this._currentRender$.complete();
}

registerRenderController(type: UnitType, ctor: IRenderModuleCtor): IDisposable {
registerRenderModule(type: UnitType, ctor: IRenderModuleCtor): IDisposable {
if (!this._renderControllers.has(type)) {
this._renderControllers.set(type, new Set());
}
Expand Down
4 changes: 2 additions & 2 deletions packages/facade/src/apis/__tests__/create-test-bed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ export function createFacadeTestBed(workbookData?: IWorkbookData, dependencies?:
injector.add([WorksheetProtectionRuleModel]);

const renderManagerService = injector.get(IRenderManagerService);
renderManagerService.registerRenderController(UniverInstanceType.UNIVER_SHEET, SheetSkeletonManagerService);
renderManagerService.registerRenderController(UniverInstanceType.UNIVER_SHEET, SheetRenderController);
renderManagerService.registerRenderModule(UniverInstanceType.UNIVER_SHEET, SheetSkeletonManagerService);
renderManagerService.registerRenderModule(UniverInstanceType.UNIVER_SHEET, SheetRenderController);

SheetsConditionalFormattingPlugin.dependencyList.forEach((d) => {
injector.add(d);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class SheetsFilterUIController extends RxDisposable {
}

private _initRenderControllers(): void {
this.disposeWithMe(this._renderManagerService.registerRenderController(UniverInstanceType.UNIVER_SHEET, SheetsFilterRenderController));
this.disposeWithMe(this._renderManagerService.registerRenderModule(UniverInstanceType.UNIVER_SHEET, SheetsFilterRenderController));
}

private _popupDisposable?: Nullable<IDisposable>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class FormulaUIController extends Disposable {
this._registerMenus();
this._registerShortcuts();
this._registerComponents();
this._registerRenderControllers();
this._registerRenderModules();
}

private _registerMenus(): void {
Expand Down Expand Up @@ -124,7 +124,7 @@ export class FormulaUIController extends Disposable {
this._componentManager.register(MORE_FUNCTIONS_COMPONENT, MoreFunctions);
}

private _registerRenderControllers(): void {
this.disposeWithMe(this._renderManagerService.registerRenderController(UniverInstanceType.UNIVER_SHEET, FormulaEditorShowController));
private _registerRenderModules(): void {
this.disposeWithMe(this._renderManagerService.registerRenderModule(UniverInstanceType.UNIVER_SHEET, FormulaEditorShowController));
}
}
2 changes: 1 addition & 1 deletion packages/sheets-hyper-link-ui/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ export class UniverSheetsHyperLinkUIPlugin extends Plugin {

dependencies.forEach((dep) => injector.add(dep));

this._renderManagerService.registerRenderController(UniverInstanceType.UNIVER_SHEET, SheetsHyperLinkRenderController);
this._renderManagerService.registerRenderModule(UniverInstanceType.UNIVER_SHEET, SheetsHyperLinkRenderController);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -751,24 +751,6 @@ export class EditingRenderController extends Disposable implements IRenderModule
return getEditorObject(this._editorBridgeService.getCurrentEditorId(), this._renderManagerService);
}

// private _init() {
// this._univerInstanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_DOC)
// .pipe(takeUntil(this.dispose$))
// .subscribe((docDataModel) => {
// if (docDataModel == null) {
// return;
// }

// const unitId = docDataModel.getUnitId();

// // Clear undo redo stack of cell editor when lose focus.
// // WTF@wzhudev: this should be implemented in end-editor controller.
// if (unitId !== DOCS_NORMAL_EDITOR_UNIT_ID_KEY) {
// this._undoRedoService.clearUndoRedo(DOCS_NORMAL_EDITOR_UNIT_ID_KEY);
// }
// });
// }

private async _handleEditorInvisible(param: IEditorBridgeServiceVisibleParam) {
const { keycode } = param;

Expand Down
4 changes: 2 additions & 2 deletions packages/sheets-ui/src/sheets-ui-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class UniverSheetsUIPlugin extends Plugin {
SheetSkeletonManagerService,
SheetRenderController,
]).forEach((m) => {
this.disposeWithMe(this._renderManagerService.registerRenderController(UniverInstanceType.UNIVER_SHEET, m));
this.disposeWithMe(this._renderManagerService.registerRenderModule(UniverInstanceType.UNIVER_SHEET, m));
});
}

Expand Down Expand Up @@ -218,7 +218,7 @@ export class UniverSheetsUIPlugin extends Plugin {
SheetPermissionInterceptorFormulaRenderController,
SheetPermissionRenderController,
]).forEach((m) => {
this.disposeWithMe(this._renderManagerService.registerRenderController(UniverInstanceType.UNIVER_SHEET, m));
this.disposeWithMe(this._renderManagerService.registerRenderModule(UniverInstanceType.UNIVER_SHEET, m));
});
}

Expand Down
Loading