Skip to content

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

fix: note editing and saving

ammarahm-ed163c2f0c62b3
diff --git a/apps/mobile/android/app/src/main/assets/texteditor.html b/apps/mobile/android/app/src/main/assets/texteditor.html
index ae3b1c7ab..83cd62ae6 100644
--- a/apps/mobile/android/app/src/main/assets/texteditor.html
+++ b/apps/mobile/android/app/src/main/assets/texteditor.html
@@ -1302,7 +1302,7 @@
type: 'title',
value: document.getElementById('titleInput').value
}
- if (titleMessage && typeof titleMessage === 'string') {
+ if (titleMessage && typeof titleMessage.value === 'string') {
window.ReactNativeWebView.postMessage(JSON.stringify(titleMessage));
}
@@ -1315,7 +1315,7 @@
value: document.getElementById('titleInput').value
}
- if (titleMessage && typeof titleMessage === 'string') {
+ if (titleMessage && typeof titleMessage.value === 'string') {
window.ReactNativeWebView.postMessage(JSON.stringify(titleMessage));
}
}
@@ -1326,7 +1326,7 @@
type: 'title',
value: document.getElementById('titleInput').value
}
- if (titleMessage && typeof titleMessage === 'string') {
+ if (titleMessage && typeof titleMessage.value === 'string') {
window.ReactNativeWebView.postMessage(JSON.stringify(titleMessage));
}
}
@@ -1640,10 +1640,12 @@
switch (type) {
case "reset": {
editor.history.clear();
- editor.setText('', 'api');
+ editor.setText('', 'silent');
document.getElementById('titleInput').value = '';
document.getElementById('titleInput').blur();
editor.blur();
+ document.getElementById('titleInput').blur();
+ window.blur();
document.getElementById('infodate').innerText = "";
document.getElementById('infosaved').innerText = "";
document.getElementById("infowords").innerText = "";
@@ -1652,6 +1654,7 @@
case "blur":
document.getElementById('titleInput').blur();
editor.blur();
+ window.blur();
break;
case "undo":
editor.history.undo();
@@ -1679,7 +1682,7 @@
}, 0)
break;
case "clearEditor":
- editor.setText('', 'api');
+ editor.setText('', 'silent');
break;
case "clearTitle":
document.getElementById('titleInput').value = '';
diff --git a/apps/mobile/src/components/ActionSheetComponent/index.js b/apps/mobile/src/components/ActionSheetComponent/index.js
index 2260afd30..098d3c0e7 100644
--- a/apps/mobile/src/components/ActionSheetComponent/index.js
+++ b/apps/mobile/src/components/ActionSheetComponent/index.js
@@ -15,6 +15,7 @@ import {useTracked} from '../../provider';
import {Actions} from '../../provider/Actions';
import {eSendEvent, openVault, ToastEvent} from '../../services/EventManager';
import {
+ eOnNoteEdited,
eOpenLoginDialog,
eOpenMoveNoteDialog,
refreshNotesPage,
@@ -193,7 +194,6 @@ export const ActionSheetComponent = ({
if (!nodispatch) {
dispatch({type: type});
- dispatch({type: Actions.PINNED});
dispatch({type: Actions.FAVORITES});
}
setNote({...toAdd});
@@ -320,7 +320,6 @@ export const ActionSheetComponent = ({
} else {
await db.notebooks.notebook(note.id).pin();
}
- dispatch({type: Actions.PINNED});
localRefresh(item.type);
},
close: false,
@@ -342,7 +341,8 @@ export const ActionSheetComponent = ({
await db.notebooks.notebook(note.id).favorite();
}
dispatch({type: Actions.FAVORITES});
- localRefresh(item.type);
+ eSendEvent(eOnNoteEdited , {id: note.id, noEdit: true});
+ localRefresh(item.type,true);
},
close: false,
check: true,
@@ -361,14 +361,11 @@ export const ActionSheetComponent = ({
if (note.locked) {
close('unlock');
- return;
} else {
db.vault
.add(note.id)
.then(() => {
- dispatch({type: Actions.NOTES});
- eSendEvent(refreshNotesPage);
- dispatch({type: Actions.PINNED});
+ eSendEvent(eOnNoteEdited , {id: note.id, noEdit: true});
close();
})
.catch(async (e) => {
@@ -401,10 +398,10 @@ export const ActionSheetComponent = ({
await db.notes
.note(note.id)
.untag(oldProps.tags[oldProps.tags.indexOf(tag)]);
- localRefresh(oldProps.type);
+ eSendEvent(eOnNoteEdited , {id: note.id, noEdit: true});
dispatch({type: Actions.TAGS});
} catch (e) {
- localRefresh(oldProps.type);
+ eSendEvent(eOnNoteEdited , {id: note.id, noEdit: true});
}
}}
style={{
@@ -457,7 +454,8 @@ export const ActionSheetComponent = ({
await db.notes.note(note.id).color(color.name);
}
dispatch({type: Actions.COLORS});
- localRefresh(note.type);
+ eSendEvent(eOnNoteEdited , {id: note.id, noEdit: true});
+ localRefresh(note.type,true);
}}
customStyle={{
width: DDS.isTab ? 400 / 10 : w / 10,
@@ -583,7 +581,7 @@ export const ActionSheetComponent = ({
) : null;
const onPressSync = async () => {
- if (!user) {
+ if (!user || !user.Id) {
ToastEvent.show(
'You must login to sync',
'error',
diff --git a/apps/mobile/src/components/SimpleList/NoteItemWrapper.js b/apps/mobile/src/components/SimpleList/NoteItemWrapper.js
index 4d8cb9c15..6adfa71de 100644
--- a/apps/mobile/src/components/SimpleList/NoteItemWrapper.js
+++ b/apps/mobile/src/components/SimpleList/NoteItemWrapper.js
@@ -30,24 +30,28 @@ export const NoteItemWrapper = ({
const onNoteChange = (data) => {
if (data.id !== note.id) {
+ if (editing) {
+ setEditing(false);
+ }
return;
}
- if (editing !== true) {
+ if (editing !== true && !data.noEdit) {
setEditing(true);
}
if (data.closed) {
setEditing(false);
}
- setNote(db.notes.note(data.id).data)
+ console.log(db.notes.note(data.id).data.headline.length);
+ setNote(db.notes.note(data.id).data);
}
useEffect(() => {
- eSubscribeEvent(eOnNoteEdited + note.id, onNoteChange);
+ eSubscribeEvent(eOnNoteEdited , onNoteChange);
return () => {
- eUnSubscribeEvent(eOnNoteEdited + note.id, onNoteChange);
+ eUnSubscribeEvent(eOnNoteEdited , onNoteChange);
}
- }, [editing,item]);
+ }, [editing,note]);
const style = useMemo(() => {
return {width: selectionMode ? '90%' : '100%', marginHorizontal: 0};
@@ -58,31 +62,35 @@ export const NoteItemWrapper = ({
if (!selectionMode) {
dispatch({type: Actions.SELECTION_MODE, enabled: true});
}
- dispatch({type: Actions.SELECTED_ITEMS, item: item});
+ dispatch({type: Actions.SELECTED_ITEMS, item: note});
};
const onPress = async () => {
- if (item.conflicted) {
- eSendEvent(eShowMergeDialog, item);
+ if (note.conflicted) {
+ eSendEvent(eShowMergeDialog, note);
return;
}
if (selectionMode) {
onLongPress();
return;
- } else if (item.locked) {
+ } else if (note.locked) {
openVault(item, true, true, false, true, false);
return;
}
if (isTrash) {
- simpleDialogEvent(TEMPLATE_TRASH(item.type));
+ simpleDialogEvent(TEMPLATE_TRASH(note.type));
} else {
- eSendEvent(eOnLoadNote, item);
+ eSendEvent(eOnLoadNote, note);
}
if (DDS.isPhone || DDS.isSmallTab) {
openEditorAnimation();
}
};
+ useEffect(() => {
+ console.log('rerendering note');
+ })
+
return (
<SelectionWrapper
@@ -102,4 +110,4 @@ export const NoteItemWrapper = ({
/>
</SelectionWrapper>
);
-};
+}
diff --git a/apps/mobile/src/components/SimpleList/index.js b/apps/mobile/src/components/SimpleList/index.js
index 638e8b0a9..d18b07d65 100644
--- a/apps/mobile/src/components/SimpleList/index.js
+++ b/apps/mobile/src/components/SimpleList/index.js
@@ -33,7 +33,7 @@ const SimpleList = ({
const [dataProvider, setDataProvider] = useState(
new DataProvider((r1, r2) => {
return r1 !== r2;
- }).cloneWithRows([]),
+ }),
);
const {width, fontScale} = useWindowDimensions();
@@ -49,7 +49,7 @@ const SimpleList = ({
loadData();
}, [listData]);
- const loadData = useCallback(() => {
+ const loadData =() => {
let mainData =
searchResults.type === type &&
focused() &&
@@ -62,16 +62,9 @@ const SimpleList = ({
d = [...d,...data];
} */
setDataProvider(
- new DataProvider((r1, r2) => {
-
- return r1.id !== r2.id ||
- r1.locked !== r2.locked ||
- r1.pinned !== r2.pinned ||
- r1.favorite !== r2.favorite ||
- r1.title !== r2.title
- }).cloneWithRows(d),
+ dataProvider.cloneWithRows(d),
);
- }, [listData, searchResults]);
+ }
const RenderSectionHeader = ({item}) => (
<Text
@@ -101,7 +94,7 @@ const SimpleList = ({
ToastEvent.show('Sync Complete', 'success');
} catch (e) {
ToastEvent.show(
- e.message,
+ "You must login to sync.",
'error',
'global',
5000,
diff --git a/apps/mobile/src/provider/Reducer.js b/apps/mobile/src/provider/Reducer.js
index 7e89ffad8..f593779e0 100644
--- a/apps/mobile/src/provider/Reducer.js
+++ b/apps/mobile/src/provider/Reducer.js
@@ -8,18 +8,12 @@ import {db} from "../utils/DB";
export const reducer = (state, action) => {
switch (action.type) {
case Actions.ALL: {
- let notes = db.notes.pinned;
- let notebooks = db.notebooks.pinned;
return {
...state,
notes: db.notes.group(),
notebooks: db.notebooks.all,
trash: db.trash.all,
- pinned: {
- notes,
- notebooks,
- },
tags: db.tags.all,
favorites: db.notes.favorites,
colorNotes: db.colors.all,
diff --git a/apps/mobile/src/views/Editor/Functions.js b/apps/mobile/src/views/Editor/Functions.js
index eb4ba21b2..eda9a81ba 100644
--- a/apps/mobile/src/views/Editor/Functions.js
+++ b/apps/mobile/src/views/Editor/Functions.js
@@ -35,6 +35,14 @@ export function isNotedEdited() {
return noteEdited;
}
+export function clearTimer() {
+ if (timer){
+ clearTimeout(timer);
+ timer = null;
+ }
+
+}
+
export const INJECTED_JAVASCRIPT = (premium) =>
premium
? `(function() {
@@ -79,15 +87,26 @@ export function checkNote() {
export async function loadNote(item) {
editing.currentlyEditing = true;
+ post('blur');
if (item && item.type === 'new') {
await clearEditor();
post('focusTitle');
canSave = true;
+ id = null;
} else {
+ await clearEditor();
note = item;
+ title = note.title;
+ id = note.id;
+ content = {};
+ content.text = await db.notes.note(id).text();
+ content.delta = note.content.delta;
+ if (!note.locked) {
+ content.delta = await db.notes.note(id).delta();
+ }
canSave = false;
- eSendEvent(eOnNoteEdited + item.id, {id: item.id});
- await onWebViewLoad();
+ eSendEvent(eOnNoteEdited , {id: item.id});
+ await loadNoteInEditor();
}
noteEdited = false;
}
@@ -101,9 +120,7 @@ const onChange = (data) => {
(content &&
JSON.stringify(content.delta) !== JSON.stringify(rawData.delta))
) {
-
noteEdited = true;
-
}
content = rawData;
} else {
@@ -118,12 +135,10 @@ const onChange = (data) => {
export const _onMessage = async (evt) => {
if (!evt || !evt.nativeEvent || !evt.nativeEvent.data) return;
let message = evt.nativeEvent.data;
-
+ clearTimeout(timer);
+ timer = null;
if (message === 'loaded') {
} else if (message !== '' && message !== 'loaded') {
- clearTimeout(timer);
-
- timer = null;
if (!canSave) {
await sleep(2000);
canSave = true;
@@ -131,25 +146,25 @@ export const _onMessage = async (evt) => {
onChange(message);
timer = setTimeout(() => {
if (noteEdited) {
- saveNote(true);
- } else {
+ saveNote("onMessage");
}
}, 500);
}
};
export async function clearEditor() {
- if (noteEdited) {
- await saveNote(true);
+ clearTimer();
+ if (noteEdited && id) {
+ await saveNote("clearEditor");
}
if (note && note.id) {
- eSendEvent(eOnNoteEdited + note.id, {id: note.id, closed: true});
+ eSendEvent(eOnNoteEdited , {id: note.id, closed: true});
}
noteEdited = false;
title = null;
content = null;
note = null;
- id = null;
+ //id = null;
saveCounter = 0;
canSave = false;
post('reset');
@@ -228,7 +243,7 @@ async function addToCollection(id) {
}
}
-export async function saveNote() {
+export async function saveNote(caller) {
if (!checkIfContentIsSavable()) return;
let lockedNote = id ? db.notes.note(id).data.locked : null;
@@ -249,10 +264,7 @@ export async function saveNote() {
eSendEvent(refreshNotesPage);
}
- eSendEvent(eOnNoteEdited + rId, {id: rId});
-
-
-
+ eSendEvent(eOnNoteEdited, {id: rId});
if (id) {
await addToCollection(id);
@@ -280,31 +292,25 @@ export async function saveNote() {
export async function onWebViewLoad(noMenu, premium, colors) {
EditorWebView.current?.injectJavaScript(INJECTED_JAVASCRIPT(premium, false));
+
await loadNoteInEditor();
- let theme = {...colors, factor: normalize(1)};
- post('theme', theme);
- await sleep(1000);
if (!checkNote()) {
+ post('blur');
Platform.OS === 'android' ? EditorWebView.current?.requestFocus() : null;
}
post('blur');
+
+ await sleep(1000);
+ let theme = {...colors, factor: normalize(1)};
+ post('theme', theme);
}
const loadNoteInEditor = async () => {
saveCounter = 0;
- content = {};
- content.text = '';
if (note?.id) {
- title = note.title;
- id = note.id;
- content.text = await db.notes.note(id).text();
post('dateEdited', timeConverter(note.dateEdited));
await sleep(50);
post('title', title);
- content.delta = note.content.delta;
- if (!note.locked) {
- content.delta = await db.notes.note(id).delta();
- }
post('delta', content.delta);
} else {
post('focusTitle');
diff --git a/apps/mobile/src/views/Editor/index.js b/apps/mobile/src/views/Editor/index.js
index e4985a405..d17fca616 100755
--- a/apps/mobile/src/views/Editor/index.js
+++ b/apps/mobile/src/views/Editor/index.js
@@ -13,7 +13,7 @@ import {
_onMessage,
_onShouldStartLoadWithRequest,
checkNote,
- clearEditor,
+ clearEditor, clearTimer,
EditorWebView,
getNote,
injectedJS,
@@ -74,6 +74,7 @@ const Editor = ({noMenu}) => {
}
return () => {
+ clearTimer()
if (handleBack) {
handleBack.remove();
handleBack = null;
@@ -82,6 +83,7 @@ const Editor = ({noMenu}) => {
}, [noMenu]);
const load = async (item) => {
+ console.log("loading new note");
await loadNote(item);
if (!DDS.isTab) {
handleBack = BackHandler.addEventListener(
@@ -126,14 +128,12 @@ const Editor = ({noMenu}) => {
if (DDS.isTab && !DDS.isSmallTab) {
simpleDialogEvent(TEMPLATE_EXIT_FULLSCREEN());
} else {
-
exitEditorAnimation();
+
if (checkNote() && isNotedEdited()) {
ToastEvent.show('Note Saved!', 'success');
}
- setTimeout(async () => {
- await clearEditor();
- }, 300);
+ await clearEditor();
if (handleBack) {
handleBack.remove();
handleBack = null;