Files
AFFiNE-Mirror/blocksuite/affine/blocks/note/src/turbo/note-painter.worker.ts
T
doodlewind dba8e00fb6 feat(editor): add basic note support in turbo renderer (#11607)
After landing layout tree refactoring, this PR adds basic note support in turbo renderer.

In this demo recording, the code and image block needs to be further supported.

[Screen Recording 2025-04-10 at 5.16.15 PM.mov <span class="graphite__hidden">(uploaded via Graphite)</span> <img class="graphite__hidden" src="https://app.graphite.dev/api/v1/graphite/video/thumbnail/lEGcysB4lFTEbCwZ8jMv/2e416b41-5609-4e52-a90f-5b7bb77db682.mov" />](https://app.graphite.dev/media/video/lEGcysB4lFTEbCwZ8jMv/2e416b41-5609-4e52-a90f-5b7bb77db682.mov)
2025-04-10 09:52:33 +00:00

50 lines
1.3 KiB
TypeScript

import type {
BlockLayout,
BlockLayoutPainter,
WorkerToHostMessage,
} from '@blocksuite/affine-gfx-turbo-renderer';
import { BlockLayoutPainterExtension } from '@blocksuite/affine-gfx-turbo-renderer/painter';
export interface NoteLayout extends BlockLayout {
type: 'affine:note';
background?: string;
}
function isNoteLayout(layout: BlockLayout): layout is NoteLayout {
return layout.type === 'affine:note';
}
class NoteLayoutPainter implements BlockLayoutPainter {
paint(
ctx: OffscreenCanvasRenderingContext2D,
layout: BlockLayout,
layoutBaseX: number,
layoutBaseY: number
): void {
if (!isNoteLayout(layout)) {
const message: WorkerToHostMessage = {
type: 'paintError',
error: 'Invalid layout format',
blockType: 'affine:note',
};
self.postMessage(message);
return;
}
// Get the layout rectangle
const x = layout.rect.x - layoutBaseX;
const y = layout.rect.y - layoutBaseY;
const width = layout.rect.w;
const height = layout.rect.h;
ctx.fillStyle = layout.background || 'rgb(255, 255, 255)';
ctx.fillRect(x, y, width, height);
ctx.strokeRect(x, y, width, height);
}
}
export const NoteLayoutPainterExtension = BlockLayoutPainterExtension(
'affine:note',
NoteLayoutPainter
);