Skip to content

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

refactor: cleanup note session preview

thecodrr48a7fb3c09b3
diff --git a/apps/web/src/components/editor/footer.js b/apps/web/src/components/editor/footer.js
index 4e636aae9..bdec48357 100644
--- a/apps/web/src/components/editor/footer.js
+++ b/apps/web/src/components/editor/footer.js
@@ -43,7 +43,7 @@ function EditorFooter() {
<Text variant="subBody" color="bgSecondaryText" mr={2}>
{formatDate(dateEdited || Date.now())}
</Text>
- <SaveStateIcon size={13} color="bgSecondaryText" />
+ {SaveStateIcon && <SaveStateIcon size={13} color="bgSecondaryText" />}
</Flex>
);
}
diff --git a/apps/web/src/components/editor/header.js b/apps/web/src/components/editor/header.js
index c1a7b4f3d..390cbf861 100644
--- a/apps/web/src/components/editor/header.js
+++ b/apps/web/src/components/editor/header.js
@@ -30,7 +30,7 @@ function Header({ readonly }) {
{!readonly && id && (
<Flex alignItems="center" flexWrap="wrap" sx={{ lineHeight: 2.5 }}>
- {tags.map((tag) => (
+ {tags?.map((tag) => (
<IconTag
testId={`tag-${tag}`}
key={tag}
diff --git a/apps/web/src/components/editor/index.js b/apps/web/src/components/editor/index.js
index 3b4a25e8d..76bea9b45 100644
--- a/apps/web/src/components/editor/index.js
+++ b/apps/web/src/components/editor/index.js
@@ -45,14 +45,15 @@ function Editor({ noteId, nonce }) {
const [isEditorLoading, setIsEditorLoading] = useState(true);
const sessionId = useStore((store) => store.session.id);
const sessionState = useStore((store) => store.session.state);
- const isPreviewMode = useStore((store) => store.session.previewMode);
- const isReadonly = useStore((store) => store.session.readonly);
+ const sessionType = useStore((store) => store.session.sessionType);
+ const isPreviewMode = sessionType === "preview";
+ const isReadonly = useStore(
+ (store) => store.session.readonly || isPreviewMode
+ );
const contentId = useStore((store) => store.session.contentId);
- const contentType = useStore((store) => store.session.content?.type);
const saveSession = useStore((store) => store.saveSession);
const newSession = useStore((store) => store.newSession);
const openSession = useStore((store) => store.openSession);
- const closePreviewSession = useStore((store) => store.closePreviewSession);
const toggleProperties = useStore((store) => store.toggleProperties);
const arePropertiesVisible = useStore((store) => store.arePropertiesVisible);
const init = useStore((store) => store.init);
@@ -67,10 +68,10 @@ function Editor({ noteId, nonce }) {
}, [init]);
const startSession = useCallback(
- async function startSession(noteId) {
+ async function startSession(noteId, force) {
if (noteId === 0) newSession(nonce);
else if (noteId) {
- await openSession(noteId);
+ await openSession(noteId, force);
}
},
[newSession, openSession, nonce]
@@ -84,19 +85,18 @@ function Editor({ noteId, nonce }) {
}, []);
const setContent = useCallback(() => {
- const {
- id,
- content: { data },
- } = editorstore.get().session;
+ const { id } = editorstore.get().session;
const editor = editorRef.current?.editor;
if (!editor || !editor.initialized) return;
async function setContents() {
- if (data) editorSetContent(editor, data);
+ console.log("SET CONTENT");
+ let content = await editorstore.get().getSessionContent();
+ if (content?.data) editorSetContent(editor, content.data);
else clearContent(editor);
editorstore.set((state) => (state.session.state = SESSION_STATES.stale));
- if (id) await db.attachments.downloadImages(id);
+ if (id && content) await db.attachments.downloadImages(id);
}
setContents();
}, [clearContent]);
@@ -108,25 +108,13 @@ function Editor({ noteId, nonce }) {
const disablePreviewMode = useCallback(
async (cancelled) => {
- const {
- content,
- oldContent,
- oldDateEdited,
- oldSessionId,
- oldDateCreated,
- } = editorstore.get().session;
-
- closePreviewSession({
- dateCreated: oldDateCreated,
- dateEdited: cancelled ? oldDateEdited : Date.now(),
- sessionId: cancelled ? oldSessionId : Date.now(),
- content: cancelled ? oldContent : content,
- });
- if (cancelled) setContent();
-
- editorRef.current?.editor?.mode.set("design");
+ const { id, content } = editorstore.get().session;
+ if (!cancelled) {
+ await editorstore.get().setSessionContent(content);
+ }
+ await startSession(id, true);
},
- [closePreviewSession, setContent]
+ [startSession]
);
useEffect(
@@ -160,12 +148,14 @@ function Editor({ noteId, nonce }) {
useEffect(() => {
const editor = editorRef.current?.editor;
- if (editor)
- if (isReadonly) {
- editor.mode.set("readonly");
- } else {
- editor.mode.set("design");
- }
+ if (!editor) return;
+
+ if (isReadonly) {
+ console.log("READONLy");
+ editor.mode.set("readonly");
+ } else {
+ editor.mode.set("design");
+ }
}, [isReadonly]);
useEffect(
@@ -228,13 +218,13 @@ function Editor({ noteId, nonce }) {
/>
{isPreviewMode && (
<Notice
- title={"Readonly mode"}
+ title={"Preview"}
subtitle={`You are previewing note version edited from ${formatDate(
editorstore.get().session.dateCreated
)} to ${formatDate(editorstore.get().session.dateEdited)}.`}
onCancel={() => disablePreviewMode(true)}
action={{
- text: "Restore this version",
+ text: "Restore",
onClick: async () => {
await disablePreviewMode(false);
await editorstore.get().saveSession();
@@ -261,37 +251,33 @@ function Editor({ noteId, nonce }) {
{isSessionReady && (
<Suspense fallback={<div />}>
- {contentType === "tiny" ? (
- <>
- <ReactMCE
- editorRef={editorRef}
- onFocus={() => toggleProperties(false)}
- onSave={saveSession}
- sessionId={sessionId}
- onChange={(content, editor) => {
- if (!content) return;
+ <ReactMCE
+ editorRef={editorRef}
+ onFocus={() => toggleProperties(false)}
+ onSave={saveSession}
+ sessionId={sessionId}
+ onChange={(content, editor) => {
+ if (!content) return;
- editorstore.get().setSessionContent({
- type: "tiny",
- data: content,
- });
+ editorstore.get().setSessionContent({
+ type: "tiny",
+ data: content,
+ });
- debouncedUpdateWordCount(editor);
- }}
- changeInterval={100}
- onInit={(editor) => {
- if (sessionId && editorstore.get().session.contentId) {
- setContent();
- } else if (nonce) clearContent();
+ debouncedUpdateWordCount(editor);
+ }}
+ changeInterval={100}
+ onInit={(editor) => {
+ if (sessionId && editorstore.get().session.contentId) {
+ setContent();
+ } else if (nonce) clearContent();
- setTimeout(() => {
- setIsEditorLoading(false);
- // a short delay to make sure toolbar has rendered.
- }, 100);
- }}
- />
- </>
- ) : null}
+ setTimeout(() => {
+ setIsEditorLoading(false);
+ // a short delay to make sure toolbar has rendered.
+ }, 100);
+ }}
+ />
</Suspense>
)}
</Flex>
diff --git a/apps/web/src/components/properties/index.js b/apps/web/src/components/properties/index.js
index b162b63b0..ccbbf8c16 100644
--- a/apps/web/src/components/properties/index.js
+++ b/apps/web/src/components/properties/index.js
@@ -59,8 +59,10 @@ function Properties() {
color,
notebooks,
attachments,
- previewMode: isPreviewMode,
+ sessionType,
+ dateCreated,
} = session;
+ const isPreviewMode = sessionType === "preview";
const changeState = useCallback(
function changeState(prop, value) {
@@ -225,7 +227,7 @@ function Properties() {
</>
)}
</Card>
- {!!notebooks?.length && (
+ {notebooks?.length > 0 && (
<Card title="Referenced In">
{notebooks.map((ref) => {
const notebook = db.notebooks.notebook(ref.id);
@@ -285,7 +287,7 @@ function Properties() {
})}
</Card>
)}
- {attachments.length > 0 && (
+ {attachments?.length > 0 && (
<Card
title="Attachments"
subtitle={`${attachments.length} attachments`}
@@ -404,6 +406,8 @@ function Properties() {
const label = `${fromDate}, ${fromTime} — ${
fromDate !== toDate ? `${toDate}, ` : ""
}${toTime}`;
+ const isSelected =
+ isPreviewMode && session.dateCreated === dateCreated;
return (
<Flex
@@ -412,6 +416,7 @@ function Properties() {
px={2}
sx={{
cursor: "pointer",
+ bg: isSelected ? "bgSecondary" : "transparent",
":hover": {
bg: "hover",
},
diff --git a/apps/web/src/hooks/use-is-user-premium.js b/apps/web/src/hooks/use-is-user-premium.js
index 0980d4727..332b7067b 100644
--- a/apps/web/src/hooks/use-is-user-premium.js
+++ b/apps/web/src/hooks/use-is-user-premium.js
@@ -10,7 +10,7 @@ export function useIsUserPremium() {
}
export function isUserPremium(user) {
- // if (process.env.REACT_APP_CI) return true;
+ if (process.env.REACT_APP_CI) return true;
if (!user) user = userstore.get().user;
const subStatus = user?.subscription?.type;
diff --git a/apps/web/src/stores/editor-store.js b/apps/web/src/stores/editor-store.js
index c9fd03f1f..169c6cd50 100644
--- a/apps/web/src/stores/editor-store.js
+++ b/apps/web/src/stores/editor-store.js
@@ -19,7 +19,7 @@ const SESSION_STATES = {
const getDefaultSession = (sessionId = Date.now()) => {
return {
- previewMode: false,
+ sessionType: "default",
readonly: false,
state: undefined,
saveState: 1, // -1 = not saved, 0 = saving, 1 = saved
@@ -36,12 +36,16 @@ const getDefaultSession = (sessionId = Date.now()) => {
color: undefined,
dateEdited: 0,
attachments: [],
- content: {
- type: "tiny",
- data: "",
- },
};
};
+
+const getDefaultPreviewSession = () => ({
+ sessionType: "preview",
+ state: undefined,
+ dateEdited: 0,
+ dateCreated: 0,
+});
+
const savingMutex = new Mutex();
class EditorStore extends BaseStore {
session = getDefaultSession();
@@ -69,13 +73,26 @@ 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 = {
...getDefaultSession(note.dateEdited),
...note,
+ sessionType: "locked",
id: undefined, // NOTE: we give a session id only after the note is opened.
- content: note.content,
state: SESSION_STATES.unlocked,
};
});
@@ -83,39 +100,6 @@ class EditorStore extends BaseStore {
hashNavigate(`/notes/${note.id}/edit`, { replace: true });
};
- openPreviewSession = (session) => {
- const { content, dateCreated, dateEdited } = session;
- this.set((state) => {
- state.session.oldContent = state.session.content;
- state.session.oldDateCreated = state.session.dateCreated;
- state.session.oldDateEdited = state.session.dateEdited;
- state.session.oldSessionId = state.session.sessionId;
-
- state.session.content = content;
- state.session.sessionId = undefined;
- state.session.dateCreated = dateCreated;
- state.session.dateEdited = dateEdited;
- state.session.previewMode = true;
- state.session.state = SESSION_STATES.new;
- });
- };
-
- closePreviewSession = (newSession) => {
- this.set((state) => {
- state.session.content = newSession.content;
- state.session.sessionId = newSession.sessionId;
- state.session.dateCreated = newSession.dateCreated;
- state.session.dateEdited = newSession.dateEdited;
- state.session.previewMode = false;
- state.session.state = SESSION_STATES.stale;
-
- delete state.session.oldContent;
- delete state.session.oldDateCreated;
- delete state.session.oldDateEdited;
- delete state.session.oldSessionId;
- });
- };
-
openSession = async (noteId, force) => {
await db.notes.init();
@@ -143,17 +127,11 @@ class EditorStore extends BaseStore {
if (note.conflicted)
return hashNavigate(`/notes/${noteId}/conflict`, { replace: true });
- let content = await db.content.insertPlaceholders(
- await db.content.raw(note.contentId),
- "/placeholder.svg"
- );
-
this.set((state) => {
const defaultSession = getDefaultSession(note.dateEdited);
state.session = {
...defaultSession,
...note,
- content: content || defaultSession.content,
state: SESSION_STATES.new,
attachments: db.attachments.ofNote(note.id, "all") || [],
};
@@ -161,15 +139,19 @@ class EditorStore extends BaseStore {
appStore.setIsEditorOpen(true);
};
- saveSession = (oldSession) => {
+ saveSession = (oldSession, content) => {
return savingMutex.runExclusive(() => {
const session = this.get().session;
if (session.readonly) return; // do not allow saving of readonly session
+
this.setSaveState(0);
- return this._saveFn()(session)
+ return this._saveFn()({ ...session, content })
.then(async (id) => {
let note = db.notes.note(id)?.data;
- if (!note) return;
+ if (!note) {
+ this.setSaveState(-1);
+ return;
+ }
/* eslint-disable */
storeSync: {
@@ -268,9 +250,25 @@ class EditorStore extends BaseStore {
};
setSessionContent = (content) => {
- const oldSession = qclone(this.get().session);
- this.get().session.content = content;
- return this.saveSession(oldSession);
+ const session = qclone(this.get().session);
+ return this.saveSession(session, content);
+ };
+
+ getSessionContent = async () => {
+ const session = this.get().session;
+ switch (session.sessionType) {
+ case "default":
+ return await db.content.insertPlaceholders(
+ await db.content.raw(session.contentId),
+ "/placeholder.svg"
+ );
+ case "preview":
+ return session.content;
+ case "locked":
+ return session.content;
+ default:
+ return;
+ }
};
toggleLocked = () => {
diff --git a/apps/web/src/stores/note-store.js b/apps/web/src/stores/note-store.js
index 47c2ded8f..d5a17c9ab 100644
--- a/apps/web/src/stores/note-store.js
+++ b/apps/web/src/stores/note-store.js
@@ -3,6 +3,7 @@ import { db } from "../common/db";
import createStore from "../common/store";
import { store as editorStore } from "./editor-store";
import { store as appStore } from "./app-store";
+import { store as selectionStore } from "./selection-store";
import Vault from "../common/vault";
import BaseStore from ".";
import { EV, EVENTS } from "notes-core/common";
@@ -32,6 +33,7 @@ class NoteStore extends BaseStore {
};
setSelectedNote = (id) => {
+ if (id === 0) selectionStore.get().toggleSelectionMode(false);
this.set((state) => (state.selectedNote = id));
};
@@ -116,10 +118,10 @@ class NoteStore extends BaseStore {
};
readonly = async (id) => {
- const note = db.notes.note(id)
+ const note = db.notes.note(id);
await note.readonly();
this.refresh();
- }
+ };
setColor = async (id, color) => {
try {