Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(editor): remove non null asserts in turbo renderer #10454

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { EditorHost } from '@blocksuite/block-std';
import { type Viewport } from '@blocksuite/block-std/gfx';
import { Pane } from 'tweakpane';

Expand All @@ -23,7 +24,7 @@ export function syncCanvasSize(canvas: HTMLCanvasElement, host: HTMLElement) {
}

export function getViewportLayout(
host: HTMLElement,
host: EditorHost,
viewport: Viewport
): ViewportLayout {
const paragraphBlocks = host.querySelectorAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {

public readonly canvas: HTMLCanvasElement = document.createElement('canvas');
private readonly worker: Worker;
private layoutCache: ViewportLayout | null = null;
private layoutCacheData: ViewportLayout | null = null;
private tile: Tile | null = null;
private viewportElement: GfxViewportElement | null = null;

Expand Down Expand Up @@ -95,6 +95,13 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
return this.std.get(GfxControllerIdentifier).viewport;
}

get layoutCache() {
if (this.layoutCacheData) return this.layoutCacheData;
const layout = getViewportLayout(this.std.host, this.viewport);
this.debugLog('Layout cache updated');
return (this.layoutCacheData = layout);
}

async refresh() {
if (this.state === 'inactive') return;

Expand All @@ -110,19 +117,15 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
else if (this.canUseBitmapCache()) {
this.debugLog('Using cached bitmap');
this.setState('ready');
this.drawCachedBitmap(this.layoutCache!);
this.drawCachedBitmap(this.layoutCache);
this.updateOptimizedBlocks();
}
// -> rendering
else {
if (!this.layoutCache) {
this.updateLayoutCache();
}
const layout = this.layoutCache!;
this.setState('rendering');
this.toggleOptimization(false);
await this.paintLayout(layout);
this.drawCachedBitmap(layout);
await this.paintLayout(this.layoutCache);
this.drawCachedBitmap(this.layoutCache);
this.updateOptimizedBlocks();
}
}
Expand All @@ -137,7 +140,7 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {

invalidate() {
this.layoutVersion++;
this.layoutCache = null;
this.layoutCacheData = null;
this.clearTile();
this.clearCanvas();
this.clearOptimizedBlocks();
Expand All @@ -150,12 +153,6 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
debugLog(message, this.state);
}

private updateLayoutCache() {
const layout = getViewportLayout(this.std.host, this.viewport);
this.layoutCache = layout;
this.debugLog('Layout cache updated');
}

private clearTile() {
if (!this.tile) return;
this.tile.bitmap.close();
Expand Down Expand Up @@ -204,9 +201,7 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
}

private handlePaintedBitmap(bitmap: ImageBitmap, resolve: () => void) {
if (this.tile) {
this.tile.bitmap.close();
}
this.clearTile();
this.tile = {
bitmap,
zoom: this.viewport.zoom,
Expand Down Expand Up @@ -281,10 +276,9 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
}

private clearOptimizedBlocks() {
if (this.viewportElement) {
this.viewportElement.clearOptimizedBlocks();
this.debugLog('Cleared optimized blocks');
}
if (!this.viewportElement) return;
this.viewportElement.clearOptimizedBlocks();
this.debugLog('Cleared optimized blocks');
}

canOptimize(): boolean {
Expand Down
Loading