Skip to content

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

prevent unnecessary note saving when note is closed

ammarahm-ed7988befb8bf5
diff --git a/apps/mobile/src/views/Editor/EditorHeader.js b/apps/mobile/src/views/Editor/EditorHeader.js
index a5734cede..23e5e8d74 100644
--- a/apps/mobile/src/views/Editor/EditorHeader.js
+++ b/apps/mobile/src/views/Editor/EditorHeader.js
@@ -1,5 +1,11 @@
-import React, {useEffect} from 'react';
-import {Keyboard, Platform, View} from 'react-native';
+import React, {useEffect, useRef} from 'react';
+import {
+ BackHandler,
+ InteractionManager,
+ Keyboard,
+ Platform,
+ View,
+} from 'react-native';
import {useSafeAreaInsets} from 'react-native-safe-area-context';
import {notesnook} from '../../../e2e/test.ids';
import {ActionIcon} from '../../components/ActionIcon';
@@ -10,12 +16,20 @@ import {
useSettingStore,
useUserStore,
} from '../../provider/stores';
-import {eSendEvent, ToastEvent} from '../../services/EventManager';
+import {DDS} from '../../services/DeviceDetection';
+import {
+ eSendEvent,
+ eSubscribeEvent,
+ eUnSubscribeEvent,
+ ToastEvent,
+} from '../../services/EventManager';
import Navigation from '../../services/Navigation';
import {editing} from '../../utils';
import {db} from '../../utils/DB';
import {
+ eClearEditor,
eCloseFullscreenEditor,
+ eOnLoadNote,
eOpenFullscreenEditor,
eOpenPublishNoteDialog,
} from '../../utils/Events';
@@ -25,9 +39,11 @@ import {EditorTitle} from './EditorTitle';
import {
checkNote,
clearEditor,
+ clearTimer,
EditorWebView,
getNote,
isNotedEdited,
+ loadNote,
setColors,
} from './Functions';
import HistoryComponent from './HistoryComponent';
@@ -44,6 +60,7 @@ const EditorHeader = () => {
const fullscreen = useSettingStore(state => state.fullscreen);
const user = useUserStore(state => state.user);
const insets = useSafeAreaInsets();
+ const handleBack = useRef();
useEffect(() => {
setColors(colors);
@@ -55,6 +72,9 @@ const EditorHeader = () => {
return;
}
tiny.call(EditorWebView, tiny.blur);
+ if (deviceMode === 'mobile') {
+ editing.movedAway = true;
+ }
setTimeout(async () => {
eSendEvent('showTooltip');
toolbarRef.current?.scrollTo({
@@ -153,6 +173,77 @@ const EditorHeader = () => {
]);
};
+ useEffect(() => {
+ eSubscribeEvent(eOnLoadNote, load);
+ eSubscribeEvent(eClearEditor, onCallClear);
+ return () => {
+ eUnSubscribeEvent(eClearEditor, onCallClear);
+ eUnSubscribeEvent(eOnLoadNote, load);
+ };
+ }, []);
+
+ useEffect(() => {
+ if (fullscreen && DDS.isTab) {
+ handleBack.current = BackHandler.addEventListener(
+ 'hardwareBackPress',
+ _onBackPress,
+ );
+ }
+
+ return () => {
+ clearTimer();
+ if (handleBack.current) {
+ handleBack.current.remove();
+ handleBack.current = null;
+ }
+ };
+ }, [fullscreen]);
+
+ const load = async item => {
+ await loadNote(item);
+ InteractionManager.runAfterInteractions(() => {
+ Keyboard.addListener('keyboardDidShow', tiny.onKeyboardShow);
+ if (!DDS.isTab) {
+ handleBack.current = BackHandler.addEventListener(
+ 'hardwareBackPress',
+ _onHardwareBackPress,
+ );
+ }
+ });
+ };
+
+ const onCallClear = async value => {
+ if (value === 'removeHandler') {
+ if (handleBack.current) {
+ handleBack.current.remove();
+ handleBack.current = null;
+ }
+ return;
+ }
+ if (value === 'addHandler') {
+ if (handleBack.current) {
+ handleBack.current.remove();
+ handleBack.current = null;
+ }
+
+ handleBack.current = BackHandler.addEventListener(
+ 'hardwareBackPress',
+ _onHardwareBackPress,
+ );
+ return;
+ }
+ if (editing.currentlyEditing) {
+ await _onBackPress();
+ }
+ };
+
+ const _onHardwareBackPress = async () => {
+ if (editing.currentlyEditing) {
+ await _onBackPress();
+ return true;
+ }
+ };
+
return (
<>
<View
diff --git a/apps/mobile/src/views/Editor/Functions.js b/apps/mobile/src/views/Editor/Functions.js
index 861f61bbb..0bfa941ba 100644
--- a/apps/mobile/src/views/Editor/Functions.js
+++ b/apps/mobile/src/views/Editor/Functions.js
@@ -3,7 +3,11 @@ import {Platform} from 'react-native';
import {presentDialog} from '../../components/Dialog/functions';
import {useEditorStore, useMenuStore} from '../../provider/stores';
import {DDS} from '../../services/DeviceDetection';
-import {eSendEvent} from '../../services/EventManager';
+import {
+ eSendEvent,
+ eSubscribeEvent,
+ eUnSubscribeEvent,
+} from '../../services/EventManager';
import Navigation from '../../services/Navigation';
import PremiumService from '../../services/PremiumService';
import {editing, InteractionManager} from '../../utils';
@@ -75,10 +79,20 @@ export function isNotedEdited() {
return noteEdited;
}
-export function clearTimer() {
+export async function clearTimer(clear) {
if (timer) {
clearTimeout(timer);
timer = null;
+ if (clear) {
+ if (
+ (content?.data &&
+ typeof content.data == 'string' &&
+ content.data?.trim().length > 0) ||
+ (title && title?.trim().length > 0 && id)
+ ) {
+ await saveNote(true);
+ }
+ }
}
if (currentEditingTimer) {
clearTimeout(currentEditingTimer);
@@ -86,8 +100,7 @@ export function clearTimer() {
}
}
-export const CHECK_STATUS = premium =>
- `(function() {
+export const CHECK_STATUS = `(function() {
setTimeout(() => {
let msg = JSON.stringify({
data: true,
@@ -177,7 +190,7 @@ export const loadNote = async item => {
await clearEditor();
}
clearNote();
-
+
noteEdited = false;
id = null;
if (Platform.OS === 'android') {
@@ -205,43 +218,50 @@ export const loadNote = async item => {
if (id === item.id && !item.forced) {
return;
}
- await setNote(item);
- clearTimer();
+
eSendEvent('loadingNote', item);
+ clearTimer();
+ await setNote(item);
webviewInit = false;
editing.isFocused = false;
- setTimeout(() => {
- eSendEvent('webviewreset');
+ setTimeout(async () => {
+ if (await checkStatus(true)) {
+ EditorWebView.current?.reload();
+ } else {
+ eSendEvent('webviewreset');
+ }
}, 1);
InteractionManager.runAfterInteractions(async () => {
- Navigation.setRoutesToUpdate([
- Navigation.routeNames.NotesPage,
- Navigation.routeNames.Favorites,
- Navigation.routeNames.Notes,
- ]);
useEditorStore.getState().setCurrentlyEditingNote(item.id);
- checkStatus();
}, 50);
}
};
-const checkStatus = (reset = false) => {
- webviewOK = false;
- EditorWebView.current?.injectJavaScript(CHECK_STATUS());
- clearTimeout(webviewTimer);
- webviewTimer = setTimeout(() => {
- if (!webviewOK) {
- if (!reset) {
- checkStatus(true);
-
- return;
+const checkStatus = async noreset => {
+ return new Promise(resolve => {
+ webviewOK = false;
+ clearTimeout(webviewTimer);
+ webviewTimer = null;
+ const onWebviewOk = () => {
+ webviewOK = true;
+ webviewInit = true;
+ clearTimeout(webviewTimer);
+ webviewTimer = null;
+ resolve(true);
+ eUnSubscribeEvent('webviewOk', onWebviewOk);
+ };
+ eSubscribeEvent('webviewOk', onWebviewOk);
+ EditorWebView.current?.injectJavaScript(CHECK_STATUS);
+
+ webviewTimer = setTimeout(() => {
+ if (!webviewOK && !noreset) {
+ webviewInit = false;
+ EditorWebView = createRef();
+ eSendEvent('webviewreset');
+ resolve(false);
}
- webviewInit = false;
- EditorWebView = createRef();
- eSendEvent('webviewreset');
- } else {
- }
- }, 3500);
+ }, 1000);
+ });
};
export const _onMessage = async evt => {
@@ -309,6 +329,7 @@ export const _onMessage = async evt => {
loadNoteInEditor();
break;
case 'running':
+ eSendEvent('webviewOk');
webviewOK = true;
break;
case 'editorSettings':
@@ -407,16 +428,8 @@ function onNoteChange() {
}
export async function clearEditor() {
- clearTimer();
+ await clearTimer(true);
try {
- if (
- (content?.data &&
- typeof content.data == 'string' &&
- content.data?.trim().length > 0) ||
- (title && title?.trim().length > 0 && id)
- ) {
- await saveNote(true);
- }
tiny.call(EditorWebView, tiny.reset, true);
clearNote();
editing.focusType = null;
@@ -496,7 +509,7 @@ async function addToCollection(id) {
let isSaving = false;
export async function saveNote(preventUpdate) {
- console.log('saving note now');
+ if (!noteEdited) return;
if (isSaving && !id) return;
isSaving = true;
try {
diff --git a/apps/mobile/src/views/Editor/index.js b/apps/mobile/src/views/Editor/index.js
index 58a51b093..f5b2e79ea 100755
--- a/apps/mobile/src/views/Editor/index.js
+++ b/apps/mobile/src/views/Editor/index.js
@@ -57,8 +57,6 @@ const Editor = React.memo(
}
},[premiumUser])
-
-
const onResetRequested = async noload => {
setResetting(true);
await sleep(30);
@@ -71,7 +69,6 @@ const Editor = React.memo(
useEffect(() => {
eSubscribeEvent('webviewreset', onResetRequested);
-
return () => {
eUnSubscribeEvent('webviewreset', onResetRequested);
};
@@ -130,6 +127,7 @@ const Editor = React.memo(
originWhitelist={['*']}
source={source}
style={style}
+
autoManageStatusBarEnabled={false}
onMessage={_onMessage}
/>