joplin/ElectronClient/app/gui/MainScreen.jsx

187 lines
4.6 KiB
React
Raw Normal View History

const React = require('react');
const { connect } = require('react-redux');
2017-11-06 20:54:58 +00:00
const { Header } = require('./Header.min.js');
const { SideBar } = require('./SideBar.min.js');
const { NoteList } = require('./NoteList.min.js');
const { NoteText } = require('./NoteText.min.js');
2017-11-08 17:51:55 +00:00
const { PromptDialog } = require('./PromptDialog.min.js');
const { Setting } = require('lib/models/setting.js');
const { Note } = require('lib/models/note.js');
2017-11-08 21:22:24 +00:00
const { Folder } = require('lib/models/folder.js');
2017-11-06 20:54:58 +00:00
const { themeStyle } = require('../theme.js');
2017-11-08 17:51:55 +00:00
const { _ } = require('lib/locale.js');
2017-11-07 21:11:14 +00:00
const layoutUtils = require('lib/layout-utils.js');
2017-11-08 17:51:55 +00:00
const { bridge } = require('electron').remote.require('./bridge');
class MainScreenComponent extends React.Component {
2017-11-08 17:51:55 +00:00
componentWillMount() {
this.setState({
newNotePromptVisible: false,
newFolderPromptVisible: false,
promptOptions: null,
noteVisiblePanes: ['editor', 'viewer'],
});
}
toggleVisiblePanes() {
let panes = this.state.noteVisiblePanes.slice();
if (panes.length === 2) {
panes = ['editor'];
} else if (panes.indexOf('editor') >= 0) {
panes = ['viewer'];
} else if (panes.indexOf('viewer') >= 0) {
panes = ['editor', 'viewer'];
}
this.setState({ noteVisiblePanes: panes });
2017-11-08 17:51:55 +00:00
}
render() {
const style = this.props.style;
2017-11-06 20:54:58 +00:00
const theme = themeStyle(this.props.theme);
const promptOptions = this.state.promptOptions;
2017-11-06 20:54:58 +00:00
const headerStyle = {
width: style.width,
};
const rowHeight = style.height - theme.headerHeight;
2017-11-07 21:11:14 +00:00
const sideBarStyle = {
width: Math.floor(layoutUtils.size(style.width * .2, 100, 300)),
2017-11-06 20:54:58 +00:00
height: rowHeight,
display: 'inline-block',
verticalAlign: 'top',
};
2017-11-07 21:11:14 +00:00
const noteListStyle = {
width: Math.floor(layoutUtils.size(style.width * .2, 100, 300)),
2017-11-06 20:54:58 +00:00
height: rowHeight,
display: 'inline-block',
verticalAlign: 'top',
};
2017-11-07 21:11:14 +00:00
const noteTextStyle = {
width: Math.floor(layoutUtils.size(style.width - sideBarStyle.width - noteListStyle.width, 0)),
2017-11-06 20:54:58 +00:00
height: rowHeight,
display: 'inline-block',
verticalAlign: 'top',
};
2017-11-08 17:51:55 +00:00
const promptStyle = {
width: style.width,
height: style.height,
};
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,
});
}
2017-11-08 21:22:24 +00:00
const headerButtons = [];
headerButtons.push({
title: _('New note'),
iconName: 'fa-file-o',
onClick: () => {
this.setState({
promptOptions: {
message: _('Note title:'),
onClose: async (answer) => {
if (answer) await createNewNote(answer, false);
this.setState({ promptOptions: null });
}
},
2017-11-08 21:22:24 +00:00
});
},
});
headerButtons.push({
title: _('New to-do'),
iconName: 'fa-check-square-o',
onClick: () => {
this.setState({
promptOptions: {
message: _('Note title:'),
onClose: async (answer) => {
if (answer) await createNewNote(answer, true);
this.setState({ promptOptions: null });
}
},
2017-11-08 21:22:24 +00:00
});
},
});
2017-11-08 17:51:55 +00:00
headerButtons.push({
title: _('New notebook'),
iconName: 'fa-folder-o',
onClick: () => {
this.setState({
promptOptions: {
message: _('Notebook title:'),
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,
});
}
this.setState({ promptOptions: null });
}
},
2017-11-08 21:22:24 +00:00
});
},
});
2017-11-08 21:22:24 +00:00
headerButtons.push({
title: _('Layout'),
iconName: 'fa-columns',
onClick: () => {
this.toggleVisiblePanes();
},
});
2017-11-08 17:51:55 +00:00
return (
<div style={style}>
<PromptDialog theme={this.props.theme} style={promptStyle} onClose={(answer) => promptOptions.onClose(answer)} message={promptOptions ? promptOptions.message : ''} visible={!!this.state.promptOptions}/>
2017-11-08 17:51:55 +00:00
<Header style={headerStyle} showBackButton={false} buttons={headerButtons} />
2017-11-06 20:54:58 +00:00
<SideBar style={sideBarStyle} />
<NoteList itemHeight={40} style={noteListStyle} />
<NoteText style={noteTextStyle} visiblePanes={this.state.noteVisiblePanes} />
</div>
);
}
}
const mapStateToProps = (state) => {
2017-11-06 20:54:58 +00:00
return {
theme: state.settings.theme,
2017-11-06 20:54:58 +00:00
};
};
const MainScreen = connect(mapStateToProps)(MainScreenComponent);
module.exports = { MainScreen };