feat(core): extract attachments from page selected

This commit is contained in:
yoyoyohamapi
2025-07-22 11:14:21 +08:00
parent 67a52ddb13
commit 9fe8d432dd
2 changed files with 38 additions and 1 deletions
@@ -33,6 +33,7 @@ import { Doc as YDoc } from 'yjs';
import { getStoreManager } from '../../manager/store';
import type { ChatContextValue } from '../components/ai-chat-content';
import {
getSelectedAttachmentsAsBlobs,
getSelectedImagesAsBlobs,
getSelectedTextContent,
selectedToCanvas,
@@ -135,6 +136,7 @@ async function extractPageSelected(
): Promise<Partial<ChatContextValue> | null> {
const text = await getSelectedTextContent(host, 'plain-text');
const images = await getSelectedImagesAsBlobs(host);
const attachments = await getSelectedAttachmentsAsBlobs(host);
const hasText = text.length > 0;
const hasImages = images.length > 0;
@@ -143,6 +145,7 @@ async function extractPageSelected(
return {
quote: text,
markdown: markdown,
attachments,
};
} else if (!hasText && hasImages && images.length === 1) {
host.command
@@ -153,6 +156,7 @@ async function extractPageSelected(
})
.run();
return {
attachments,
images,
};
} else {
@@ -162,6 +166,7 @@ async function extractPageSelected(
quote: text,
markdown,
images,
attachments,
};
}
}
@@ -6,7 +6,11 @@ import {
getSurfaceBlock,
type SurfaceBlockComponent,
} from '@blocksuite/affine/blocks/surface';
import { DatabaseBlockModel, ImageBlockModel } from '@blocksuite/affine/model';
import {
AttachmentBlockModel,
DatabaseBlockModel,
ImageBlockModel,
} from '@blocksuite/affine/model';
import {
getBlockSelectionsCommand,
getImageSelectionsCommand,
@@ -232,6 +236,34 @@ export const getSelectedImagesAsBlobs = async (host: EditorHost) => {
return blobs.filter((blob): blob is File => !!blob);
};
export const getSelectedAttachmentsAsBlobs = async (host: EditorHost) => {
const [_, data] = host.command.exec(getSelectedBlocksCommand, {
types: ['block'],
});
const blocks = data.selectedBlocks ?? [];
const attachments: { sourceId: string; name: string }[] = [];
for (const block of blocks) {
if (block.model instanceof AttachmentBlockModel) {
const { sourceId, name } = block.model.props;
if (sourceId && name) {
attachments.push({ sourceId, name });
}
}
}
const blobs = await Promise.all(
attachments.map(attachment => {
return host.store.blobSync.get(attachment.sourceId);
})
);
return blobs
.filter(blob => blob)
.map((blob, index) => new File([blob as Blob], attachments[index].name));
};
export const getSelectedNoteAnchor = (host: EditorHost, id: string) => {
return host.querySelector(`affine-edgeless-note[data-block-id="${id}"]`);
};