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): allow force refresh in worker renderer #10289

Merged
merged 1 commit into from
Feb 19, 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
37 changes: 29 additions & 8 deletions blocksuite/affine/shared/src/viewport-renderer/dom-utils.ts
Original file line number Diff line number Diff line change
@@ -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 { getSentenceRects, segmentSentences } from './text-utils.js';
import type { ParagraphLayout, SectionLayout } from './types.js';
import type { ViewportTurboRendererExtension } from './viewport-renderer.js';

export function syncCanvasSize(canvas: HTMLCanvasElement, host: HTMLElement) {
const hostRect = host.getBoundingClientRect();
Expand Down Expand Up @@ -87,23 +91,40 @@ export function getSectionLayout(
}

export function initTweakpane(
viewportElement: HTMLElement,
onStateChange: (value: boolean) => void
renderer: ViewportTurboRendererExtension,
viewportElement: HTMLElement
) {
const debugPane = new Pane({ container: viewportElement });
const paneElement = debugPane.element;
paneElement.style.position = 'absolute';
paneElement.style.top = '10px';
paneElement.style.left = '10px';
paneElement.style.right = '10px';
paneElement.style.width = '250px';
debugPane.title = 'Viewport Turbo Renderer';

const params = { enabled: true };
debugPane
.addBinding(params, 'enabled', {
label: 'Enable',
.addBinding({ paused: true }, 'paused', {
label: 'Paused',
})
.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);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,15 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
const viewportElement = document.querySelector('.affine-edgeless-viewport');
if (viewportElement) {
viewportElement.append(this.canvas);
initTweakpane(viewportElement as HTMLElement, (value: boolean) => {
this.state = value ? 'monitoring' : 'paused';
this.canvas.style.display = value ? 'block' : 'none';
});
initTweakpane(this, viewportElement as HTMLElement);
}
syncCanvasSize(this.canvas, this.std.host);
this.viewport.viewportUpdated.on(() => {
this.refresh().catch(console.error);
});

document.fonts.load('15px Inter').then(() => {
this.state = 'monitoring';
// this.state = 'monitoring';
this.refresh().catch(console.error);
});
}
Expand All @@ -82,7 +79,9 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
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

if (this.canUseCache()) {
Expand Down Expand Up @@ -140,8 +139,6 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
}

private drawCachedBitmap(section: SectionLayout) {
if (this.state === 'paused') return;

const bitmap = this.tile!.bitmap;
const ctx = this.canvas.getContext('2d');
if (!ctx) return;
Expand Down
2 changes: 1 addition & 1 deletion blocksuite/presets/src/__tests__/utils/renderer-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { setupEditor } from './setup.js';

async function init() {
setupEditor('edgeless', [ViewportTurboRendererExtension]);
addSampleNotes(doc, 1);
addSampleNotes(doc, 100);
doc.load();
}

Expand Down
Loading