-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): improve scroll anchoring logic (#8269)
Upstreams: toeverything/blocksuite#8378
- Loading branch information
Showing
4 changed files
with
39 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 31 additions & 103 deletions
134
packages/frontend/core/src/modules/editor/utils/scroll-anchoring.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,43 @@ | ||
import { notify } from '@affine/component'; | ||
import { I18n } from '@affine/i18n'; | ||
import type { BlockStdScope, SelectionManager } from '@blocksuite/block-std'; | ||
import type { | ||
DocMode, | ||
EdgelessRootService, | ||
PageRootService, | ||
} from '@blocksuite/blocks'; | ||
import { Bound, deserializeXYWH } from '@blocksuite/global/utils'; | ||
import type { BlockStdScope } from '@blocksuite/block-std'; | ||
import { | ||
GfxControllerIdentifier, | ||
type GfxModel, | ||
} from '@blocksuite/block-std/gfx'; | ||
import type { DocMode } from '@blocksuite/blocks'; | ||
|
||
function scrollAnchoringInEdgelessMode( | ||
service: EdgelessRootService, | ||
id: string | ||
) { | ||
requestAnimationFrame(() => { | ||
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') { | ||
pageSelection = parentComponent.std.selection; | ||
if (!pageSelection) return; | ||
|
||
const { left: x, width: w } = parentComponent.getBoundingClientRect(); | ||
const { top: y, height: h } = blockComponent.getBoundingClientRect(); | ||
const coord = service.viewport.toModelCoordFromClientCoord([x, y]); | ||
bounds = new Bound( | ||
coord[0], | ||
coord[1], | ||
w / service.viewport.zoom, | ||
h / service.viewport.zoom | ||
); | ||
} else { | ||
if (!service.getElementById) return; | ||
const model = service.getElementById(id); | ||
if (!model) return; | ||
|
||
bounds = Bound.fromXYWH(deserializeXYWH(model.xywh)); | ||
} | ||
|
||
if (!bounds) { | ||
notify.error({ title: I18n['Block not found']() }); | ||
return; | ||
} | ||
|
||
const { zoom, centerX, centerY } = service.getFitToScreenData( | ||
[20, 20, 100, 20], | ||
[bounds] | ||
); | ||
|
||
service.viewport.setCenter(centerX, centerY); | ||
service.viewport.setZoom(zoom); | ||
|
||
if (pageSelection) { | ||
pageSelection.setGroup('note', [ | ||
pageSelection.create('block', { | ||
blockId: id, | ||
}), | ||
]); | ||
} else { | ||
service.selection.set({ | ||
elements: [id], | ||
editing: false, | ||
}); | ||
// TODO(@fundon): it should be a command | ||
export function scrollAnchoring(std: BlockStdScope, mode: DocMode, id: string) { | ||
let key = 'blockIds'; | ||
let exists = false; | ||
|
||
if (mode === 'page') { | ||
exists = std.doc.hasBlock(id); | ||
} else { | ||
const controller = std.getOptional(GfxControllerIdentifier); | ||
if (!controller) return; | ||
|
||
exists = !!controller.getElementById<GfxModel>(id)?.xywh; | ||
if (controller.surface?.hasElementById(id)) { | ||
key = 'elementIds'; | ||
} | ||
} | ||
|
||
// const surfaceComponent = service.std.view.getBlock(service.surface.id); | ||
// if (!surfaceComponent) return; | ||
// (surfaceComponent as SurfaceBlockComponent).refresh(); | ||
|
||
// TODO(@fundon): toolbar should be hidden | ||
}); | ||
} | ||
|
||
function scrollAnchoringInPageMode(service: PageRootService, id: string) { | ||
const blockComponent = service.std.view.getBlock(id); | ||
if (!blockComponent) { | ||
notify.error({ title: I18n['Block not found']() }); | ||
if (!exists) { | ||
notify.error({ | ||
title: I18n['Block not found'](), | ||
message: I18n['Block not found description'](), | ||
}); | ||
return; | ||
} | ||
|
||
blockComponent.scrollIntoView({ | ||
behavior: 'instant', | ||
block: 'center', | ||
}); | ||
|
||
const selection = service.std.selection; | ||
if (!selection) return; | ||
const selection = std.selection; | ||
|
||
selection.setGroup('note', [ | ||
selection.create('block', { | ||
blockId: id, | ||
selection.setGroup('scene', [ | ||
selection.create('highlight', { | ||
mode, | ||
[key]: [id], | ||
}), | ||
]); | ||
|
||
// TODO(@fundon): toolbar should be hidden | ||
} | ||
|
||
// TODO(@fundon): it should be a command | ||
export function scrollAnchoring(std: BlockStdScope, mode: DocMode, id: string) { | ||
if (mode === 'edgeless') { | ||
const service = std.getService<EdgelessRootService>('affine:page'); | ||
if (!service) return; | ||
|
||
scrollAnchoringInEdgelessMode(service, id); | ||
|
||
return; | ||
} | ||
|
||
const service = std.getService<PageRootService>('affine:page'); | ||
if (!service) return; | ||
|
||
scrollAnchoringInPageMode(service, id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters