Files
AFFiNE/blocksuite/affine/blocks/code/src/turbo/code-painter.worker.ts
doodlewind a5ab66d6cd feat(editor): add basic code support in turbo renderer (#11619)
This PR adds basic support for code block:

[Screen Recording 2025-04-10 at 8.13.26 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/5d749979-f7f1-4e4d-ba5b-bc4ba29f8b83.mov" />](https://app.graphite.dev/media/video/lEGcysB4lFTEbCwZ8jMv/5d749979-f7f1-4e4d-ba5b-bc4ba29f8b83.mov)
2025-04-11 04:28:51 +00:00

54 lines
1.4 KiB
TypeScript

import type {
BlockLayout,
BlockLayoutPainter,
WorkerToHostMessage,
} from '@blocksuite/affine-gfx-turbo-renderer';
import { BlockLayoutPainterExtension } from '@blocksuite/affine-gfx-turbo-renderer/painter';
export interface CodeLayout extends BlockLayout {
type: 'affine:code';
}
function isCodeLayout(layout: BlockLayout): layout is CodeLayout {
return layout.type === 'affine:code';
}
class CodeLayoutPainter implements BlockLayoutPainter {
paint(
ctx: OffscreenCanvasRenderingContext2D,
layout: BlockLayout,
layoutBaseX: number,
layoutBaseY: number
): void {
if (!isCodeLayout(layout)) {
const message: WorkerToHostMessage = {
type: 'paintError',
error: 'Invalid layout format',
blockType: 'affine:code',
};
self.postMessage(message);
return;
}
// Get the layout dimensions
const x = layout.rect.x - layoutBaseX;
const y = layout.rect.y - layoutBaseY;
const width = layout.rect.w;
const height = layout.rect.h;
// Simple white rectangle for now
ctx.fillStyle = 'white';
ctx.fillRect(x, y, width, height);
// Add a border to visualize the code block
ctx.strokeStyle = 'rgba(0, 0, 0, 0.1)';
ctx.lineWidth = 1;
ctx.strokeRect(x, y, width, height);
}
}
export const CodeLayoutPainterExtension = BlockLayoutPainterExtension(
'affine:code',
CodeLayoutPainter
);