Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: migrate from delta to content

thecodrr6b38d05a8fc8
diff --git a/packages/core/__tests__/lookup.test.js b/packages/core/__tests__/lookup.skip.js
similarity index 100%
rename from packages/core/__tests__/lookup.test.js
rename to packages/core/__tests__/lookup.skip.js
diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.js
index 74d3baf70..ba1104917 100644
--- a/packages/core/__tests__/notes.test.js
+++ b/packages/core/__tests__/notes.test.js
@@ -26,13 +26,13 @@ test("add note", () =>
noteTest().then(async ({ db, id }) => {
let note = db.notes.note(id);
expect(note.data).toBeDefined();
- expect(await note.text()).toStrictEqual(TEST_NOTE.content.text);
+ expect(await note.content()).toStrictEqual(TEST_NOTE.content.data);
}));
-test("get delta of note", () =>
+test("get note content", () =>
noteTest().then(async ({ db, id }) => {
- let delta = await db.notes.note(id).delta();
- expect(delta).toStrictEqual(TEST_NOTE.content.delta);
+ let content = await db.notes.note(id).content();
+ expect(content).toStrictEqual(TEST_NOTE.content.data);
}));
test("delete note", () =>
@@ -56,7 +56,7 @@ test("get all notes", () =>
test("note without a title should get title from content", () =>
noteTest().then(async ({ db, id }) => {
let note = db.notes.note(id);
- expect(note.title).toBe("I am a");
+ expect(note.title).toBe("Hello This is");
}));
test("update note", () =>
@@ -65,8 +65,8 @@ test("update note", () =>
id,
title: "I am a new title",
content: {
- text: LONG_TEXT,
- delta: [],
+ type: "delta",
+ data: [],
},
pinned: true,
favorite: true,
@@ -75,7 +75,7 @@ test("update note", () =>
id = await db.notes.add(noteData);
let note = db.notes.note(id);
expect(note.title).toBe(noteData.title);
- expect(await note.text()).toStrictEqual(noteData.content.text);
+ expect(await note.content()).toStrictEqual(noteData.content.data);
expect(note.data.pinned).toBe(true);
expect(note.data.favorite).toBe(true);
}));
@@ -86,8 +86,8 @@ test("updating empty note should delete it", () =>
id,
title: "\n",
content: {
- text: "\n",
- delta: [{ insert: "\n" }],
+ type: "delta",
+ data: [{ insert: "\n" }],
},
});
expect(id).toBeUndefined();
@@ -98,8 +98,8 @@ test("updating empty note should delete it", () =>
test("note with text longer than 150 characters should have ... in the headline", () =>
noteTest({
content: {
- text: LONG_TEXT,
- delta: [],
+ type: "delta",
+ data: [{ insert: LONG_TEXT }, { insert: "\n" }],
},
}).then(({ db, id }) => {
let note = db.notes.note(id);
@@ -219,5 +219,5 @@ test("export note to md", () =>
test("export note to txt", () =>
noteTest().then(async ({ db, id }) => {
const txt = await db.notes.note(id).export("txt");
- expect(txt.includes("I am a text")).toBeTruthy();
+ expect(txt.includes("Hello")).toBeTruthy();
}));
diff --git a/packages/core/__tests__/topics.test.js b/packages/core/__tests__/topics.test.js
index 4fabebb70..cd80032c5 100644
--- a/packages/core/__tests__/topics.test.js
+++ b/packages/core/__tests__/topics.test.js
@@ -61,10 +61,12 @@ test("get topic", () =>
let topics = db.notebooks.notebook(id).topics;
await topics.add("Home");
let topic = topics.topic("Home");
- let noteId = await db.notes.add({ content: { text: "Hello", delta: [] } });
+ let noteId = await db.notes.add({
+ content: { type: "delta", data: [{ insert: "Hello" }] },
+ });
await topic.add(noteId);
topic = topics.topic("Home");
- expect(await db.text.get(topic.all[0].content.text)).toBe("Hello");
+ expect(await db.content.get(topic.all[0].contentId)).toBeDefined();
expect(topic.totalNotes).toBe(1);
}));
diff --git a/packages/core/__tests__/trash.test.js b/packages/core/__tests__/trash.test.js
index 80e38ff40..fb055b0a8 100644
--- a/packages/core/__tests__/trash.test.js
+++ b/packages/core/__tests__/trash.test.js
@@ -19,11 +19,11 @@ test("permanently delete a note", () =>
const note = db.notes.note(id);
await db.notes.delete(id);
expect(db.trash.all.length).toBe(1);
- expect(await note.delta()).toBeDefined();
+ expect(await note.content()).toBeDefined();
await db.trash.delete(db.trash.all[0].id);
expect(db.trash.all.length).toBe(0);
- const delta = await db.delta.raw(note.data.content.delta);
- expect(delta.deleted).toBe(true);
+ const content = await db.content.raw(note.data.contentId);
+ expect(content.deleted).toBe(true);
}));
test("restore a deleted note that was in a notebook", () =>
@@ -37,8 +37,7 @@ test("restore a deleted note that was in a notebook", () =>
let note = db.notes.note(id);
expect(note).toBeDefined();
- expect(await note.text()).toBe(TEST_NOTE.content.text);
- expect(await note.delta()).toStrictEqual(TEST_NOTE.content.delta);
+ expect(await note.content()).toBe(TEST_NOTE.content.data);
const notebook = db.notebooks.notebook(nbId);
expect(notebook.topics.topic("General").has(id)).toBe(true);
@@ -55,7 +54,7 @@ test("delete a locked note", () =>
await db.vault.add(id);
await db.notes.delete(id);
expect(db.trash.all.length).toBe(1);
- expect(await db.delta.get(note.data.content.delta)).toBeDefined();
+ expect(await db.content.get(note.data.contentId)).toBeDefined();
}));
test("restore a deleted locked note", () =>
@@ -65,7 +64,7 @@ test("restore a deleted locked note", () =>
await db.vault.add(id);
await db.notes.delete(id);
expect(db.trash.all.length).toBe(1);
- expect(await db.delta.get(note.data.content.delta)).toBeDefined();
+ expect(await db.content.get(note.data.contentId)).toBeDefined();
await db.trash.restore(db.trash.all[0].id);
expect(db.trash.all.length).toBe(0);
note = db.notes.note(id);
diff --git a/packages/core/__tests__/utils/index.js b/packages/core/__tests__/utils/index.js
index e5bd41562..980460950 100644
--- a/packages/core/__tests__/utils/index.js
+++ b/packages/core/__tests__/utils/index.js
@@ -27,13 +27,11 @@ const notebookTest = (notebook = TEST_NOTEBOOK) =>
var TEST_NOTE = {
content: {
- delta: {
- ops: [
- { insert: "Hello\n" },
- { insert: "This is colorful", attributes: { color: "#f00" } },
- ],
- },
- text: "I am a text",
+ type: "delta",
+ data: [
+ { insert: "Hello\n" },
+ { insert: "This is colorful", attributes: { color: "#f00" } },
+ ],
},
};
diff --git a/packages/core/__tests__/vault.test.js b/packages/core/__tests__/vault.test.js
index 32554410b..c67d2303a 100644
--- a/packages/core/__tests__/vault.test.js
+++ b/packages/core/__tests__/vault.test.js
@@ -48,15 +48,10 @@ test("lock a note", () =>
expect(note.headline).toBe("");
- const delta = await db.delta.raw(note.data.content.delta);
- expect(delta.noteId).toBeDefined();
- expect(delta.data.iv).toBeDefined();
- expect(delta.data.cipher).toBeDefined();
-
- const text = await db.text.raw(note.data.content.text);
- expect(text.noteId).toBeDefined();
- expect(text.data.iv).toBeDefined();
- expect(text.data.cipher).toBeDefined();
+ const content = await db.content.raw(note.data.contentId);
+ expect(content.noteId).toBeDefined();
+ expect(content.data.iv).toBeDefined();
+ expect(content.data.cipher).toBeDefined();
}));
test("unlock a note", () =>
@@ -65,7 +60,8 @@ test("unlock a note", () =>
await db.vault.add(id);
const note = await db.vault.open(id, "password");
expect(note.id).toBe(id);
- expect(note.content.delta.ops).toBeDefined();
+ expect(note.content.data).toBeDefined();
+ expect(note.content.type).toBe("delta");
}));
test("unlock a note permanently", () =>
@@ -76,7 +72,7 @@ test("unlock a note permanently", () =>
const note = db.notes.note(id);
expect(note.id).toBe(id);
expect(note.headline).not.toBe("");
- const delta = await db.delta.raw(note.data.content.delta);
- expect(delta.data.ops).toBeDefined();
- expect(typeof delta.data.ops).toBe("object");
+ const content = await db.content.raw(note.data.contentId);
+ expect(content.data).toBeDefined();
+ expect(typeof content.data).toBe("object");
}));
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index 7a2f0ec3f..56ed6c154 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -66,9 +66,7 @@ class Database {
/** @type {Tags} */
this.colors = await Tags.new(this, "colors");
/** @type {Content} */
- this.delta = await Content.new(this, "delta", false);
- /** @type {Content} */
- this.text = await Content.new(this, "text", false);
+ this.content = await Content.new(this, "content", false);
/** @type {Trash} */
this.trash = await Trash.new(this, "trash");
diff --git a/packages/core/api/sync/__tests__/collector.test.js b/packages/core/api/sync/__tests__/collector.test.js
index 0fdd76f14..d6f1fa9d7 100644
--- a/packages/core/api/sync/__tests__/collector.test.js
+++ b/packages/core/api/sync/__tests__/collector.test.js
@@ -51,8 +51,7 @@ const tests = [
type: "note",
title: "someTitle",
}),
- getMainCollectionParams("delta", { ops: [{ insert: "true" }] }),
- getMainCollectionParams("text", "true"),
+ getMainCollectionParams("content", { ops: [{ insert: "true" }] }),
];
describe.each(tests)("%s preparation", (collection, add, addExtra) => {
diff --git a/packages/core/api/sync/__tests__/merger.test.js b/packages/core/api/sync/__tests__/merger.test.js
index ce1d459c2..18b18e71e 100644
--- a/packages/core/api/sync/__tests__/merger.test.js
+++ b/packages/core/api/sync/__tests__/merger.test.js
@@ -116,21 +116,19 @@ test("local delta updated after lastSyncedTimestamp should cause merge conflict"
StorageInterface.clear();
return noteTest().then(async ({ db, id }) => {
await login(db);
- const content = {
- text: "my name is abdullah",
- delta: { ops: [{ insert: "my name is abdullah" }] },
- };
- const deltaId = db.notes.note(id).data.content.delta;
+
+ const contentId = db.notes.note(id).data.contentId;
const merger = new Merger(db);
const result = await merger.merge(
{
- delta: [
+ content: [
{
- id: deltaId,
+ id: contentId,
...(await getEncrypted({
- id: deltaId,
+ id: contentId,
noteId: id,
- data: JSON.stringify(content.delta),
+ type: "delta",
+ data: [{ insert: "my name is" }],
dateEdited: 2919,
conflicted: false,
resolved: false,
@@ -140,9 +138,9 @@ test("local delta updated after lastSyncedTimestamp should cause merge conflict"
},
200
);
- const localDelta = await db.delta.raw(deltaId);
- expect(localDelta.conflicted.id).toBe(deltaId);
- expect(localDelta.conflicted.noteId).toBe(id);
+ const localContent = await db.content.raw(contentId);
+ expect(localContent.conflicted.id).toBe(contentId);
+ expect(localContent.conflicted.noteId).toBe(id);
expect(result).toBe(true);
expect(await db.context.read("hasConflicts")).toBe(true);
});
diff --git a/packages/core/api/sync/__tests__/sync.test.js b/packages/core/api/sync/__tests__/sync.test.js
index 1516d0088..3793895a5 100644
--- a/packages/core/api/sync/__tests__/sync.test.js
+++ b/packages/core/api/sync/__tests__/sync.test.js
@@ -41,49 +41,51 @@ test("sync without merge conflicts, cause merge conflicts, resolve them and then
// 4. edit the note's content
await db.notes.add({
id: noteId,
- content: { text: "i am a text", delta: { ops: [{ insert: "text" }] } },
+ content: { type: "delta", data: [{ insert: "text" }] },
});
// 5. sync again and expect conflicts
- const deltaId = db.notes.note(noteId).data.content.delta;
- const delta = {
- id: deltaId,
+ const contentId = db.notes.note(noteId).data.contentId;
+ const content = {
+ id: contentId,
...(await getEncrypted({
- id: deltaId,
+ id: contentId,
+ type: "delta",
dateEdited: Date.now(),
conflicted: false,
- data: { ops: [{ insert: "text" }] },
+ data: [{ insert: "text" }],
})),
};
fetchMock
- .once(JSON.stringify({ notes: [], delta: [delta], synced: false }))
+ .once(JSON.stringify({ notes: [], content: [content], synced: false }))
.once(JSON.stringify({ lastSynced: Date.now() }), { status: 200 });
await expect(db.sync()).rejects.toThrow(
"Merge conflicts detected. Please resolve all conflicts to continue syncing."
);
- let rawDelta = await db.delta.raw(deltaId);
- expect(rawDelta.conflicted.id).toBe(deltaId);
- expect(rawDelta.conflicted.data).toBeTruthy();
+ let rawContent = await db.content.raw(contentId);
+ expect(rawContent.conflicted.id).toBe(contentId);
+ expect(rawContent.conflicted.data).toBeTruthy();
// 6. Resolve conflicts
await db.notes.add({
id: noteId,
conflicted: false,
content: {
- text: "i am a text",
- delta: { data: { ops: [{ insert: "text" }] }, resolved: true },
+ type: "delta",
+ data: [{ insert: "text" }],
+ resolved: true,
},
});
- rawDelta = await db.delta.raw(deltaId);
- expect(rawDelta.conflicted).toBe(false);
+ rawContent = await db.content.raw(contentId);
+ expect(rawContent.conflicted).toBe(false);
//expect(rawDelta.resolved).toBe(true);
// 7. Resync (no conflicts should appear)
fetchMock
- .once(JSON.stringify({ notes: [], delta: [delta], synced: false }))
+ .once(JSON.stringify({ notes: [], content: [content], synced: false }))
.once(JSON.stringify({ lastSynced: Date.now() }), { status: 200 });
await expect(db.sync()).resolves.toBeUndefined();
});
diff --git a/packages/core/api/sync/collector.js b/packages/core/api/sync/collector.js
index edaad3502..263b4fc40 100644
--- a/packages/core/api/sync/collector.js
+++ b/packages/core/api/sync/collector.js
@@ -19,8 +19,7 @@ class Collector {
return {
notes: await this._collect(this._db.notes.raw),
notebooks: await this._collect(this._db.notebooks.raw),
- delta: await this._collect(await this._db.delta.all()),
- text: await this._collect(await this._db.text.all()),
+ content: await this._collect(await this._db.content.all()),
tags: await this._collect(this._db.tags.raw),
colors: await this._collect(this._db.colors.raw),
trash: await this._collect(this._db.trash.raw),
diff --git a/packages/core/api/sync/merger.js b/packages/core/api/sync/merger.js
index 778199e97..514cb81ae 100644
--- a/packages/core/api/sync/merger.js
+++ b/packages/core/api/sync/merger.js
@@ -65,8 +65,7 @@ class Merger {
notes,
synced,
notebooks,
- delta,
- text,
+ content,
tags,
colors,
trash,
@@ -92,32 +91,26 @@ class Merger {
);
await this._mergeArrayWithConflicts(
- delta,
- (id) => this._db.delta.raw(id),
- (item) => this._db.delta.add(item),
+ content,
+ (id) => this._db.content.raw(id),
+ (item) => this._db.content.add(item),
async (local, remote) => {
// merge conflicts resolver
const note = this._db.notes.note(local.noteId).data;
if (local.deleted || note.locked) {
- // if note is locked or delta is deleted we keep the most recent version.
+ // if note is locked or content is deleted we keep the most recent version.
if (remote.dateEdited > local.dateEdited)
- await this._db.delta.add({ id: local.id, ...remote });
+ await this._db.content.add({ id: local.id, ...remote });
} else {
// otherwise we trigger the conflicts
- await this._db.delta.add({ ...local, conflicted: remote });
+ await this._db.content.add({ ...local, conflicted: remote });
await this._db.notes.add({ id: local.noteId, conflicted: true });
await this._db.context.write("hasConflicts", true);
}
}
);
- await this._mergeArray(
- text,
- (id) => this._db.text.raw(id),
- (item) => this._db.text.add(item)
- );
-
await this._mergeArray(
tags,
(id) => this._db.tags.tag(id),
diff --git a/packages/core/api/vault.js b/packages/core/api/vault.js
index 8ddc59250..347afa809 100644
--- a/packages/core/api/vault.js
+++ b/packages/core/api/vault.js
@@ -76,8 +76,7 @@ export default class Vault {
*/
async add(noteId) {
await this._check();
- const note = this._db.notes.note(noteId).data;
- await this._lockNote(noteId, note);
+ await this._lockNote(noteId);
}
/**
@@ -112,7 +111,7 @@ export default class Vault {
if (!note) return;
//await this._check();
let id = note.id || getId();
- return await this._lockNote(id, note);
+ return await this._lockNote(id);
}
// Private & internal methods
@@ -140,61 +139,32 @@ export default class Vault {
}
/** @private */
- async _encryptContent(content, ids) {
- let { text, delta } = content;
- let { deltaId, textId } = ids;
-
- if (!delta.ops) delta = await this._db.delta.get(deltaId);
- if (text === textId) text = await this._db.text.get(textId);
-
- [text, delta] = await Promise.all([
- this._context.encrypt({ password: this._password }, text),
- this._context.encrypt(
- { password: this._password },
- JSON.stringify(delta)
- ),
- ]);
- await Promise.all([
- this._db.text.add({ id: textId, data: text }),
- this._db.delta.add({ id: deltaId, data: delta }),
- ]);
+ async _encryptContent(contentId, content) {
+ let encryptedContent = await this._context.encrypt(
+ { password: this._password },
+ JSON.stringify(content)
+ );
+
+ await this._db.content.add({ id: contentId, data: encryptedContent });
}
/** @private */
- async _decryptContent(content) {
- let { text, delta } = content;
-
- [text, delta] = await Promise.all([
- this._db.text.get(text),
- this._db.text.get(delta),
- ]);
-
- [text, delta] = await Promise.all([
- this._context.decrypt({ password: this._password }, text),
- this._context.decrypt({ password: this._password }, delta),
- ]);
+ async _decryptContent(contentId) {
+ let encryptedContent = await this._db.content.raw(contentId);
- delta = JSON.parse(delta);
+ let decryptedContent = await this._context.decrypt(
+ { password: this._password },
+ encryptedContent.data
+ );
- return {
- delta,
- text,
- };
+ return { type: encryptedContent.type, data: JSON.parse(decryptedContent) };
}
/** @private */
- async _lockNote(id, note) {
- let oldNote = this._db.notes.note(id);
+ async _lockNote(id) {
+ let note = this._db.notes.note(id);
- let deltaId = 0;
- let textId = 0;
-
- if (oldNote && oldNote.data.content) {
- deltaId = oldNote.data.content.delta;
- textId = oldNote.data.content.text;
- }
-
- await this._encryptContent(note.content, { textId, deltaId });
+ await this._encryptContent(note.data.contentId, await note.content());
return await this._db.notes.add({
id,
@@ -205,25 +175,23 @@ export default class Vault {
/** @private */
async _unlockNote(note, perm = false) {
- if (!note.locked) return;
-
- let { delta, text } = await this._decryptContent(note.content);
+ let content = await this._decryptContent(note.contentId);
if (perm) {
await this._db.notes.add({
id: note.id,
locked: false,
- content: {
- delta,
- text,
- },
+ headline: note.headline,
+ contentId: note.contentId,
+ content,
});
+ // await this._db.content.add({ id: note.contentId, data: content });
return;
}
return {
...note,
- content: { delta, text },
+ content,
};
}
diff --git a/packages/core/collections/content.js b/packages/core/collections/content.js
index 751c1f778..cf840d11f 100644
--- a/packages/core/collections/content.js
+++ b/packages/core/collections/content.js
@@ -16,6 +16,7 @@ export default class Content extends Collection {
await this._collection.addItem({
noteId: content.noteId,
id,
+ type: content.type,
data: content.data || content,
conflicted: content.conflicted || false,
resolved: !!content.resolved,
diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js
index 094360dee..db09a437e 100644
--- a/packages/core/collections/notes.js
+++ b/packages/core/collections/notes.js
@@ -9,6 +9,7 @@ import {
import Note from "../models/note";
import getId from "../utils/id";
import { EV } from "../common";
+import { getContentFromData } from "../content-types";
var tfun = require("transfun/transfun.js").tfun;
if (!tfun) {
tfun = global.tfun;
@@ -23,64 +24,52 @@ export default class Notes extends Collection {
let id = noteArg.id || getId();
let oldNote = this._collection.getItem(id);
- let deltaId = 0;
- let textId = 0;
-
- if (oldNote && oldNote.content) {
- deltaId = oldNote.content.delta;
- textId = oldNote.content.text;
- }
let note = {
...oldNote,
...noteArg,
};
- if (isNoteEmpty(note)) {
- if (oldNote) {
- EV.publish("notes:removeEmptyNote", id);
- await this.remove(id);
- }
- return;
- }
+ if (!oldNote && !noteArg.content) return;
- const { text, delta } = note.content;
+ if (noteArg.content && noteArg.content.data && noteArg.content.type) {
+ const { type, data, conflicted, resolved } = noteArg.content;
- if (!textId && isHex(text)) textId = text;
- if (!deltaId && isHex(delta)) deltaId = delta;
+ let content = getContentFromData(type, data);
+ if (!content) throw new Error("Invalid content type.");
+ note.title = getNoteTitle(note, content);
+ note.headline = getNoteHeadline(note, content);
- if (delta && typeof delta === "object") {
- deltaId = await this._db.delta.add({
- noteId: id,
- id: deltaId,
- data: delta.data || delta,
- conflicted: delta.conflicted,
- resolved: delta.resolved,
- });
- }
+ if (isNoteEmpty(note, content)) {
+ if (oldNote) {
+ EV.publish("notes:removeEmptyNote", id);
+ await this.remove(id);
+ }
+ return;
+ }
- if (text !== textId) {
- textId = await this._db.text.add({
+ note.contentId = await this._db.content.add({
noteId: id,
- id: textId,
- data: text,
+ id: note.contentId,
+ type,
+ data,
+ conflicted,
+ resolved,
});
- note.title = getNoteTitle(note);
- note.headline = getNoteHeadline(note);
}
note = {
id,
+ contentId: note.contentId,
type: "note",
title: note.title,
- content: { text: textId, delta: deltaId },
+ headline: note.headline,
pinned: !!note.pinned,
locked: !!note.locked,
notebook: note.notebook || {},
colors: note.colors || [],
tags: note.tags || [],
favorite: !!note.favorite,
- headline: note.headline,
dateCreated: note.dateCreated,
conflicted: !!note.conflicted,
};
@@ -233,50 +222,18 @@ export default class Notes extends Collection {
}
}
-function isNoteEmpty(note) {
- if (!note.content) return true;
- const {
- title,
- content: { delta },
- locked,
- } = note;
- const text = getText(note);
-
+function isNoteEmpty(note, content) {
+ const { title, locked } = note;
const isTitleEmpty = !title || !title.trim().length;
- const isTextEmpty = !isHex(text) && (!text || !text.trim().length);
- const isDeltaEmpty = !isHex(delta) && _isDeltaEmpty(delta);
- return !locked && isTitleEmpty && isTextEmpty && isDeltaEmpty;
-}
-
-function _isDeltaEmpty(delta) {
- let obj = delta;
- if (delta.constructor === Object) obj = delta.ops;
-
- return (
- !obj ||
- obj
- .map((o) => o.insert)
- .join("")
- .trim().length <= 0
- );
+ return !locked && isTitleEmpty && content.isEmpty();
}
-function getNoteHeadline(note) {
+function getNoteHeadline(note, content) {
if (note.locked) return "";
- const text = getText(note);
- return text.substring(0, 150) + (text.length > 150 ? "..." : "");
-}
-
-function getNoteTitle(note) {
- if (note.title && note.title.length > 0) return note.title.trim();
- const text = getText(note);
- return text.split(" ").slice(0, 3).join(" ").trim();
+ return content.toHeadline();
}
-function getText(note) {
- if (!note.content || !note.content.text) return "";
- const { text } = note.content;
- if (text.data != null) return text.data;
- else if (text != null) return text;
- else return "";
+function getNoteTitle(note, content) {
+ if (note.title && note.title.trim().length > 0) return note.title.trim();
+ return content.toTitle();
}
diff --git a/packages/core/collections/trash.js b/packages/core/collections/trash.js
index 05a35e418..1da96580d 100644
--- a/packages/core/collections/trash.js
+++ b/packages/core/collections/trash.js
@@ -44,8 +44,7 @@ export default class Trash extends Collection {
let item = this._collection.getItem(id);
if (!item) continue;
if (item.itemType === "note") {
- await this._db.delta.remove(item.content.delta);
- await this._db.text.remove(item.content.text);
+ await this._db.content.remove(item.contentId);
}
await this._collection.removeItem(id);
}
diff --git a/packages/core/content-types/delta.js b/packages/core/content-types/delta.js
new file mode 100644
index 000000000..24e44767c
--- /dev/null
+++ b/packages/core/content-types/delta.js
@@ -0,0 +1,42 @@
+import { QuillDeltaToHtmlConverter } from "quill-delta-to-html";
+import { deltaToMarkdown } from "quill-delta-to-markdown";
+
+class Delta {
+ constructor(data) {
+ this.data = data;
+ this._text = this.toTXT();
+ }
+
+ toHTML() {
+ const deltaConverter = new QuillDeltaToHtmlConverter(this.data, {
+ classPrefix: "nn",
+ inlineStyles: true,
+ });
+ return deltaConverter.convert();
+ }
+
+ toTXT() {
+ return this.data.reduce(function (text, op) {
+ if (!op.insert) return text;
+ if (typeof op.insert !== "string") return text + " ";
+ return text + op.insert;
+ }, "");
+ }
+
+ toMD() {
+ return deltaToMarkdown(this.data);
+ }
+
+ toTitle() {
+ return this._text.split(/\W+/).slice(0, 3).join(" ").trim();
+ }
+
+ toHeadline() {
+ return this._text.split(" ").slice(0, 20).join(" ") + "...";
+ }
+
+ isEmpty() {
+ return this._text.trim().length <= 0;
+ }
+}
+export default Delta;
diff --git a/packages/core/content-types/index.js b/packages/core/content-types/index.js
new file mode 100644
index 000000000..dee0f518b
--- /dev/null
+++ b/packages/core/content-types/index.js
@@ -0,0 +1,10 @@
+import Delta from "./delta";
+
+export function getContentFromData(type, data) {
+ switch (type) {
+ case "delta":
+ return new Delta(data);
+ default:
+ return null;
+ }
+}
diff --git a/packages/core/models/note.js b/packages/core/models/note.js
index 117683ba0..2dea0fa99 100644
--- a/packages/core/models/note.js
+++ b/packages/core/models/note.js
@@ -1,7 +1,7 @@
-import Converter from "../utils/converter";
import MarkdownBuilder from "../utils/templates/markdown/builder";
import HTMLBuilder from "../utils/templates/html/builder";
import TextBuilder from "../utils/templates/text/builder";
+import { getContentFromData } from "../content-types";
export default class Note {
/**
@@ -57,27 +57,25 @@ export default class Note {
editedOn: this.dateEdited,
createdOn: this.data.dateCreated,
};
+ const { data, type } = await this._db.content.raw(this._note.contentId);
+ let content = getContentFromData(type, data);
switch (to) {
case "html":
- templateData.content = Converter.deltaToHTML(await this.delta());
+ templateData.content = content.toHTML();
return HTMLBuilder.buildHTML(templateData);
case "txt":
- templateData.content = await this.text();
+ templateData.content = content._text;
return TextBuilder.buildText(templateData);
case "md":
- templateData.content = Converter.deltaToMD(await this.delta());
+ templateData.content = content.toMD();
return MarkdownBuilder.buildMarkdown(templateData);
default:
throw new Error("Export format not supported.");
}
}
- delta() {
- return this._db.delta.get(this._note.content.delta);
- }
-
- text() {
- return this._db.text.get(this._note.content.text);
+ content() {
+ return this._db.content.get(this._note.contentId);
}
color(color) {
diff --git a/packages/core/utils/converter.js b/packages/core/utils/converter.js
deleted file mode 100644
index 6a45c3471..000000000
--- a/packages/core/utils/converter.js
+++ /dev/null
@@ -1,16 +0,0 @@
-import { QuillDeltaToHtmlConverter } from "quill-delta-to-html";
-import { deltaToMarkdown } from "quill-delta-to-markdown";
-
-function deltaToHTML(delta) {
- const deltaConverter = new QuillDeltaToHtmlConverter(delta.ops || delta, {
- classPrefix: "nn",
- inlineStyles: true,
- });
- return deltaConverter.convert();
-}
-
-function deltaToMD(delta) {
- return deltaToMarkdown(delta.ops || delta);
-}
-
-export default { deltaToHTML, deltaToMD };