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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: refactor zoom controller
  • Loading branch information
wzhudev committed Jun 17, 2024
commit d3c13aebb18ddca73a590fae47f0d2c29d352821
172 changes: 61 additions & 111 deletions packages/docs-ui/src/controllers/zoom.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,170 +14,124 @@
* limitations under the License.
*/

import type { ICommandInfo } from '@univerjs/core';
import type { DocumentDataModel, ICommandInfo } from '@univerjs/core';
import {
Disposable,
ICommandService,
IUniverInstanceService,
LifecycleStages,
OnLifecycle,
toDisposable,
} from '@univerjs/core';
import type { IDocObjectParam } from '@univerjs/docs';
import { DocSkeletonManagerService, getDocObject, SetDocZoomRatioCommand, SetDocZoomRatioOperation, TextSelectionManagerService, VIEWPORT_KEY } from '@univerjs/docs';
import type { IWheelEvent } from '@univerjs/engine-render';
import { IRenderManagerService } from '@univerjs/engine-render';
import { DocSkeletonManagerService, neoGetDocObject, SetDocZoomRatioCommand, SetDocZoomRatioOperation, TextSelectionManagerService, VIEWPORT_KEY } from '@univerjs/docs';
import type { IRenderContext, IRenderModule, IWheelEvent } from '@univerjs/engine-render';
import { IEditorService } from '@univerjs/ui';
import { Inject } from '@wendellhu/redi';

interface ISetDocMutationParams {
unitId: string;
}

