Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CoreMirror's key maps instead of global event handler to run query at a cursor #158

Merged
merged 1 commit into from
Aug 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions src/components/GraphiQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,6 @@ export class GraphiQL extends React.Component {

// Utility for keeping CodeMirror correctly sized.
this.codeMirrorSizer = new CodeMirrorSizer();

// Add shortcut for running a query.
document.addEventListener('keydown', this._keyHandler, true);
}

componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -255,8 +252,6 @@ export class GraphiQL extends React.Component {
this._storageSet('editorFlex', this.state.editorFlex);
this._storageSet('variableEditorHeight', this.state.variableEditorHeight);
this._storageSet('docExplorerWidth', this.state.docExplorerWidth);

document.removeEventListener('keydown', this._keyHandler, true);
}

render() {
Expand Down Expand Up @@ -323,6 +318,7 @@ export class GraphiQL extends React.Component {
value={this.state.query}
onEdit={this.handleEditQuery}
onHintInformationRender={this.handleHintInformationRender}
onRunQuery={this.handleEditorRunQuery}
/>
<div className="variable-editor" style={variableStyle}>
<div
Expand All @@ -337,6 +333,7 @@ export class GraphiQL extends React.Component {
variableToType={this.state.variableToType}
onEdit={this.handleEditVariables}
onHintInformationRender={this.handleHintInformationRender}
onRunQuery={this.handleEditorRunQuery}
/>
</div>
</div>
Expand Down Expand Up @@ -575,14 +572,6 @@ export class GraphiQL extends React.Component {
return;
}

_keyHandler = event => {
// "Run Query" event.
if ((event.metaKey || event.ctrlKey) && event.keyCode === 13) {
event.preventDefault();
this._runQueryAtCursor();
}
}

_runQueryAtCursor() {
if (this.state.subscription) {
this.handleStopQuery();
Expand Down Expand Up @@ -668,6 +657,10 @@ export class GraphiQL extends React.Component {
});
}

handleEditorRunQuery = () => {
this._runQueryAtCursor();
}

_onClickHintInformation = event => {
if (event.target.className === 'typeName') {
const typeName = event.target.innerHTML;
Expand Down
12 changes: 12 additions & 0 deletions src/components/QueryEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class QueryEditor extends React.Component {
value: PropTypes.string,
onEdit: PropTypes.func,
onHintInformationRender: PropTypes.func,
onRunQuery: PropTypes.func,
}

constructor(props) {
Expand Down Expand Up @@ -83,6 +84,17 @@ export class QueryEditor extends React.Component {
'Alt-Space': () => this.editor.showHint({ completeSingle: true }),
'Shift-Space': () => this.editor.showHint({ completeSingle: true }),

'Cmd-Enter': () => {
if (this.props.onRunQuery) {
this.props.onRunQuery();
}
},
'Ctrl-Enter': () => {
if (this.props.onRunQuery) {
this.props.onRunQuery();
}
},

// Editor improvements
'Ctrl-Left': 'goSubwordLeft',
'Ctrl-Right': 'goSubwordRight',
Expand Down
12 changes: 12 additions & 0 deletions src/components/VariableEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class VariableEditor extends React.Component {
value: PropTypes.string,
onEdit: PropTypes.func,
onHintInformationRender: PropTypes.func,
onRunQuery: PropTypes.func,
}

constructor(props) {
Expand Down Expand Up @@ -79,6 +80,17 @@ export class VariableEditor extends React.Component {
'Alt-Space': () => this.editor.showHint({ completeSingle: false }),
'Shift-Space': () => this.editor.showHint({ completeSingle: false }),

'Cmd-Enter': () => {
if (this.props.onRunQuery) {
this.props.onRunQuery();
}
},
'Ctrl-Enter': () => {
if (this.props.onRunQuery) {
this.props.onRunQuery();
}
},

// Editor improvements
'Ctrl-Left': 'goSubwordLeft',
'Ctrl-Right': 'goSubwordRight',
Expand Down