Skip to content

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

fix: improve bulk note export

thecodrrc69399afa805
diff --git a/apps/web/src/common/dialogcontroller.js b/apps/web/src/common/dialogcontroller.js
index 402d8933e..fd047e569 100644
--- a/apps/web/src/common/dialogcontroller.js
+++ b/apps/web/src/common/dialogcontroller.js
@@ -10,8 +10,6 @@ import { db } from "./db";
import { showToast } from "../utils/toast";
import { Flex, Text } from "rebass";
import * as Icon from "../components/icons";
-import download from "../utils/download";
-import { zip } from "../utils/zip";
import Config from "../utils/config";
import Dialogs from "../components/dialogs";
@@ -258,77 +256,17 @@ export function showEmailVerificationDialog() {
export function showExportDialog(noteIds) {
return showDialog((Dialogs, perform) => (
<Dialogs.ExportDialog
+ noteIds={noteIds}
title={
noteIds.length > 1 ? `Export ${noteIds.length} notes` : "Export note"
}
icon={Icon.Export}
onClose={() => perform(false)}
- exportNote={async (format) => {
- if (format === "pdf") {
- if (noteIds.length > 1)
- showToast("error", "Multiple notes cannot be exported as PDF.");
- const note = db.notes.note(noteIds[0]);
- let result = await exportToPDF(await note.export("html"));
- perform(result);
- return;
- }
-
- var files = [];
- for (var noteId of noteIds) {
- const note = db.notes.note(noteId);
- const content = await note.export(format);
- if (!content) continue;
- files.push({ filename: note.title, content });
- }
- if (!files.length) return perform(false);
- if (files.length === 1) {
- download(files[0].filename, files[0].content, format);
- } else {
- const zipped = await zip(files, format);
- download("notes", zipped, "zip");
- }
- perform(true);
- }}
+ onDone={() => perform(true)}
/>
));
}
-async function exportToPDF(content) {
- if (!content) return false;
- return new Promise((resolve) => {
- return import("print-js").then(async ({ default: printjs }) => {
- printjs({
- printable: content,
- type: "raw-html",
- onPrintDialogClose: () => {
- resolve();
- },
- });
- return true;
- // TODO
- // const doc = new jsPDF("p", "px", "letter");
- // const div = document.createElement("div");
- // const { width, height } = doc.internal.pageSize;
- // div.innerHTML = content;
- // div.style.width = width - 80 + "px";
- // div.style.height = height - 80 + "px";
- // div.style.position = "absolute";
- // div.style.top = 0;
- // div.style.left = 0;
- // div.style.margin = "40px";
- // div.style.fontSize = "11px";
- // document.body.appendChild(div);
-
- // await doc.html(div, {
- // callback: async (doc) => {
- // div.remove();
- // resolve(doc.output());
- // },
- // });
- });
- });
-}
-
export function showLoadingDialog(dialogData) {
const { title, message, subtitle, action } = dialogData;
return showDialog((Dialogs, perform) => (
diff --git a/apps/web/src/components/dialogs/exportdialog.js b/apps/web/src/components/dialogs/exportdialog.js
index 0a661a0c9..52cb07954 100644
--- a/apps/web/src/components/dialogs/exportdialog.js
+++ b/apps/web/src/components/dialogs/exportdialog.js
@@ -1,7 +1,10 @@
-import React from "react";
+import React, { useState } from "react";
import { Flex, Button, Text } from "rebass";
import * as Icon from "../icons";
import Dialog from "./dialog";
+import download from "../../utils/download";
+import { zip } from "../../utils/zip";
+import { db } from "../../common/db";
const formats = [
{
@@ -31,12 +34,15 @@ const formats = [
},
];
function ExportDialog(props) {
+ const { noteIds } = props;
+ const [progress, setProgress] = useState();
+
return (
<Dialog
isOpen={true}
title={props.title}
icon={props.icon}
- description="You can export your note to Markdown, HTML, PDF or Text."
+ description="You can export your notes as Markdown, HTML, PDF or Text."
onClose={props.onClose}
scrollable
negativeButton={{
@@ -44,36 +50,104 @@ function ExportDialog(props) {
text: "Cancel",
}}
>
- <Flex flexDirection="column">
- {formats.map((format) => (
- <Button
- data-test-id={`export-dialog-${format.type}`}
- variant="tertiary"
- sx={{ display: "flex" }}
- mb={1}
- onClick={() => props.exportNote(format.type)}
- >
- <format.icon
- color="primary"
- sx={{ p: 1, bg: "shade", borderRadius: "default" }}
- />
- <Flex flexDirection="column" alignItems="flex-start" ml={2}>
- <Text variant="title" textAlign="left">
- {format.title}
- </Text>
- <Text
- variant="subtitle"
- textAlign="left"
- fontWeight="normal"
- color="gray"
- >
- {format.subtitle}
- </Text>
- </Flex>
- </Button>
- ))}
+ <Flex my={2} flexDirection="row" justifyContent="center">
+ {progress ? (
+ <Flex>
+ <Text variant="body">
+ Processing note {progress} of {noteIds.length}
+ </Text>
+ </Flex>
+ ) : (
+ <>
+ {formats.map(({ type, title, icon: Icon }) => {
+ if (type === "pdf" && noteIds.length > 1) return null;
+
+ return (
+ <Button
+ key={type}
+ data-test-id={`export-dialog-${type}`}
+ variant="tertiary"
+ sx={{
+ display: "flex",
+ flexDirection: "column",
+ alignItems: "center",
+ mr: 2,
+ ":last-of-type": { mr: 0 },
+ }}
+ onClick={async () => {
+ const format = type;
+ if (format === "pdf") {
+ const note = db.notes.note(noteIds[0]);
+ let result = await exportToPDF(await note.export("html"));
+ return result;
+ }
+
+ var files = [];
+ let index = 0;
+ for (var noteId of noteIds) {
+ setProgress(index++);
+ const note = db.notes.note(noteId);
+ const content = await note.export(format);
+ if (!content) continue;
+ files.push({ filename: note.title, content });
+ }
+ if (!files.length) return false;
+ if (files.length === 1) {
+ download(files[0].filename, files[0].content, format);
+ } else {
+ const zipped = await zip(files, format);
+ download("notes", zipped, "zip");
+ }
+
+ setProgress();
+ props.onDone();
+ }}
+ >
+ <Icon color="primary" size={48} />
+ <Text textAlign="center">{title}</Text>
+ </Button>
+ );
+ })}
+ </>
+ )}
</Flex>
</Dialog>
);
}
export default ExportDialog;
+
+async function exportToPDF(content) {
+ if (!content) return false;
+ return new Promise((resolve) => {
+ return import("print-js").then(async ({ default: printjs }) => {
+ printjs({
+ printable: content,
+ type: "raw-html",
+ onPrintDialogClose: () => {
+ resolve();
+ },
+ });
+ return true;
+ // TODO
+ // const doc = new jsPDF("p", "px", "letter");
+ // const div = document.createElement("div");
+ // const { width, height } = doc.internal.pageSize;
+ // div.innerHTML = content;
+ // div.style.width = width - 80 + "px";
+ // div.style.height = height - 80 + "px";
+ // div.style.position = "absolute";
+ // div.style.top = 0;
+ // div.style.left = 0;
+ // div.style.margin = "40px";
+ // div.style.fontSize = "11px";
+ // document.body.appendChild(div);
+
+ // await doc.html(div, {
+ // callback: async (doc) => {
+ // div.remove();
+ // resolve(doc.output());
+ // },
+ // });
+ });
+ });
+}
diff --git a/apps/web/src/components/route-container/index.js b/apps/web/src/components/route-container/index.js
index 031e56f56..b98c3c98f 100644
--- a/apps/web/src/components/route-container/index.js
+++ b/apps/web/src/components/route-container/index.js
@@ -128,6 +128,7 @@ function Header(props) {
mb={2}
notebook
alignItems="center"
+ sx={{ cursor: "pointer" }}
onClick={() => {
if (shouldSelectAll) toggleSelectionMode(false);
else selectAll();
@@ -150,7 +151,7 @@ function SelectionOptions(props) {
if (!isSelectionMode || !options) return null;
return (
- <Flex>
+ <Flex flexShrink={0}>
{options.map((option) => (
<option.icon
title={option.title}