Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(client): use same logic for setting boolean with inheritance
Elian Doran0b065063f200
diff --git a/apps/client/src/menus/tree_context_menu.ts b/apps/client/src/menus/tree_context_menu.tsindex 551732e9db..4f28cc1f4b 100644--- a/apps/client/src/menus/tree_context_menu.ts+++ b/apps/client/src/menus/tree_context_menu.ts@@ -159,7 +159,7 @@ export default class TreeContextMenu implements SelectMenuItemEventListener<Treehandler: async () => {const note = await froca.getNote(this.node.data.noteId);if (!note) return;- attributes.toggleBooleanWithInheritance(note, "subtreeHidden");+ attributes.setBooleanWithInheritance(note, "subtreeHidden", !hasSubtreeHidden);}},{diff --git a/apps/client/src/services/attributes.ts b/apps/client/src/services/attributes.tsindex 3b45d2d460..34c07d638a 100644--- a/apps/client/src/services/attributes.ts+++ b/apps/client/src/services/attributes.ts@@ -33,25 +33,32 @@ export async function setRelation(noteId: string, name: string, value: string =}/**- * Toggles a boolean label on the given note, taking inheritance into account. If the label is owned by the note, it- * will be removed. If the label is inherited from a parent note, it will be overridden to `false`. If the label does- * not exist, it will be added with an empty value.+ * Sets a boolean label on the given note, taking inheritance into account. If the desired value matches the inherited+ * value, any owned label will be removed to allow the inherited value to take effect. If the desired value differs+ * from the inherited value, an owned label will be created or updated to reflect the desired value.** When checking if the boolean value is set, don't use `note.hasLabel`; instead use `note.isLabelTruthy`.*- * @param note the note on which to toggle the label.- * @param labelName the name of the label to toggle.+ * @param note the note on which to set the boolean label.+ * @param labelName the name of the label to set.+ * @param value the boolean value to set for the label.*/-export async function toggleBooleanWithInheritance(note: FNote, labelName: string) {- if (note.hasLabel(labelName)) {- // Can either be owned by us or inherited from parent.- if (note.hasOwnedLabel(labelName)) {+export async function setBooleanWithInheritance(note: FNote, labelName: string, value: boolean) {+ const actualValue = note.isLabelTruthy(labelName);+ if (actualValue === value) return;++ if (value) {+ if (note.getLabelValue(labelName) === "false") {+ // Remove the override so that the inherited true takes effect.removeOwnedLabelByName(note, labelName);} else {- setLabel(note.noteId, labelName, "false");+ setLabel(note.noteId, labelName, "");}+ } else if (note.hasOwnedLabel(labelName)) {+ removeOwnedLabelByName(note, labelName);} else {- addLabel(note.noteId, labelName);+ // Label is inherited - override to false.+ setLabel(note.noteId, labelName, "false");}}@@ -166,7 +173,7 @@ export default {setLabel,setRelation,setAttribute,- toggleBooleanWithInheritance,+ setBooleanWithInheritance,removeAttributeById,removeOwnedLabelByName,removeOwnedRelationByName,diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsxindex 610f65dedd..8eb2cc0efd 100644--- a/apps/client/src/widgets/react/hooks.tsx+++ b/apps/client/src/widgets/react/hooks.tsx@@ -646,22 +646,7 @@ export function useNoteLabelBoolean(note: FNote | undefined | null, labelName: Fconst setter = useCallback((value: boolean) => {if (note) {- const actualValue = note.isLabelTruthy(labelName);- if (actualValue === value) return;-- if (value) {- if (note.getLabelValue(labelName) === "false") {- // Remove the override so that the inherited true takes effect.- attributes.removeOwnedLabelByName(note, labelName);- } else {- attributes.setLabel(note.noteId, labelName, "");- }- } else if (note.hasOwnedLabel(labelName)) {- attributes.removeOwnedLabelByName(note, labelName);- } else {- // Label is inherited - override to false.- attributes.setLabel(note.noteId, labelName, "false");- }+ attributes.setBooleanWithInheritance(note, labelName, value);}}, [note, labelName]);