refactor(editor): reduce getService (#10100)

This commit is contained in:
Saul-Mirone
2025-02-11 12:26:01 +00:00
parent dbf0f9dc20
commit 6b78d2dcf2
33 changed files with 189 additions and 300 deletions

View File

@@ -1,7 +1,7 @@
import type { DefaultOpenProperty } from '@affine/core/components/doc-properties';
import { GfxControllerIdentifier } from '@blocksuite/affine/block-std/gfx';
import {
type DocMode,
EdgelessRootService,
FeatureFlagService as BSFeatureFlagService,
HighlightSelection,
type ReferenceParams,
@@ -73,13 +73,11 @@ export class Editor extends Entity {
isPresenting$ = new LiveData<boolean>(false);
togglePresentation() {
const edgelessRootService =
this.editorContainer$.value?.host?.std.getService(
'affine:page'
) as EdgelessRootService;
if (!edgelessRootService) return;
edgelessRootService.gfx.tool.setTool({
const gfx = this.editorContainer$.value?.host?.std.get(
GfxControllerIdentifier
);
if (!gfx) return;
gfx.tool.setTool({
type: !this.isPresenting$.value ? 'frameNavigator' : 'default',
});
}
@@ -200,7 +198,7 @@ export class Editor extends Entity {
this.editorContainer$.next(editorContainer);
const unsubs: (() => void)[] = [];
const rootService = editorContainer.host?.std.getService('affine:page');
const gfx = editorContainer.host?.std.get(GfxControllerIdentifier);
// ----- Scroll Position and Selection -----
// if we have default scroll position, we should restore it
@@ -209,9 +207,9 @@ export class Editor extends Entity {
} else if (
this.mode$.value === 'edgeless' &&
this.scrollPosition.edgeless &&
rootService instanceof EdgelessRootService
gfx
) {
rootService.viewport.setViewport(this.scrollPosition.edgeless.zoom, [
gfx.viewport.setViewport(this.scrollPosition.edgeless.zoom, [
this.scrollPosition.edgeless.centerX,
this.scrollPosition.edgeless.centerY,
]);
@@ -253,14 +251,11 @@ export class Editor extends Entity {
if (this.mode$.value === 'page' && scrollViewport) {
this.scrollPosition.page = scrollViewport.scrollTop;
this.workbenchView?.setScrollPosition(scrollViewport.scrollTop);
} else if (
this.mode$.value === 'edgeless' &&
rootService instanceof EdgelessRootService
) {
} else if (this.mode$.value === 'edgeless' && gfx) {
const pos = {
centerX: rootService.viewport.centerX,
centerY: rootService.viewport.centerY,
zoom: rootService.viewport.zoom,
centerX: gfx.viewport.centerX,
centerY: gfx.viewport.centerY,
zoom: gfx.viewport.zoom,
};
this.scrollPosition.edgeless = pos;
this.workbenchView?.setScrollPosition(pos);
@@ -270,10 +265,8 @@ export class Editor extends Entity {
unsubs.push(() => {
scrollViewport?.removeEventListener('scroll', saveScrollPosition);
});
if (rootService instanceof EdgelessRootService) {
unsubs.push(
rootService.viewport.viewportUpdated.on(saveScrollPosition).dispose
);
if (gfx) {
unsubs.push(gfx.viewport.viewportUpdated.on(saveScrollPosition).dispose);
}
// update selection when focusAt$ changed

View File

@@ -10,10 +10,8 @@ import { EditorOutlineViewer } from '@affine/core/components/blocksuite/outline-
import { PageNotFound } from '@affine/core/desktop/pages/404';
import { EditorService } from '@affine/core/modules/editor';
import { DebugLogger } from '@affine/debug';
import {
type EdgelessRootService,
RefNodeSlotsProvider,
} from '@blocksuite/affine/blocks';
import { GfxControllerIdentifier } from '@blocksuite/affine/block-std/gfx';
import { RefNodeSlotsProvider } from '@blocksuite/affine/blocks';
import {
Bound,
type Disposable,
@@ -46,25 +44,22 @@ function fitViewport(
throw new Error('editor host is not ready');
}
const rootService =
editor.host.std.getService<EdgelessRootService>('affine:page');
if (!rootService) {
return;
}
rootService.viewport.onResize();
const gfx = editor.host.std.get(GfxControllerIdentifier);
const viewport = gfx.viewport;
viewport.onResize();
if (xywh) {
const viewport = {
const newViewport = {
xywh: xywh,
padding: [60, 20, 20, 20] as [number, number, number, number],
};
rootService.viewport.setViewportByBound(
Bound.deserialize(viewport.xywh),
viewport.padding,
viewport.setViewportByBound(
Bound.deserialize(newViewport.xywh),
newViewport.padding,
false
);
} else {
rootService.gfx.fitToScreen({
gfx.fitToScreen({
smooth: false,
});
}