Skip to content

Commit

Permalink
Editor: Add keyboard shortcuts support for formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Oct 16, 2018
1 parent b6570f8 commit 5c052ce
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions packages/editor/src/components/rich-text/format-edit.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/**
* WordPress dependencies
*/
import { Fragment, Component } from '@wordpress/element';
import { Component, Fragment } from '@wordpress/element';
import { getActiveFormat, getFormatTypes } from '@wordpress/rich-text';
import { Fill } from '@wordpress/components';
import { Fill, KeyboardShortcuts } from '@wordpress/components';
import { rawShortcut } from '@wordpress/keycodes';

let currentEditor;

function FillToolbarSlot( { name, children } ) {
return (
<Fill name={ `RichText.ToolbarControls.${ name }` }>
Expand All @@ -16,23 +14,32 @@ function FillToolbarSlot( { name, children } ) {
);
}

// To do: don't rely on editor instance.
class Shortcut extends Component {
constructor( { type, character, onUse } ) {
constructor() {
super( ...arguments );
currentEditor.shortcuts.add( rawShortcut[ type ]( character ), '', () => {
onUse();
} );

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

onUse() {
this.props.onUse();
}

render() {
return null;
const { character, type } = this.props;

return (
<KeyboardShortcuts
bindGlobal
shortcuts={ {
[ rawShortcut[ type ]( character ) ]: this.onUse,
} }
/>
);
}
}

const FormatEdit = ( { value, onChange, editor } ) => {
currentEditor = editor;

const FormatEdit = ( { onChange, value } ) => {
return (
<Fragment>
{ getFormatTypes().map( ( { name, edit: Edit }, i ) => {
Expand Down

0 comments on commit 5c052ce

Please sign in to comment.