fix(editor): support worker renderer zoom (#9943)

This commit is contained in:
Yifeng Wang
2025-02-05 12:16:06 +08:00
committed by GitHub
parent 0553ae72b5
commit 8afc50c730
4 changed files with 22 additions and 11 deletions
@@ -23,14 +23,14 @@ export class SwitchModeAnimator {
async switchMode() { async switchMode() {
this.initOverlay(); this.initOverlay();
const beginLayout = this.renderer.getHostLayout(); const beginLayout = this.renderer.hostLayout;
await this.renderer.render(false); await this.renderer.render(false);
document.body.append(this.overlay); document.body.append(this.overlay);
this.editor.mode = this.editor.mode === 'page' ? 'edgeless' : 'page'; this.editor.mode = this.editor.mode === 'page' ? 'edgeless' : 'page';
await wait(); await wait();
const endLayout = this.renderer.getHostLayout(); const endLayout = this.renderer.hostLayout;
this.overlay.style.display = 'inherit'; this.overlay.style.display = 'inherit';
await this.animate( await this.animate(
@@ -1,3 +1,4 @@
import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx';
import type { AffineEditorContainer } from '@blocksuite/presets'; import type { AffineEditorContainer } from '@blocksuite/presets';
import { getSentenceRects, segmentSentences } from './text-utils.js'; import { getSentenceRects, segmentSentences } from './text-utils.js';
@@ -26,16 +27,21 @@ export class CanvasRenderer {
this.worker.postMessage({ type: 'init', data: { width, height, dpr } }); this.worker.postMessage({ type: 'init', data: { width, height, dpr } });
} }
getHostRect() { get hostRect() {
const hostRect = this.editorContainer.host!.getBoundingClientRect(); return this.editorContainer.host!.getBoundingClientRect();
return hostRect;
} }
getHostLayout() { get hostZoom() {
return this.editorContainer.std.get(GfxControllerIdentifier).viewport.zoom;
}
get hostLayout() {
const paragraphBlocks = this.editorContainer.host!.querySelectorAll( const paragraphBlocks = this.editorContainer.host!.querySelectorAll(
'.affine-paragraph-rich-text-wrapper [data-v-text="true"]' '.affine-paragraph-rich-text-wrapper [data-v-text="true"]'
); );
const zoom = this.hostZoom;
const paragraphs: ParagraphLayout[] = Array.from(paragraphBlocks).map(p => { const paragraphs: ParagraphLayout[] = Array.from(paragraphBlocks).map(p => {
const sentences = segmentSentences(p.textContent || ''); const sentences = segmentSentences(p.textContent || '');
const sentenceLayouts = sentences.map(sentence => { const sentenceLayouts = sentences.map(sentence => {
@@ -45,18 +51,20 @@ export class CanvasRenderer {
rects, rects,
}; };
}); });
return { return {
sentences: sentenceLayouts, sentences: sentenceLayouts,
scale: zoom,
}; };
}); });
const hostRect = this.getHostRect(); const hostRect = this.hostRect;
const editorContainerRect = this.editorContainer.getBoundingClientRect(); const editorContainerRect = this.editorContainer.getBoundingClientRect();
return { paragraphs, hostRect, editorContainerRect }; return { paragraphs, hostRect, editorContainerRect };
} }
public async render(toScreen = true): Promise<void> { public async render(toScreen = true): Promise<void> {
const { paragraphs, hostRect, editorContainerRect } = this.getHostLayout(); const { paragraphs, hostRect, editorContainerRect } = this.hostLayout;
this.initWorkerSize(hostRect.width, hostRect.height); this.initWorkerSize(hostRect.width, hostRect.height);
return new Promise(resolve => { return new Promise(resolve => {
@@ -42,10 +42,12 @@ class CanvasWorkerManager {
const { canvas, ctx } = this; const { canvas, ctx } = this;
if (!canvas || !ctx) return; if (!canvas || !ctx) return;
ctx.font = '15px Inter';
const baselineY = getBaseline();
paragraphs.forEach(paragraph => { paragraphs.forEach(paragraph => {
const scale = paragraph.scale ?? 1;
const fontSize = 15 * scale;
ctx.font = `${fontSize}px Inter`;
const baselineY = getBaseline() * scale;
paragraph.sentences.forEach(sentence => { paragraph.sentences.forEach(sentence => {
ctx.strokeStyle = 'yellow'; ctx.strokeStyle = 'yellow';
sentence.rects.forEach(textRect => { sentence.rects.forEach(textRect => {
@@ -5,6 +5,7 @@ export interface SentenceLayout {
export interface ParagraphLayout { export interface ParagraphLayout {
sentences: SentenceLayout[]; sentences: SentenceLayout[];
scale: number;
} }
export interface TextRect { export interface TextRect {