Skip to content

Commit

Permalink
Title: Override history of textarea to dispatch undo
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Mar 10, 2018
1 parent 871a36e commit 0a1481a
Showing 1 changed file with 66 additions and 24 deletions.
90 changes: 66 additions & 24 deletions editor/components/post-title/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';
import Textarea from 'react-autosize-textarea';
import classnames from 'classnames';

Expand All @@ -12,15 +11,14 @@ import { __ } from '@wordpress/i18n';
import { Component, compose } from '@wordpress/element';
import { keycodes } from '@wordpress/utils';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
import { withContext, withFocusOutside } from '@wordpress/components';
import { KeyboardShortcuts, withContext, withFocusOutside } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';

/**
* Internal dependencies
*/
import './style.scss';
import PostPermalink from '../post-permalink';
import { getEditedPostAttribute } from '../../store/selectors';
import { insertBlock, editPost, clearSelectedBlock } from '../../store/actions';

/**
* Constants
Expand All @@ -36,6 +34,7 @@ class PostTitle extends Component {
this.onSelect = this.onSelect.bind( this );
this.onUnselect = this.onUnselect.bind( this );
this.onKeyDown = this.onKeyDown.bind( this );
this.redirectHistory = this.redirectHistory.bind( this );

this.state = {
isSelected: false,
Expand Down Expand Up @@ -67,6 +66,26 @@ class PostTitle extends Component {
}
}

/**
* Emulates behavior of an undo or redo on its corresponding key press
* combination. This is a workaround to React's treatment of undo in a
* controlled textarea where characters are updated one at a time.
* Instead, leverage the store's undo handling of title changes.
*
* @see https://github.com/facebook/react/issues/8514
*
* @param {KeyboardEvent} event Key event.
*/
redirectHistory( event ) {
if ( event.shiftKey ) {
this.props.onRedo();
} else {
this.props.onUndo();
}

event.preventDefault();
}

render() {
const { title, placeholder } = this.props;
const { isSelected } = this.state;
Expand All @@ -75,35 +94,57 @@ class PostTitle extends Component {
return (
<div className={ className }>
{ isSelected && <PostPermalink /> }
<Textarea
className="editor-post-title__input"
value={ title }
onChange={ this.onChange }
placeholder={ placeholder || __( 'Add title' ) }
aria-label={ placeholder || __( 'Add title' ) }
onFocus={ this.onSelect }
onKeyDown={ this.onKeyDown }
onKeyPress={ this.onUnselect }
/>
<KeyboardShortcuts
shortcuts={ {
'mod+z': this.redirectHistory,
'mod+shift+z': this.redirectHistory,
} }
>
<Textarea
className="editor-post-title__input"
value={ title }
onChange={ this.onChange }
placeholder={ placeholder || __( 'Add title' ) }
aria-label={ placeholder || __( 'Add title' ) }
onFocus={ this.onSelect }
onKeyDown={ this.onKeyDown }
onKeyPress={ this.onUnselect }
/>
</KeyboardShortcuts>
</div>
);
}
}

const applyConnect = connect(
( state ) => ( {
title: getEditedPostAttribute( state, 'title' ),
} ),
{
const applyWithSelect = withSelect( ( select ) => {
const { getEditedPostAttribute } = select( 'core/editor' );

return {
title: getEditedPostAttribute( 'title' ),
};
} );

const applyWithDispatch = withDispatch( ( dispatch ) => {
const {
insertBlock,
editPost,
clearSelectedBlock,
undo,
redo,
} = dispatch( 'core/editor' );

return {
onEnterPress() {
return insertBlock( createBlock( getDefaultBlockName() ), 0 );
insertBlock( createBlock( getDefaultBlockName() ), 0 );
},
onUpdate( title ) {
return editPost( { title } );
editPost( { title } );
},
onUndo: undo,
onRedo: redo,
clearSelectedBlock,
}
);
};
} );

const applyEditorSettings = withContext( 'editor' )(
( settings ) => ( {
Expand All @@ -112,7 +153,8 @@ const applyEditorSettings = withContext( 'editor' )(
);

export default compose(
applyConnect,
applyWithSelect,
applyWithDispatch,
applyEditorSettings,
withFocusOutside
)( PostTitle );

0 comments on commit 0a1481a

Please sign in to comment.