mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-22 22:49:45 +01:00
refactor: seperate note add & merging
This commit is contained in:
@@ -335,16 +335,15 @@ test("repairing notebook references should delete non-existent notebooks", () =>
|
|||||||
expect(note.notebooks.length).toBe(0);
|
expect(note.notebooks.length).toBe(0);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
test("adding a remote note with an invalid tag should clean the tag array", () =>
|
test("adding a note with an invalid tag should clean the tag array", () =>
|
||||||
databaseTest().then(async (db) => {
|
databaseTest().then(async (db) => {
|
||||||
await expect(
|
await expect(
|
||||||
db.notes.add({
|
db.notes.add({
|
||||||
...TEST_NOTE,
|
...TEST_NOTE,
|
||||||
id: "helloworld",
|
id: "helloworld",
|
||||||
remote: true,
|
|
||||||
tags: ["/.,"],
|
tags: ["/.,"],
|
||||||
})
|
})
|
||||||
).resolves.toBeUndefined();
|
).resolves.toBe("helloworld");
|
||||||
|
|
||||||
const note = db.notes.note("helloworld");
|
const note = db.notes.note("helloworld");
|
||||||
expect(note.tags.length).toBe(0);
|
expect(note.tags.length).toBe(0);
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ class Merger {
|
|||||||
await this._mergeArray(
|
await this._mergeArray(
|
||||||
notes,
|
notes,
|
||||||
(id) => this._db.notes.note(id),
|
(id) => this._db.notes.note(id),
|
||||||
(item) => this._db.notes.add(item)
|
(item) => this._db.notes.merge(item)
|
||||||
);
|
);
|
||||||
|
|
||||||
await this._mergeArray(
|
await this._mergeArray(
|
||||||
|
|||||||
@@ -1,55 +1,38 @@
|
|||||||
import Collection from "./collection";
|
import Collection from "./collection";
|
||||||
import Note from "../models/note";
|
import Note from "../models/note";
|
||||||
import getId from "../utils/id";
|
import getId from "../utils/id";
|
||||||
import { EV, EVENTS } from "../common";
|
|
||||||
import { getContentFromData } from "../content-types";
|
import { getContentFromData } from "../content-types";
|
||||||
import qclone from "qclone/src/qclone";
|
import qclone from "qclone/src/qclone";
|
||||||
import sort from "fast-sort";
|
import sort from "fast-sort";
|
||||||
import { deleteItem } from "../utils/array";
|
import { deleteItem } from "../utils/array";
|
||||||
|
|
||||||
export default class Notes extends Collection {
|
export default class Notes extends Collection {
|
||||||
async add(noteArg) {
|
async merge(remoteNote) {
|
||||||
if (!noteArg) return;
|
if (!remoteNote) return;
|
||||||
if (noteArg.deleted) {
|
if (remoteNote.deleted) return await this._collection.addItem(remoteNote);
|
||||||
await this._collection.addItem(noteArg);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let id = noteArg.id || getId();
|
const id = remoteNote.id;
|
||||||
let oldNote = this._collection.getItem(id);
|
const localNote = this._collection.getItem(id);
|
||||||
|
if (localNote) {
|
||||||
|
if (localNote.color) await this._db.colors.untag(localNote.color, id);
|
||||||
|
|
||||||
if (noteArg.remote || noteArg.migrated) {
|
for (let tag of localNote.tags || []) {
|
||||||
const { color, tags } = noteArg;
|
|
||||||
|
|
||||||
if (oldNote) {
|
|
||||||
if (!!oldNote.color && oldNote.color !== color) {
|
|
||||||
await this._db.colors.untag(oldNote.color, id);
|
|
||||||
}
|
|
||||||
if (!!oldNote.tags) {
|
|
||||||
for (let tag of oldNote.tags) {
|
|
||||||
await this._db.tags.untag(tag, id);
|
await this._db.tags.untag(tag, id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await this._resolveColorAndTags(remoteNote);
|
||||||
|
|
||||||
|
return await this._collection.addItem(remoteNote);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (color) {
|
async add(noteArg) {
|
||||||
await this._db.colors.add(color, id);
|
if (!noteArg) return;
|
||||||
}
|
if (noteArg.remote)
|
||||||
|
throw new Error("Please use db.notes.merge to merge remote notes.");
|
||||||
|
|
||||||
if (tags && tags.length) {
|
let id = noteArg.id || getId();
|
||||||
for (let i = 0; i < tags.length; ++i) {
|
let oldNote = this._collection.getItem(id);
|
||||||
const tag = tags[i];
|
|
||||||
const addedTag = await this._db.tags.add(tag, id).catch(() => void 0);
|
|
||||||
if (!addedTag) {
|
|
||||||
tags.splice(i, 1);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (addedTag.title !== tag) tags[i] = addedTag.title;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return await this._collection.addItem(noteArg);
|
|
||||||
}
|
|
||||||
|
|
||||||
let note = {
|
let note = {
|
||||||
...oldNote,
|
...oldNote,
|
||||||
@@ -91,7 +74,7 @@ export default class Notes extends Collection {
|
|||||||
id,
|
id,
|
||||||
contentId: note.contentId,
|
contentId: note.contentId,
|
||||||
type: "note",
|
type: "note",
|
||||||
title: getNoteTitle(note, oldNote).replace(/[\r\n\t]+/g, " "),
|
title: getNoteTitle(note, oldNote).replace(/[\r\n\t\v]+/g, " "),
|
||||||
headline: note.headline,
|
headline: note.headline,
|
||||||
pinned: !!note.pinned,
|
pinned: !!note.pinned,
|
||||||
locked: !!note.locked,
|
locked: !!note.locked,
|
||||||
@@ -105,16 +88,7 @@ export default class Notes extends Collection {
|
|||||||
conflicted: !!note.conflicted,
|
conflicted: !!note.conflicted,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!oldNote || oldNote.deleted) {
|
await this.merge(note);
|
||||||
if (note.color) await this._db.colors.add(note.color, id);
|
|
||||||
|
|
||||||
for (let tag of note.tags) {
|
|
||||||
if (!tag || !tag.trim()) continue;
|
|
||||||
await this._db.tags.add(tag, id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await this._collection.addItem(note);
|
|
||||||
return note.id;
|
return note.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,6 +240,24 @@ export default class Notes extends Collection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async _resolveColorAndTags(note) {
|
||||||
|
const { color, tags, id } = note;
|
||||||
|
|
||||||
|
if (color) await this._db.colors.add(color, id);
|
||||||
|
|
||||||
|
if (tags && tags.length) {
|
||||||
|
for (let i = 0; i < tags.length; ++i) {
|
||||||
|
const tag = tags[i];
|
||||||
|
const addedTag = await this._db.tags.add(tag, id).catch(() => void 0);
|
||||||
|
if (!addedTag) {
|
||||||
|
tags.splice(i, 1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (addedTag.title !== tag) tags[i] = addedTag.title;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNoteHeadline(note, content) {
|
function getNoteHeadline(note, content) {
|
||||||
|
|||||||
Reference in New Issue
Block a user