mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-22 19:53:48 +08:00
fix(editor): unify file size formatting method (#12444)
Closes: [BS-3524](https://linear.app/affine-design/issue/BS-3524/统一文件大小单位,与-af-一致)   <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Updated file size formatting throughout the app to use a new, consistent utility for displaying file sizes. - Improved clarity and uniformity of file size information in attachments, images, and related notifications. - Enhanced type support to explicitly allow null values for file size descriptions. - **Bug Fixes** - Adjusted file size display in tests to match updated formatting standards. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -22,7 +22,7 @@ import {
|
|||||||
TelemetryProvider,
|
TelemetryProvider,
|
||||||
ThemeProvider,
|
ThemeProvider,
|
||||||
} from '@blocksuite/affine-shared/services';
|
} from '@blocksuite/affine-shared/services';
|
||||||
import { humanFileSize } from '@blocksuite/affine-shared/utils';
|
import { formatSize } from '@blocksuite/affine-shared/utils';
|
||||||
import {
|
import {
|
||||||
AttachmentIcon,
|
AttachmentIcon,
|
||||||
ResetIcon,
|
ResetIcon,
|
||||||
@@ -316,7 +316,7 @@ export class AttachmentBlockComponent extends CaptionedBlockComponent<Attachment
|
|||||||
errorIcon: WarningIcon(),
|
errorIcon: WarningIcon(),
|
||||||
icon: AttachmentIcon(),
|
icon: AttachmentIcon(),
|
||||||
title: name,
|
title: name,
|
||||||
description: humanFileSize(size),
|
description: formatSize(size),
|
||||||
});
|
});
|
||||||
|
|
||||||
return { ...resolvedState, kind };
|
return { ...resolvedState, kind };
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import {
|
|||||||
FileSizeLimitProvider,
|
FileSizeLimitProvider,
|
||||||
TelemetryProvider,
|
TelemetryProvider,
|
||||||
} from '@blocksuite/affine-shared/services';
|
} from '@blocksuite/affine-shared/services';
|
||||||
import { humanFileSize } from '@blocksuite/affine-shared/utils';
|
import { formatSize } from '@blocksuite/affine-shared/utils';
|
||||||
import { Bound, type IVec, Vec } from '@blocksuite/global/gfx';
|
import { Bound, type IVec, Vec } from '@blocksuite/global/gfx';
|
||||||
import type { BlockStdScope } from '@blocksuite/std';
|
import type { BlockStdScope } from '@blocksuite/std';
|
||||||
import { GfxControllerIdentifier } from '@blocksuite/std/gfx';
|
import { GfxControllerIdentifier } from '@blocksuite/std/gfx';
|
||||||
@@ -93,7 +93,7 @@ function hasExceeded(
|
|||||||
const exceeded = files.some(file => file.size > maxFileSize);
|
const exceeded = files.some(file => file.size > maxFileSize);
|
||||||
|
|
||||||
if (exceeded) {
|
if (exceeded) {
|
||||||
const size = humanFileSize(maxFileSize, true, 0);
|
const size = formatSize(maxFileSize);
|
||||||
toast(std.host, `You can only upload files less than ${size}`);
|
toast(std.host, `You can only upload files less than ${size}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import {
|
|||||||
ThemeProvider,
|
ThemeProvider,
|
||||||
ToolbarRegistryIdentifier,
|
ToolbarRegistryIdentifier,
|
||||||
} from '@blocksuite/affine-shared/services';
|
} from '@blocksuite/affine-shared/services';
|
||||||
import { humanFileSize } from '@blocksuite/affine-shared/utils';
|
import { formatSize } from '@blocksuite/affine-shared/utils';
|
||||||
import { IS_MOBILE } from '@blocksuite/global/env';
|
import { IS_MOBILE } from '@blocksuite/global/env';
|
||||||
import { BrokenImageIcon, ImageIcon } from '@blocksuite/icons/lit';
|
import { BrokenImageIcon, ImageIcon } from '@blocksuite/icons/lit';
|
||||||
import { BlockSelection } from '@blocksuite/std';
|
import { BlockSelection } from '@blocksuite/std';
|
||||||
@@ -142,7 +142,7 @@ export class ImageBlockComponent extends CaptionedBlockComponent<ImageBlockModel
|
|||||||
errorIcon: BrokenImageIcon(),
|
errorIcon: BrokenImageIcon(),
|
||||||
icon: ImageIcon(),
|
icon: ImageIcon(),
|
||||||
title: 'Image',
|
title: 'Image',
|
||||||
description: humanFileSize(size),
|
description: formatSize(size),
|
||||||
});
|
});
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import {
|
|||||||
} from '@blocksuite/affine-model';
|
} from '@blocksuite/affine-model';
|
||||||
import { ThemeProvider } from '@blocksuite/affine-shared/services';
|
import { ThemeProvider } from '@blocksuite/affine-shared/services';
|
||||||
import { unsafeCSSVarV2 } from '@blocksuite/affine-shared/theme';
|
import { unsafeCSSVarV2 } from '@blocksuite/affine-shared/theme';
|
||||||
import { humanFileSize } from '@blocksuite/affine-shared/utils';
|
import { formatSize } from '@blocksuite/affine-shared/utils';
|
||||||
import { BrokenImageIcon, ImageIcon } from '@blocksuite/icons/lit';
|
import { BrokenImageIcon, ImageIcon } from '@blocksuite/icons/lit';
|
||||||
import { GfxBlockComponent } from '@blocksuite/std';
|
import { GfxBlockComponent } from '@blocksuite/std';
|
||||||
import { GfxViewInteractionExtension } from '@blocksuite/std/gfx';
|
import { GfxViewInteractionExtension } from '@blocksuite/std/gfx';
|
||||||
@@ -128,7 +128,7 @@ export class ImageEdgelessBlockComponent extends GfxBlockComponent<ImageBlockMod
|
|||||||
errorIcon: BrokenImageIcon(),
|
errorIcon: BrokenImageIcon(),
|
||||||
icon: ImageIcon(),
|
icon: ImageIcon(),
|
||||||
title: 'Image',
|
title: 'Image',
|
||||||
description: humanFileSize(size),
|
description: formatSize(size),
|
||||||
});
|
});
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ import {
|
|||||||
NativeClipboardProvider,
|
NativeClipboardProvider,
|
||||||
} from '@blocksuite/affine-shared/services';
|
} from '@blocksuite/affine-shared/services';
|
||||||
import {
|
import {
|
||||||
|
formatSize,
|
||||||
getBlockProps,
|
getBlockProps,
|
||||||
humanFileSize,
|
|
||||||
isInsidePageEditor,
|
isInsidePageEditor,
|
||||||
readImageSize,
|
readImageSize,
|
||||||
transformModel,
|
transformModel,
|
||||||
@@ -241,7 +241,7 @@ function hasExceeded(
|
|||||||
const exceeded = files.some(file => file.size > maxFileSize);
|
const exceeded = files.some(file => file.size > maxFileSize);
|
||||||
|
|
||||||
if (exceeded) {
|
if (exceeded) {
|
||||||
const size = humanFileSize(maxFileSize, true, 0);
|
const size = formatSize(maxFileSize);
|
||||||
toast(std.host, `You can only upload files less than ${size}`);
|
toast(std.host, `You can only upload files less than ${size}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export type StateKind =
|
|||||||
export type StateInfo = {
|
export type StateInfo = {
|
||||||
icon: TemplateResult;
|
icon: TemplateResult;
|
||||||
title?: string;
|
title?: string;
|
||||||
description?: string;
|
description?: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ResolvedStateInfoPart = {
|
export type ResolvedStateInfoPart = {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
"@types/hast": "^3.0.4",
|
"@types/hast": "^3.0.4",
|
||||||
"@types/lodash-es": "^4.17.12",
|
"@types/lodash-es": "^4.17.12",
|
||||||
"@types/mdast": "^4.0.4",
|
"@types/mdast": "^4.0.4",
|
||||||
|
"bytes": "^3.1.2",
|
||||||
"dompurify": "^3.2.4",
|
"dompurify": "^3.2.4",
|
||||||
"fractional-indexing": "^3.2.0",
|
"fractional-indexing": "^3.2.0",
|
||||||
"lit": "^3.2.0",
|
"lit": "^3.2.0",
|
||||||
@@ -70,6 +71,7 @@
|
|||||||
"!dist/__tests__"
|
"!dist/__tests__"
|
||||||
],
|
],
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/bytes": "^3.1.5",
|
||||||
"vitest": "3.1.3"
|
"vitest": "3.1.3"
|
||||||
},
|
},
|
||||||
"version": "0.21.0"
|
"version": "0.21.0"
|
||||||
|
|||||||
@@ -25,3 +25,4 @@ export * from './title';
|
|||||||
export * from './url';
|
export * from './url';
|
||||||
export * from './virtual-padding';
|
export * from './virtual-padding';
|
||||||
export * from './zod-schema';
|
export * from './zod-schema';
|
||||||
|
export { default as formatSize } from 'bytes';
|
||||||
|
|||||||
@@ -7,39 +7,3 @@ export function rangeWrap(n: number, min: number, max: number) {
|
|||||||
n = (n - min + max) % max;
|
n = (n - min + max) % max;
|
||||||
return min + (Number.isNaN(n) ? 0 : n);
|
return min + (Number.isNaN(n) ? 0 : n);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Format bytes as human-readable text.
|
|
||||||
*
|
|
||||||
* @param bytes Number of bytes.
|
|
||||||
* @param si True to use metric (SI) units, aka powers of 1000. False to use
|
|
||||||
* binary (IEC), aka powers of 1024.
|
|
||||||
* @param dp Number of decimal places to display.
|
|
||||||
*
|
|
||||||
* @return Formatted string.
|
|
||||||
*
|
|
||||||
* Credit: https://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable-string
|
|
||||||
*/
|
|
||||||
export function humanFileSize(bytes: number, si = true, dp = 1) {
|
|
||||||
const thresh = si ? 1000 : 1024;
|
|
||||||
|
|
||||||
if (Math.abs(bytes) < thresh) {
|
|
||||||
return bytes + ' bytes';
|
|
||||||
}
|
|
||||||
|
|
||||||
const units = si
|
|
||||||
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
||||||
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
|
||||||
let u = -1;
|
|
||||||
const r = 10 ** dp;
|
|
||||||
|
|
||||||
do {
|
|
||||||
bytes /= thresh;
|
|
||||||
++u;
|
|
||||||
} while (
|
|
||||||
Math.round(Math.abs(bytes) * r) / r >= thresh &&
|
|
||||||
u < units.length - 1
|
|
||||||
);
|
|
||||||
|
|
||||||
return bytes.toFixed(dp) + ' ' + units[u];
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { getAttachmentFileIcon } from '@blocksuite/affine/components/icons';
|
import { getAttachmentFileIcon } from '@blocksuite/affine/components/icons';
|
||||||
import { SignalWatcher, WithDisposable } from '@blocksuite/affine/global/lit';
|
import { SignalWatcher, WithDisposable } from '@blocksuite/affine/global/lit';
|
||||||
import type { AttachmentBlockModel } from '@blocksuite/affine-model';
|
import type { AttachmentBlockModel } from '@blocksuite/affine-model';
|
||||||
import { humanFileSize } from '@blocksuite/affine-shared/utils';
|
import { formatSize } from '@blocksuite/affine-shared/utils';
|
||||||
import {
|
import {
|
||||||
ArrowDownBigIcon,
|
ArrowDownBigIcon,
|
||||||
ArrowUpBigIcon,
|
ArrowUpBigIcon,
|
||||||
@@ -18,7 +18,7 @@ const DPI = window.devicePixelRatio;
|
|||||||
|
|
||||||
type FileInfo = {
|
type FileInfo = {
|
||||||
name: string;
|
name: string;
|
||||||
size: string;
|
size: string | null;
|
||||||
isPDF: boolean;
|
isPDF: boolean;
|
||||||
icon: TemplateResult;
|
icon: TemplateResult;
|
||||||
};
|
};
|
||||||
@@ -161,7 +161,7 @@ export class AttachmentViewerPanel extends SignalWatcher(
|
|||||||
name,
|
name,
|
||||||
icon,
|
icon,
|
||||||
isPDF,
|
isPDF,
|
||||||
size: humanFileSize(size),
|
size: formatSize(size),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!isPDF) return;
|
if (!isPDF) return;
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ test('can insert attachment from slash menu', async ({ page }, testInfo) => {
|
|||||||
await waitLoading();
|
await waitLoading();
|
||||||
|
|
||||||
expect(await getName()).toBe(FILE_NAME);
|
expect(await getName()).toBe(FILE_NAME);
|
||||||
expect(await getSize()).toBe('45.8 kB');
|
expect(await getSize()).toBe('44.73KB');
|
||||||
|
|
||||||
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
|
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
|
||||||
`${testInfo.title}.json`
|
`${testInfo.title}.json`
|
||||||
@@ -285,7 +285,7 @@ test(`support dragging attachment block directly`, async ({
|
|||||||
await waitLoading();
|
await waitLoading();
|
||||||
|
|
||||||
expect(await getName()).toBe(FILE_NAME);
|
expect(await getName()).toBe(FILE_NAME);
|
||||||
expect(await getSize()).toBe('45.8 kB');
|
expect(await getSize()).toBe('44.73KB');
|
||||||
|
|
||||||
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
|
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
|
||||||
`${testInfo.title}_1.json`
|
`${testInfo.title}_1.json`
|
||||||
|
|||||||
@@ -3703,9 +3703,11 @@ __metadata:
|
|||||||
"@lit/context": "npm:^1.1.2"
|
"@lit/context": "npm:^1.1.2"
|
||||||
"@preact/signals-core": "npm:^1.8.0"
|
"@preact/signals-core": "npm:^1.8.0"
|
||||||
"@toeverything/theme": "npm:^1.1.14"
|
"@toeverything/theme": "npm:^1.1.14"
|
||||||
|
"@types/bytes": "npm:^3.1.5"
|
||||||
"@types/hast": "npm:^3.0.4"
|
"@types/hast": "npm:^3.0.4"
|
||||||
"@types/lodash-es": "npm:^4.17.12"
|
"@types/lodash-es": "npm:^4.17.12"
|
||||||
"@types/mdast": "npm:^4.0.4"
|
"@types/mdast": "npm:^4.0.4"
|
||||||
|
bytes: "npm:^3.1.2"
|
||||||
dompurify: "npm:^3.2.4"
|
dompurify: "npm:^3.2.4"
|
||||||
fractional-indexing: "npm:^3.2.0"
|
fractional-indexing: "npm:^3.2.0"
|
||||||
lit: "npm:^3.2.0"
|
lit: "npm:^3.2.0"
|
||||||
|
|||||||
Reference in New Issue
Block a user