From 100b98bff876cebe1fe8d010ece6e30c37104a24 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sun, 5 Nov 2017 18:36:27 +0000 Subject: [PATCH] Saving/loading notes from Electron --- ElectronClient/app/gui/NoteList.jsx | 4 +- ElectronClient/app/gui/NoteText.jsx | 77 ++++++++++++++++--- .../lib/components/screens/note.js | 7 +- .../components/shared/note-screen-shared.js | 4 +- 4 files changed, 75 insertions(+), 17 deletions(-) diff --git a/ElectronClient/app/gui/NoteList.jsx b/ElectronClient/app/gui/NoteList.jsx index ed3618888..21f9def25 100644 --- a/ElectronClient/app/gui/NoteList.jsx +++ b/ElectronClient/app/gui/NoteList.jsx @@ -5,7 +5,7 @@ const { connect } = require('react-redux'); class NoteListComponent extends React.Component { itemRenderer(index, item) { - const onClick = () => { + const onClick = (item) => { this.props.dispatch({ type: 'NOTES_SELECT', noteId: item.id, @@ -14,7 +14,7 @@ class NoteListComponent extends React.Component { let classes = ['item']; classes.push(index % 2 === 0 ? 'even' : 'odd'); - return
{ onClick() }} className={classes.join(' ')} key={index}>{item.title}
+ return
{ onClick(item) }} className={classes.join(' ')} key={index}>{item.title + ' ' + item.id.substr(0,4)}
} render() { diff --git a/ElectronClient/app/gui/NoteText.jsx b/ElectronClient/app/gui/NoteText.jsx index 4143afd93..5929ac859 100644 --- a/ElectronClient/app/gui/NoteText.jsx +++ b/ElectronClient/app/gui/NoteText.jsx @@ -1,16 +1,30 @@ const React = require('react'); +const { Note } = require('lib/models/note.js'); const { connect } = require('react-redux'); const { MdToHtml } = require('lib/markdown-utils.js'); +const shared = require('lib/components/shared/note-screen-shared.js'); class NoteTextComponent extends React.Component { - componentWillMount() { + constructor() { + super(); + + this.state = { + note: Note.new(), + mode: 'view', + noteMetadata: '', + showNoteMetadata: false, + folder: null, + lastSavedNote: null, + isLoading: true, + webviewReady: false, + }; + } + + async componentWillMount() { this.mdToHtml_ = new MdToHtml(); - this.setState({ - note: null, - webviewReady: false, - }); + await shared.initState(this); } componentDidMount() { @@ -23,7 +37,39 @@ class NoteTextComponent extends React.Component { } componentWillReceiveProps(nextProps) { - if (nextProps.noteId) this.reloadNote(); + if (nextProps.noteId) this.reloadNote(nextProps.noteId); + } + + isModified() { + return shared.isModified(this); + } + + refreshNoteMetadata(force = null) { + return shared.refreshNoteMetadata(this, force); + } + + title_changeText(text) { + shared.noteComponent_change(this, 'title', text); + } + + body_changeText(text) { + shared.noteComponent_change(this, 'body', text); + } + + async saveNoteButton_press() { + await shared.saveNoteButton_press(this); + } + + async saveOneProperty(name, value) { + await shared.saveOneProperty(this, name, value); + } + + toggleIsTodo_onPress() { + shared.toggleIsTodo_onPress(this); + } + + showMetadata_onPress() { + shared.showMetadata_onPress(this); } webview_domReady() { @@ -31,21 +77,23 @@ class NoteTextComponent extends React.Component { webviewReady: true, }); - this.webview_.openDevTools(); + // this.webview_.openDevTools(); this.webview_.addEventListener('ipc-message', (event) => { const msg = event.channel; if (msg.indexOf('checkboxclick:') === 0) { const newBody = this.mdToHtml_.handleCheckboxClick(msg, this.state.note.body); - // this.saveOneProperty('body', newBody); - //if (onCheckboxChange) onCheckboxChange(newBody); + this.saveOneProperty('body', newBody); } }) } - async reloadNote() { - const note = this.props.noteId ? await Note.load(this.props.noteId) : null; + async reloadNote(noteId) { + const note = noteId ? await Note.load(noteId) : null; + + console.info('Reload note: ' + noteId, note); + this.setState({ note: note, }); @@ -55,6 +103,8 @@ class NoteTextComponent extends React.Component { const note = this.state.note; const body = note ? note.body : 'no note'; + console.info('NOTE: ' + (note ? note.title + ' ' + note.id : 'UNDEFINED')); + if (this.state.webviewReady) { const mdOptions = { onResourceLoaded: () => { @@ -86,6 +136,11 @@ const mapStateToProps = (state) => { return { noteId: state.selectedNoteId, notes: state.notes, + folderId: state.selectedFolderId, + itemType: state.selectedItemType, + folders: state.folders, + theme: state.settings.theme, + showAdvancedOptions: state.settings.showAdvancedOptions, }; }; diff --git a/ReactNativeClient/lib/components/screens/note.js b/ReactNativeClient/lib/components/screens/note.js index dcb76127f..359323861 100644 --- a/ReactNativeClient/lib/components/screens/note.js +++ b/ReactNativeClient/lib/components/screens/note.js @@ -42,7 +42,6 @@ class NoteScreenComponent extends BaseScreenComponent { folder: null, lastSavedNote: null, isLoading: true, - resources: {}, titleTextInputHeight: 20, }; @@ -137,7 +136,11 @@ class NoteScreenComponent extends BaseScreenComponent { await shared.initState(this); - shared.refreshNoteMetadata(this); + this.refreshNoteMetadata(); + } + + refreshNoteMetadata(force = null) { + return shared.refreshNoteMetadata(this, force); } componentWillUnmount() { diff --git a/ReactNativeClient/lib/components/shared/note-screen-shared.js b/ReactNativeClient/lib/components/shared/note-screen-shared.js index ef0af236a..6854ab8f1 100644 --- a/ReactNativeClient/lib/components/shared/note-screen-shared.js +++ b/ReactNativeClient/lib/components/shared/note-screen-shared.js @@ -40,7 +40,7 @@ shared.saveNoteButton_press = async function(comp) { note: note, }); if (isNew) Note.updateGeolocation(note.id); - shared.refreshNoteMetadata(comp); + comp.refreshNoteMetadata(); } shared.saveOneProperty = async function(comp, name, value) { @@ -111,7 +111,7 @@ shared.initState = async function(comp) { shared.showMetadata_onPress = function(comp) { comp.setState({ showNoteMetadata: !comp.state.showNoteMetadata }); - shared.refreshNoteMetadata(comp, true); + comp.refreshNoteMetadata(true); } shared.toggleIsTodo_onPress = function(comp) {