mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-25 02:13:00 +08:00
fix(editor): image size and xywh when converting attachment to image (#10200)
In Edgeless, the image size should be read when converting attachment to image: * fix `size` * fix `xywh`
This commit is contained in:
28
blocksuite/affine/shared/src/utils/image.ts
Normal file
28
blocksuite/affine/shared/src/utils/image.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import DOMPurify from 'dompurify';
|
||||
|
||||
export function readImageSize(file: File | Blob) {
|
||||
return new Promise<{ width: number; height: number }>(resolve => {
|
||||
const size = { width: 0, height: 0 };
|
||||
if (!file.type.startsWith('image/')) {
|
||||
resolve(size);
|
||||
return;
|
||||
}
|
||||
|
||||
const img = new Image();
|
||||
|
||||
img.onload = () => {
|
||||
size.width = img.width;
|
||||
size.height = img.height;
|
||||
URL.revokeObjectURL(img.src);
|
||||
resolve(size);
|
||||
};
|
||||
|
||||
img.onerror = () => {
|
||||
URL.revokeObjectURL(img.src);
|
||||
resolve(size);
|
||||
};
|
||||
|
||||
const sanitizedURL = DOMPurify.sanitize(URL.createObjectURL(file));
|
||||
img.src = sanitizedURL;
|
||||
});
|
||||
}
|
||||
@@ -8,6 +8,7 @@ export * from './edgeless';
|
||||
export * from './event';
|
||||
export * from './file';
|
||||
export * from './fractional-indexing';
|
||||
export * from './image';
|
||||
export * from './insert';
|
||||
export * from './is-abort-error';
|
||||
export * from './math';
|
||||
|
||||
Reference in New Issue
Block a user