More keyboard shortcuts

This commit is contained in:
Jori Lallo
2016-07-17 21:55:59 -07:00
parent e10e6fb548
commit 5d281f7868
5 changed files with 37 additions and 4 deletions

View File

@@ -64,7 +64,11 @@ class Layout extends React.Component {
</Flex> </Flex>
{ this.props.search && ( { this.props.search && (
<Flex> <Flex>
<div className={ styles.search } onClick={ this.search }> <div
onClick={ this.search }
className={ styles.search }
title="Search (/)"
>
<img src={ require('assets/icons/search.svg') } /> <img src={ require('assets/icons/search.svg') } />
</div> </div>
</Flex> </Flex>

View File

@@ -20,6 +20,7 @@ class MarkdownEditor extends React.Component {
text: React.PropTypes.string, text: React.PropTypes.string,
onChange: React.PropTypes.func.isRequired, onChange: React.PropTypes.func.isRequired,
replaceText: React.PropTypes.func.isRequired, replaceText: React.PropTypes.func.isRequired,
onSave: React.PropTypes.func.isRequired,
// This is actually not used but it triggers // This is actually not used but it triggers
// re-render to help with CodeMirror focus issues // re-render to help with CodeMirror focus issues
@@ -120,6 +121,8 @@ class MarkdownEditor extends React.Component {
theme: 'atlas', theme: 'atlas',
extraKeys: { extraKeys: {
Enter: 'newlineAndIndentContinueMarkdownList', Enter: 'newlineAndIndentContinueMarkdownList',
"Ctrl-Enter": this.props.onSave,
"Cmd-Enter": this.props.onSave,
}, },
placeholder: "# Start with a title...", placeholder: "# Start with a title...",
}; };

View File

@@ -20,6 +20,7 @@ import SaveAction from './components/SaveAction';
const DISREGARD_CHANGES = `You have unsaved changes. const DISREGARD_CHANGES = `You have unsaved changes.
Are you sure you want to disgard them?`; Are you sure you want to disgard them?`;
@keydown(['cmd+enter', 'ctrl+enter', 'esc'])
@withRouter @withRouter
@observer @observer
class DocumentEdit extends Component { class DocumentEdit extends Component {
@@ -71,6 +72,15 @@ class DocumentEdit extends Component {
}); });
} }
componentWillReceiveProps = (nextProps) => {
const key = nextProps.keydown.event;
// Cmd + Enter
if (key && key.key === 'Enter' && (key.metaKey || key.ctrl.Key)) {
this.onSave();
}
}
onSave = () => { onSave = () => {
// if (this.props.title.length === 0) { // if (this.props.title.length === 0) {
// alert("Please add a title before saving (hint: Write a markdown header)"); // alert("Please add a title before saving (hint: Write a markdown header)");
@@ -144,6 +154,7 @@ class DocumentEdit extends Component {
store={ this.store } store={ this.store }
scrollTop={ this.state.scrollTop } scrollTop={ this.state.scrollTop }
onScroll={ this.onScroll } onScroll={ this.onScroll }
onSave={ this.onSave }
/> />
) } ) }
</Layout> </Layout>

View File

@@ -22,6 +22,7 @@ const Editor = observer((props) => {
text={ store.text } text={ store.text }
replaceText={ store.replaceText } replaceText={ store.replaceText }
preview={ store.preview } preview={ store.preview }
onSave={ props.onSave }
/> />
</EditorPane> </EditorPane>
{ store.preview ? ( { store.preview ? (
@@ -35,4 +36,4 @@ const Editor = observer((props) => {
); );
}); });
export default Editor; export default Editor;

View File

@@ -2,6 +2,7 @@ import React, { PropTypes } from 'react';
import { toJS } from 'mobx'; import { toJS } from 'mobx';
import { Link, browserHistory } from 'react-router'; import { Link, browserHistory } from 'react-router';
import { observer } from 'mobx-react'; import { observer } from 'mobx-react';
import keydown, { keydownScoped } from 'react-keydown';
import DocumentSceneStore, { import DocumentSceneStore, {
DOCUMENT_PREFERENCES DOCUMENT_PREFERENCES
@@ -21,6 +22,7 @@ const cx = classNames.bind(styles);
import treeStyles from 'components/Tree/Tree.scss'; import treeStyles from 'components/Tree/Tree.scss';
@keydown(['cmd+/', 'ctrl+/'])
@observer(['ui']) @observer(['ui'])
class DocumentScene extends React.Component { class DocumentScene extends React.Component {
static propTypes = { static propTypes = {
@@ -44,6 +46,11 @@ class DocumentScene extends React.Component {
} }
componentWillReceiveProps = (nextProps) => { componentWillReceiveProps = (nextProps) => {
const key = nextProps.keydown.event;
if (key && key.key === '/' && (key.metaKey || key.ctrl.Key)) {
this.toggleSidebar();
}
// Reload on url change // Reload on url change
const oldId = this.props.params.id; const oldId = this.props.params.id;
const newId = nextProps.params.id; const newId = nextProps.params.id;
@@ -87,6 +94,10 @@ class DocumentScene extends React.Component {
}; };
} }
toggleSidebar = () => {
this.props.ui.toggleSidebar();
}
renderNode = (node) => { renderNode = (node) => {
return ( return (
<span className={ treeStyles.nodeLabel } onClick={this.onClickNode.bind(null, node)}> <span className={ treeStyles.nodeLabel } onClick={this.onClickNode.bind(null, node)}>
@@ -98,7 +109,6 @@ class DocumentScene extends React.Component {
render() { render() {
const { const {
sidebar, sidebar,
toggleSidebar,
} = this.props.ui; } = this.props.ui;
const doc = this.store.document; const doc = this.store.document;
@@ -153,7 +163,11 @@ class DocumentScene extends React.Component {
/> />
</div> </div>
) } ) }
<div className={ styles.sidebarToggle } onClick={ toggleSidebar }> <div
onClick={ this.toggleSidebar }
className={ styles.sidebarToggle }
title="Toggle sidebar (Cmd+/)"
>
<img <img
src={ require("assets/icons/menu.svg") } src={ require("assets/icons/menu.svg") }
className={ styles.menuIcon } className={ styles.menuIcon }