A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
feat: implement multiple notes export
thecodrr46fd1d72aa50
diff --git a/apps/web/package.json b/apps/web/package.jsonindex 21fc644a4..ea9893b74 100644--- a/apps/web/package.json+++ b/apps/web/package.json@@ -33,6 +33,7 @@"react-window": "^1.8.6","rebass": "^4.0.7","timeago-react": "^3.0.0",+ "uzip": "^0.20201126.0","zustand": "^2.2.3"},"devDependencies": {diff --git a/apps/web/src/common/selectionoptions.js b/apps/web/src/common/selectionoptions.jsindex 6bd8ad07c..da767d35c 100644--- a/apps/web/src/common/selectionoptions.js+++ b/apps/web/src/common/selectionoptions.js@@ -7,6 +7,8 @@ import { store as trashStore } from "../stores/trash-store";import { db } from "./index";import { showMoveNoteDialog } from "../components/dialogs/movenotedialog";import { showDeleteConfirmation } from "../components/dialogs/confirm";+import { showExportDialog } from "../components/dialogs/exportdialog";+import { showToast } from "../utils/toast";function createOption(key, icon, onClick) {return {@@ -23,54 +25,58 @@ function createOptions(options = []) {return [...options, DeleteOption];}-const DeleteOption = createOption("deleteOption", Icon.Trash, async function (- state-) {- const item = state.selectedItems[0];+const DeleteOption = createOption(+ "deleteOption",+ Icon.Trash,+ async function (state) {+ const item = state.selectedItems[0];- if (!(await showDeleteConfirmation(item.type, true))) return;+ if (!(await showDeleteConfirmation(item.type, true))) return;- var isAnyNoteOpened = false;- const items = state.selectedItems.map((item) => {- if (item.id === editorStore.get().session.id) isAnyNoteOpened = true;- if (item.locked) return 0;- return item.id;- });+ var isAnyNoteOpened = false;+ const items = state.selectedItems.map((item) => {+ if (item.id === editorStore.get().session.id) isAnyNoteOpened = true;+ if (item.locked) return 0;+ return item.id;+ });- if (item.dateDeleted) {- // we are in trash- await db.trash.delete(...items);- trashStore.refresh();- return;- }+ if (item.dateDeleted) {+ // we are in trash+ await db.trash.delete(...items);+ trashStore.refresh();+ return;+ }- if (isAnyNoteOpened) {- editorStore.newSession();- }+ if (isAnyNoteOpened) {+ editorStore.newSession();+ }- if (item.type === "note") {- await db.notes.delete(...items);- notesStore.refresh();- } else if (item.type === "notebook") {- await db.notebooks.delete(...items);- nbStore.refresh();- } else if (item.notebookId) {- // its a topic- await db.notebooks.notebook(item.notebookId).topics.delete(...items);- // TODO refresh topics+ if (item.type === "note") {+ await db.notes.delete(...items);+ notesStore.refresh();+ } else if (item.type === "notebook") {+ await db.notebooks.delete(...items);+ nbStore.refresh();+ } else if (item.notebookId) {+ // its a topic+ await db.notebooks.notebook(item.notebookId).topics.delete(...items);+ // TODO refresh topics+ }}-});+);-const UnfavoriteOption = createOption("unfavoriteOption", Icon.Star, function (- state-) {- // we know only notes can be favorited- state.selectedItems.forEach(async (item) => {- if (!item.favorite) return;- await db.notes.note(item.id).favorite();- });- notesStore.setContext({ type: "favorites" });-});+const UnfavoriteOption = createOption(+ "unfavoriteOption",+ Icon.Star,+ function (state) {+ // we know only notes can be favorited+ state.selectedItems.forEach(async (item) => {+ if (!item.favorite) return;+ await db.notes.note(item.id).favorite();+ });+ notesStore.setContext({ type: "favorites" });+ }+);const AddToNotebookOption = createOption("atnOption",@@ -84,17 +90,16 @@ const AddToNotebookOption = createOption(});-const ExportOption = createOption("exportOption", Icon.Export, async function (- state-) {- alert("TODO.");- //const items = state.selectedItems.map((item) => item.id);-- // if (await showMoveNoteDialog(items)) {- // //TODO show proper snack- // console.log("Notes moved successfully!");- // }-});+const ExportOption = createOption(+ "exportOption",+ Icon.Export,+ async function (state) {+ const items = state.selectedItems.map((item) => item.id);+ if (await showExportDialog(items)) {+ await showToast("success", `${items.length} notes exported!`);+ }+ }+);const RestoreOption = createOption("restoreOption",diff --git a/apps/web/src/components/dialogs/exportdialog.js b/apps/web/src/components/dialogs/exportdialog.jsindex 380655410..439dd90a4 100644--- a/apps/web/src/components/dialogs/exportdialog.js+++ b/apps/web/src/components/dialogs/exportdialog.js@@ -2,6 +2,7 @@ import React from "react";import { Flex, Button, Text } from "rebass";import { db } from "../../common";import download from "../../utils/download";+import { zip } from "../../utils/zip";import * as Icon from "../icons";import Dialog, { showDialog } from "./dialog";@@ -71,17 +72,29 @@ function ExportDialog(props) {);}-export function showExportDialog(noteId) {+export function showExportDialog(noteIds) {return showDialog((perform) => (<ExportDialog- title={"Export your Note"}+ title={+ noteIds.length > 1 ? `Export ${noteIds.length} notes` : "Export note"+ }icon={Icon.Export}onClose={() => perform(false)}exportNote={async (format) => {- const note = db.notes.note(noteId);- const content = await note.export(format);- if (!content) return perform(false);- download(note.title, content, format);+ 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 = zip(files, format);+ download("notes", zipped, "zip");+ }perform(true);}}/>diff --git a/apps/web/src/components/menu/index.js b/apps/web/src/components/menu/index.jsindex 2c90f687a..92fb55da9 100644--- a/apps/web/src/components/menu/index.js+++ b/apps/web/src/components/menu/index.js@@ -48,7 +48,7 @@ function Menu(props) {}}>{item.component ? (- <item.component data={props.data} />+ item.component) : (<Text as="span" fontFamily="body" fontSize="menu">{item.title}diff --git a/apps/web/src/components/note/index.js b/apps/web/src/components/note/index.jsindex 750c1d1f0..499e8712e 100644--- a/apps/web/src/components/note/index.js+++ b/apps/web/src/components/note/index.js@@ -23,7 +23,7 @@ const pin = async (note) => {function menuItems(note, context) {return [- { title: "colors", component: Colors },+ { title: "colors", component: <Colors data={note} /> },{title: note.notebook?.id ? "Move" : "Add to",onClick: async () => {@@ -43,7 +43,7 @@ function menuItems(note, context) {{title: "Export",onClick: async () => {- if (await showExportDialog(note))+ if (await showExportDialog([note.id]))showToast("success", `Note exported successfully!`);},onlyPro: true,diff --git a/apps/web/src/utils/zip.js b/apps/web/src/utils/zip.jsnew file mode 100644index 000000000..d37ad2ee6--- /dev/null+++ b/apps/web/src/utils/zip.js@@ -0,0 +1,14 @@+import uzip from "uzip";+import { sanitizeFilename } from "./filename";++const textEncoder = new TextEncoder();+function zip(files, format) {+ const obj = {};+ files.forEach((file) => {+ obj[`${sanitizeFilename(file.filename)}.${format}`] = textEncoder.encode(+ file.content+ );+ });+ return uzip.encode(obj);+}+export { zip };diff --git a/apps/web/yarn.lock b/apps/web/yarn.lockindex 0ea7f7205..280fe9659 100644--- a/apps/web/yarn.lock+++ b/apps/web/yarn.lock@@ -12798,6 +12798,11 @@ uuid@^8.3.1:resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.1.tgz#2ba2e6ca000da60fce5a196954ab241131e05a31"integrity sha512-FOmRr+FmWEIG8uhZv6C2bTgEVXsHk08kE7mPlrBbEe+c3r9pjceVPgupIfNIhc4yx55H69OXANrUaSuu9eInKg==+uzip@^0.20201126.0:+ version "0.20201126.0"+ resolved "https://registry.yarnpkg.com/uzip/-/uzip-0.20201126.0.tgz#88dac2fa972cdccd4eef00b40b21c71467556744"+ integrity sha512-qru89GgrQ0wX8ZoHGx1uOVGAA4jNRi8Vneuf7UESfLv22HX4vIQGS3e5KnENXdY8+t1aLcHHMcUHikaKFn79sQ==+v8-compile-cache@^2.0.3:version "2.2.0"resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.2.0.tgz#9471efa3ef9128d2f7c6a7ca39c4dd6b5055b132"