better size format

This commit is contained in:
Nriver 2023-06-02 16:48:54 +08:00
parent e47c3a27b0
commit 671879f0c7
3 changed files with 17 additions and 14 deletions

View File

@ -19,6 +19,18 @@ function padNum(num) {
return `${num <= 9 ? "0" : ""}${num}`; return `${num <= 9 ? "0" : ""}${num}`;
} }
function formatSize(bytes, decimals = 2) {
if (!+bytes) return '0 Bytes'
const k = 1024
const dm = decimals < 0 ? 0 : decimals
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`
}
function formatTime(date) { function formatTime(date) {
return `${padNum(date.getHours())}:${padNum(date.getMinutes())}`; return `${padNum(date.getHours())}:${padNum(date.getMinutes())}`;
} }
@ -358,6 +370,7 @@ export default {
reloadFrontendApp, reloadFrontendApp,
parseDate, parseDate,
padNum, padNum,
formatSize,
formatTime, formatTime,
formatTimeWithSeconds, formatTimeWithSeconds,
formatDate, formatDate,

View File

@ -136,7 +136,7 @@ export default class FilePropertiesWidget extends NoteContextAwareWidget {
const noteComplement = await this.noteContext.getNoteComplement(); const noteComplement = await this.noteContext.getNoteComplement();
this.$fileSize.text(`${noteComplement.contentLength} bytes`); this.$fileSize.text(utils.formatSize(noteComplement.contentLength));
// open doesn't work for protected notes since it works through browser which isn't in protected session // open doesn't work for protected notes since it works through browser which isn't in protected session
this.$openButton.toggle(!note.isProtected); this.$openButton.toggle(!note.isProtected);

View File

@ -1,5 +1,6 @@
import NoteContextAwareWidget from "../note_context_aware_widget.js"; import NoteContextAwareWidget from "../note_context_aware_widget.js";
import server from "../../services/server.js"; import server from "../../services/server.js";
import utils from "../../services/utils.js";
const TPL = ` const TPL = `
<div class="note-info-widget"> <div class="note-info-widget">
@ -105,12 +106,12 @@ export default class NoteInfoWidget extends NoteContextAwareWidget {
this.$subTreeSize.empty().append($('<span class="bx bx-loader bx-spin"></span>')); this.$subTreeSize.empty().append($('<span class="bx bx-loader bx-spin"></span>'));
const noteSizeResp = await server.get(`stats/note-size/${this.noteId}`); const noteSizeResp = await server.get(`stats/note-size/${this.noteId}`);
this.$noteSize.text(this.formatSize(noteSizeResp.noteSize)); this.$noteSize.text(utils.formatSize(noteSizeResp.noteSize));
const subTreeResp = await server.get(`stats/subtree-size/${this.noteId}`); const subTreeResp = await server.get(`stats/subtree-size/${this.noteId}`);
if (subTreeResp.subTreeNoteCount > 1) { if (subTreeResp.subTreeNoteCount > 1) {
this.$subTreeSize.text(`(subtree size: ${this.formatSize(subTreeResp.subTreeSize)} in ${subTreeResp.subTreeNoteCount} notes)`); this.$subTreeSize.text(`(subtree size: ${utils.formatSize(subTreeResp.subTreeSize)} in ${subTreeResp.subTreeNoteCount} notes)`);
} }
else { else {
this.$subTreeSize.text(""); this.$subTreeSize.text("");
@ -143,17 +144,6 @@ export default class NoteInfoWidget extends NoteContextAwareWidget {
this.$noteSizesWrapper.hide(); this.$noteSizesWrapper.hide();
} }
formatSize(size) {
size = Math.max(Math.round(size / 1024), 1);
if (size < 1024) {
return `${size} KiB`;
}
else {
return `${Math.round(size / 102.4) / 10} MiB`;
}
}
entitiesReloadedEvent({loadResults}) { entitiesReloadedEvent({loadResults}) {
if (loadResults.isNoteReloaded(this.noteId) || loadResults.isNoteContentReloaded(this.noteId)) { if (loadResults.isNoteReloaded(this.noteId) || loadResults.isNoteContentReloaded(this.noteId)) {
this.refresh(); this.refresh();