From a46c5a22430404677868db50af9e795c61323274 Mon Sep 17 00:00:00 2001 From: zadam Date: Thu, 29 Jun 2023 00:14:12 +0200 Subject: [PATCH] use isLabelTruthy() for most binary labels --- src/public/app/components/note_context.js | 8 ++++---- src/public/app/entities/fnote.js | 2 +- src/public/app/services/note_list_renderer.js | 2 +- src/public/app/widgets/bookmark_buttons.js | 2 +- .../widgets/buttons/launcher/script_launcher.js | 2 +- src/public/app/widgets/containers/launcher.js | 2 +- src/public/app/widgets/editability_select.js | 4 ++-- src/public/app/widgets/note_wrapper.js | 2 +- .../widgets/ribbon_widgets/book_properties.js | 2 +- .../app/widgets/ribbon_widgets/similar_notes.js | 2 +- src/services/notes.js | 2 +- src/share/routes.js | 4 ++-- src/share/shaca/entities/snote.js | 16 +++++++++++++++- src/views/share/page.ejs | 4 ++-- 14 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/public/app/components/note_context.js b/src/public/app/components/note_context.js index 9116304b2..365b68ad1 100644 --- a/src/public/app/components/note_context.js +++ b/src/public/app/components/note_context.js @@ -85,7 +85,7 @@ class NoteContext extends Component { async setHoistedNoteIfNeeded() { if (this.hoistedNoteId === 'root' && this.notePath.startsWith("root/_hidden") - && !this.note.hasLabel("keepCurrentHoisting") + && !this.note.isLabelTruthy("keepCurrentHoisting") ) { // hidden subtree displays only when hoisted, so it doesn't make sense to keep root as hoisted note @@ -221,7 +221,7 @@ class NoteContext extends Component { return false; } - if (this.note.hasLabel('readOnly')) { + if (this.note.isLabelTruthy('readOnly')) { return true; } @@ -236,7 +236,7 @@ class NoteContext extends Component { : options.getInt('autoReadonlySizeCode'); return blob.contentLength > sizeLimit - && !this.note.hasLabel('autoReadOnlyDisabled'); + && !this.note.isLabelTruthy('autoReadOnlyDisabled'); } async entitiesReloadedEvent({loadResults}) { @@ -261,7 +261,7 @@ class NoteContext extends Component { && this.note.hasChildren() && ['book', 'text', 'code'].includes(this.note.type) && this.note.mime !== 'text/x-sqlite;schema=trilium' - && !this.note.hasLabel('hideChildrenOverview'); + && !this.note.isLabelTruthy('hideChildrenOverview'); } async getTextEditor(callback) { diff --git a/src/public/app/entities/fnote.js b/src/public/app/entities/fnote.js index 4a6f0c847..7d243d2bd 100644 --- a/src/public/app/entities/fnote.js +++ b/src/public/app/entities/fnote.js @@ -761,7 +761,7 @@ class FNote { } getPromotedDefinitionAttributes() { - if (this.hasLabel('hidePromotedAttributes')) { + if (this.isLabelTruthy('hidePromotedAttributes')) { return []; } diff --git a/src/public/app/services/note_list_renderer.js b/src/public/app/services/note_list_renderer.js index dca6ac363..873b1f09d 100644 --- a/src/public/app/services/note_list_renderer.js +++ b/src/public/app/services/note_list_renderer.js @@ -230,7 +230,7 @@ class NoteListRenderer { const pageNotes = await froca.getNotes(pageNoteIds); for (const note of pageNotes) { - const $card = await this.renderNote(note, this.parentNote.hasLabel('expanded')); + const $card = await this.renderNote(note, this.parentNote.isLabelTruthy('expanded')); $container.append($card); } diff --git a/src/public/app/widgets/bookmark_buttons.js b/src/public/app/widgets/bookmark_buttons.js index 1bc534527..53121ecb9 100644 --- a/src/public/app/widgets/bookmark_buttons.js +++ b/src/public/app/widgets/bookmark_buttons.js @@ -20,7 +20,7 @@ export default class BookmarkButtons extends FlexContainer { for (const note of await bookmarkParentNote.getChildNotes()) { this.noteIds.push(note.noteId); - const buttonWidget = note.hasLabel("bookmarkFolder") + const buttonWidget = note.isLabelTruthy("bookmarkFolder") ? new BookmarkFolderWidget(note) : new OpenNoteButtonWidget(note) .class("launcher-button"); diff --git a/src/public/app/widgets/buttons/launcher/script_launcher.js b/src/public/app/widgets/buttons/launcher/script_launcher.js index b5cfebbf9..71ca35f1c 100644 --- a/src/public/app/widgets/buttons/launcher/script_launcher.js +++ b/src/public/app/widgets/buttons/launcher/script_launcher.js @@ -10,7 +10,7 @@ export default class ScriptLauncher extends AbstractLauncher { } async launch() { - if (this.launcherNote.hasLabel('scriptInLauncherContent')) { + if (this.launcherNote.isLabelTruthy('scriptInLauncherContent')) { await this.launcherNote.executeScript(); } else { const script = await this.launcherNote.getRelationTarget('script'); diff --git a/src/public/app/widgets/containers/launcher.js b/src/public/app/widgets/containers/launcher.js index bb85f8b84..a4697c76f 100644 --- a/src/public/app/widgets/containers/launcher.js +++ b/src/public/app/widgets/containers/launcher.js @@ -31,7 +31,7 @@ export default class LauncherWidget extends BasicWidget { throw new Error(`Note '${note.noteId}' '${note.title}' is not a launcher even though it's in the launcher subtree`); } - if (!utils.isDesktop() && note.hasLabel('desktopOnly')) { + if (!utils.isDesktop() && note.isLabelTruthy('desktopOnly')) { return false; } diff --git a/src/public/app/widgets/editability_select.js b/src/public/app/widgets/editability_select.js index 3aedd38e3..2cc4f14de 100644 --- a/src/public/app/widgets/editability_select.js +++ b/src/public/app/widgets/editability_select.js @@ -65,10 +65,10 @@ export default class EditabilitySelectWidget extends NoteContextAwareWidget { async refreshWithNote(note) { let editability = 'auto' - if (this.note.hasLabel('readOnly')) { + if (this.note.isLabelTruthy('readOnly')) { editability = 'readOnly'; } - else if (this.note.hasLabel('autoReadOnlyDisabled')) { + else if (this.note.isLabelTruthy('autoReadOnlyDisabled')) { editability = 'autoReadOnlyDisabled'; } diff --git a/src/public/app/widgets/note_wrapper.js b/src/public/app/widgets/note_wrapper.js index a724c3b6d..91e9ff3cf 100644 --- a/src/public/app/widgets/note_wrapper.js +++ b/src/public/app/widgets/note_wrapper.js @@ -44,7 +44,7 @@ export default class NoteWrapperWidget extends FlexContainer { this.$widget.toggleClass("full-content-width", ['image', 'mermaid', 'book', 'render', 'canvas', 'webView'].includes(note.type) - || !!note?.hasLabel('fullContentWidth') + || !!note?.isLabelTruthy('fullContentWidth') ); this.$widget.addClass(note.getCssClass()); diff --git a/src/public/app/widgets/ribbon_widgets/book_properties.js b/src/public/app/widgets/ribbon_widgets/book_properties.js index 48861150d..90147b7bf 100644 --- a/src/public/app/widgets/ribbon_widgets/book_properties.js +++ b/src/public/app/widgets/ribbon_widgets/book_properties.js @@ -72,7 +72,7 @@ export default class BookPropertiesWidget extends NoteContextAwareWidget { this.$expandChildrenButton = this.$widget.find('.expand-children-button'); this.$expandChildrenButton.on('click', async () => { - if (!this.note.hasLabel('expanded')) { + if (!this.note.isLabelTruthy('expanded')) { await attributeService.addLabel(this.noteId, 'expanded'); } diff --git a/src/public/app/widgets/ribbon_widgets/similar_notes.js b/src/public/app/widgets/ribbon_widgets/similar_notes.js index 0d47375fd..562e1c0c1 100644 --- a/src/public/app/widgets/ribbon_widgets/similar_notes.js +++ b/src/public/app/widgets/ribbon_widgets/similar_notes.js @@ -42,7 +42,7 @@ export default class SimilarNotesWidget extends NoteContextAwareWidget { isEnabled() { return super.isEnabled() && this.note.type !== 'search' - && !this.note.hasLabel('similarNotesWidgetDisabled'); + && !this.note.isLabelTruthy('similarNotesWidgetDisabled'); } getTitle() { diff --git a/src/services/notes.js b/src/services/notes.js index ff02f683f..22c9d858c 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -661,7 +661,7 @@ function saveLinks(note, content) { /** @param {BNote} note */ function saveRevisionIfNeeded(note) { // files and images are versioned separately - if (note.type === 'file' || note.type === 'image' || note.hasLabel('disableVersioning')) { + if (note.type === 'file' || note.type === 'image' || note.isLabelTruthy('disableVersioning')) { return; } diff --git a/src/share/routes.js b/src/share/routes.js index 31fc29370..2191413d1 100644 --- a/src/share/routes.js +++ b/src/share/routes.js @@ -27,7 +27,7 @@ function getSharedSubTreeRoot(note) { } function addNoIndexHeader(note, res) { - if (note.hasLabel('shareDisallowRobotIndexing')) { + if (note.isLabelTruthy('shareDisallowRobotIndexing')) { res.setHeader('X-Robots-Tag', 'noindex'); } } @@ -113,7 +113,7 @@ function register(router) { addNoIndexHeader(note, res); - if (note.hasLabel('shareRaw')) { + if (note.isLabelTruthy('shareRaw')) { res.setHeader('Content-Type', note.mime) .send(note.getContent()); diff --git a/src/share/shaca/entities/snote.js b/src/share/shaca/entities/snote.js index 2f4bc2fac..adfc5d02e 100644 --- a/src/share/shaca/entities/snote.js +++ b/src/share/shaca/entities/snote.js @@ -83,7 +83,7 @@ class SNote extends AbstractShacaEntity { return this.getChildBranches() .filter(branch => !branch.isHidden) .map(branch => branch.getNote()) - .filter(childNote => !childNote.hasLabel('shareHiddenFromTree')); + .filter(childNote => !childNote.isLabelTruthy('shareHiddenFromTree')); } /** @returns {boolean} */ @@ -238,6 +238,20 @@ class SNote extends AbstractShacaEntity { */ hasLabel(name) { return this.hasAttribute(LABEL, name); } + /** + * @param {string} name - label name + * @returns {boolean} true if label exists (including inherited) and does not have "false" value. + */ + isLabelTruthy(name) { + const label = this.getLabel(name); + + if (!label) { + return false; + } + + return label && label.value !== 'false'; + } + /** * @param {string} name - label name * @returns {boolean} true if label exists (excluding inherited) diff --git a/src/views/share/page.ejs b/src/views/share/page.ejs index 41e0a5c19..2bdb1050e 100644 --- a/src/views/share/page.ejs +++ b/src/views/share/page.ejs @@ -13,7 +13,7 @@ <% } %> - <% if (!note.hasLabel("shareOmitDefaultCss")) { %> + <% if (!note.isLabelTruthy("shareOmitDefaultCss")) { %> <% } %> @@ -26,7 +26,7 @@ <% for (const jsRelation of note.getRelations("shareJs")) { %> <% } %> - <% if (note.hasLabel('shareDisallowRobotIndexing')) { %> + <% if (note.isLabelTruthy('shareDisallowRobotIndexing')) { %> <% } %> <%- header %>