@OnLifecycle(LifecycleStages.Rendered, ZoomController)
export class ZoomController extends Disposable {
private _initializedRender = new Set();

export class DocZoomRenderController extends Disposable implements IRenderModule {
constructor(
private readonly _context: IRenderContext<DocumentDataModel>,
@Inject(DocSkeletonManagerService) private readonly _docSkeletonManagerService: DocSkeletonManagerService,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@ICommandService private readonly _commandService: ICommandService,
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
@Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService,
@IEditorService private readonly _editorService: IEditorService
) {
super();

this._initialize();
}

override dispose(): void {
super.dispose();
this._init();
}

private _initialize() {
this._skeletonListener();
this._commandExecutedListener();
this._initialRenderRefresh();
private _init() {
this._initSkeletonListener();
this._initCommandExecutedListener();
this._initRenderRefresher();
}

private _initialRenderRefresh() {
private _initRenderRefresher() {
this._docSkeletonManagerService.currentSkeleton$.subscribe((param) => {
if (param == null) {
return;
}

const { unitId } = param;

const currentRender = this._renderManagerService.getRenderById(unitId);

if (currentRender == null) {
return;
}

if (
this._initializedRender.has(unitId) || this._editorService.isEditor(unitId)
) {
const { unitId, scene } = this._context;
if (this._editorService.isEditor(unitId)) {
return;
}

this._initializedRender.add(unitId);

const { scene } = currentRender;

this.disposeWithMe(
toDisposable(
scene.onMouseWheelObserver.add((e: IWheelEvent) => {
if (!e.ctrlKey) {
return;
}
this.disposeWithMe(scene.onMouseWheelObserver.add((e: IWheelEvent) => {
if (!e.ctrlKey) {
return;
}

const documentModel = this._univerInstanceService.getCurrentUniverDocInstance();
if (!documentModel) {
return;
}
const documentModel = this._univerInstanceService.getCurrentUniverDocInstance();
if (!documentModel) {
return;
}

const deltaFactor = Math.abs(e.deltaX);
let ratioDelta = deltaFactor < 40 ? 0.2 : deltaFactor < 80 ? 0.4 : 0.2;
ratioDelta *= e.deltaY > 0 ? -1 : 1;
if (scene.scaleX < 1) {
ratioDelta /= 2;
}
const deltaFactor = Math.abs(e.deltaX);
let ratioDelta = deltaFactor < 40 ? 0.2 : deltaFactor < 80 ? 0.4 : 0.2;
ratioDelta *= e.deltaY > 0 ? -1 : 1;
if (scene.scaleX < 1) {
ratioDelta /= 2;
}

const currentRatio = documentModel.zoomRatio;
const currentRatio = documentModel.zoomRatio;

let nextRatio = +Number.parseFloat(`${currentRatio + ratioDelta}`).toFixed(1);
nextRatio = nextRatio >= 4 ? 4 : nextRatio <= 0.1 ? 0.1 : nextRatio;
let nextRatio = +Number.parseFloat(`${currentRatio + ratioDelta}`).toFixed(1);
nextRatio = nextRatio >= 4 ? 4 : nextRatio <= 0.1 ? 0.1 : nextRatio;

this._commandService.executeCommand(SetDocZoomRatioCommand.id, {
zoomRatio: nextRatio,
unitId: documentModel.getUnitId(),
});
this._commandService.executeCommand(SetDocZoomRatioCommand.id, {
zoomRatio: nextRatio,
unitId: documentModel.getUnitId(),
});

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

// private _zoomEventBinding() {
// const scene = this._getDocObject()?.scene;
// if (scene == null) {
// return;
// }

// const viewportMain = scene.getViewport(VIEWPORT_KEY.VIEW_MAIN);
// }

private _skeletonListener() {
this.disposeWithMe(
toDisposable(
this._docSkeletonManagerService.currentSkeleton$.subscribe((param) => {
if (param == null) {
return;
}
private _initSkeletonListener() {
this.disposeWithMe(this._docSkeletonManagerService.currentSkeleton$.subscribe((param) => {
if (param == null) {
return;
}

const documentModel = this._univerInstanceService.getCurrentUniverDocInstance();
if (!documentModel) return;
const documentModel = this._univerInstanceService.getCurrentUniverDocInstance();
if (!documentModel) return;

const zoomRatio = documentModel.zoomRatio || 1;
const zoomRatio = documentModel.zoomRatio || 1;

this._updateViewZoom(zoomRatio, false);
})
)
);
this._updateViewZoom(zoomRatio, false);
}));
}

private _commandExecutedListener() {
private _initCommandExecutedListener() {
const updateCommandList = [SetDocZoomRatioOperation.id];

this.disposeWithMe(
this._commandService.onCommandExecuted((command: ICommandInfo) => {
if (updateCommandList.includes(command.id)) {
const documentModel = this._univerInstanceService.getCurrentUniverDocInstance();
if (!documentModel) return;
this.disposeWithMe(this._commandService.onCommandExecuted((command: ICommandInfo) => {
if (updateCommandList.includes(command.id)) {
const documentModel = this._univerInstanceService.getCurrentUniverDocInstance();
if (!documentModel) return;

const params = command.params;
const { unitId } = params as ISetDocMutationParams;
if (!(unitId === documentModel.getUnitId())) {
return;
}
const params = command.params;
const { unitId } = params as ISetDocMutationParams;
if (!(unitId === documentModel.getUnitId())) {
return;
}

const zoomRatio = documentModel.zoomRatio || 1;
const zoomRatio = documentModel.zoomRatio || 1;

this._updateViewZoom(zoomRatio);
}
})
);
this._updateViewZoom(zoomRatio);
}
}));
}

private _updateViewZoom(zoomRatio: number, needRefreshSelection = true) {
const docObject = this._getDocObject();
if (docObject == null) {
return;
}

const docObject = neoGetDocObject(this._context);
docObject.scene.scale(zoomRatio, zoomRatio);

this._calculatePagePosition(docObject, zoomRatio);
Expand Down Expand Up @@ -248,8 +202,4 @@ export class ZoomController extends Disposable {

return this;
}

private _getDocObject() {
return getDocObject(this._univerInstanceService, this._renderManagerService);
}
}
6 changes: 4 additions & 2 deletions packages/docs-ui/src/docs-ui-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import { DocClipboardController } from './controllers/clipboard.controller';
import { DocEditorBridgeController } from './controllers/doc-editor-bridge.controller';
import { DocRenderController } from './views/doc-canvas-view';
import { DocFloatingObjectController } from './controllers/doc-floating-object.controller';
import { ZoomController } from './controllers/zoom.controller';
import { DocZoomRenderController } from './controllers/zoom.controller';
import { TextSelectionController } from './controllers/text-selection.controller';
import { BackScrollController } from './controllers/back-scroll.controller';
import { DocCanvasPopManagerService } from './services/doc-popup-manager.service';
Expand Down Expand Up @@ -113,10 +113,11 @@ export class UniverDocsUIPlugin extends Plugin {
},
],
[DocClipboardController],

// Some of the controllers here should be moved to RenderUnit.
[DocEditorBridgeController],
[DocsRenderService],
[DocFloatingObjectController],
[ZoomController],
[TextSelectionController],
[BackScrollController],
[
Expand Down Expand Up @@ -163,6 +164,7 @@ export class UniverDocsUIPlugin extends Plugin {
private _initRenderBasics(): void {
([
DocRenderController,
DocZoomRenderController,
]).forEach((m) => {
this._renderManagerSrv.registerRenderController(UniverInstanceType.UNIVER_DOC, m);
});
Expand Down
19 changes: 17 additions & 2 deletions packages/docs/src/basics/component-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* limitations under the License.
*/

import { type IUniverInstanceService, type Nullable, UniverInstanceType } from '@univerjs/core';
import type { DocBackground, Documents, Engine, IRenderManagerService, Scene } from '@univerjs/engine-render';
import type { DocumentDataModel, IUniverInstanceService, Nullable } from '@univerjs/core';
import { UniverInstanceType } from '@univerjs/core';
import type { DocBackground, Documents, Engine, IRenderContext, IRenderManagerService, Scene } from '@univerjs/engine-render';
import { DOCS_VIEW_KEY } from './docs-view-key';

export interface IDocObjectParam {
Expand All @@ -25,6 +26,20 @@ export interface IDocObjectParam {
engine: Engine;
}

export function neoGetDocObject(renderContext: IRenderContext<DocumentDataModel>) {
const { mainComponent, scene, engine, components } = renderContext;
const document = mainComponent as Documents;
const docBackground = components.get(DOCS_VIEW_KEY.BACKGROUND) as DocBackground;

return {
document,
docBackground,
scene,
engine,
};
}

/** @deprecated After migrating to `RenderUnit`, use `neoGetDocObject` instead. */
export function getDocObject(
univerInstanceService: IUniverInstanceService,
renderManagerService: IRenderManagerService
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

export type { IDocObjectParam } from './basics/component-tools';
export { getDocObject, getDocObjectById } from './basics/component-tools';
export { getDocObject, neoGetDocObject, getDocObjectById } from './basics/component-tools';
export * from './basics/docs-view-key';
export { BreakLineCommand } from './commands/commands/break-line.command';
export {
Expand Down