mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-20 11:31:47 +08:00
a5ab66d6cd
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)
72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
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],
|
|
};
|
|
}
|
|
}
|