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)
This commit is contained in:
doodlewind
2025-04-11 04:28:51 +00:00
parent 714f2e79dc
commit a5ab66d6cd
9 changed files with 148 additions and 13 deletions

View File

@@ -11,6 +11,7 @@
"license": "MIT",
"dependencies": {
"@blocksuite/affine-components": "workspace:*",
"@blocksuite/affine-gfx-turbo-renderer": "workspace:*",
"@blocksuite/affine-inline-latex": "workspace:*",
"@blocksuite/affine-inline-link": "workspace:*",
"@blocksuite/affine-inline-preset": "workspace:*",
@@ -35,7 +36,8 @@
},
"exports": {
".": "./src/index.ts",
"./effects": "./src/effects.ts"
"./effects": "./src/effects.ts",
"./turbo-painter": "./src/turbo/code-painter.worker.ts"
},
"files": [
"src",

View File

@@ -3,3 +3,5 @@ export * from './code-block';
export * from './code-block-config';
export * from './code-block-spec';
export * from './code-toolbar';
export * from './turbo/code-layout-handler';
export * from './turbo/code-painter.worker';

View File

@@ -0,0 +1,71 @@
import type { Rect } from '@blocksuite/affine-gfx-turbo-renderer';
import {
BlockLayoutHandlerExtension,
BlockLayoutHandlersIdentifier,
} from '@blocksuite/affine-gfx-turbo-renderer';
import type { Container } from '@blocksuite/global/di';
import type { EditorHost, GfxBlockComponent } from '@blocksuite/std';
import { clientToModelCoord, type ViewportRecord } from '@blocksuite/std/gfx';
import type { BlockModel } from '@blocksuite/store';
import type { CodeLayout } from './code-painter.worker';
export class CodeLayoutHandlerExtension extends BlockLayoutHandlerExtension<CodeLayout> {
readonly blockType = 'affine:code';
static override setup(di: Container) {
di.addImpl(
BlockLayoutHandlersIdentifier('code'),
CodeLayoutHandlerExtension
);
}
override queryLayout(
model: BlockModel,
host: EditorHost,
viewportRecord: ViewportRecord
): CodeLayout | null {
const component = host.std.view.getBlock(model.id) as GfxBlockComponent;
if (!component) return null;
const codeBlockElement = component.querySelector(
'.affine-code-block-container'
);
if (!codeBlockElement) return null;
const { zoom, viewScale } = viewportRecord;
const codeLayout: CodeLayout = {
type: 'affine:code',
blockId: model.id,
rect: { x: 0, y: 0, w: 0, h: 0 },
};
// Get the bounding rect of the code block
const clientRect = codeBlockElement.getBoundingClientRect();
if (!clientRect) return null;
// Convert client coordinates to model coordinates
const [modelX, modelY] = clientToModelCoord(viewportRecord, [
clientRect.x,
clientRect.y,
]);
codeLayout.rect = {
x: modelX,
y: modelY,
w: clientRect.width / zoom / viewScale,
h: clientRect.height / zoom / viewScale,
};
return codeLayout;
}
calculateBound(layout: CodeLayout) {
const rect: Rect = layout.rect;
return {
rect,
subRects: [rect],
};
}
}

View File

@@ -0,0 +1,53 @@
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
);

View File

@@ -8,6 +8,7 @@
"include": ["./src"],
"references": [
{ "path": "../../components" },
{ "path": "../../gfx/turbo-renderer" },
{ "path": "../../inlines/latex" },
{ "path": "../../inlines/link" },
{ "path": "../../inlines/preset" },