From bb42b5fb19381cf6fac2d21e2194f680a00e04e3 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 4 Mar 2025 18:35:42 +0200 Subject: [PATCH] feat(note_language): allow removing language tag --- src/public/app/services/attributes.ts | 23 +++++++++++++++++++ src/public/app/widgets/note_language.ts | 4 +--- .../app/widgets/view_widgets/calendar_view.ts | 18 ++------------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/public/app/services/attributes.ts b/src/public/app/services/attributes.ts index 767d8c3af..b47e297cd 100644 --- a/src/public/app/services/attributes.ts +++ b/src/public/app/services/attributes.ts @@ -23,6 +23,28 @@ async function removeAttributeById(noteId: string, attributeId: string) { await server.remove(`notes/${noteId}/attributes/${attributeId}`); } +/** + * Sets the attribute of the given note to the provided value if its truthy, or removes the attribute if the value is falsy. + * For an attribute with an empty value, pass an empty string instead. + * + * @param note the note to set the attribute to. + * @param type the type of attribute (label or relation). + * @param name the name of the attribute to set. + * @param value the value of the attribute to set. + */ +async function setAttribute(note: FNote, type: "label" | "relation", name: string, value: string | null | undefined) { + if (value) { + // Create or update the attribute. + await server.put(`notes/${note.noteId}/set-attribute`, { type, name, value }); + } else { + // Remove the attribute if it exists on the server but we don't define a value for it. + const attributeId = note.getAttribute(type, name)?.attributeId; + if (attributeId) { + await server.remove(`notes/${note.noteId}/attributes/${attributeId}`); + } + } +} + /** * @returns - returns true if this attribute has the potential to influence the note in the argument. * That can happen in multiple ways: @@ -66,6 +88,7 @@ function isAffecting(attrRow: AttributeRow, affectedNote: FNote | null | undefin export default { addLabel, setLabel, + setAttribute, removeAttributeById, isAffecting }; diff --git a/src/public/app/widgets/note_language.ts b/src/public/app/widgets/note_language.ts index 63b83bed6..e81121119 100644 --- a/src/public/app/widgets/note_language.ts +++ b/src/public/app/widgets/note_language.ts @@ -76,9 +76,7 @@ export default class NoteLanguageWidget extends NoteContextAwareWidget { return; } - if (languageId) { - attributes.setLabel(this.note.noteId, "language", languageId); - } + attributes.setAttribute(this.note, "label", "language", languageId); } async refreshWithNote(note: FNote) { diff --git a/src/public/app/widgets/view_widgets/calendar_view.ts b/src/public/app/widgets/view_widgets/calendar_view.ts index e38f95690..fbc093ce5 100644 --- a/src/public/app/widgets/view_widgets/calendar_view.ts +++ b/src/public/app/widgets/view_widgets/calendar_view.ts @@ -229,8 +229,8 @@ export default class CalendarView extends ViewMode { return; } - CalendarView.#setAttribute(note, "label", "startDate", startDate); - CalendarView.#setAttribute(note, "label", "endDate", endDate); + attributes.setAttribute(note, "label", "startDate", startDate); + attributes.setAttribute(note, "label", "endDate", endDate); } onEntitiesReloaded({ loadResults }: EventData<"entitiesReloaded">) { @@ -435,20 +435,6 @@ export default class CalendarView extends ViewMode { return [ note.title ]; } - static async #setAttribute(note: FNote, type: "label" | "relation", name: string, value: string | null | undefined) { - if (value) { - // Create or update the attribute. - await server.put(`notes/${note.noteId}/set-attribute`, { type, name, value }); - } else { - // Remove the attribute if it exists on the server but we don't define a value for it. - const attributeId = note.getAttribute(type, name)?.attributeId; - if (attributeId) { - await server.remove(`notes/${note.noteId}/attributes/${attributeId}`); - } - } - await ws.waitForMaxKnownEntityChangeId(); - } - static #formatDateToLocalISO(date: Date | null | undefined) { if (!date) { return undefined;