Skip to content

Commit

Permalink
Remove window width check, use UA check
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Feb 7, 2018
1 parent eabcf3d commit 2d1c883
Showing 1 changed file with 28 additions and 35 deletions.
63 changes: 28 additions & 35 deletions blocks/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import { EVENTS } from './constants';
const { BACKSPACE, DELETE, ENTER } = keycodes;

/**
* Holds the rectangle of the root node, to use across instances when needed.
* Holds the offset of the root node, to use across instances when needed.
*/
let scrollPosition;
let offsetTop;

export function createTinyMCEElement( type, props, ...children ) {
if ( props[ 'data-mce-bogus' ] === 'all' ) {
Expand Down Expand Up @@ -219,27 +219,20 @@ export default class RichText extends Component {
}

onFocus() {
// For small screens (virtual keyboard), always scroll the focussed
// editor into view. Unfortunately we cannot detect virtual keyboards.
if ( window.innerWidth < 784 ) {
// Do not scroll until click/touch finished.
setTimeout( () => {
if ( this.editor.removed ) {
return;
}

const rootNode = this.editor.getBody();
const rootRect = rootNode.getBoundingClientRect();
const caretRect = this.editor.selection.getRng().getClientRects()[ 0 ];
const offset = caretRect ? caretRect.top - rootRect.top : 0;

scrollIntoView( rootNode, getScrollContainer( rootNode ), {
// Give enough room for toolbar. Must be top.
// Unfortunately we cannot scroll to bottom as the
// virtual keyboard overlaps the window.
offsetTop: 100 - offset,
alignWithTop: true,
} );
// For virtual keyboards, always scroll the focussed editor into view.
// Unfortunately we cannot detect virtual keyboards, so we check UA.
if ( /iPad|iPhone|iPod|Android/i.test( window.navigator.userAgent ) ) {
const rootNode = this.editor.getBody();
const rootRect = rootNode.getBoundingClientRect();
const caretRect = this.editor.selection.getRng().getClientRects()[ 0 ];
const offset = caretRect ? caretRect.top - rootRect.top : 0;

scrollIntoView( rootNode, getScrollContainer( rootNode ), {
// Give enough room for toolbar. Must be top.
// Unfortunately we cannot scroll to bottom as the virtual
// keyboard does not change the window size.
offsetTop: 100 - offset,
alignWithTop: true,
} );
}

Expand Down Expand Up @@ -623,12 +616,10 @@ export default class RichText extends Component {
if ( event.shiftKey || ! this.props.onSplit ) {
this.editor.execCommand( 'InsertLineBreak', false, event );
} else {
// For small screens, save the root node rectangle so it can
// the position can be scrolled to in the next focussed
// For type writing offect, save the root node offset so it
// can the position can be scrolled to in the next focussed
// instance.
if ( window.innerWidth < 784 ) {
scrollPosition = this.editor.getBody().getBoundingClientRect();
}
offsetTop = rootNode.getBoundingClientRect().top;

this.splitContent();
}
Expand Down Expand Up @@ -867,16 +858,18 @@ export default class RichText extends Component {
}

onTinyMCEMount( node ) {
if ( ! offsetTop ) {
return;
}

// When a new instance is created, scroll the root node into the
// position of the root node that captured ENTER.
if ( scrollPosition && window.innerWidth < 784 ) {
scrollIntoView( node, getScrollContainer( node ), {
offsetTop: scrollPosition.top,
alignWithTop: true,
} );
scrollIntoView( node, getScrollContainer( node ), {
offsetTop,
alignWithTop: true,
} );

scrollPosition = null;
}
offsetTop = null;
}

render() {
Expand Down

0 comments on commit 2d1c883

Please sign in to comment.