Skip to content

Commit

Permalink
Select objects when clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Feb 15, 2019
1 parent 38cb03d commit dfc532c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/editor/src/components/rich-text/editable.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ export default class Editable extends Component {
onCompositionEnd,
onFocus,
onBlur,
onMouseDown,
onTouchStart,
} = this.props;

ariaProps.role = 'textbox';
Expand All @@ -188,6 +190,8 @@ export default class Editable extends Component {
onBlur,
onKeyDown,
onCompositionEnd,
onMouseDown,
onTouchStart,
} );
}
}
29 changes: 29 additions & 0 deletions packages/editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export class RichText extends Component {
this.setRef = this.setRef.bind( this );
this.valueToEditableHTML = this.valueToEditableHTML.bind( this );
this.handleHorizontalNavigation = this.handleHorizontalNavigation.bind( this );
this.onPointerDown = this.onPointerDown.bind( this );

this.formatToValue = memize( this.formatToValue.bind( this ), { size: 1 } );

Expand Down Expand Up @@ -742,6 +743,32 @@ export class RichText extends Component {
this.onSplit( before, after, ...blocks );
}

/**
* Select object when they are clicked. The browser will not set any
* selection when clicking e.g. an image.
*
* @param {SyntheticEvent} event Synthetic mousedown or touchstart event.
*/
onPointerDown( event ) {
const { target } = event;

// If the child element has no text content, it must be an object.
if ( target === this.editableRef || target.textContent ) {
return;
}

const { parentNode } = target;
const index = Array.from( parentNode.childNodes ).indexOf( target );
const range = target.ownerDocument.createRange();
const selection = getSelection();

range.setStart( target.parentNode, index );
range.setEnd( target.parentNode, index + 1 );

selection.removeAllRanges();
selection.addRange( range );
}

componentDidUpdate( prevProps ) {
const { tagName, value, isSelected } = this.props;

Expand Down Expand Up @@ -978,6 +1005,8 @@ export class RichText extends Component {
onKeyDown={ this.onKeyDown }
onFocus={ this.onFocus }
onBlur={ this.onBlur }
onMouseDown={ this.onPointerDown }
onTouchStart={ this.onPointerDown }
multilineTag={ this.multilineTag }
multilineWrapperTags={ this.multilineWrapperTags }
setRef={ this.setRef }
Expand Down

0 comments on commit dfc532c

Please sign in to comment.