refactor(editor): allow force refresh in worker renderer (#10289)
This commit is contained in:
@@ -1,8 +1,12 @@
|
|||||||
import type { Viewport } from '@blocksuite/block-std/gfx';
|
import {
|
||||||
|
GfxControllerIdentifier,
|
||||||
|
type Viewport,
|
||||||
|
} from '@blocksuite/block-std/gfx';
|
||||||
import { Pane } from 'tweakpane';
|
import { Pane } from 'tweakpane';
|
||||||
|
|
||||||
import { getSentenceRects, segmentSentences } from './text-utils.js';
|
import { getSentenceRects, segmentSentences } from './text-utils.js';
|
||||||
import type { ParagraphLayout, SectionLayout } from './types.js';
|
import type { ParagraphLayout, SectionLayout } from './types.js';
|
||||||
|
import type { ViewportTurboRendererExtension } from './viewport-renderer.js';
|
||||||
|
|
||||||
export function syncCanvasSize(canvas: HTMLCanvasElement, host: HTMLElement) {
|
export function syncCanvasSize(canvas: HTMLCanvasElement, host: HTMLElement) {
|
||||||
const hostRect = host.getBoundingClientRect();
|
const hostRect = host.getBoundingClientRect();
|
||||||
@@ -87,23 +91,40 @@ export function getSectionLayout(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function initTweakpane(
|
export function initTweakpane(
|
||||||
viewportElement: HTMLElement,
|
renderer: ViewportTurboRendererExtension,
|
||||||
onStateChange: (value: boolean) => void
|
viewportElement: HTMLElement
|
||||||
) {
|
) {
|
||||||
const debugPane = new Pane({ container: viewportElement });
|
const debugPane = new Pane({ container: viewportElement });
|
||||||
const paneElement = debugPane.element;
|
const paneElement = debugPane.element;
|
||||||
paneElement.style.position = 'absolute';
|
paneElement.style.position = 'absolute';
|
||||||
paneElement.style.top = '10px';
|
paneElement.style.top = '10px';
|
||||||
paneElement.style.left = '10px';
|
paneElement.style.right = '10px';
|
||||||
paneElement.style.width = '250px';
|
paneElement.style.width = '250px';
|
||||||
debugPane.title = 'Viewport Turbo Renderer';
|
debugPane.title = 'Viewport Turbo Renderer';
|
||||||
|
|
||||||
const params = { enabled: true };
|
|
||||||
debugPane
|
debugPane
|
||||||
.addBinding(params, 'enabled', {
|
.addBinding({ paused: true }, 'paused', {
|
||||||
label: 'Enable',
|
label: 'Paused',
|
||||||
})
|
})
|
||||||
.on('change', ({ value }) => {
|
.on('change', ({ value }) => {
|
||||||
onStateChange(value);
|
renderer.state = value ? 'paused' : 'monitoring';
|
||||||
});
|
});
|
||||||
|
|
||||||
|
debugPane
|
||||||
|
.addBinding({ keepDOM: true }, 'keepDOM', {
|
||||||
|
label: 'Keep DOM',
|
||||||
|
})
|
||||||
|
.on('change', ({ value }) => {
|
||||||
|
const container = viewportElement.querySelector('gfx-viewport')!;
|
||||||
|
(container as HTMLElement).style.display = value ? 'block' : 'none';
|
||||||
|
});
|
||||||
|
|
||||||
|
debugPane.addButton({ title: 'Fit Viewport' }).on('click', () => {
|
||||||
|
const gfx = renderer.std.get(GfxControllerIdentifier);
|
||||||
|
gfx.fitToScreen();
|
||||||
|
});
|
||||||
|
|
||||||
|
debugPane.addButton({ title: 'Force Refresh' }).on('click', () => {
|
||||||
|
renderer.refresh(true).catch(console.error);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,10 +49,7 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
const viewportElement = document.querySelector('.affine-edgeless-viewport');
|
const viewportElement = document.querySelector('.affine-edgeless-viewport');
|
||||||
if (viewportElement) {
|
if (viewportElement) {
|
||||||
viewportElement.append(this.canvas);
|
viewportElement.append(this.canvas);
|
||||||
initTweakpane(viewportElement as HTMLElement, (value: boolean) => {
|
initTweakpane(this, viewportElement as HTMLElement);
|
||||||
this.state = value ? 'monitoring' : 'paused';
|
|
||||||
this.canvas.style.display = value ? 'block' : 'none';
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
syncCanvasSize(this.canvas, this.std.host);
|
syncCanvasSize(this.canvas, this.std.host);
|
||||||
this.viewport.viewportUpdated.on(() => {
|
this.viewport.viewportUpdated.on(() => {
|
||||||
@@ -60,7 +57,7 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
});
|
});
|
||||||
|
|
||||||
document.fonts.load('15px Inter').then(() => {
|
document.fonts.load('15px Inter').then(() => {
|
||||||
this.state = 'monitoring';
|
// this.state = 'monitoring';
|
||||||
this.refresh().catch(console.error);
|
this.refresh().catch(console.error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -82,7 +79,9 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
return this.std.get(GfxControllerIdentifier).viewport;
|
return this.std.get(GfxControllerIdentifier).viewport;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async refresh() {
|
async refresh(force = false) {
|
||||||
|
if (this.state === 'paused' && !force) return;
|
||||||
|
|
||||||
await nextTick(); // Improves stability during zooming
|
await nextTick(); // Improves stability during zooming
|
||||||
|
|
||||||
if (this.canUseCache()) {
|
if (this.canUseCache()) {
|
||||||
@@ -140,8 +139,6 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private drawCachedBitmap(section: SectionLayout) {
|
private drawCachedBitmap(section: SectionLayout) {
|
||||||
if (this.state === 'paused') return;
|
|
||||||
|
|
||||||
const bitmap = this.tile!.bitmap;
|
const bitmap = this.tile!.bitmap;
|
||||||
const ctx = this.canvas.getContext('2d');
|
const ctx = this.canvas.getContext('2d');
|
||||||
if (!ctx) return;
|
if (!ctx) return;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { setupEditor } from './setup.js';
|
|||||||
|
|
||||||
async function init() {
|
async function init() {
|
||||||
setupEditor('edgeless', [ViewportTurboRendererExtension]);
|
setupEditor('edgeless', [ViewportTurboRendererExtension]);
|
||||||
addSampleNotes(doc, 1);
|
addSampleNotes(doc, 100);
|
||||||
doc.load();
|
doc.load();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user