Skip to content

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

feat: simplify opening preview sessions

thecodrreb1e2fc56d91
diff --git a/apps/web/src/common/app-events.js b/apps/web/src/common/app-events.js
index 9ae4db4ae..be32ecf39 100644
--- a/apps/web/src/common/app-events.js
+++ b/apps/web/src/common/app-events.js
@@ -3,7 +3,6 @@ import EventManager from "notes-core/utils/eventmanager";
export const AppEventManager = new EventManager();
export const AppEvents = {
UPDATE_ATTACHMENT_PROGRESS: "updateAttachmentProgress",
- UPDATE_WORD_COUNT: "updateWordCount",
UPDATE_STATUS: "updateStatus",
REMOVE_STATUS: "removeStatus",
};
diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx
index 725674b46..ad2bd853e 100644
--- a/apps/web/src/components/editor/index.tsx
+++ b/apps/web/src/components/editor/index.tsx
@@ -12,8 +12,7 @@ import Toolbar from "./toolbar";
import { AppEventManager, AppEvents } from "../../common/app-events";
import { FlexScrollContainer } from "../scroll-container";
import { formatDate } from "notes-core/utils/date";
-import { debounce, debounceWithId } from "../../utils/debounce";
-import { CharacterCounter } from "./types";
+import { debounceWithId } from "../../utils/debounce";
import Tiptap from "./tiptap";
import Header from "./header";
import { Attachment } from "../icons";
@@ -26,12 +25,11 @@ import { db } from "../../common/db";
import useMobile from "../../utils/use-mobile";
import Titlebox from "./title-box";
-function updateWordCount(counter?: CharacterCounter) {
- AppEventManager.publish(
- AppEvents.UPDATE_WORD_COUNT,
- counter ? counter.words() : 0
- );
-}
+type PreviewSession = {
+ content: { data: string; type: string };
+ dateCreated: number;
+ dateEdited: number;
+};
function onEditorChange(noteId: string, sessionId: string, content: string) {
if (!content) return;
@@ -48,7 +46,6 @@ function onTitleChange(noteId: string, title: string) {
editorstore.get().setTitle(noteId, title);
}
-const debouncedUpdateWordCount = debounce(updateWordCount, 1000);
const debouncedOnEditorChange = debounceWithId(onEditorChange, 100);
const debouncedOnTitleChange = debounceWithId(onTitleChange, 100);
@@ -62,22 +59,37 @@ export default function EditorManager({
const isNewSession = !!nonce && noteId === 0;
const isOldSession = !nonce && !!noteId;
+ // the only state that changes. Everything else is
+ // stored in refs. Update this value to trigger an
+ // update.
+ const [timestamp, setTimestamp] = useState<number>(0);
+
const content = useRef<string>("");
const title = useRef<string>("");
- const [timestamp, setTimestamp] = useState<number>(0);
+ const previewSession = useRef<PreviewSession>();
+ const [dropRef, overlayRef] = useDragOverlay();
const arePropertiesVisible = useStore((store) => store.arePropertiesVisible);
const toggleProperties = useStore((store) => store.toggleProperties);
- const isPreviewMode = useStore(
- (store) => store.session.sessionType === "preview"
- );
const isReadonly = useStore(
- (store) => store.session.readonly || isPreviewMode
+ (store) => store.session.readonly || !!previewSession.current
);
- const [dropRef, overlayRef] = useDragOverlay();
// TODO move this somewhere more appropriate
// const init = useStore((store) => store.init);
+ const openSession = useCallback(async (noteId) => {
+ await editorstore.get().openSession(noteId);
+
+ const { getSessionContent, session } = editorstore.get();
+ const sessionContent = await getSessionContent();
+
+ previewSession.current = undefined;
+ title.current = session.title;
+ content.current = sessionContent?.data;
+ setTimestamp(Date.now());
+ if (noteId && sessionContent) await db.attachments?.downloadImages(noteId);
+ }, []);
+
useEffect(() => {
if (!isNewSession) return;
@@ -87,40 +99,14 @@ export default function EditorManager({
title.current = "";
content.current = "";
setTimestamp(Date.now());
- console.log("Updating timestamp (new)");
})();
}, [isNewSession, nonce]);
- useEffect(() => {
- if (!noteId || isOldSession) return;
-
- (async function () {
- const sessionContent = await editorstore.get().getSessionContent();
- content.current = sessionContent?.data;
- setTimestamp(Date.now());
- if (noteId && sessionContent)
- await db.attachments?.downloadImages(noteId);
- console.log("Updating timestamp (preview)");
- })();
- }, [noteId, isPreviewMode, isOldSession]);
-
useEffect(() => {
if (!isOldSession) return;
- (async function () {
- await editorstore.get().openSession(noteId);
-
- const { getSessionContent, session } = editorstore.get();
- const sessionContent = await getSessionContent();
-
- title.current = session.title;
- content.current = sessionContent?.data;
- setTimestamp(Date.now());
- if (noteId && sessionContent)
- await db.attachments?.downloadImages(noteId);
- console.log("Updating timestamp (old)");
- })();
- }, [noteId, isOldSession]);
+ openSession(noteId);
+ }, [openSession, noteId, isOldSession]);
return (
<Flex
@@ -134,7 +120,12 @@ export default function EditorManager({
overflow: "hidden",
}}
>
- {isPreviewMode && <PreviewModeNotice />}
+ {previewSession.current && (
+ <PreviewModeNotice
+ {...previewSession.current}
+ onDiscard={() => openSession(noteId)}
+ />
+ )}
<Editor
nonce={timestamp}
title={title.current}
@@ -144,7 +135,20 @@ export default function EditorManager({
onRequestFocus: () => toggleProperties(false),
}}
/>
- {arePropertiesVisible && <Properties />}
+ {arePropertiesVisible && (
+ <Properties
+ onOpenPreviewSession={async (session: PreviewSession) => {
+ const { content: sessionContent } = session;
+ content.current = sessionContent.data;
+ previewSession.current = session;
+ setTimestamp(Date.now());
+
+ if (noteId && sessionContent.data) {
+ await db.content?.downloadMedia(noteId, sessionContent, true);
+ }
+ }}
+ />
+ )}
<DropZone overlayRef={overlayRef} />
</Flex>
);
@@ -197,7 +201,6 @@ export function Editor(props: EditorProps) {
hash: string;
src: string;
}) => {
- console.log(hash, src);
if (groupId?.startsWith("monograph")) return;
editor.loadImage(hash, src);
}
@@ -315,14 +318,21 @@ function EditorChrome(props: PropsWithChildren<EditorProps>) {
);
}
-function PreviewModeNotice() {
- const disablePreviewMode = useCallback(async (cancelled) => {
- const { id, sessionId, content } = editorstore.get().session;
- if (!cancelled) {
- await editorstore.saveSessionContent(id, sessionId, content);
- }
- await editorstore.openSession(id, true);
- }, []);
+type PreviewModeNoticeProps = PreviewSession & {
+ onDiscard: () => void;
+};
+function PreviewModeNotice(props: PreviewModeNoticeProps) {
+ const { dateCreated, dateEdited, content, onDiscard } = props;
+ const disablePreviewMode = useCallback(
+ async (cancelled) => {
+ const { id, sessionId } = editorstore.get().session;
+ if (!cancelled) {
+ await editorstore.saveSessionContent(id, sessionId, content);
+ }
+ onDiscard();
+ },
+ [onDiscard, content]
+ );
return (
<Flex
@@ -334,9 +344,8 @@ function PreviewModeNotice() {
<Flex flexDirection={"column"} mr={4}>
<Text variant={"subtitle"}>Preview</Text>
<Text variant={"body"}>
- You are previewing note version edited from{" "}
- {formatDate(editorstore.get().session.dateCreated)} to{" "}
- {formatDate(editorstore.get().session.dateEdited)}.
+ You are previewing note version edited from {formatDate(dateCreated)}{" "}
+ to {formatDate(dateEdited)}.
</Text>
</Flex>
<Flex>
diff --git a/apps/web/src/components/properties/index.js b/apps/web/src/components/properties/index.js
index 1e2ecd6a2..de10dcb94 100644
--- a/apps/web/src/components/properties/index.js
+++ b/apps/web/src/components/properties/index.js
@@ -44,10 +44,11 @@ const metadataItems = [
},
];
-function Properties() {
+function Properties(props) {
+ const { onOpenPreviewSession } = props;
+
const [versionHistory, setVersionHistory] = useState([]);
- const openPreviewSession = useStore((store) => store.openPreviewSession);
const toggleProperties = useStore((store) => store.toggleProperties);
const isFocusMode = useAppStore((store) => store.isFocusMode);
const session = useStore((store) => store.session);
@@ -271,7 +272,12 @@ function Properties() {
)} occupied`}
>
{attachments.map((attachment, i) => (
- <Attachment item={attachment} index={i} isCompact />
+ <Attachment
+ key={attachment.id}
+ item={attachment}
+ index={i}
+ isCompact
+ />
))}
</Card>
)}
@@ -300,6 +306,7 @@ function Properties() {
return (
<Flex
+ key={session.id}
data-test-id={`session-${index}`}
py={1}
px={2}
@@ -322,7 +329,7 @@ function Properties() {
try {
const decryptedContent =
await db.vault.decryptContent(content, password);
- openPreviewSession({
+ onOpenPreviewSession({
content: decryptedContent,
dateCreated: session.dateCreated,
dateEdited: session.dateModified,
@@ -333,7 +340,7 @@ function Properties() {
}
});
} else {
- openPreviewSession({
+ onOpenPreviewSession({
content,
dateCreated: session.dateCreated,
dateEdited: session.dateModified,
diff --git a/apps/web/src/stores/editor-store.js b/apps/web/src/stores/editor-store.js
index d06eca16f..94907a678 100644
--- a/apps/web/src/stores/editor-store.js
+++ b/apps/web/src/stores/editor-store.js
@@ -39,13 +39,6 @@ export const getDefaultSession = (sessionId = Date.now()) => {
};
};
-const getDefaultPreviewSession = () => ({
- sessionType: "preview",
- state: undefined,
- dateEdited: 0,
- dateCreated: 0,
-});
-
class EditorStore extends BaseStore {
session = getDefaultSession();
arePropertiesVisible = false;
@@ -72,19 +65,6 @@ class EditorStore extends BaseStore {
});
};
- openPreviewSession = (session) => {
- this.set((state) => {
- state.session = {
- ...getDefaultPreviewSession(),
- ...session,
- id: state.session.id,
- title: state.session.title,
- state: SESSION_STATES.new,
- };
- });
- appStore.setIsEditorOpen(true);
- };
-
openLockedSession = async (note) => {
this.set((state) => {
state.session = {
@@ -253,8 +233,6 @@ class EditorStore extends BaseStore {
switch (session.sessionType) {
case "default":
return await db.content.raw(session.contentId);
- case "preview":
- return session.content;
case "locked":
return session.content;
default: