Skip to content

Build your personal knowledge base with Trilium Notes

feat(note_language): allow removing language tag

Elian Doranbb42b5fb1938
diff --git a/src/public/app/services/attributes.ts b/src/public/app/services/attributes.ts
index 767d8c3af9..b47e297cdd 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 63b83bed63..e811211196 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 e38f95690a..fbc093ce51 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;