Skip to content

Commit

Permalink
chore(core): improve scroll anchoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Sep 12, 2024
1 parent 3211788 commit 2c0569b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
9 changes: 4 additions & 5 deletions packages/frontend/core/src/modules/editor/entities/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,10 @@ export class Editor extends Entity {
get(this.mode$) === 'edgeless' && selector?.elementIds?.length
? selector?.elementIds?.[0]
: selector?.blockIds?.[0];
if (id) {
return { id, refreshKey: selector?.refreshKey };
} else {
return null;
}

if (!id) return null;

return { id, refreshKey: selector?.refreshKey };
});
if (focusAt$.value === null && docTitle) {
const title = docTitle.querySelector<
Expand Down
42 changes: 21 additions & 21 deletions packages/frontend/core/src/modules/editor/utils/scroll-anchoring.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
import type { BlockStdScope } from '@blocksuite/block-std';
import type { BlockStdScope, SelectionManager } from '@blocksuite/block-std';
import type {
DocMode,
EdgelessRootService,
PageRootService,
} from '@blocksuite/blocks';
import { ZOOM_MAX } from '@blocksuite/blocks';
import { Bound, deserializeXYWH } from '@blocksuite/global/utils';

function scrollAnchoringInEdgelessMode(
service: EdgelessRootService,
id: string
) {
requestAnimationFrame(() => {
let isNotInNote = true;
let bounds: Bound | null = null;
let pageSelection: SelectionManager | null = null;

const blockComponent = service.std.view.getBlock(id);

const parentComponent = blockComponent?.parentComponent;
if (parentComponent && parentComponent.flavour === 'affine:note') {
isNotInNote = false;

const selection = parentComponent.std.selection;
if (!selection) return;

selection.set([
selection.create('block', {
blockId: id,
}),
]);
pageSelection = parentComponent.std.selection;
if (!pageSelection) return;

const { left: x, width: w } = parentComponent.getBoundingClientRect();
const { top: y, height: h } = blockComponent.getBoundingClientRect();
Expand All @@ -48,20 +40,28 @@ function scrollAnchoringInEdgelessMode(

if (!bounds) return;

if (isNotInNote) {
const { zoom, centerX, centerY } = service.getFitToScreenData(
[20, 20, 100, 20],
[bounds],
ZOOM_MAX

Check failure on line 46 in packages/frontend/core/src/modules/editor/utils/scroll-anchoring.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected 0-2 arguments, but got 3.
);

service.viewport.setCenter(centerX, centerY);
service.viewport.setZoom(zoom);

if (pageSelection) {
pageSelection.set([
pageSelection.create('block', {
blockId: id,
}),
]);
} else {
service.selection.set({
elements: [id],
editing: false,
});
}

const { zoom, centerX, centerY } = service.getFitToScreenData(
[20, 20, 20, 20],
[bounds]
);

service.viewport.setViewport(zoom, [centerX, centerY]);

// const surfaceComponent = service.std.view.getBlock(service.surface.id);
// if (!surfaceComponent) return;
// (surfaceComponent as SurfaceBlockComponent).refresh();
Expand Down

0 comments on commit 2c0569b

Please sign in to comment.