2017-11-06 20:35:04 +02:00
|
|
|
const React = require('react');
|
|
|
|
const { connect } = require('react-redux');
|
2017-11-06 22:54:58 +02:00
|
|
|
const { Header } = require('./Header.min.js');
|
2017-11-06 20:35:04 +02:00
|
|
|
const { SideBar } = require('./SideBar.min.js');
|
|
|
|
const { NoteList } = require('./NoteList.min.js');
|
|
|
|
const { NoteText } = require('./NoteText.min.js');
|
2017-11-08 19:51:55 +02:00
|
|
|
const { PromptDialog } = require('./PromptDialog.min.js');
|
|
|
|
const { Setting } = require('lib/models/setting.js');
|
2017-11-12 01:13:14 +02:00
|
|
|
const { Tag } = require('lib/models/tag.js');
|
2017-11-08 19:51:55 +02:00
|
|
|
const { Note } = require('lib/models/note.js');
|
2017-11-08 23:22:24 +02:00
|
|
|
const { Folder } = require('lib/models/folder.js');
|
2017-11-06 22:54:58 +02:00
|
|
|
const { themeStyle } = require('../theme.js');
|
2017-11-08 19:51:55 +02:00
|
|
|
const { _ } = require('lib/locale.js');
|
2017-11-07 23:11:14 +02:00
|
|
|
const layoutUtils = require('lib/layout-utils.js');
|
2017-11-08 19:51:55 +02:00
|
|
|
const { bridge } = require('electron').remote.require('./bridge');
|
2017-11-06 20:35:04 +02:00
|
|
|
|
|
|
|
class MainScreenComponent extends React.Component {
|
|
|
|
|
2017-11-08 19:51:55 +02:00
|
|
|
componentWillMount() {
|
2017-11-10 21:18:19 +02:00
|
|
|
this.setState({
|
2017-11-10 22:34:36 +02:00
|
|
|
promptOptions: null,
|
2017-11-10 21:18:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-11 19:36:47 +02:00
|
|
|
componentWillReceiveProps(newProps) {
|
|
|
|
if (newProps.windowCommand) {
|
|
|
|
this.doCommand(newProps.windowCommand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 21:18:19 +02:00
|
|
|
toggleVisiblePanes() {
|
2017-11-12 19:02:20 +02:00
|
|
|
this.props.dispatch({
|
|
|
|
type: 'NOTE_VISIBLE_PANES_TOGGLE',
|
|
|
|
});
|
2017-11-08 19:51:55 +02:00
|
|
|
}
|
|
|
|
|
2017-11-12 01:13:14 +02:00
|
|
|
async doCommand(command) {
|
2017-11-11 19:36:47 +02:00
|
|
|
if (!command) return;
|
|
|
|
|
|
|
|
const createNewNote = async (title, isTodo) => {
|
|
|
|
const folderId = Setting.value('activeFolderId');
|
|
|
|
if (!folderId) return;
|
|
|
|
|
|
|
|
const note = await Note.save({
|
|
|
|
title: title,
|
|
|
|
parent_id: folderId,
|
|
|
|
is_todo: isTodo ? 1 : 0,
|
|
|
|
});
|
|
|
|
Note.updateGeolocation(note.id);
|
|
|
|
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'NOTE_SELECT',
|
|
|
|
id: note.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let commandProcessed = true;
|
|
|
|
|
|
|
|
if (command.name === 'newNote') {
|
|
|
|
this.setState({
|
|
|
|
promptOptions: {
|
2017-11-12 01:13:14 +02:00
|
|
|
label: _('Note title:'),
|
2017-11-11 19:36:47 +02:00
|
|
|
onClose: async (answer) => {
|
|
|
|
if (answer) await createNewNote(answer, false);
|
|
|
|
this.setState({ promptOptions: null });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else if (command.name === 'newTodo') {
|
|
|
|
this.setState({
|
|
|
|
promptOptions: {
|
2017-11-12 01:13:14 +02:00
|
|
|
label: _('To-do title:'),
|
2017-11-11 19:36:47 +02:00
|
|
|
onClose: async (answer) => {
|
|
|
|
if (answer) await createNewNote(answer, true);
|
|
|
|
this.setState({ promptOptions: null });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else if (command.name === 'newNotebook') {
|
|
|
|
this.setState({
|
|
|
|
promptOptions: {
|
2017-11-12 01:13:14 +02:00
|
|
|
label: _('Notebook title:'),
|
2017-11-11 19:36:47 +02:00
|
|
|
onClose: async (answer) => {
|
|
|
|
if (answer) {
|
|
|
|
let folder = null;
|
|
|
|
try {
|
|
|
|
folder = await Folder.save({ title: answer }, { userSideValidation: true });
|
|
|
|
} catch (error) {
|
|
|
|
bridge().showErrorMessageBox(error.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'FOLDER_SELECT',
|
|
|
|
id: folder.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-12 01:13:14 +02:00
|
|
|
this.setState({ promptOptions: null });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else if (command.name === 'setTags') {
|
|
|
|
const tags = await Tag.tagsByNoteId(command.noteId);
|
|
|
|
const tagTitles = tags.map((a) => { return a.title });
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
promptOptions: {
|
|
|
|
label: _('Add or remove tags:'),
|
|
|
|
description: _('Separate each tag by a comma.'),
|
|
|
|
value: tagTitles.join(', '),
|
|
|
|
onClose: async (answer) => {
|
|
|
|
if (answer !== null) {
|
|
|
|
const tagTitles = answer.split(',').map((a) => { return a.trim() });
|
|
|
|
await Tag.setNoteTagsByTitles(command.noteId, tagTitles);
|
|
|
|
}
|
2017-11-11 19:36:47 +02:00
|
|
|
this.setState({ promptOptions: null });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
commandProcessed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (commandProcessed) {
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'WINDOW_COMMAND',
|
|
|
|
name: null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 20:35:04 +02:00
|
|
|
render() {
|
|
|
|
const style = this.props.style;
|
2017-11-06 22:54:58 +02:00
|
|
|
const theme = themeStyle(this.props.theme);
|
2017-11-10 22:34:36 +02:00
|
|
|
const promptOptions = this.state.promptOptions;
|
2017-11-06 22:54:58 +02:00
|
|
|
|
|
|
|
const headerStyle = {
|
|
|
|
width: style.width,
|
|
|
|
};
|
|
|
|
|
|
|
|
const rowHeight = style.height - theme.headerHeight;
|
2017-11-06 20:35:04 +02:00
|
|
|
|
2017-11-07 23:11:14 +02:00
|
|
|
const sideBarStyle = {
|
2017-11-12 01:13:14 +02:00
|
|
|
width: Math.floor(layoutUtils.size(style.width * .2, 150, 300)),
|
2017-11-06 22:54:58 +02:00
|
|
|
height: rowHeight,
|
2017-11-06 20:35:04 +02:00
|
|
|
display: 'inline-block',
|
|
|
|
verticalAlign: 'top',
|
|
|
|
};
|
|
|
|
|
2017-11-07 23:11:14 +02:00
|
|
|
const noteListStyle = {
|
2017-11-12 01:13:14 +02:00
|
|
|
width: Math.floor(layoutUtils.size(style.width * .2, 150, 300)),
|
2017-11-06 22:54:58 +02:00
|
|
|
height: rowHeight,
|
2017-11-06 20:35:04 +02:00
|
|
|
display: 'inline-block',
|
|
|
|
verticalAlign: 'top',
|
|
|
|
};
|
|
|
|
|
2017-11-07 23:11:14 +02:00
|
|
|
const noteTextStyle = {
|
2017-11-10 00:44:10 +02:00
|
|
|
width: Math.floor(layoutUtils.size(style.width - sideBarStyle.width - noteListStyle.width, 0)),
|
2017-11-06 22:54:58 +02:00
|
|
|
height: rowHeight,
|
2017-11-06 20:35:04 +02:00
|
|
|
display: 'inline-block',
|
|
|
|
verticalAlign: 'top',
|
|
|
|
};
|
|
|
|
|
2017-11-08 19:51:55 +02:00
|
|
|
const promptStyle = {
|
|
|
|
width: style.width,
|
|
|
|
height: style.height,
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:34:36 +02:00
|
|
|
const headerButtons = [];
|
|
|
|
|
|
|
|
headerButtons.push({
|
|
|
|
title: _('New note'),
|
|
|
|
iconName: 'fa-file-o',
|
2017-11-11 19:36:47 +02:00
|
|
|
onClick: () => { this.doCommand({ name: 'newNote' }) },
|
2017-11-10 22:34:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
headerButtons.push({
|
|
|
|
title: _('New to-do'),
|
|
|
|
iconName: 'fa-check-square-o',
|
2017-11-11 19:36:47 +02:00
|
|
|
onClick: () => { this.doCommand({ name: 'newTodo' }) },
|
2017-11-10 22:34:36 +02:00
|
|
|
});
|
2017-11-08 19:51:55 +02:00
|
|
|
|
2017-11-10 22:34:36 +02:00
|
|
|
headerButtons.push({
|
|
|
|
title: _('New notebook'),
|
|
|
|
iconName: 'fa-folder-o',
|
2017-11-11 19:36:47 +02:00
|
|
|
onClick: () => { this.doCommand({ name: 'newNotebook' }) },
|
2017-11-10 22:34:36 +02:00
|
|
|
});
|
2017-11-08 23:22:24 +02:00
|
|
|
|
2017-11-10 22:34:36 +02:00
|
|
|
headerButtons.push({
|
|
|
|
title: _('Layout'),
|
|
|
|
iconName: 'fa-columns',
|
|
|
|
onClick: () => {
|
|
|
|
this.toggleVisiblePanes();
|
|
|
|
},
|
|
|
|
});
|
2017-11-08 19:51:55 +02:00
|
|
|
|
2017-11-06 20:35:04 +02:00
|
|
|
return (
|
|
|
|
<div style={style}>
|
2017-11-12 01:13:14 +02:00
|
|
|
<PromptDialog
|
|
|
|
value={promptOptions && promptOptions.value ? promptOptions.value : ''}
|
|
|
|
theme={this.props.theme}
|
|
|
|
style={promptStyle}
|
|
|
|
onClose={(answer) => promptOptions.onClose(answer)}
|
|
|
|
label={promptOptions ? promptOptions.label : ''}
|
|
|
|
description={promptOptions ? promptOptions.description : null}
|
|
|
|
visible={!!this.state.promptOptions} />
|
2017-11-08 19:51:55 +02:00
|
|
|
<Header style={headerStyle} showBackButton={false} buttons={headerButtons} />
|
2017-11-06 22:54:58 +02:00
|
|
|
<SideBar style={sideBarStyle} />
|
|
|
|
<NoteList itemHeight={40} style={noteListStyle} />
|
2017-11-12 19:02:20 +02:00
|
|
|
<NoteText style={noteTextStyle} visiblePanes={this.props.noteVisiblePanes} />
|
2017-11-06 20:35:04 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = (state) => {
|
2017-11-06 22:54:58 +02:00
|
|
|
return {
|
2017-11-10 22:34:36 +02:00
|
|
|
theme: state.settings.theme,
|
2017-11-11 19:36:47 +02:00
|
|
|
windowCommand: state.windowCommand,
|
2017-11-12 19:02:20 +02:00
|
|
|
noteVisiblePanes: state.noteVisiblePanes,
|
2017-11-06 22:54:58 +02:00
|
|
|
};
|
2017-11-06 20:35:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const MainScreen = connect(mapStateToProps)(MainScreenComponent);
|
|
|
|
|
|
|
|
module.exports = { MainScreen };
|