Skip to content

Commit

Permalink
chore(blocks): reuse attachment upload method (#9007)
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 18, 2024
1 parent bf0a93f commit 789a172
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 32 deletions.
13 changes: 5 additions & 8 deletions packages/blocks/src/attachment-block/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function isAttachmentUploading(blockId: string) {
/**
* This function will not verify the size of the file.
*/
async function uploadAttachmentBlob(
export async function uploadAttachmentBlob(
editorHost: EditorHost,
blockId: string,
blob: Blob
Expand All @@ -63,15 +63,12 @@ async function uploadAttachmentBlob(
} finally {
setAttachmentUploaded(blockId);

const attachmentModel = doc.getBlockById(
blockId
) as AttachmentBlockModel | null;
const block = doc.getBlock(blockId);

doc.withoutTransact(() => {
if (!attachmentModel) {
return;
}
doc.updateBlock(attachmentModel, {
if (!block) return;

doc.updateBlock(block.model, {
sourceId,
} satisfies Partial<AttachmentBlockProps>);
});
Expand Down
26 changes: 2 additions & 24 deletions packages/blocks/src/root-block/edgeless/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ import {
Vec,
} from '@blocksuite/global/utils';

import {
setAttachmentUploaded,
setAttachmentUploading,
} from '../../../attachment-block/utils.js';
import { uploadAttachmentBlob } from '../../../attachment-block/utils.js';
import { calcBoundByOrigin, readImageSize } from '../components/utils.js';
import { DEFAULT_NOTE_OFFSET_X, DEFAULT_NOTE_OFFSET_Y } from './consts.js';
import { addBlock } from './crud.js';
Expand Down Expand Up @@ -102,26 +99,7 @@ export async function addAttachments(

// upload file and update the attachment model
const uploadPromises = dropInfos.map(async ({ blockId, file }) => {
let sourceId: string | undefined;
try {
setAttachmentUploading(blockId);
sourceId = await std.doc.blobSync.set(file);
} catch (error) {
console.error(error);
if (error instanceof Error) {
toast(
std.host,
`Failed to upload attachment! ${error.message || error.toString()}`
);
}
} finally {
setAttachmentUploaded(blockId);
std.doc.withoutTransact(() => {
gfx.updateElement(blockId, {
sourceId,
} satisfies Partial<AttachmentBlockProps>);
});
}
await uploadAttachmentBlob(std.host, blockId, file);
return blockId;
});
const blockIds = await Promise.all(uploadPromises);
Expand Down

0 comments on commit 789a172

Please sign in to comment.