mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 15:09:33 +01:00
feat: allow note to be in multiple notebooks and topics
This commit is contained in:
@@ -2,7 +2,7 @@ import {
|
|||||||
StorageInterface,
|
StorageInterface,
|
||||||
notebookTest,
|
notebookTest,
|
||||||
TEST_NOTEBOOK,
|
TEST_NOTEBOOK,
|
||||||
TEST_NOTE
|
TEST_NOTE,
|
||||||
} from "./utils";
|
} from "./utils";
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
@@ -26,7 +26,7 @@ test("search all notebooks", () =>
|
|||||||
notebookTest({
|
notebookTest({
|
||||||
...TEST_NOTEBOOK,
|
...TEST_NOTEBOOK,
|
||||||
title: "I will be searched.",
|
title: "I will be searched.",
|
||||||
description: "searched description"
|
description: "searched description",
|
||||||
}).then(({ db }) => {
|
}).then(({ db }) => {
|
||||||
let filtered = db.notebooks.filter("searhed");
|
let filtered = db.notebooks.filter("searhed");
|
||||||
expect(filtered.length).toBeGreaterThan(0);
|
expect(filtered.length).toBeGreaterThan(0);
|
||||||
@@ -54,20 +54,17 @@ test("unpin a notebook", () =>
|
|||||||
test("delete a notebook", () =>
|
test("delete a notebook", () =>
|
||||||
notebookTest().then(async ({ db, id }) => {
|
notebookTest().then(async ({ db, id }) => {
|
||||||
let noteId = await db.notes.add(TEST_NOTE);
|
let noteId = await db.notes.add(TEST_NOTE);
|
||||||
await db.notebooks
|
await db.notebooks.notebook(id).topics.topic("General").add(noteId);
|
||||||
.notebook(id)
|
expect(db.notebooks.notebook(id).topics.topic("General").has(noteId)).toBe(
|
||||||
.topics.topic("General")
|
true
|
||||||
.add(noteId);
|
);
|
||||||
expect(
|
|
||||||
db.notebooks
|
|
||||||
.notebook(id)
|
|
||||||
.topics.topic("General")
|
|
||||||
.has(noteId)
|
|
||||||
).toBe(true);
|
|
||||||
let note = db.notes.note(noteId);
|
let note = db.notes.note(noteId);
|
||||||
expect(note.notebook.id).toBe(id);
|
|
||||||
|
expect(note.notebooks.some((n) => n.id === id)).toBe(true);
|
||||||
|
|
||||||
await db.notebooks.delete(id);
|
await db.notebooks.delete(id);
|
||||||
expect(db.notebooks.notebook(id).data.deleted).toBe(true);
|
expect(db.notebooks.notebook(id).data.deleted).toBe(true);
|
||||||
note = db.notes.note(noteId);
|
note = db.notes.note(noteId);
|
||||||
expect(note.notebook.id).toBeUndefined();
|
|
||||||
|
expect(note.notebooks.length).toBe(0);
|
||||||
}));
|
}));
|
||||||
|
|||||||
@@ -165,7 +165,7 @@ test("add note to topic", () =>
|
|||||||
expect(topic.totalNotes).toBe(1);
|
expect(topic.totalNotes).toBe(1);
|
||||||
expect(db.notebooks.notebook(notebookId).data.totalNotes).toBe(1);
|
expect(db.notebooks.notebook(notebookId).data.totalNotes).toBe(1);
|
||||||
let note = db.notes.note(id);
|
let note = db.notes.note(id);
|
||||||
expect(note.notebook.id).toBe(notebookId);
|
expect(note.notebooks.some((n) => n.id === notebookId)).toBe(true);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
test("duplicate note to topic should not be added", () =>
|
test("duplicate note to topic should not be added", () =>
|
||||||
@@ -190,7 +190,7 @@ test("move note", () =>
|
|||||||
await db.notebooks.notebook(notebookId2).topics.add("Home2");
|
await db.notebooks.notebook(notebookId2).topics.add("Home2");
|
||||||
await db.notes.move({ id: notebookId2, topic: "Home2" }, id);
|
await db.notes.move({ id: notebookId2, topic: "Home2" }, id);
|
||||||
let note = db.notes.note(id);
|
let note = db.notes.note(id);
|
||||||
expect(note.notebook.id).toBe(notebookId2);
|
expect(note.notebooks.some((n) => n.id === notebookId2)).toBe(true);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
test("moving note to same notebook and topic should do nothing", () =>
|
test("moving note to same notebook and topic should do nothing", () =>
|
||||||
@@ -202,7 +202,7 @@ test("moving note to same notebook and topic should do nothing", () =>
|
|||||||
await topic.add(id);
|
await topic.add(id);
|
||||||
await db.notes.move({ id: notebookId, topic: "Home" }, id);
|
await db.notes.move({ id: notebookId, topic: "Home" }, id);
|
||||||
let note = db.notes.note(id);
|
let note = db.notes.note(id);
|
||||||
expect(note.notebook.id).toBe(notebookId);
|
expect(note.notebooks.some((n) => n.id === notebookId)).toBe(true);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
test("export note to html", () =>
|
test("export note to html", () =>
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ test("restore a deleted note that was in a notebook", () =>
|
|||||||
const notebook = db.notebooks.notebook(nbId);
|
const notebook = db.notebooks.notebook(nbId);
|
||||||
expect(notebook.topics.topic("General").has(id)).toBe(true);
|
expect(notebook.topics.topic("General").has(id)).toBe(true);
|
||||||
|
|
||||||
expect(note.notebook.id).toBe(nbId);
|
expect(note.notebooks.some((n) => n.id === nbId)).toBe(true);
|
||||||
expect(note.notebook.topic).toBeDefined();
|
|
||||||
expect(notebook.topics.has("General")).toBeDefined();
|
expect(notebook.topics.has("General")).toBeDefined();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -92,7 +92,7 @@ test("delete a notebook", () =>
|
|||||||
await db.notebooks.notebook(id).topics.topic("General").add(noteId);
|
await db.notebooks.notebook(id).topics.topic("General").add(noteId);
|
||||||
await db.notebooks.delete(id);
|
await db.notebooks.delete(id);
|
||||||
expect(db.notebooks.notebook(id).data.deleted).toBe(true);
|
expect(db.notebooks.notebook(id).data.deleted).toBe(true);
|
||||||
expect(db.notes.note(noteId).notebook).toStrictEqual({});
|
expect(db.notes.note(noteId).notebook).toBeUndefined();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
test("restore a deleted notebook", () =>
|
test("restore a deleted notebook", () =>
|
||||||
@@ -104,9 +104,9 @@ test("restore a deleted notebook", () =>
|
|||||||
let notebook = db.notebooks.notebook(id);
|
let notebook = db.notebooks.notebook(id);
|
||||||
expect(notebook).toBeDefined();
|
expect(notebook).toBeDefined();
|
||||||
let note = db.notes.note(noteId);
|
let note = db.notes.note(noteId);
|
||||||
expect(note.notebook.id).toBe(id);
|
const index = note.notebooks.findIndex((n) => n.id === id);
|
||||||
expect(note.notebook.topic).toBeDefined();
|
expect(note.notebooks[index]).toBeDefined();
|
||||||
expect(notebook.topics.topic(note.notebook.topic)).toBeDefined();
|
expect(notebook.topics.topic(note.notebooks[index].topic)).toBeDefined();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
test("restore a notebook that has deleted notes", () =>
|
test("restore a notebook that has deleted notes", () =>
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ export default class Notes extends Collection {
|
|||||||
headline: note.headline,
|
headline: note.headline,
|
||||||
pinned: !!note.pinned,
|
pinned: !!note.pinned,
|
||||||
locked: !!note.locked,
|
locked: !!note.locked,
|
||||||
notebook: note.notebook || undefined,
|
notebooks: note.notebooks || undefined,
|
||||||
colors: note.colors || [],
|
colors: note.colors || [],
|
||||||
tags: note.tags || [],
|
tags: note.tags || [],
|
||||||
favorite: !!note.favorite,
|
favorite: !!note.favorite,
|
||||||
|
|||||||
@@ -177,10 +177,12 @@ const migrations = {
|
|||||||
if (await migrations.handleDeleted(db, "notes", item)) return;
|
if (await migrations.handleDeleted(db, "notes", item)) return;
|
||||||
|
|
||||||
const contentId = item.content.delta;
|
const contentId = item.content.delta;
|
||||||
|
const notebook = item.notebook;
|
||||||
delete item.content;
|
delete item.content;
|
||||||
|
delete item.notebook;
|
||||||
item.contentId = contentId;
|
item.contentId = contentId;
|
||||||
item.remote = true;
|
item.remote = true;
|
||||||
if (!item.notebook.id) item.notebook = undefined;
|
if (notebook) item.notebooks = [notebook];
|
||||||
await db.notes.add(item);
|
await db.notes.add(item);
|
||||||
},
|
},
|
||||||
delta: async function (db, item) {
|
delta: async function (db, item) {
|
||||||
|
|||||||
@@ -39,8 +39,8 @@ export default class Note {
|
|||||||
return this._note.id;
|
return this._note.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
get notebook() {
|
get notebooks() {
|
||||||
return this._note.notebook;
|
return this._note.notebooks;
|
||||||
}
|
}
|
||||||
|
|
||||||
get dateEdited() {
|
get dateEdited() {
|
||||||
|
|||||||
@@ -25,20 +25,19 @@ export default class Topic {
|
|||||||
let note = this._db.notes.note(noteId);
|
let note = this._db.notes.note(noteId);
|
||||||
if (this.has(noteId) || !note || note.data.deleted) continue;
|
if (this.has(noteId) || !note || note.data.deleted) continue;
|
||||||
topic.notes.push(noteId);
|
topic.notes.push(noteId);
|
||||||
if (note.notebook && note.notebook.id && note.notebook.topic) {
|
|
||||||
|
const array = note.notebooks || [];
|
||||||
if (
|
if (
|
||||||
note.notebook.id === this._notebookId &&
|
array.some(
|
||||||
note.notebook.topic === topic.id
|
(item) => item.id === this._notebookId && item.topic === topic.id
|
||||||
|
)
|
||||||
)
|
)
|
||||||
return this;
|
return this;
|
||||||
await this._db.notebooks
|
array.push({ id: this._notebookId, topic: topic.id });
|
||||||
.notebook(note.notebook.id)
|
|
||||||
.topics.topic(note.notebook.topic)
|
|
||||||
.delete(note.id);
|
|
||||||
}
|
|
||||||
await this._db.notes.add({
|
await this._db.notes.add({
|
||||||
id: noteId,
|
id: noteId,
|
||||||
notebook: { id: this._notebookId, topic: topic.id },
|
notebooks: array,
|
||||||
});
|
});
|
||||||
topic.totalNotes++;
|
topic.totalNotes++;
|
||||||
}
|
}
|
||||||
@@ -48,12 +47,20 @@ export default class Topic {
|
|||||||
async delete(...noteIds) {
|
async delete(...noteIds) {
|
||||||
const topic = qclone(this._topic);
|
const topic = qclone(this._topic);
|
||||||
for (let noteId of noteIds) {
|
for (let noteId of noteIds) {
|
||||||
if (!this.has(noteId)) return this;
|
let note = this._db.notes.note(noteId);
|
||||||
|
if (!this.has(noteId) || !note || note.data.deleted) return this;
|
||||||
let index = topic.notes.findIndex((n) => n === noteId);
|
let index = topic.notes.findIndex((n) => n === noteId);
|
||||||
topic.notes.splice(index, 1);
|
topic.notes.splice(index, 1);
|
||||||
|
|
||||||
|
const array = note.notebooks || [];
|
||||||
|
index = array.findIndex(
|
||||||
|
(n) => n.id === this._notebookId && n.topic === topic.id
|
||||||
|
);
|
||||||
|
array.splice(index, 1);
|
||||||
|
|
||||||
await this._db.notes.add({
|
await this._db.notes.add({
|
||||||
id: noteId,
|
id: noteId,
|
||||||
notebook: {},
|
notebooks: array,
|
||||||
});
|
});
|
||||||
topic.totalNotes--;
|
topic.totalNotes--;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user