Skip to content

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

feat: implement note exporting

thecodrraac8b9e9abca
diff --git a/apps/web/src/components/dialogs/dialog.js b/apps/web/src/components/dialogs/dialog.js
index 0924beb30..6e8893733 100644
--- a/apps/web/src/components/dialogs/dialog.js
+++ b/apps/web/src/components/dialogs/dialog.js
@@ -13,7 +13,7 @@ function Dialog(props) {
<Modal
isOpen={props.isOpen || false}
shouldCloseOnOverlayClick={true}
- onRequestClose={props.negativeButton.onClick}
+ onRequestClose={props?.negativeButton?.onClick}
style={{
content: {
top: "50%",
diff --git a/apps/web/src/components/dialogs/exportdialog.js b/apps/web/src/components/dialogs/exportdialog.js
new file mode 100644
index 000000000..5a3294abd
--- /dev/null
+++ b/apps/web/src/components/dialogs/exportdialog.js
@@ -0,0 +1,57 @@
+import React from "react";
+import { Flex, Button, Text, Box } from "rebass";
+import { db } from "../../common";
+import download from "../../utils/download";
+import * as Icon from "../icons";
+import Dialog, { showDialog } from "./dialog";
+
+function ExportDialog(props) {
+ return (
+ <Dialog isOpen={true} title={props.title} icon={props.icon}>
+ <Box>
+ <Text variant="body" mb={2}>
+ Please choose a format to export the note into:
+ </Text>
+ <Flex my={1} justifyContent="center" alignItems="center">
+ <Button
+ variant="tertiary"
+ mr={2}
+ onClick={() => props.exportNote("html")}
+ >
+ <Icon.HTML size={100} color="dimPrimary" /> HTML
+ </Button>
+ <Button
+ variant="tertiary"
+ mr={2}
+ onClick={() => props.exportNote("md")}
+ >
+ <Icon.Markdown size={100} color="dimPrimary" />
+ Markdown
+ </Button>
+ <Button
+ variant="tertiary"
+ mr={2}
+ onClick={() => props.exportNote("txt")}
+ >
+ <Icon.Text size={100} color="dimPrimary" />
+ Text
+ </Button>
+ </Flex>
+ </Box>
+ </Dialog>
+ );
+}
+
+export function showExportDialog(note) {
+ return showDialog((perform) => (
+ <ExportDialog
+ title={"Export Note"}
+ icon={Icon.Export}
+ exportNote={async (format) => {
+ const content = await db.notes.note(note.id).export(format);
+ download(note.title, content, format);
+ perform(true);
+ }}
+ />
+ ));
+}
diff --git a/apps/web/src/components/icons/index.js b/apps/web/src/components/icons/index.js
index 42169a5e2..97637ad00 100644
--- a/apps/web/src/components/icons/index.js
+++ b/apps/web/src/components/icons/index.js
@@ -4,7 +4,7 @@ import * as Icons from "@mdi/js";
import { useTheme } from "emotion-theming";
import Animated from "../animated";
-function Icon({ name, size = 24, color = "icon", stroke, rotate }) {
+function Icon({ name, size = 24, color = "icon", stroke, rotate, hover }) {
const theme = useTheme();
return (
<MDIIcon
@@ -61,6 +61,7 @@ export const Home = createIcon(Icons.mdiHomeOutline);
export const Restore = createIcon(Icons.mdiRecycle);
export const Sync = createIcon(Icons.mdiSync);
export const Loading = createIcon(Icons.mdiLoading);
+export const Export = createIcon(Icons.mdiExport);
/** Properties Icons */
export const ChevronLeft = createIcon(Icons.mdiChevronLeft);
@@ -76,3 +77,8 @@ export const Checkmark = createIcon(Icons.mdiCheck);
export const Properties = createIcon(Icons.mdiDotsHorizontal);
+// FORMATS
+
+export const Markdown = createIcon(Icons.mdiLanguageMarkdownOutline);
+export const HTML = createIcon(Icons.mdiLanguageHtml5);
+export const Text = createIcon(Icons.mdiFormatTitle);
diff --git a/apps/web/src/components/note/index.js b/apps/web/src/components/note/index.js
index c9c0b6303..2419d5c2f 100644
--- a/apps/web/src/components/note/index.js
+++ b/apps/web/src/components/note/index.js
@@ -12,6 +12,7 @@ import { db, COLORS } from "../../common";
import { useTheme } from "emotion-theming";
import Colors from "../menu/colors";
import { isMobile } from "../../utils/dimensions";
+import { showExportDialog } from "../dialogs/exportdialog";
function menuItems(note, context) {
return [
@@ -34,6 +35,13 @@ function menuItems(note, context) {
onClick: () => store.favorite(note),
onlyPro: true,
},
+ {
+ title: "Export",
+ onClick: async () => {
+ await showExportDialog(note);
+ },
+ onlyPro: true,
+ },
{ title: "Edit", onClick: () => editorStore.openSession(note) },
{
title: note.locked ? "Unlock" : "Lock",
diff --git a/apps/web/src/theme/variants/button.js b/apps/web/src/theme/variants/button.js
index c21fb1903..6a902fd01 100644
--- a/apps/web/src/theme/variants/button.js
+++ b/apps/web/src/theme/variants/button.js
@@ -58,6 +58,9 @@ class Tertiary {
bg: "transparent",
border: "2px solid",
borderColor: "border",
+ ":hover": {
+ borderColor: "primary",
+ },
};
}
}
diff --git a/apps/web/src/utils/download.js b/apps/web/src/utils/download.js
new file mode 100644
index 000000000..5c28e64ec
--- /dev/null
+++ b/apps/web/src/utils/download.js
@@ -0,0 +1,53 @@
+import { sanitizeFilename } from "./filename";
+
+const map = {
+ md: "text/markdown",
+ html: "text/html",
+ txt: "text/plain",
+};
+
+export default function download(filename, text, type) {
+ downloadFile(
+ text,
+ sanitizeFilename(filename, { replacement: "-" }) + `.${type}`,
+ map[type]
+ );
+}
+
+function downloadFile(data, filename, mime, bom) {
+ var blobData = typeof bom !== "undefined" ? [bom, data] : [data];
+ var blob = new Blob(blobData, { type: mime || "application/octet-stream" });
+ if (typeof window.navigator.msSaveBlob !== "undefined") {
+ // IE workaround for "HTML7007: One or more blob URLs were
+ // revoked by closing the blob for which they were created.
+ // These URLs will no longer resolve as the data backing
+ // the URL has been freed."
+ window.navigator.msSaveBlob(blob, filename);
+ } else {
+ var blobURL =
+ window.URL && window.URL.createObjectURL
+ ? window.URL.createObjectURL(blob)
+ : window.webkitURL.createObjectURL(blob);
+ var tempLink = document.createElement("a");
+ tempLink.style.display = "none";
+ tempLink.href = blobURL;
+ tempLink.setAttribute("download", filename);
+
+ // Safari thinks _blank anchor are pop ups. We only want to set _blank
+ // target if the browser does not support the HTML5 download attribute.
+ // This allows you to download files in desktop safari if pop up blocking
+ // is enabled.
+ if (typeof tempLink.download === "undefined") {
+ tempLink.setAttribute("target", "_blank");
+ }
+
+ document.body.appendChild(tempLink);
+ tempLink.click();
+
+ // Fixes "webkit blob resource error 1"
+ setTimeout(function () {
+ document.body.removeChild(tempLink);
+ window.URL.revokeObjectURL(blobURL);
+ }, 200);
+ }
+}
diff --git a/apps/web/src/utils/filename.js b/apps/web/src/utils/filename.js
new file mode 100644
index 000000000..a19e63d14
--- /dev/null
+++ b/apps/web/src/utils/filename.js
@@ -0,0 +1,54 @@
+/*jshint node:true*/
+
+/**
+ * Replaces characters in strings that are illegal/unsafe for filenames.
+ * Unsafe characters are either removed or replaced by a substitute set
+ * in the optional `options` object.
+ *
+ * Illegal Characters on Various Operating Systems
+ * / ? < > \ : * | "
+ * https://kb.acronis.com/content/39790
+ *
+ * Unicode Control codes
+ * C0 0x00-0x1f & C1 (0x80-0x9f)
+ * http://en.wikipedia.org/wiki/C0_and_C1_control_codes
+ *
+ * Reserved filenames on Unix-based systems (".", "..")
+ * Reserved filenames in Windows ("CON", "PRN", "AUX", "NUL", "COM1",
+ * "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
+ * "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", and
+ * "LPT9") case-insesitively and with or without filename extensions.
+ *
+ * Capped at 255 characters in length.
+ * http://unix.stackexchange.com/questions/32795/what-is-the-maximum-allowed-filename-and-folder-size-with-ecryptfs
+ *
+ * @param {String} input Original filename
+ * @param {Object} options {replacement: String | Function }
+ * @return {String} Sanitized filename
+ */
+
+var illegalRe = /[/?<>\\:*|"]/g;
+//var controlRe = /[x00-x1f\x80-\x9f]/g;
+var reservedRe = /^\.+$/;
+var windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
+var windowsTrailingRe = /[. ]+$/;
+var whitespace = /\W+/g;
+
+function sanitize(input, replacement) {
+ if (typeof input !== "string") {
+ throw new Error("Input must be string");
+ }
+ var sanitized = input
+ .replace(whitespace, replacement)
+ .replace(illegalRe, replacement)
+ .replace(reservedRe, replacement)
+ .replace(windowsReservedRe, replacement)
+ .replace(windowsTrailingRe, replacement);
+
+ return sanitized.slice(0, 254).toLowerCase();
+}
+
+export function sanitizeFilename(input, options) {
+ var replacement = (options && options.replacement) || "";
+ return sanitize(input, replacement);
+}
diff --git a/apps/web/yarn.lock b/apps/web/yarn.lock
index a15aa63d1..1928fbacb 100644
--- a/apps/web/yarn.lock
+++ b/apps/web/yarn.lock
@@ -3364,6 +3364,13 @@ crypto-browserify@^3.11.0:
randombytes "^2.0.0"
randomfill "^1.0.3"
+crypto-random-string@^3.2.0:
+ version "3.2.0"
+ resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-3.2.0.tgz#d513ef0c2ac6ff7cad5769de585d9bf2ad5a2b4d"
+ integrity sha512-8vPu5bsKaq2uKRy3OL7h1Oo7RayAWB8sYexLKAqvCXVib8SxgbmoF1IN4QMKjBv8uI8mp5gPPMbiRah25GMrVQ==
+ dependencies:
+ type-fest "^0.8.1"
+
css-blank-pseudo@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/css-blank-pseudo/-/css-blank-pseudo-0.1.4.tgz#dfdefd3254bf8a82027993674ccf35483bfcb3c5"
@@ -7307,8 +7314,9 @@ normalize-url@^3.0.0, normalize-url@^3.0.1:
"notes-core@git+ssh://[email protected]:streetwriters/notesnook-core.git":
version "1.5.0"
- resolved "git+ssh://[email protected]:streetwriters/notesnook-core.git#715d3b561e189e570c5abe1214371175bf4ebba0"
+ resolved "git+ssh://[email protected]:streetwriters/notesnook-core.git#68fc61037d85bf48cb150e3e828d800071018d8a"
dependencies:
+ crypto-random-string "^3.2.0"
event-source-polyfill "^1.0.16"
fast-sort "^2.0.1"
fuzzysearch "^1.0.3"