web: add support for importing nested notebooks

This commit is contained in:
Abdullah Atta
2024-09-09 13:13:46 +05:00
parent ca2848e491
commit 983220b2f2
3 changed files with 1456 additions and 336 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -18,7 +18,7 @@
"@henrygd/queue": "^1.0.6",
"@mdi/js": "^7.2.96",
"@mdi/react": "^1.6.1",
"@notesnook-importer/core": "^2.0.0",
"@notesnook-importer/core": "^2.1.1",
"@notesnook/common": "file:../../packages/common",
"@notesnook/core": "file:../../packages/core",
"@notesnook/crypto": "file:../../packages/crypto",

View File

@@ -21,7 +21,8 @@ import { db } from "../common/db";
import {
Note,
Notebook,
ContentType
ContentType,
LegacyNotebook
} from "@notesnook-importer/core/dist/src/models";
import {
ATTACHMENTS_DIRECTORY_NAME,
@@ -182,9 +183,16 @@ async function processNote(entry: ZipEntry, attachments: Record<string, any>) {
}
for (const nb of notebooks) {
const notebookId = await importNotebook(nb).catch(() => undefined);
if (!notebookId) continue;
await db.notes.addToNotebook(notebookId, noteId);
if ("notebook" in nb) {
const notebookId = await importLegacyNotebook(nb).catch(() => undefined);
if (!notebookId) continue;
await db.notes.addToNotebook(notebookId, noteId);
} else {
const notebookIds = await importNotebook(nb).catch(() => undefined);
if (!notebookIds) continue;
for (const notebookId of notebookIds)
await db.notes.addToNotebook(notebookId, noteId);
}
}
}
@@ -193,8 +201,11 @@ async function fileToJson<T>(file: ZipEntry) {
return JSON.parse(text) as T;
}
async function importNotebook(
notebook: Notebook | undefined
/**
* @deprecated
*/
async function importLegacyNotebook(
notebook: LegacyNotebook | undefined
): Promise<string | undefined> {
if (!notebook) return;
const nb = await db.notebooks.find(notebook.notebook);
@@ -204,3 +215,29 @@ async function importNotebook(
title: notebook.notebook
});
}
async function importNotebook(
notebook: Notebook,
parentId?: string
): Promise<string[]> {
if (!notebook) return [];
const id =
(await db.notebooks.find(notebook.title))?.id ||
(await db.notebooks.add({
title: notebook.title
}));
if (!id) throw new Error(`Failed to import notebook: ${notebook.title}`);
if (parentId)
await db.relations.add(
{ type: "notebook", id: parentId },
{ type: "notebook", id: id }
);
const assignedNotebooks: string[] = notebook.children.length > 0 ? [] : [id];
for (const child of notebook.children || []) {
assignedNotebooks.push(...(await importNotebook(child, id)));
}
return assignedNotebooks;
}