mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 15:09:33 +01:00
web: fix pdf exports
This commit is contained in:
committed by
Abdullah Atta
parent
a20a683b17
commit
05cc947cb4
@@ -40,7 +40,6 @@ export class ExportStream extends ReadableStream<ZipFile> {
|
||||
signal?: AbortSignal,
|
||||
onProgress?: (current: number, text: string) => void
|
||||
) {
|
||||
const textEncoder = new TextEncoder();
|
||||
let index = 0;
|
||||
const counters: Record<string, number> = {};
|
||||
let vaultUnlocked = false;
|
||||
@@ -60,103 +59,99 @@ export class ExportStream extends ReadableStream<ZipFile> {
|
||||
}
|
||||
},
|
||||
async pull(controller) {
|
||||
if (signal?.aborted) {
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (signal?.aborted) {
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
|
||||
const note = db.notes?.note(noteIds[index++]);
|
||||
if (!note) return;
|
||||
if (!vaultUnlocked && note.data.locked) return;
|
||||
const note = db.notes?.note(noteIds[index++]);
|
||||
if (!note) return;
|
||||
if (!vaultUnlocked && note.data.locked) return;
|
||||
|
||||
onProgress && onProgress(index, `Exporting "${note.title}"...`);
|
||||
onProgress && onProgress(index, `Exporting "${note.title}"...`);
|
||||
|
||||
const rawContent = await db.content?.raw(note.data.contentId);
|
||||
const content = note.data.locked
|
||||
? await db.vault?.decryptContent(rawContent)
|
||||
: rawContent;
|
||||
const rawContent = await db.content?.raw(note.data.contentId);
|
||||
const content = note.data.locked
|
||||
? await db.vault?.decryptContent(rawContent)
|
||||
: rawContent;
|
||||
|
||||
const exported = await note
|
||||
.export(format === "pdf" ? "html" : format, content)
|
||||
.catch((e: Error) => {
|
||||
console.error(note.data, e);
|
||||
showToast(
|
||||
"error",
|
||||
`Failed to export note "${note.title}": ${e.message}`
|
||||
);
|
||||
const exported = await note
|
||||
.export(format === "pdf" ? "html" : format, content)
|
||||
.catch((e: Error) => {
|
||||
console.error(note.data, e);
|
||||
showToast(
|
||||
"error",
|
||||
`Failed to export note "${note.title}": ${e.message}`
|
||||
);
|
||||
});
|
||||
|
||||
if (typeof exported !== "string") {
|
||||
showToast("error", `Failed to export note "${note.title}"`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (format === "pdf") {
|
||||
await exportToPDF(note.title, exported);
|
||||
controller.error("PDF export.");
|
||||
return;
|
||||
}
|
||||
|
||||
const filename = sanitizeFilename(note.title, { replacement: "-" });
|
||||
const ext = FORMAT_TO_EXT[format];
|
||||
const filenameWithExtension = [filename, ext].join(".");
|
||||
|
||||
const notebooks = [
|
||||
...(db.relations?.to({ id: note.id, type: "note" }, "notebook") ||
|
||||
[]),
|
||||
...(note.notebooks || []).map(
|
||||
(ref: { id: string; topics: string[] }) => {
|
||||
const notebook = db.notebooks?.notebook(ref.id);
|
||||
const topics: any[] = notebook?.topics.all || [];
|
||||
|
||||
return {
|
||||
title: notebook?.title,
|
||||
topics: ref.topics
|
||||
.map((topicId: string) =>
|
||||
topics.find((topic) => topic.id === topicId)
|
||||
)
|
||||
.filter(Boolean)
|
||||
};
|
||||
}
|
||||
)
|
||||
];
|
||||
|
||||
const filePaths: Array<string> =
|
||||
notebooks.length > 0
|
||||
? notebooks
|
||||
.map((notebook) => {
|
||||
if (notebook.topics.length > 0)
|
||||
return notebook.topics.map((topic: { title: string }) =>
|
||||
[
|
||||
notebook.title,
|
||||
topic.title,
|
||||
filenameWithExtension
|
||||
].join("/")
|
||||
);
|
||||
return [notebook.title, filenameWithExtension].join("/");
|
||||
})
|
||||
.flat()
|
||||
: [filenameWithExtension];
|
||||
|
||||
filePaths.forEach((filePath) => {
|
||||
controller.enqueue({
|
||||
path: makeUniqueFilename(filePath, counters),
|
||||
data: exported,
|
||||
mtime: new Date(note.data.dateEdited),
|
||||
ctime: new Date(note.data.dateCreated)
|
||||
});
|
||||
});
|
||||
|
||||
if (typeof exported !== "string") {
|
||||
showToast("error", `Failed to export note "${note.title}"`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (format === "pdf") {
|
||||
await exportToPDF(note.title, exported);
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
|
||||
const filename = sanitizeFilename(note.title, { replacement: "-" });
|
||||
const ext = FORMAT_TO_EXT[format];
|
||||
const notebooks = db.relations
|
||||
?.to({ id: note.id, type: "note" }, "notebook")
|
||||
.map((notebook) => {
|
||||
return { title: notebook.title, topics: Array<string> };
|
||||
});
|
||||
const notebooksWithTopics: Array<{
|
||||
id: string;
|
||||
topics: Array<string>;
|
||||
}> = note?.notebooks;
|
||||
|
||||
if (notebooksWithTopics)
|
||||
notebooks?.push(
|
||||
...notebooksWithTopics.map((_notebook) => {
|
||||
const notebook = db.notebooks?.notebook(_notebook.id);
|
||||
const _topics = notebook?.topics.all;
|
||||
let topics: any;
|
||||
|
||||
_notebook.topics.map((topicId: string) => {
|
||||
topics = _topics?.filter((topic) => {
|
||||
return topic.id === topicId;
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
title: notebook?.title,
|
||||
topics: topics?.map((topic) => topic.title)
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
const filenameWithExtension = [filename, ext].join(".").toLowerCase();
|
||||
const filePaths: Array<string> = [];
|
||||
|
||||
if (notebooks && notebooks.length > 0) {
|
||||
notebooks.forEach((notebook) => {
|
||||
if (notebook.topics.length > 0)
|
||||
notebook.topics.forEach((topic) => {
|
||||
filePaths.push(
|
||||
`/${notebook.title}/${topic}/${filenameWithExtension}`
|
||||
);
|
||||
});
|
||||
else filePaths.push(`/${notebook.title}/${filenameWithExtension}`);
|
||||
});
|
||||
} else {
|
||||
filePaths.push(filenameWithExtension);
|
||||
}
|
||||
|
||||
filePaths.forEach((filePath) => {
|
||||
controller.enqueue({
|
||||
path: makeUniqueFilename(filePath, counters),
|
||||
data: textEncoder.encode(exported),
|
||||
mtime: new Date(note.data.dateEdited),
|
||||
ctime: new Date(note.data.dateCreated)
|
||||
});
|
||||
});
|
||||
|
||||
if (index === noteIds.length) {
|
||||
controller.close();
|
||||
} catch (e) {
|
||||
controller.error(e);
|
||||
} finally {
|
||||
if (index === noteIds.length) {
|
||||
controller.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user