From 5a7fde7d218ded45dfe0ce9e6279eb886067b4a8 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 7 Nov 2017 21:46:23 +0000 Subject: [PATCH] Auto save note --- ElectronClient/app/gui/NoteText.jsx | 41 +++++++++++++++++------------ 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/ElectronClient/app/gui/NoteText.jsx b/ElectronClient/app/gui/NoteText.jsx index c00532e37..e036d22a0 100644 --- a/ElectronClient/app/gui/NoteText.jsx +++ b/ElectronClient/app/gui/NoteText.jsx @@ -28,6 +28,7 @@ class NoteTextComponent extends React.Component { this.webviewListeners_ = null; this.ignoreNextEditorScroll_ = false; + this.scheduleSaveTimeout_ = null; } mdToHtml() { @@ -37,18 +38,32 @@ class NoteTextComponent extends React.Component { } async componentWillMount() { - - await shared.initState(this); } componentWillUnmount() { + this.saveIfNeeded(); + this.mdToHtml_ = null; this.destroyWebview(); } + async saveIfNeeded() { + if (this.scheduleSaveTimeout_) clearTimeout(this.scheduleSaveTimeout_); + this.scheduleSaveTimeout_ = null; + if (!shared.isModified(this)) return; + await shared.saveNoteButton_press(this); + } + + scheduleSave() { + if (this.scheduleSaveTimeout_) clearTimeout(this.scheduleSaveTimeout_); + this.scheduleSaveTimeout_ = setTimeout(() => { + this.saveIfNeeded(); + }, 500); + } + async componentWillReceiveProps(nextProps) { - if ('noteId' in nextProps) { + if ('noteId' in nextProps && nextProps.noteId !== this.props.noteId) { this.mdToHtml_ = null; const noteId = nextProps.noteId; @@ -73,23 +88,17 @@ class NoteTextComponent extends React.Component { title_changeText(text) { shared.noteComponent_change(this, 'title', text); + this.scheduleSave(); } - body_changeText(text) { - shared.noteComponent_change(this, 'body', text); - //this.updateScrollHeight(); - } - - async saveNoteButton_press() { - await shared.saveNoteButton_press(this); - } - - async saveOneProperty(name, value) { - await shared.saveOneProperty(this, name, value); + editor_change(event) { + shared.noteComponent_change(this, 'body', event.target.value); + this.scheduleSave(); } toggleIsTodo_onPress() { shared.toggleIsTodo_onPress(this); + this.scheduleSave(); } showMetadata_onPress() { @@ -210,8 +219,6 @@ class NoteTextComponent extends React.Component { const note = this.state.note; const body = note ? note.body : ''; - console.info(this.state.scrollHeight); - const viewerStyle = { width: Math.floor(style.width / 2), height: style.height, @@ -240,7 +247,7 @@ class NoteTextComponent extends React.Component { } const viewer = { this.webview_ref(elem); } } /> - const editor = + const editor = return (