mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 20:38:52 +00:00
Currently, `GfxViewportElement` hides DOM blocks outside the viewport using `display: none` to optimize performance. However, this approach presents two issues: 1. Even when hidden, all top-level blocks still undergo frequent CSS transform updates during viewport panning and zooming. 2. Hidden blocks cannot access DOM layout information, preventing `TurboRenderer` from updating the complete canvas bitmap. To address this, this PR introduces a refactoring that divides all top-level edgeless blocks into two states: `idle` and `active`. The improvements are as follows: 1. Blocks outside the viewport are set to the `idle` state, meaning they no longer update their DOM during viewport panning or zooming. Only `active` blocks within the viewport are updated frame by frame. 2. For `idle` blocks, the hiding method switches from `display: none` to `visibility: hidden`, ensuring their layout information remains accessible to `TurboRenderer`. [Screen Recording 2025-03-07 at 3.23.56 PM.mov <span class="graphite__hidden">(uploaded via Graphite)</span> <img class="graphite__hidden" src="https://app.graphite.dev/api/v1/graphite/video/thumbnail/lEGcysB4lFTEbCwZ8jMv/4bac640b-f5b6-4b0b-904d-5899f96cf375.mov" />](https://app.graphite.dev/media/video/lEGcysB4lFTEbCwZ8jMv/4bac640b-f5b6-4b0b-904d-5899f96cf375.mov) While this minimizes DOM updates, it introduces a trade-off: `idle` blocks retain an outdated layout state. Since their positions are updated using a lazy update strategy, their layout state remains frozen at the moment they were last moved out of the viewport:  To resolve this, the PR serializes and stores the viewport field of the block at that moment on the `idle` block itself. This allows the correct layout, positioned in the model coordinate system, to be restored from the stored data.
217 lines
5.4 KiB
TypeScript
217 lines
5.4 KiB
TypeScript
import { WithDisposable } from '@blocksuite/global/lit';
|
|
import { css, html } from 'lit';
|
|
import { property } from 'lit/decorators.js';
|
|
|
|
import { PropTypes, requiredProperties } from '../view/decorators/required.js';
|
|
import {
|
|
type BlockComponent,
|
|
type EditorHost,
|
|
ShadowlessElement,
|
|
} from '../view/index.js';
|
|
import type { GfxBlockElementModel } from './model/gfx-block-model.js';
|
|
import { Viewport } from './viewport.js';
|
|
|
|
/**
|
|
* A wrapper around `requestConnectedFrame` that only calls at most once in one frame
|
|
*/
|
|
export function requestThrottledConnectedFrame<
|
|
T extends (...args: unknown[]) => void,
|
|
>(func: T, element?: HTMLElement): T {
|
|
let raqId: number | undefined = undefined;
|
|
let latestArgs: unknown[] = [];
|
|
|
|
return ((...args: unknown[]) => {
|
|
latestArgs = args;
|
|
|
|
if (raqId === undefined) {
|
|
raqId = requestAnimationFrame(() => {
|
|
raqId = undefined;
|
|
|
|
if (!element || element.isConnected) {
|
|
func(...latestArgs);
|
|
}
|
|
});
|
|
}
|
|
}) as T;
|
|
}
|
|
|
|
function setBlockState(view: BlockComponent | null, state: 'active' | 'idle') {
|
|
if (!view) return;
|
|
|
|
if (state === 'active') {
|
|
view.style.visibility = 'visible';
|
|
view.style.pointerEvents = 'auto';
|
|
view.classList.remove('block-idle');
|
|
view.classList.add('block-active');
|
|
view.dataset.blockState = 'active';
|
|
} else {
|
|
view.style.visibility = 'hidden';
|
|
view.style.pointerEvents = 'none';
|
|
view.classList.remove('block-active');
|
|
view.classList.add('block-idle');
|
|
view.dataset.blockState = 'idle';
|
|
}
|
|
}
|
|
|
|
@requiredProperties({
|
|
viewport: PropTypes.instanceOf(Viewport),
|
|
})
|
|
export class GfxViewportElement extends WithDisposable(ShadowlessElement) {
|
|
static override styles = css`
|
|
gfx-viewport {
|
|
position: absolute;
|
|
left: 0;
|
|
top: 0;
|
|
contain: size layout style;
|
|
display: block;
|
|
transform: none;
|
|
}
|
|
|
|
/* CSS for idle blocks that are hidden but maintain layout */
|
|
.block-idle {
|
|
visibility: hidden;
|
|
pointer-events: none;
|
|
will-change: transform;
|
|
contain: size layout style;
|
|
}
|
|
|
|
/* CSS for active blocks participating in viewport transformations */
|
|
.block-active {
|
|
visibility: visible;
|
|
pointer-events: auto;
|
|
}
|
|
`;
|
|
|
|
private readonly _hideOutsideBlock = () => {
|
|
if (!this.host) return;
|
|
|
|
const { host } = this;
|
|
const modelsInViewport = this.getModelsInViewport();
|
|
|
|
modelsInViewport.forEach(model => {
|
|
const view = host.std.view.getBlock(model.id);
|
|
setBlockState(view, 'active');
|
|
|
|
if (this._lastVisibleModels?.has(model)) {
|
|
this._lastVisibleModels!.delete(model);
|
|
}
|
|
});
|
|
|
|
this._lastVisibleModels?.forEach(model => {
|
|
const view = host.std.view.getBlock(model.id);
|
|
setBlockState(view, 'idle');
|
|
});
|
|
|
|
this._lastVisibleModels = modelsInViewport;
|
|
};
|
|
|
|
private _lastVisibleModels?: Set<GfxBlockElementModel>;
|
|
|
|
private readonly _pendingChildrenUpdates: {
|
|
id: string;
|
|
resolve: () => void;
|
|
}[] = [];
|
|
|
|
private readonly _refreshViewport = requestThrottledConnectedFrame(() => {
|
|
this._hideOutsideBlock();
|
|
}, this);
|
|
|
|
private _updatingChildrenFlag = false;
|
|
|
|
override connectedCallback(): void {
|
|
super.connectedCallback();
|
|
|
|
const viewportUpdateCallback = () => {
|
|
this._refreshViewport();
|
|
};
|
|
|
|
if (!this.enableChildrenSchedule) {
|
|
delete this.scheduleUpdateChildren;
|
|
}
|
|
|
|
this._hideOutsideBlock();
|
|
this.disposables.add(
|
|
this.viewport.viewportUpdated.on(() => viewportUpdateCallback())
|
|
);
|
|
this.disposables.add(
|
|
this.viewport.sizeUpdated.on(() => viewportUpdateCallback())
|
|
);
|
|
}
|
|
|
|
override render() {
|
|
return html``;
|
|
}
|
|
|
|
scheduleUpdateChildren? = (id: string) => {
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
|
|
|
this._pendingChildrenUpdates.push({ id, resolve });
|
|
|
|
if (!this._updatingChildrenFlag) {
|
|
this._updatingChildrenFlag = true;
|
|
const schedule = () => {
|
|
if (this._pendingChildrenUpdates.length) {
|
|
const childToUpdates = this._pendingChildrenUpdates.splice(
|
|
0,
|
|
this.maxConcurrentRenders
|
|
);
|
|
|
|
childToUpdates.forEach(({ resolve }) => resolve());
|
|
|
|
if (this._pendingChildrenUpdates.length) {
|
|
requestAnimationFrame(() => {
|
|
this.isConnected && schedule();
|
|
});
|
|
} else {
|
|
this._updatingChildrenFlag = false;
|
|
}
|
|
}
|
|
};
|
|
|
|
requestAnimationFrame(() => {
|
|
this.isConnected && schedule();
|
|
});
|
|
}
|
|
|
|
return promise;
|
|
};
|
|
|
|
@property({ attribute: false })
|
|
accessor getModelsInViewport: () => Set<GfxBlockElementModel> = () =>
|
|
new Set();
|
|
|
|
@property({ attribute: false })
|
|
accessor host: undefined | EditorHost;
|
|
|
|
@property({ type: Number })
|
|
accessor maxConcurrentRenders: number = 2;
|
|
|
|
@property({ attribute: false })
|
|
accessor enableChildrenSchedule: boolean = true;
|
|
|
|
@property({ attribute: false })
|
|
accessor viewport!: Viewport;
|
|
|
|
setBlocksActive(blockIds: string[]): void {
|
|
if (!this.host) return;
|
|
|
|
blockIds.forEach(id => {
|
|
const view = this.host?.std.view.getBlock(id);
|
|
if (view) {
|
|
setBlockState(view, 'active');
|
|
}
|
|
});
|
|
}
|
|
|
|
setBlocksIdle(blockIds: string[]): void {
|
|
if (!this.host) return;
|
|
|
|
blockIds.forEach(id => {
|
|
const view = this.host?.std.view.getBlock(id);
|
|
if (view) {
|
|
setBlockState(view, 'idle');
|
|
}
|
|
});
|
|
}
|
|
}
|