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 note publishing
thecodrr96048bdaac2a
diff --git a/apps/web/src/components/editor/toolbar.js b/apps/web/src/components/editor/toolbar.jsindex 7b4e9cff0..4047224ef 100644--- a/apps/web/src/components/editor/toolbar.js+++ b/apps/web/src/components/editor/toolbar.js@@ -7,9 +7,11 @@ import { useStore as useThemeStore } from "../../stores/theme-store";import { useStore, store } from "../../stores/editor-store";import { showToast } from "../../utils/toast";import Animated from "../animated";+import { showPublishView } from "../publish-view";function Toolbar(props) {const sessionState = useStore((store) => store.session.state);+ const isLocked = useStore((store) => store.session.locked);const [undoable, setUndoable] = useState(false);const [redoable, setRedoable] = useState(false);const [isFullscreen, setIsFullscreen] = useState(false);@@ -111,6 +113,13 @@ function Toolbar(props) {enabled: redoable,onClick: () => tinymce.activeEditor.execCommand("Redo"),},+ {+ title: "Publish",+ icon: Icon.Publish,+ enabled: !isLocked,+ new: true,+ onClick: () => showPublishView(store.get().session.id, "top"),+ },{title: "Properties",icon: Icon.Properties,@@ -126,6 +135,7 @@ function Toolbar(props) {toggleFocusMode,toggleProperties,isFocusMode,+ isLocked,theme,toggleNightMode,]@@ -195,6 +205,20 @@ function Toolbar(props) {<Text display={["none", "none", "block"]} fontSize="body" ml={1}>{tool.title}</Text>+ {tool.new && (+ <Text+ variant="subBody"+ fontSize={10}+ ml={1}+ bg="primary"+ color="static"+ px={"3px"}+ py="1px"+ sx={{ borderRadius: "default" }}+ >+ NEW+ </Text>+ )}</Flex></Button>))}diff --git a/apps/web/src/components/icons/index.js b/apps/web/src/components/icons/index.jsindex 0c215b623..fc085b108 100644--- a/apps/web/src/components/icons/index.js+++ b/apps/web/src/components/icons/index.js@@ -141,6 +141,7 @@ export const Fullscreen = createIcon(Icons.mdiFullscreen);export const ExitFullscreen = createIcon(Icons.mdiFullscreenExit);export const Announcement = createIcon(Icons.mdiBullhorn);+export const Publish = createIcon(Icons.mdiCloudUploadOutline);export const Copy = createIcon(Icons.mdiContentCopy);@@ -148,3 +149,4 @@ export const Select = createIcon(Icons.mdiCheckboxMultipleMarkedCircleOutline);export const NotebookEdit = createIcon(Icons.mdiBookEditOutline);export const DeleteForver = createIcon(Icons.mdiDeleteForeverOutline);+export const Monographs = createIcon(Icons.mdiTextBoxMultipleOutline);diff --git a/apps/web/src/components/note/index.js b/apps/web/src/components/note/index.jsindex b7cd9c1a1..bcec4da58 100644--- a/apps/web/src/components/note/index.js+++ b/apps/web/src/components/note/index.js@@ -14,6 +14,7 @@ import { showItemDeletedToast } from "../../common/toasts";import { showUnpinnedToast } from "../../common/toasts";import { showToast } from "../../utils/toast";import { hashNavigate } from "../../navigation";+import { showPublishView } from "../publish-view";function Note(props) {const { item, index, context } = props;@@ -161,6 +162,18 @@ const menuItems = [title: () => "Colors",component: ({ data }) => <Colors note={data.note} />,},+ {+ key: "publish",+ isNew: true,+ disabled: ({ note }) => !db.monographs.isPublished(note.id) && note.locked,+ disableReason: "You cannot publish a locked note.",+ icon: Icon.Publish,+ title: ({ note }) =>+ db.monographs.isPublished(note.id) ? "Unpublish" : "Publish",+ onClick: async ({ note }) => {+ await showPublishView(note.id, "bottom");+ },+ },{key: "addtonotebook",title: () => "Add to notebook(s)",diff --git a/apps/web/src/components/publish-view/index.js b/apps/web/src/components/publish-view/index.jsnew file mode 100644index 000000000..a75665842--- /dev/null+++ b/apps/web/src/components/publish-view/index.js@@ -0,0 +1,225 @@+import React, { useEffect, useState } from "react";+import ReactDOM from "react-dom";+import { Flex, Text, Button } from "rebass";+import * as Icon from "../icons";+import Toggle from "../toggle";+import Field from "../field";+import { db } from "../../common/db";+import ClipboardJS from "clipboard";+import ThemeProvider from "../theme-provider";+import { showToast } from "../../utils/toast";++function PublishView(props) {+ const { noteId, position, onClose } = props;+ const [publishId, setPublishId] = useState();+ const [isPasswordProtected, setIsPasswordProtected] = useState(false);+ const [selfDestruct, setSelfDestruct] = useState(false);+ const [isPublishing, setIsPublishing] = useState(false);++ useEffect(() => {+ setPublishId(db.monographs.monograph(noteId));+ }, [noteId]);++ useEffect(() => {+ var clipboard = new ClipboardJS(".copyPublishLink");+ clipboard.on("success", function (e) {+ e.clearSelection();+ });+ clipboard.on("error", function () {+ console.error("Error while copying text.");+ });++ return () => {+ clipboard?.destroy();+ };+ }, []);++ return (+ <Flex+ sx={{+ position: "absolute",+ zIndex: 999,+ width: ["100%", 350, 350],+ border: "1px solid",+ borderColor: "border",+ borderRadius: "default",+ ...position,+ }}+ bg="background"+ p={2}+ flexDirection="column"+ >+ <Text variant="title">Publish note</Text>+ <Text variant="body" color="fontTertiary">+ This note will be published to a public URL.+ </Text>+ {publishId && (+ <Flex+ mt={1}+ bg="shade"+ p={1}+ sx={{+ border: "1px solid",+ borderColor: "primary",+ borderRadius: "default",+ }}+ justifyContent="space-between"+ >+ <Flex flexDirection="column" mr={2} overflow="hidden">+ <Text variant="body" fontWeight="bold">+ This note is published.+ </Text>+ <Text+ variant="subBody"+ as="a"+ target="_blank"+ href={`https://monographs.notesnook.com/${publishId}`}+ sx={{+ textOverflow: "ellipsis",+ whiteSpace: "nowrap",+ overflow: "hidden",+ }}+ >+ {`https://monographs.notesnook.com/${publishId}`}+ </Text>+ </Flex>+ <Button+ variant="anchor"+ className="copyPublishLink"+ data-clipboard-text={`https://monographs.notesnook.com/${publishId}`}+ >+ <Icon.Copy size={20} color="primary" onClick={() => {}} />+ </Button>+ </Flex>+ )}+ <Toggle+ title="Self destruct?"+ onTip="Note will be automatically unpublished after first view."+ offTip="Note will stay published until manually unpublished."+ isToggled={selfDestruct}+ onToggled={() => setSelfDestruct((s) => !s)}+ />+ <Toggle+ title="Password protect?"+ onTip="Protect published note with a password."+ offTip="Do not protect published note with a password."+ isToggled={isPasswordProtected}+ onToggled={() => setIsPasswordProtected((s) => !s)}+ />+ {isPasswordProtected && (+ <Field+ id="publishPassword"+ label="Password"+ helpText="Enter password to encrypt this note"+ required+ sx={{ my: 1 }}+ />+ )}+ <Flex alignItems="center" justifyContent="space-evenly" mt={2}>+ <Button+ flex={1}+ mr={2}+ disabled={isPublishing}+ onClick={async () => {+ try {+ setIsPublishing(true);+ const password =+ document.getElementById("publishPassword")?.value;++ const publishId = await db.monographs.publish(noteId, {+ selfDestruct,+ password,+ });+ setPublishId(publishId);+ showToast("success", "Note published.");+ } catch (e) {+ console.error(e);+ showToast("error", "Note could not be published: " + e.message);+ } finally {+ setIsPublishing(false);+ }+ }}+ >+ {isPublishing ? (+ <>+ <Icon.Loading color="static" />+ </>+ ) : publishId ? (+ "Update"+ ) : (+ "Publish"+ )}+ </Button>+ {publishId && (+ <Button+ flex={1}+ bg="errorBg"+ mr={2}+ color="error"+ disabled={isPublishing}+ onClick={async () => {+ try {+ setIsPublishing(true);+ await db.monographs.unpublish(noteId);+ setPublishId();+ onClose(true);+ showToast("success", "Note unpublished.");+ } catch (e) {+ console.error(e);+ showToast(+ "error",+ "Note could not be unpublished: " + e.message+ );+ } finally {+ setIsPublishing(false);+ }+ }}+ >+ Unpublish+ </Button>+ )}+ <Button+ flex={1}+ disabled={isPublishing}+ variant="secondary"+ onClick={() => {+ onClose(false);+ }}+ >+ Cancel+ </Button>+ </Flex>+ </Flex>+ );+}++export default PublishView;++export function showPublishView(noteId, location = "top") {+ const root = document.getElementById("dialogContainer");++ if (root) {+ return new Promise((resolve) => {+ const perform = (result) => {+ ReactDOM.unmountComponentAtNode(root);+ resolve(result);+ };+ ReactDOM.render(+ <ThemeProvider>+ <PublishView+ noteId={noteId}+ position={{+ top: location === "top" ? [0, 50, 50] : undefined,+ right: location === "top" ? [0, 20, 20] : undefined,+ bottom: location === "bottom" ? 0 : undefined,+ left: location === "bottom" ? 0 : undefined,+ }}+ onClose={perform}+ />+ </ThemeProvider>,+ root+ );+ });+ }+ return Promise.reject("No element with id 'dialogContainer'");+}diff --git a/apps/web/src/navigation/hash-routes.js b/apps/web/src/navigation/hash-routes.jsindex 4602b3f7b..43a3251a3 100644--- a/apps/web/src/navigation/hash-routes.js+++ b/apps/web/src/navigation/hash-routes.js@@ -25,9 +25,6 @@ import { hashNavigate } from ".";import { Suspense } from "react";import EditorLoading from "../components/editor/loading";import EditorPlaceholder from "../components/editor/-placeholder";-import { store as userStore } from "../stores/user-store";-import { upgrade } from "../common/checkout";-import { isUserPremium } from "../common";const Editor = React.lazy(() => import("../components/editor"));const hashroutes = {@@ -53,9 +50,11 @@ const hashroutes = {showEditTopicDialog(notebookId, topicId);},"/notes/create": () => {+ closeOpenedDialog();hashNavigate("/notes/create", { addNonce: true, replace: true });},"/notes/create/:nonce": ({ nonce }) => {+ closeOpenedDialog();return (<Suspense fallback={<EditorLoading />}><Editor noteId={0} nonce={nonce} />@@ -63,6 +62,7 @@ const hashroutes = {);},"/notes/:noteId/edit": ({ noteId }) => {+ closeOpenedDialog();return (<Suspense fallback={<EditorLoading text="Opening note..." />}><Editor noteId={noteId} />@@ -70,6 +70,7 @@ const hashroutes = {);},"/notes/:noteId/unlock": ({ noteId }) => {+ closeOpenedDialog();return (<RouteContainerbuttons={{@@ -89,6 +90,7 @@ const hashroutes = {);},"/notes/:noteId/conflict": ({ noteId }) => {+ closeOpenedDialog();return <DiffViewer noteId={noteId} />;},"/signup": () => {