From 7e71029d1ccde2a16a3ab9ed073c2e9872f73999 Mon Sep 17 00:00:00 2001 From: zadam Date: Fri, 2 Jun 2023 16:05:02 +0200 Subject: [PATCH] use note size format also in file properties --- src/public/app/services/frontend_script_api.js | 7 +++++++ src/public/app/services/utils.js | 14 +++++++++++++- .../widgets/ribbon_widgets/file_properties.js | 2 +- .../widgets/ribbon_widgets/note_info_widget.js | 18 ++++-------------- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/public/app/services/frontend_script_api.js b/src/public/app/services/frontend_script_api.js index 98d4e8460..a7b2b1467 100644 --- a/src/public/app/services/frontend_script_api.js +++ b/src/public/app/services/frontend_script_api.js @@ -483,6 +483,13 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null, $contain */ this.randomString = utils.randomString; + /** + * @method + * @param {int} size in bytes + * @return {string} formatted string + */ + this.formatNoteSize = utils.formatNoteSize; + this.logMessages = {}; this.logSpacedUpdates = {}; diff --git a/src/public/app/services/utils.js b/src/public/app/services/utils.js index e0ef2d767..bf468c578 100644 --- a/src/public/app/services/utils.js +++ b/src/public/app/services/utils.js @@ -354,6 +354,17 @@ function escapeRegExp(str) { return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); } +function formatNoteSize(size) { + size = Math.max(Math.round(size / 1024), 1); + + if (size < 1024) { + return `${size} KiB`; + } + else { + return `${Math.round(size / 102.4) / 10} MiB`; + } +} + export default { reloadFrontendApp, parseDate, @@ -396,5 +407,6 @@ export default { filterAttributeName, isValidAttributeName, sleep, - escapeRegExp + escapeRegExp, + formatNoteSize }; diff --git a/src/public/app/widgets/ribbon_widgets/file_properties.js b/src/public/app/widgets/ribbon_widgets/file_properties.js index 454c73f1d..afa051168 100644 --- a/src/public/app/widgets/ribbon_widgets/file_properties.js +++ b/src/public/app/widgets/ribbon_widgets/file_properties.js @@ -136,7 +136,7 @@ export default class FilePropertiesWidget extends NoteContextAwareWidget { const noteComplement = await this.noteContext.getNoteComplement(); - this.$fileSize.text(`${noteComplement.contentLength} bytes`); + this.$fileSize.text(utils.formatNoteSize(noteComplement.contentLength)); // open doesn't work for protected notes since it works through browser which isn't in protected session this.$openButton.toggle(!note.isProtected); diff --git a/src/public/app/widgets/ribbon_widgets/note_info_widget.js b/src/public/app/widgets/ribbon_widgets/note_info_widget.js index 28459da5d..3b7756cf7 100644 --- a/src/public/app/widgets/ribbon_widgets/note_info_widget.js +++ b/src/public/app/widgets/ribbon_widgets/note_info_widget.js @@ -1,5 +1,6 @@ import NoteContextAwareWidget from "../note_context_aware_widget.js"; import server from "../../services/server.js"; +import utils from "../../services/utils.js"; const TPL = `
@@ -105,12 +106,12 @@ export default class NoteInfoWidget extends NoteContextAwareWidget { this.$subTreeSize.empty().append($('')); const noteSizeResp = await server.get(`stats/note-size/${this.noteId}`); - this.$noteSize.text(this.formatSize(noteSizeResp.noteSize)); + this.$noteSize.text(utils.formatNoteSize(noteSizeResp.noteSize)); const subTreeResp = await server.get(`stats/subtree-size/${this.noteId}`); if (subTreeResp.subTreeNoteCount > 1) { - this.$subTreeSize.text(`(subtree size: ${this.formatSize(subTreeResp.subTreeSize)} in ${subTreeResp.subTreeNoteCount} notes)`); + this.$subTreeSize.text(`(subtree size: ${utils.formatNoteSize(subTreeResp.subTreeSize)} in ${subTreeResp.subTreeNoteCount} notes)`); } else { this.$subTreeSize.text(""); @@ -142,18 +143,7 @@ export default class NoteInfoWidget extends NoteContextAwareWidget { this.$calculateButton.show(); 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}) { if (loadResults.isNoteReloaded(this.noteId) || loadResults.isNoteContentReloaded(this.noteId)) { this.refresh();