From 69468487c23bea5bb8852db8e02ae4a81616a1f2 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Mon, 23 Sep 2024 17:26:50 +0500 Subject: [PATCH] core: note title reset on updating a note --- packages/core/__tests__/notes.test.ts | 13 +++++++++++++ packages/core/src/collections/notes.ts | 22 +++++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/packages/core/__tests__/notes.test.ts b/packages/core/__tests__/notes.test.ts index c72eb94c2..156431a74 100644 --- a/packages/core/__tests__/notes.test.ts +++ b/packages/core/__tests__/notes.test.ts @@ -121,6 +121,19 @@ test("setting note title to empty should set the default title", () => expect(note?.title.startsWith("Note ")).toBe(true); })); +test("changing content shouldn't reset the note title ", () => + noteTest({ title: "I am a note" }).then(async ({ db, id }) => { + await db.notes.add({ + id, + content: { + type: TEST_NOTE.content.type, + data: "

This is a very colorful existence.

" + } + }); + const note = await db.notes.note(id); + expect(note?.title).toBe("I am a note"); + })); + test("note should get headline from content", () => noteTest({ ...TEST_NOTE, diff --git a/packages/core/src/collections/notes.ts b/packages/core/src/collections/notes.ts index 6cdd678c5..59690683b 100644 --- a/packages/core/src/collections/notes.ts +++ b/packages/core/src/collections/notes.ts @@ -109,17 +109,21 @@ export class Notes implements ICollection { }); } - if (item.title) { + if (typeof item.title !== "undefined") { item.title = item.title.replace(NEWLINE_STRIP_REGEX, " "); dateEdited = Date.now(); - } else { - item.title = formatTitle( - this.db.settings.getTitleFormat(), - this.db.settings.getDateFormat(), - this.db.settings.getTimeFormat(), - headline?.split(" ").splice(0, 10).join(" ") || "", - this.totalNotes - ); + } + + if (!isUpdating || item.title === "") { + item.title = + item.title || + formatTitle( + this.db.settings.getTitleFormat(), + this.db.settings.getDateFormat(), + this.db.settings.getTimeFormat(), + headline?.split(" ").splice(0, 10).join(" ") || "", + this.totalNotes + ); } if (isUpdating) {