Skip to content

Commit

Permalink
Move getInsertPosition from RichText to InlineBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jayshenk committed May 15, 2018
1 parent 7d6e6a1 commit b8a225a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
15 changes: 1 addition & 14 deletions editor/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export class RichText extends Component {
this.onPaste = this.onPaste.bind( this );
this.onCreateUndoLevel = this.onCreateUndoLevel.bind( this );
this.setFocusedElement = this.setFocusedElement.bind( this );
this.getInsertPosition = this.getInsertPosition.bind( this );

this.state = {
formats: {},
Expand Down Expand Up @@ -441,18 +440,6 @@ export class RichText extends Component {
};
}

getInsertPosition() {
// The container is relatively positioned.
const containerPosition = this.containerRef.current.getBoundingClientRect();
const rect = getRectangleFromRange( this.editor.selection.getRng() );

return {
top: rect.top - containerPosition.top,
left: rect.right - containerPosition.left,
height: rect.height,
};
}

/**
* Handles a keydown event from tinyMCE.
*
Expand Down Expand Up @@ -888,7 +875,7 @@ export class RichText extends Component {
{ isSelected &&
<InlineBlocks
editor={ this.editor }
getInsertPosition={ this.getInsertPosition }
containerRef={ this.containerRef }
/>
}
<Autocomplete onReplace={ this.props.onReplace } completers={ autocompleters }>
Expand Down
23 changes: 18 additions & 5 deletions editor/components/rich-text/inline-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { Component, Fragment, compose } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
import { withSafeTimeout } from '@wordpress/components';
import { getRectangleFromRange } from '@wordpress/utils';

/**
* Internal dependencies
Expand All @@ -16,6 +17,7 @@ class InlineBlocks extends Component {
super( ...arguments );

this.insert = this.insert.bind( this );
this.getInsertPosition = this.getInsertPosition.bind( this );
this.onSelectMedia = this.onSelectMedia.bind( this );
this.openMediaLibrary = this.openMediaLibrary.bind( this );
this.closeMediaLibrary = this.closeMediaLibrary.bind( this );
Expand Down Expand Up @@ -44,6 +46,20 @@ class InlineBlocks extends Component {
this.props.setInsertUnavailable();
}

getInsertPosition() {
const { containerRef, editor } = this.props;

// The container is relatively positioned.
const containerPosition = containerRef.current.getBoundingClientRect();
const rect = getRectangleFromRange( editor.selection.getRng() );

return {
top: rect.top - containerPosition.top,
left: rect.right - containerPosition.left,
height: rect.height,
};
}

insert() {
const {
inlineBlockForInsert,
Expand Down Expand Up @@ -81,17 +97,14 @@ class InlineBlocks extends Component {
}

render() {
const {
isInlineInsertionPointVisible,
getInsertPosition,
} = this.props;
const { isInlineInsertionPointVisible } = this.props;
const { mediaLibraryOpen } = this.state;

return (
<Fragment>
{ isInlineInsertionPointVisible &&
<InlineInsertionPoint
style={ getInsertPosition() }
style={ this.getInsertPosition() }
/>
}
{ mediaLibraryOpen &&
Expand Down

0 comments on commit b8a225a

Please sign in to comment.