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

87: Toggle inspector on global ALT+F12 shortcut. #92

Merged
merged 4 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
28 changes: 26 additions & 2 deletions src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* For licensing, see LICENSE.md.
*/

/* global document */
/* global document, window */

import React, { Component } from 'react';
import { Rnd } from 'react-rnd';
Expand Down Expand Up @@ -118,18 +118,38 @@ export class DocsButton extends Component {
}

class ToggleButtonVisual extends Component {
constructor( props ) {
super( props );

this.handleShortcut = this.handleShortcut.bind( this );
}

render() {
return <button
type="button"
onClick={this.props.toggleIsCollapsed}
title="Toggle inspector"
title="Toggle inspector (Alt+F12)"
className={[
'ck-inspector-navbox__navigation__toggle',
this.props.isCollapsed ? ' ck-inspector-navbox__navigation__toggle_up' : ''
].join( ' ' )}>
Toggle inspector
</button>;
}

componentDidMount() {
window.addEventListener( 'keydown', this.handleShortcut );
}

componentWillUnmount() {
window.removeEventListener( 'keydown', this.handleShortcut );
}

handleShortcut( event ) {
if ( isToggleShortcut( event ) ) {
this.props.toggleIsCollapsed();
}
}
}

export const ToggleButton = connect( ( { ui: { isCollapsed } } ) => {
Expand Down Expand Up @@ -158,3 +178,7 @@ export const EditorInstanceSelector = connect(
function updateBodyHeight( height ) {
document.body.style.setProperty( '--ck-inspector-height', height );
}

function isToggleShortcut( event ) {
return event.altKey && event.key === 'F12';
}
24 changes: 22 additions & 2 deletions tests/inspector/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ import TestEditor from '../utils/testeditor';

import {
SET_HEIGHT,
SET_CURRENT_EDITOR_NAME
SET_CURRENT_EDITOR_NAME,
TOGGLE_IS_COLLAPSED
} from '../../src/data/actions';

describe( '<InspectorUI />', () => {
let editors, wrapper, store, editor1Element, editor2Element, dispatchSpy;

const origAddEventListener = window.addEventListener;
const windowEventMap = {};
const container = document.createElement( 'div' );
document.body.appendChild( container );

Expand All @@ -34,6 +37,12 @@ describe( '<InspectorUI />', () => {
document.body.appendChild( editor1Element );
document.body.appendChild( editor2Element );

window.addEventListener = ( event, cb ) => {
windowEventMap[ event ] = cb;

origAddEventListener( event, cb );
};

return Promise.all( [
TestEditor.create( editor1Element ),
TestEditor.create( editor2Element )
Expand Down Expand Up @@ -74,6 +83,8 @@ describe( '<InspectorUI />', () => {
editor1Element.remove();
editor2Element.remove();

window.addEventListener = origAddEventListener;

return Promise.all( Array.from( editors )
.map( ( [ , editor ] ) => editor.destroy() ) );
} );
Expand Down Expand Up @@ -326,7 +337,8 @@ describe( '<InspectorUI />', () => {
const toggle = getToggle();
const button = toggle.find( 'button' );

expect( toggle.props().onClick ).to.equal( wrapper.props().toggleIsCollapsed );
expect( toggle.props().toggleIsCollapsed ).to.be.a( 'function' );
expect( button.props().onClick ).to.equal( toggle.props().toggleIsCollapsed );
expect( button ).to.not.have.className( 'ck-inspector-navbox__navigation__toggle_up' );

store.dispatch( {
Expand All @@ -340,6 +352,14 @@ describe( '<InspectorUI />', () => {

expect( getToggle().find( 'button' ) ).to.have.className( 'ck-inspector-navbox__navigation__toggle_up' );
} );

it( 'should toggle the UI on a global ALT+F12 keyboard shortcut', () => {
windowEventMap.keydown( { key: 'F12', altKey: true } );

sinon.assert.calledWithExactly( dispatchSpy.lastCall, {
type: TOGGLE_IS_COLLAPSED
} );
} );
} );
} );

Expand Down