mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-07 01:09:54 +08:00
refactor(editor): remove non null asserts in turbo renderer (#10454)
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import type { EditorHost } from '@blocksuite/block-std';
|
||||||
import { type Viewport } from '@blocksuite/block-std/gfx';
|
import { type Viewport } from '@blocksuite/block-std/gfx';
|
||||||
import { Pane } from 'tweakpane';
|
import { Pane } from 'tweakpane';
|
||||||
|
|
||||||
@@ -23,7 +24,7 @@ export function syncCanvasSize(canvas: HTMLCanvasElement, host: HTMLElement) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getViewportLayout(
|
export function getViewportLayout(
|
||||||
host: HTMLElement,
|
host: EditorHost,
|
||||||
viewport: Viewport
|
viewport: Viewport
|
||||||
): ViewportLayout {
|
): ViewportLayout {
|
||||||
const paragraphBlocks = host.querySelectorAll(
|
const paragraphBlocks = host.querySelectorAll(
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
|
|
||||||
public readonly canvas: HTMLCanvasElement = document.createElement('canvas');
|
public readonly canvas: HTMLCanvasElement = document.createElement('canvas');
|
||||||
private readonly worker: Worker;
|
private readonly worker: Worker;
|
||||||
private layoutCache: ViewportLayout | null = null;
|
private layoutCacheData: ViewportLayout | null = null;
|
||||||
private tile: Tile | null = null;
|
private tile: Tile | null = null;
|
||||||
private viewportElement: GfxViewportElement | null = null;
|
private viewportElement: GfxViewportElement | null = null;
|
||||||
|
|
||||||
@@ -95,6 +95,13 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
return this.std.get(GfxControllerIdentifier).viewport;
|
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() {
|
async refresh() {
|
||||||
if (this.state === 'inactive') return;
|
if (this.state === 'inactive') return;
|
||||||
|
|
||||||
@@ -110,19 +117,15 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
else if (this.canUseBitmapCache()) {
|
else if (this.canUseBitmapCache()) {
|
||||||
this.debugLog('Using cached bitmap');
|
this.debugLog('Using cached bitmap');
|
||||||
this.setState('ready');
|
this.setState('ready');
|
||||||
this.drawCachedBitmap(this.layoutCache!);
|
this.drawCachedBitmap(this.layoutCache);
|
||||||
this.updateOptimizedBlocks();
|
this.updateOptimizedBlocks();
|
||||||
}
|
}
|
||||||
// -> rendering
|
// -> rendering
|
||||||
else {
|
else {
|
||||||
if (!this.layoutCache) {
|
|
||||||
this.updateLayoutCache();
|
|
||||||
}
|
|
||||||
const layout = this.layoutCache!;
|
|
||||||
this.setState('rendering');
|
this.setState('rendering');
|
||||||
this.toggleOptimization(false);
|
this.toggleOptimization(false);
|
||||||
await this.paintLayout(layout);
|
await this.paintLayout(this.layoutCache);
|
||||||
this.drawCachedBitmap(layout);
|
this.drawCachedBitmap(this.layoutCache);
|
||||||
this.updateOptimizedBlocks();
|
this.updateOptimizedBlocks();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -137,7 +140,7 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
|
|
||||||
invalidate() {
|
invalidate() {
|
||||||
this.layoutVersion++;
|
this.layoutVersion++;
|
||||||
this.layoutCache = null;
|
this.layoutCacheData = null;
|
||||||
this.clearTile();
|
this.clearTile();
|
||||||
this.clearCanvas();
|
this.clearCanvas();
|
||||||
this.clearOptimizedBlocks();
|
this.clearOptimizedBlocks();
|
||||||
@@ -150,12 +153,6 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
debugLog(message, this.state);
|
debugLog(message, this.state);
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateLayoutCache() {
|
|
||||||
const layout = getViewportLayout(this.std.host, this.viewport);
|
|
||||||
this.layoutCache = layout;
|
|
||||||
this.debugLog('Layout cache updated');
|
|
||||||
}
|
|
||||||
|
|
||||||
private clearTile() {
|
private clearTile() {
|
||||||
if (!this.tile) return;
|
if (!this.tile) return;
|
||||||
this.tile.bitmap.close();
|
this.tile.bitmap.close();
|
||||||
@@ -204,9 +201,7 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private handlePaintedBitmap(bitmap: ImageBitmap, resolve: () => void) {
|
private handlePaintedBitmap(bitmap: ImageBitmap, resolve: () => void) {
|
||||||
if (this.tile) {
|
this.clearTile();
|
||||||
this.tile.bitmap.close();
|
|
||||||
}
|
|
||||||
this.tile = {
|
this.tile = {
|
||||||
bitmap,
|
bitmap,
|
||||||
zoom: this.viewport.zoom,
|
zoom: this.viewport.zoom,
|
||||||
@@ -281,10 +276,9 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private clearOptimizedBlocks() {
|
private clearOptimizedBlocks() {
|
||||||
if (this.viewportElement) {
|
if (!this.viewportElement) return;
|
||||||
this.viewportElement.clearOptimizedBlocks();
|
this.viewportElement.clearOptimizedBlocks();
|
||||||
this.debugLog('Cleared optimized blocks');
|
this.debugLog('Cleared optimized blocks');
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
canOptimize(): boolean {
|
canOptimize(): boolean {
|
||||||
|
|||||||
Reference in New Issue
Block a user