mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-24 05:07:06 +08:00
fix(editor): handle resize in turbo renderer (#10465)
This commit is contained in:
@@ -28,8 +28,8 @@ interface Tile {
|
|||||||
zoom: number;
|
zoom: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
// With high enough zoom, fallback to DOM rendering
|
const zoomThreshold = 1; // With high enough zoom, fallback to DOM rendering
|
||||||
const zoomThreshold = 1;
|
const debounceTime = 1000; // During this period, fallback to DOM
|
||||||
const debug = false; // Toggle for debug logs
|
const debug = false; // Toggle for debug logs
|
||||||
|
|
||||||
export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
||||||
@@ -66,6 +66,10 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
this.viewportElement = element;
|
this.viewportElement = element;
|
||||||
syncCanvasSize(this.canvas, this.std.host);
|
syncCanvasSize(this.canvas, this.std.host);
|
||||||
this.setState('pending');
|
this.setState('pending');
|
||||||
|
|
||||||
|
this.disposables.add(
|
||||||
|
this.viewport.sizeUpdated.on(() => this.handleResize())
|
||||||
|
);
|
||||||
this.disposables.add(
|
this.disposables.add(
|
||||||
this.viewport.viewportUpdated.on(() => {
|
this.viewport.viewportUpdated.on(() => {
|
||||||
this.refresh().catch(console.error);
|
this.refresh().catch(console.error);
|
||||||
@@ -134,7 +138,7 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
() => {
|
() => {
|
||||||
this.refresh().catch(console.error);
|
this.refresh().catch(console.error);
|
||||||
},
|
},
|
||||||
1000, // During this period, fallback to DOM
|
debounceTime,
|
||||||
{ leading: false, trailing: true }
|
{ leading: false, trailing: true }
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -257,6 +261,13 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
this.state = newState;
|
this.state = newState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
canOptimize(): boolean {
|
||||||
|
const isReady = this.state === 'ready';
|
||||||
|
const isBelowZoomThreshold = this.viewport.zoom <= zoomThreshold;
|
||||||
|
const result = isReady && isBelowZoomThreshold;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
private updateOptimizedBlocks() {
|
private updateOptimizedBlocks() {
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
if (!this.viewportElement || !this.layoutCache) return;
|
if (!this.viewportElement || !this.layoutCache) return;
|
||||||
@@ -281,13 +292,6 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
this.debugLog('Cleared optimized blocks');
|
this.debugLog('Cleared optimized blocks');
|
||||||
}
|
}
|
||||||
|
|
||||||
canOptimize(): boolean {
|
|
||||||
const isReady = this.state === 'ready';
|
|
||||||
const isBelowZoomThreshold = this.viewport.zoom <= zoomThreshold;
|
|
||||||
const result = isReady && isBelowZoomThreshold;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private toggleOptimization(value: boolean) {
|
private toggleOptimization(value: boolean) {
|
||||||
if (
|
if (
|
||||||
this.viewportElement &&
|
this.viewportElement &&
|
||||||
@@ -297,4 +301,11 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
this.debugLog(`${value ? 'Enabled' : 'Disabled'} optimization`);
|
this.debugLog(`${value ? 'Enabled' : 'Disabled'} optimization`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private handleResize() {
|
||||||
|
this.debugLog('Container resized, syncing canvas size');
|
||||||
|
syncCanvasSize(this.canvas, this.std.host);
|
||||||
|
this.invalidate();
|
||||||
|
this.debouncedRefresh();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user