Skip to content

Commit

Permalink
Reuse isHorizontalEdge for RichText (#5159)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Feb 22, 2018
1 parent ab55c97 commit 35122e1
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 68 deletions.
52 changes: 3 additions & 49 deletions blocks/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import 'element-closest';
* WordPress dependencies
*/
import { createElement, Component, renderToString } from '@wordpress/element';
import { keycodes, createBlobURL } from '@wordpress/utils';
import { keycodes, createBlobURL, isHorizontalEdge } from '@wordpress/utils';
import { withSafeTimeout, Slot, Fill } from '@wordpress/components';

/**
Expand Down Expand Up @@ -468,52 +468,6 @@ export class RichText extends Component {
};
}

/**
* Determines if the current selection within the editor is at the start.
*
* @return {boolean} Whether or not the selection is at the start of the editor.
*/
isStartOfEditor() {
const range = this.editor.selection.getRng();
if ( range.startOffset !== 0 || ! range.collapsed ) {
return false;
}
const start = range.startContainer;
const body = this.editor.getBody();
let element = start;
while ( element !== body ) {
const child = element;
element = element.parentNode;
if ( element.firstChild !== child ) {
return false;
}
}
return true;
}

/**
* Determines if the current selection within the editor is at the end.
*
* @return {boolean} Whether or not the selection is at the end of the editor.
*/
isEndOfEditor() {
const range = this.editor.selection.getRng();
if ( range.endOffset !== range.endContainer.textContent.length || ! range.collapsed ) {
return false;
}
const start = range.endContainer;
const body = this.editor.getBody();
let element = start;
while ( element !== body ) {
const child = element;
element = element.parentNode;
if ( element.lastChild !== child ) {
return false;
}
}
return true;
}

/**
* Handles a keydown event from tinyMCE.
*
Expand All @@ -524,8 +478,8 @@ export class RichText extends Component {
const rootNode = this.editor.getBody();

if (
( event.keyCode === BACKSPACE && this.isStartOfEditor() ) ||
( event.keyCode === DELETE && this.isEndOfEditor() )
( event.keyCode === BACKSPACE && isHorizontalEdge( rootNode, true ) ) ||
( event.keyCode === DELETE && isHorizontalEdge( rootNode, false ) )
) {
if ( ! this.props.onMerge && ! this.props.onRemove ) {
return;
Expand Down
13 changes: 7 additions & 6 deletions editor/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import tinymce from 'tinymce';
* WordPress dependencies
*/
import { Component, findDOMNode, compose } from '@wordpress/element';
import { keycodes, focus } from '@wordpress/utils';
import {
keycodes,
focus,
getScrollContainer,
placeCaretAtHorizontalEdge,
placeCaretAtVerticalEdge,
} from '@wordpress/utils';
import {
BlockEdit,
createBlock,
Expand All @@ -26,11 +32,6 @@ import { __, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import {
getScrollContainer,
placeCaretAtHorizontalEdge,
placeCaretAtVerticalEdge,
} from '../../utils/dom';
import BlockMover from '../block-mover';
import BlockDropZone from '../block-drop-zone';
import BlockSettingsMenu from '../block-settings-menu';
Expand Down
2 changes: 1 addition & 1 deletion editor/components/copy-handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { connect } from 'react-redux';
*/
import { Component } from '@wordpress/element';
import { serialize } from '@wordpress/blocks';
import { documentHasSelection } from '@wordpress/utils';

/**
* Internal dependencies
*/
import { documentHasSelection } from '../../utils/dom';
import { removeBlocks } from '../../store/actions';
import {
getMultiSelectedBlocks,
Expand Down
6 changes: 1 addition & 5 deletions editor/components/multi-select-scroll-into-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import scrollIntoView from 'dom-scroll-into-view';
*/
import { Component } from '@wordpress/element';
import { query } from '@wordpress/data';

/**
* Internal dependencies
*/
import { getScrollContainer } from '../../utils/dom';
import { getScrollContainer } from '@wordpress/utils';

class MultiSelectScrollIntoView extends Component {
componentDidUpdate() {
Expand Down
15 changes: 8 additions & 7 deletions editor/components/writing-flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import { find, last, reverse, get } from 'lodash';
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { keycodes, focus } from '@wordpress/utils';

/**
* Internal dependencies
*/
import { BlockListBlock } from '../block-list/block';
import {
keycodes,
focus,
computeCaretRect,
isHorizontalEdge,
isVerticalEdge,
placeCaretAtHorizontalEdge,
placeCaretAtVerticalEdge,
} from '../../utils/dom';
} from '@wordpress/utils';

/**
* Internal dependencies
*/
import { BlockListBlock } from '../block-list/block';
import {
getPreviousBlockUid,
getNextBlockUid,
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { keycodes };
export { decodeEntities };

export * from './blob-cache';
export * from './dom';
export * from './mediaupload';
export * from './terms';
export * from './deprecation';
Expand Down
File renamed without changes.

0 comments on commit 35122e1

Please sign in to comment.