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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyboard shortcut to focus on response viewer #1265

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/insomnia-app/app/common/hotkeys.js
Expand Up @@ -156,6 +156,14 @@ export const FOCUS_FILTER: Hotkey = {
keycode: keycodes.f
};

export const FOCUS_RESPONSE: Hotkey = {
description: 'Focus Response',
meta: true,
alt: false,
shift: false,
keycode: keycodes.singlequote
};

export const SHOW_COOKIES: Hotkey = {
description: 'Edit Cookies',
meta: true,
Expand Down
Expand Up @@ -34,6 +34,7 @@ class Shortcuts extends PureComponent {
{this.renderHotkey(hotkeys.SHOW_ENVIRONMENTS)}
{this.renderHotkey(hotkeys.TOGGLE_ENVIRONMENTS_MENU)}
{this.renderHotkey(hotkeys.FOCUS_URL)}
{this.renderHotkey(hotkeys.FOCUS_RESPONSE)}
{this.renderHotkey(hotkeys.TOGGLE_METHOD_DROPDOWN)}
{this.renderHotkey(hotkeys.TOGGLE_SIDEBAR)}
{this.renderHotkey(hotkeys.TOGGLE_HISTORY_DROPDOWN)}
Expand Down
12 changes: 12 additions & 0 deletions packages/insomnia-app/app/ui/components/viewers/response-raw.js
Expand Up @@ -42,6 +42,18 @@ class ResponseRaw extends PureComponent {
return false;
}

focus() {
if (this._textarea) {
this._textarea.focus();
}
}

selectAll() {
if (this._textarea) {
this._textarea.select();
}
}

render() {
const { fontSize } = this.props;
return (
Expand Down
Expand Up @@ -16,6 +16,8 @@ import {
PREVIEW_MODE_FRIENDLY,
PREVIEW_MODE_RAW
} from '../../../common/constants';
import * as hotkeys from '../../../common/hotkeys';
import KeydownBinder from '../keydown-binder';

let alwaysShowLargeResponses = false;

Expand Down Expand Up @@ -47,6 +49,8 @@ type State = {

@autobind
class ResponseViewer extends React.Component<Props, State> {
_selectableView: any;

constructor(props: Props) {
super(props);
this.state = {
Expand Down Expand Up @@ -153,7 +157,34 @@ class ResponseViewer extends React.Component<Props, State> {
return false;
}

render() {
_setSelectableViewRef(n: any) {
this._selectableView = n;
}

_isViewSelectable() {
return (
this._selectableView != null &&
typeof this._selectableView.focus === 'function' &&
typeof this._selectableView.selectAll === 'function'
);
}

_handleKeyDown(e: KeyboardEvent) {
if (!this._isViewSelectable()) {
return;
}

hotkeys.executeHotKey(e, hotkeys.FOCUS_RESPONSE, () => {
if (!this._isViewSelectable()) {
return;
}

this._selectableView.focus();
this._selectableView.selectAll();
});
}

_renderView() {
const {
bytes,
download,
Expand Down Expand Up @@ -328,6 +359,7 @@ class ResponseViewer extends React.Component<Props, State> {
return (
<ResponseRaw
key={responseId}
ref={this._setSelectableViewRef}
value={this._decodeIconv(bodyBuffer, charset)}
fontSize={editorFontSize}
/>
Expand All @@ -351,6 +383,7 @@ class ResponseViewer extends React.Component<Props, State> {
return (
<CodeEditor
uniquenessKey={responseId}
ref={this._setSelectableViewRef}
onClickLink={this._handleOpenLink}
defaultValue={body}
updateFilter={updateFilter}
Expand All @@ -369,6 +402,10 @@ class ResponseViewer extends React.Component<Props, State> {
);
}
}

render() {
return <KeydownBinder onKeydown={this._handleKeyDown}>{this._renderView()}</KeydownBinder>;
}
}

export default ResponseViewer;