mirror of
https://github.com/zadam/trilium.git
synced 2026-01-08 07:34:25 +01:00
refactor(pdf_attachments): deduplicate font size
This commit is contained in:
parent
43a749b6a7
commit
6513e2cfca
@ -187,13 +187,15 @@ export function formatSize(size: number | null | undefined) {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
size = Math.max(Math.round(size / 1024), 1);
|
if (size === 0) {
|
||||||
|
return "0 B";
|
||||||
if (size < 1024) {
|
|
||||||
return `${size} KiB`;
|
|
||||||
}
|
}
|
||||||
return `${Math.round(size / 102.4) / 10} MiB`;
|
|
||||||
|
|
||||||
|
const k = 1024;
|
||||||
|
const sizes = ["B", "KB", "MB", "GB"];
|
||||||
|
const i = Math.floor(Math.log(size) / Math.log(k));
|
||||||
|
|
||||||
|
return `${Math.round((size / Math.pow(k, i)) * 100) / 100} ${sizes[i]}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function toObject<T, R>(array: T[], fn: (arg0: T) => [key: string, value: R]) {
|
function toObject<T, R>(array: T[], fn: (arg0: T) => [key: string, value: R]) {
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import "./PdfAttachments.css";
|
import "./PdfAttachments.css";
|
||||||
|
|
||||||
|
import { formatSize } from "../../services/utils";
|
||||||
import { useActiveNoteContext, useGetContextData, useNoteProperty } from "../react/hooks";
|
import { useActiveNoteContext, useGetContextData, useNoteProperty } from "../react/hooks";
|
||||||
import Icon from "../react/Icon";
|
import Icon from "../react/Icon";
|
||||||
import RightPanelWidget from "./RightPanelWidget";
|
import RightPanelWidget from "./RightPanelWidget";
|
||||||
@ -52,7 +53,7 @@ function PdfAttachmentItem({
|
|||||||
attachment: AttachmentInfo;
|
attachment: AttachmentInfo;
|
||||||
onDownload: (filename: string) => void;
|
onDownload: (filename: string) => void;
|
||||||
}) {
|
}) {
|
||||||
const sizeText = formatFileSize(attachment.size);
|
const sizeText = formatSize(attachment.size);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="pdf-attachment-item" onClick={() => onDownload(attachment.filename)}>
|
<div className="pdf-attachment-item" onClick={() => onDownload(attachment.filename)}>
|
||||||
@ -65,11 +66,3 @@ function PdfAttachmentItem({
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatFileSize(bytes: number): string {
|
|
||||||
if (bytes === 0) return "0 B";
|
|
||||||
const k = 1024;
|
|
||||||
const sizes = ["B", "KB", "MB", "GB"];
|
|
||||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
||||||
return `${Math.round((bytes / Math.pow(k, i)) * 100) / 100 } ${ sizes[i]}`;
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user