2017-05-12 22:23:54 +02:00
|
|
|
import React, { Component } from 'react';
|
2017-07-13 20:47:31 +02:00
|
|
|
import { View, Button, TextInput, WebView, Text } from 'react-native';
|
2017-05-12 22:23:54 +02:00
|
|
|
import { connect } from 'react-redux'
|
2017-06-24 20:06:28 +02:00
|
|
|
import { Log } from 'lib/log.js'
|
|
|
|
import { Note } from 'lib/models/note.js'
|
2017-07-13 23:50:21 +02:00
|
|
|
import { Folder } from 'lib/models/folder.js'
|
2017-06-24 20:06:28 +02:00
|
|
|
import { ScreenHeader } from 'lib/components/screen-header.js';
|
|
|
|
import { Checkbox } from 'lib/components/checkbox.js'
|
|
|
|
import { _ } from 'lib/locale.js';
|
2017-07-10 23:34:26 +02:00
|
|
|
import marked from 'lib/marked.js';
|
2017-05-12 22:23:54 +02:00
|
|
|
|
|
|
|
class NoteScreenComponent extends React.Component {
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
static navigationOptions(options) {
|
2017-05-16 21:57:09 +02:00
|
|
|
return { header: null };
|
|
|
|
}
|
2017-05-12 22:23:54 +02:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2017-07-10 23:34:26 +02:00
|
|
|
this.state = {
|
|
|
|
note: Note.new(),
|
|
|
|
mode: 'view',
|
2017-07-13 20:47:31 +02:00
|
|
|
noteMetadata: '',
|
|
|
|
showNoteMetadata: false,
|
2017-07-13 23:50:21 +02:00
|
|
|
folder: null,
|
2017-07-10 23:34:26 +02:00
|
|
|
}
|
2017-05-12 22:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
2017-05-22 22:22:50 +02:00
|
|
|
if (!this.props.noteId) {
|
2017-05-24 22:51:50 +02:00
|
|
|
let note = this.props.itemType == 'todo' ? Note.newTodo(this.props.folderId) : Note.new(this.props.folderId);
|
|
|
|
this.setState({ note: note });
|
2017-07-13 20:47:31 +02:00
|
|
|
this.refreshNoteMetadata();
|
2017-05-22 22:22:50 +02:00
|
|
|
} else {
|
|
|
|
Note.load(this.props.noteId).then((note) => {
|
|
|
|
this.setState({ note: note });
|
2017-07-13 20:47:31 +02:00
|
|
|
this.refreshNoteMetadata();
|
2017-05-22 22:22:50 +02:00
|
|
|
});
|
|
|
|
}
|
2017-07-13 23:50:21 +02:00
|
|
|
|
|
|
|
this.refreshFolder();
|
|
|
|
}
|
|
|
|
|
|
|
|
async currentFolder() {
|
|
|
|
let folderId = this.props.folderId;
|
|
|
|
if (!folderId) {
|
|
|
|
if (this.state.note && this.state.note.parent_id) folderId = this.state.note.parent_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!folderId) return Folder.defaultFolder();
|
|
|
|
|
|
|
|
return Folder.load(folderId);
|
|
|
|
}
|
|
|
|
|
|
|
|
async refreshFolder() {
|
|
|
|
this.setState({ folder: await this.currentFolder() });
|
2017-05-12 22:23:54 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
noteComponent_change(propName, propValue) {
|
2017-05-12 22:23:54 +02:00
|
|
|
this.setState((prevState, props) => {
|
|
|
|
let note = Object.assign({}, prevState.note);
|
|
|
|
note[propName] = propValue;
|
|
|
|
return { note: note }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-13 20:47:31 +02:00
|
|
|
async refreshNoteMetadata(force = null) {
|
|
|
|
if (force !== true && !this.state.showNoteMetadata) return;
|
|
|
|
|
|
|
|
let noteMetadata = await Note.serializeAllProps(this.state.note);
|
|
|
|
this.setState({ noteMetadata: noteMetadata });
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
title_changeText(text) {
|
2017-05-12 22:23:54 +02:00
|
|
|
this.noteComponent_change('title', text);
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
body_changeText(text) {
|
2017-05-12 22:23:54 +02:00
|
|
|
this.noteComponent_change('body', text);
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:29:00 +02:00
|
|
|
async saveNoteButton_press() {
|
2017-07-13 23:50:21 +02:00
|
|
|
let note = Object.assign({}, this.state.note);
|
|
|
|
|
|
|
|
if (!this.state.note.parent_id) {
|
|
|
|
let folder = await Folder.defaultFolder();
|
|
|
|
if (!folder) {
|
|
|
|
Log.warn('Cannot save note without a notebook');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
note.parent_id = folder.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
let isNew = !note.id;
|
|
|
|
note = await Note.save(note);
|
2017-07-05 23:29:00 +02:00
|
|
|
this.setState({ note: note });
|
|
|
|
if (isNew) Note.updateGeolocation(note.id);
|
2017-07-13 20:47:31 +02:00
|
|
|
this.refreshNoteMetadata();
|
2017-05-12 22:23:54 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
deleteNote_onPress(noteId) {
|
2017-06-04 17:01:52 +02:00
|
|
|
Log.info('DELETE', noteId);
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
attachFile_onPress(noteId) {
|
2017-07-13 20:47:31 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
showMetadata_onPress() {
|
|
|
|
this.setState({ showNoteMetadata: !this.state.showNoteMetadata });
|
|
|
|
this.refreshNoteMetadata(true);
|
2017-06-04 17:01:52 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
menuOptions() {
|
2017-06-04 17:01:52 +02:00
|
|
|
return [
|
|
|
|
{ title: _('Attach file'), onPress: () => { this.attachFile_onPress(this.state.note.id); } },
|
|
|
|
{ title: _('Delete note'), onPress: () => { this.deleteNote_onPress(this.state.note.id); } },
|
2017-07-13 20:47:31 +02:00
|
|
|
{ title: _('Toggle metadata'), onPress: () => { this.showMetadata_onPress(); } },
|
2017-06-04 17:01:52 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-05-12 22:23:54 +02:00
|
|
|
render() {
|
2017-05-24 22:51:50 +02:00
|
|
|
const note = this.state.note;
|
|
|
|
const isTodo = !!Number(note.is_todo);
|
2017-07-13 23:50:21 +02:00
|
|
|
const folder = this.state.folder;
|
2017-05-24 22:51:50 +02:00
|
|
|
let todoComponents = null;
|
|
|
|
|
|
|
|
if (note.is_todo) {
|
|
|
|
todoComponents = (
|
|
|
|
<View>
|
|
|
|
<Button title="test" onPress={this.saveNoteButton_press} />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-07-10 23:34:26 +02:00
|
|
|
let bodyComponent = null;
|
|
|
|
if (this.state.mode == 'view') {
|
|
|
|
const source = {
|
|
|
|
html: note ? marked(note.body, { gfm: true, breaks: true }) : '',
|
|
|
|
};
|
|
|
|
|
|
|
|
bodyComponent = (
|
|
|
|
<View style={{flex:1}}>
|
|
|
|
<WebView source={source}/>
|
|
|
|
<Button title="Edit note" onPress={() => { this.setState({ mode: 'edit' }); }}/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
bodyComponent = <TextInput style={{flex: 1, textAlignVertical: 'top', fontFamily: 'monospace'}} multiline={true} value={note.body} onChangeText={(text) => this.body_changeText(text)} />
|
|
|
|
}
|
|
|
|
|
2017-07-13 23:50:21 +02:00
|
|
|
let title = null;
|
|
|
|
let noteHeaderTitle = note && note.title ? note.title : _('New note');
|
|
|
|
if (folder) {
|
|
|
|
title = folder.title + ' > ' + noteHeaderTitle;
|
|
|
|
} else {
|
|
|
|
title = noteHeaderTitle;
|
|
|
|
}
|
2017-07-13 20:47:31 +02:00
|
|
|
|
2017-05-12 22:23:54 +02:00
|
|
|
return (
|
|
|
|
<View style={{flex: 1}}>
|
2017-07-13 23:50:21 +02:00
|
|
|
<ScreenHeader navState={this.props.navigation.state} menuOptions={this.menuOptions()} title={title} />
|
2017-05-24 22:51:50 +02:00
|
|
|
<View style={{ flexDirection: 'row' }}>
|
2017-06-06 22:01:43 +02:00
|
|
|
{ isTodo && <Checkbox checked={!!Number(note.todo_completed)} /> }<TextInput style={{flex:1}} value={note.title} onChangeText={(text) => this.title_changeText(text)} />
|
2017-05-24 22:51:50 +02:00
|
|
|
</View>
|
2017-07-13 20:47:31 +02:00
|
|
|
{ bodyComponent }
|
2017-05-24 22:51:50 +02:00
|
|
|
{ todoComponents }
|
2017-06-06 22:01:43 +02:00
|
|
|
<Button title="Save note" onPress={() => this.saveNoteButton_press()} />
|
2017-07-13 20:47:31 +02:00
|
|
|
{ this.state.showNoteMetadata && <Text>{this.state.noteMetadata}</Text> }
|
2017-05-12 22:23:54 +02:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const NoteScreen = connect(
|
|
|
|
(state) => {
|
|
|
|
return {
|
2017-05-22 22:22:50 +02:00
|
|
|
noteId: state.selectedNoteId,
|
|
|
|
folderId: state.selectedFolderId,
|
2017-05-24 22:51:50 +02:00
|
|
|
itemType: state.selectedItemType,
|
2017-05-12 22:23:54 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
)(NoteScreenComponent)
|
|
|
|
|
|
|
|
export { NoteScreen };
|