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
fix: save state change causing statusbar ui jumps
thecodrr06be693b582e
diff --git a/apps/web/package.json b/apps/web/package.jsonindex 3b25db4bd..85fc22b61 100644--- a/apps/web/package.json+++ b/apps/web/package.json@@ -7,7 +7,7 @@"homepage": "https://notesnook.com/","repository": "https://github.com/streetwriters/notesnook","dependencies": {- "@mdi/js": "^5.9.55",+ "@mdi/js": "^6.5.95","@mdi/react": "^1.4.0","@notesnook/desktop": "file:desktop","@rebass/forms": "^4.0.6",diff --git a/apps/web/src/components/editor/footer.js b/apps/web/src/components/editor/footer.jsindex 8ebe5b2ae..4e636aae9 100644--- a/apps/web/src/components/editor/footer.js+++ b/apps/web/src/components/editor/footer.js@@ -1,14 +1,23 @@import { formatDate } from "notes-core/utils/date";-import React, { useEffect, useState } from "react";+import { useEffect, useState } from "react";import { Flex, Text } from "rebass";import { AppEventManager, AppEvents } from "../../common/app-events";import { useStore } from "../../stores/editor-store";+import { Loading, Saved, NotSaved } from "../icons";++const SAVE_STATE_ICON_MAP = {+ "-1": NotSaved,+ 0: Loading,+ 1: Saved,+};function EditorFooter() {const [totalWords, setTotalWords] = useState(0);const dateEdited = useStore((store) => store.session.dateEdited);const id = useStore((store) => store.session.id);- const isSaving = useStore((store) => store.session.isSaving);+ const SaveStateIcon = useStore(+ (store) => SAVE_STATE_ICON_MAP[store.session.saveState]+ );useEffect(() => {const updateWordCountEvent = AppEventManager.subscribe(@@ -23,24 +32,19 @@ function EditorFooter() {if (!id) return null;return (<Flex alignItems="center">- <Text variant="subBody" color="bgSecondaryText">- <Text as="span" data-test-id="editor-word-count">- {totalWords + " words"}- </Text>- <TextSeperator />+ <Text+ data-test-id="editor-word-count"+ variant="subBody"+ color="bgSecondaryText"+ mr={2}+ >+ {totalWords + " words"}+ </Text>+ <Text variant="subBody" color="bgSecondaryText" mr={2}>{formatDate(dateEdited || Date.now())}- <TextSeperator />- {isSaving ? "Saving" : "Saved"}</Text>+ <SaveStateIcon size={13} color="bgSecondaryText" /></Flex>);}export default EditorFooter;--const TextSeperator = () => {- return (- <Text as="span" mx={1}>- •- </Text>- );-};diff --git a/apps/web/src/components/icons/index.js b/apps/web/src/components/icons/index.jsindex 2c4ab0a07..930a8b06a 100644--- a/apps/web/src/components/icons/index.js+++ b/apps/web/src/components/icons/index.js@@ -48,7 +48,7 @@ import {mdiThemeLightDark,mdiCheckCircle,mdiLanguageMarkdownOutline,- mdiFilePdfOutline,+ mdiFilePdfBox,mdiLanguageHtml5,mdiFormatTitle,mdiAlertCircle,@@ -56,7 +56,6 @@ import {mdiToggleSwitchOffOutline,mdiToggleSwitchOutline,mdiBackupRestore,- mdiCurrencyUsdCircleOutline,mdiPencil,mdiUndoVariant,mdiRedoVariant,@@ -127,6 +126,9 @@ import {mdiLinkVariantOff,mdiNetworkOffOutline,mdiCheckNetworkOutline,+ mdiContentSaveCheckOutline,+ mdiContentSaveAlertOutline,+ mdiCurrencyUsd,} from "@mdi/js";import { useTheme } from "emotion-theming";import { AnimatedFlex } from "../animated";@@ -240,7 +242,7 @@ export const CheckCircle = createIcon(mdiCheckCircle);export const CheckCircleOutline = createIcon(mdiCheckCircleOutline);export const Properties = createIcon(mdiDotsVertical);export const Markdown = createIcon(mdiLanguageMarkdownOutline);-export const PDF = createIcon(mdiFilePdfOutline);+export const PDF = createIcon(mdiFilePdfBox);export const Attachment = createIcon(mdiAttachment);export const Write = createIcon(mdiPencilOutline);export const HTML = createIcon(mdiLanguageHtml5);@@ -252,7 +254,7 @@ export const Info = createIcon(mdiInformation);export const ToggleUnchecked = createIcon(mdiToggleSwitchOffOutline);export const ToggleChecked = createIcon(mdiToggleSwitchOutline);export const Backup = createIcon(mdiBackupRestore);-export const Buy = createIcon(mdiCurrencyUsdCircleOutline);+export const Buy = createIcon(mdiCurrencyUsd);export const Edit = createIcon(mdiPencil);export const Undo = createIcon(mdiUndoVariant);export const Redo = createIcon(mdiRedoVariant);@@ -321,3 +323,6 @@ export const ShortcutLink = createIcon(mdiLinkVariant);export const RemoveShortcutLink = createIcon(mdiLinkVariantOff);export const Offline = createIcon(mdiNetworkOffOutline);export const Online = createIcon(mdiCheckNetworkOutline);++export const Saved = createIcon(mdiContentSaveCheckOutline);+export const NotSaved = createIcon(mdiContentSaveAlertOutline);diff --git a/apps/web/src/stores/editor-store.js b/apps/web/src/stores/editor-store.jsindex e2b93aff9..4476eac45 100644--- a/apps/web/src/stores/editor-store.js+++ b/apps/web/src/stores/editor-store.js@@ -7,6 +7,7 @@ import BaseStore from ".";import { EV, EVENTS } from "notes-core/common";import { hashNavigate } from "../navigation";import { qclone } from "qclone";+import { Mutex } from "async-mutex";const SESSION_STATES = {stale: "stale",@@ -19,11 +20,11 @@ const SESSION_STATES = {const getDefaultSession = (sessionId = Date.now()) => {return {readonly: false,+ state: undefined,+ saveState: 1, // -1 = not saved, 0 = saving, 1 = savedsessionId,contentId: undefined,notebooks: undefined,- state: undefined,- isSaving: false,title: "",id: undefined,pinned: false,@@ -40,6 +41,7 @@ const getDefaultSession = (sessionId = Date.now()) => {},};};+const savingMutex = new Mutex();class EditorStore extends BaseStore {session = getDefaultSession();arePropertiesVisible = false;@@ -156,71 +158,70 @@ class EditorStore extends BaseStore {};saveSession = (oldSession) => {- const session = this.get().session;- if (session.isSaving) return; // avoid multiple in-queue saves; only save one time.- if (session.state === SESSION_STATES.preview) return; // do not allow saving of preview-only content-- this.set((state) => (state.session.isSaving = true));- return this._saveFn()(session)- .then(async (id) => {- let note = db.notes.note(id)?.data;- if (!note) {- this.set((state) => (state.session.isSaving = false));- return;- }- /* eslint-disable */- storeSync: {- if (oldSession?.tags?.length !== session.tags.length)- tagStore.refresh();-- if (oldSession?.color !== session.color) appStore.refreshNavItems();-- if (!oldSession?.context) break storeSync;-- const { type, value } = oldSession.context;- if (type === "topic") await db.notes.move(value, id);- else if (type === "color") await db.notes.note(id).color(value);- else if (type === "tag") await db.notes.note(id).tag(value);-- // update the note.- note = db.notes.note(id)?.data;- }- /* eslint-enable */-- if (!this.get().session.id) {- noteStore.setSelectedNote(id);- }-- const attachments = db.attachments.ofNote(note.id, "all");- this.set((state) => {- state.session.id = note.id;- state.session.isSaving = false;- state.session.notebooks = note.notebooks;- state.session.attachments = attachments;+ 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)+ .then(async (id) => {+ let note = db.notes.note(id)?.data;+ if (!note) return;++ /* eslint-disable */+ storeSync: {+ if (oldSession?.tags?.length !== session.tags.length)+ tagStore.refresh();++ if (oldSession?.color !== session.color) appStore.refreshNavItems();++ if (!oldSession?.context) break storeSync;++ const { type, value } = oldSession.context;+ if (type === "topic") await db.notes.move(value, id);+ else if (type === "color") await db.notes.note(id).color(value);+ else if (type === "tag") await db.notes.note(id).tag(value);++ // update the note.+ note = db.notes.note(id)?.data;+ }+ /* eslint-enable */++ if (!this.get().session.id) {+ noteStore.setSelectedNote(id);+ }++ const attachments = db.attachments.ofNote(note.id, "all");+ this.set((state) => {+ state.session.id = note.id;+ state.session.notebooks = note.notebooks;+ state.session.attachments = attachments;+ state.session.saveState = 1;+ });++ if (!oldSession) {+ noteStore.refresh();+ return;+ }++ if (+ attachments?.length !== oldSession.attachments.length ||+ note.headline !== oldSession.headline ||+ note.title !== oldSession.title+ )+ noteStore.refresh();++ if (!oldSession.id) {+ hashNavigate(`/notes/${id}/edit`, { replace: true });+ }+ })+ .catch((err) => {+ this.setSaveState(-1);+ console.error(err);+ if (session.locked) {+ hashNavigate(`/notes/${session.id}/unlock`, { replace: true });+ }});-- if (!oldSession) {- noteStore.refresh();- return;- }-- if (- attachments?.length !== oldSession.attachments.length ||- note.headline !== oldSession.headline ||- note.title !== oldSession.title- )- noteStore.refresh();-- if (!oldSession.id) {- hashNavigate(`/notes/${id}/edit`, { replace: true });- }- })- .catch((err) => {- console.error(err);- if (session.locked) {- hashNavigate(`/notes/${session.id}/unlock`, { replace: true });- }- });+ });};newSession = async (nonce) => {@@ -282,6 +283,12 @@ class EditorStore extends BaseStore {return this._setTag(tag);};+ setSaveState = (saveState) => {+ return this.set((state) => {+ state.session.saveState = saveState;+ });+ };+toggleProperties = (toggleState) => {this.set((state) =>