feat: allow note to be in multiple notebooks and topics

This commit is contained in:
thecodrr
2020-12-05 12:38:15 +05:00
parent 23daab7266
commit 97d396a01e
7 changed files with 46 additions and 40 deletions

View File

@@ -2,7 +2,7 @@ import {
StorageInterface,
notebookTest,
TEST_NOTEBOOK,
TEST_NOTE
TEST_NOTE,
} from "./utils";
beforeEach(async () => {
@@ -26,7 +26,7 @@ test("search all notebooks", () =>
notebookTest({
...TEST_NOTEBOOK,
title: "I will be searched.",
description: "searched description"
description: "searched description",
}).then(({ db }) => {
let filtered = db.notebooks.filter("searhed");
expect(filtered.length).toBeGreaterThan(0);
@@ -54,20 +54,17 @@ test("unpin a notebook", () =>
test("delete a notebook", () =>
notebookTest().then(async ({ db, id }) => {
let noteId = await db.notes.add(TEST_NOTE);
await db.notebooks
.notebook(id)
.topics.topic("General")
.add(noteId);
expect(
db.notebooks
.notebook(id)
.topics.topic("General")
.has(noteId)
).toBe(true);
await db.notebooks.notebook(id).topics.topic("General").add(noteId);
expect(db.notebooks.notebook(id).topics.topic("General").has(noteId)).toBe(
true
);
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);
expect(db.notebooks.notebook(id).data.deleted).toBe(true);
note = db.notes.note(noteId);
expect(note.notebook.id).toBeUndefined();
expect(note.notebooks.length).toBe(0);
}));

View File

@@ -165,7 +165,7 @@ test("add note to topic", () =>
expect(topic.totalNotes).toBe(1);
expect(db.notebooks.notebook(notebookId).data.totalNotes).toBe(1);
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", () =>
@@ -190,7 +190,7 @@ test("move note", () =>
await db.notebooks.notebook(notebookId2).topics.add("Home2");
await db.notes.move({ id: notebookId2, topic: "Home2" }, 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", () =>
@@ -202,7 +202,7 @@ test("moving note to same notebook and topic should do nothing", () =>
await topic.add(id);
await db.notes.move({ id: notebookId, topic: "Home" }, 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", () =>

View File

@@ -42,8 +42,8 @@ test("restore a deleted note that was in a notebook", () =>
const notebook = db.notebooks.notebook(nbId);
expect(notebook.topics.topic("General").has(id)).toBe(true);
expect(note.notebook.id).toBe(nbId);
expect(note.notebook.topic).toBeDefined();
expect(note.notebooks.some((n) => n.id === nbId)).toBe(true);
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.delete(id);
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", () =>
@@ -104,9 +104,9 @@ test("restore a deleted notebook", () =>
let notebook = db.notebooks.notebook(id);
expect(notebook).toBeDefined();
let note = db.notes.note(noteId);
expect(note.notebook.id).toBe(id);
expect(note.notebook.topic).toBeDefined();
expect(notebook.topics.topic(note.notebook.topic)).toBeDefined();
const index = note.notebooks.findIndex((n) => n.id === id);
expect(note.notebooks[index]).toBeDefined();
expect(notebook.topics.topic(note.notebooks[index].topic)).toBeDefined();
}));
test("restore a notebook that has deleted notes", () =>

View File

@@ -66,7 +66,7 @@ export default class Notes extends Collection {
headline: note.headline,
pinned: !!note.pinned,
locked: !!note.locked,
notebook: note.notebook || undefined,
notebooks: note.notebooks || undefined,
colors: note.colors || [],
tags: note.tags || [],
favorite: !!note.favorite,

View File

@@ -177,10 +177,12 @@ const migrations = {
if (await migrations.handleDeleted(db, "notes", item)) return;
const contentId = item.content.delta;
const notebook = item.notebook;
delete item.content;
delete item.notebook;
item.contentId = contentId;
item.remote = true;
if (!item.notebook.id) item.notebook = undefined;
if (notebook) item.notebooks = [notebook];
await db.notes.add(item);
},
delta: async function (db, item) {

View File

@@ -39,8 +39,8 @@ export default class Note {
return this._note.id;
}
get notebook() {
return this._note.notebook;
get notebooks() {
return this._note.notebooks;
}
get dateEdited() {

View File

@@ -25,20 +25,19 @@ export default class Topic {
let note = this._db.notes.note(noteId);
if (this.has(noteId) || !note || note.data.deleted) continue;
topic.notes.push(noteId);
if (note.notebook && note.notebook.id && note.notebook.topic) {
const array = note.notebooks || [];
if (
note.notebook.id === this._notebookId &&
note.notebook.topic === topic.id
array.some(
(item) => item.id === this._notebookId && item.topic === topic.id
)
)
return this;
await this._db.notebooks
.notebook(note.notebook.id)
.topics.topic(note.notebook.topic)
.delete(note.id);
}
array.push({ id: this._notebookId, topic: topic.id });
await this._db.notes.add({
id: noteId,
notebook: { id: this._notebookId, topic: topic.id },
notebooks: array,
});
topic.totalNotes++;
}
@@ -48,12 +47,20 @@ export default class Topic {
async delete(...noteIds) {
const topic = qclone(this._topic);
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);
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({
id: noteId,
notebook: {},
notebooks: array,
});
topic.totalNotes--;
}