mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-16 11:47:54 +01:00
web: add support for importing nested notebooks
This commit is contained in:
1741
apps/web/package-lock.json
generated
1741
apps/web/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user