Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
scrollToRect (rect) {
const { top: caretTop } = rect;
const container = getScrollContainer(this.editor.getBody());
if (! container) {
return;
}
// When scrolling, avoid positioning the caret at the very top of
// the viewport, providing some "air" and some textual context for
// the user, and avoiding toolbars.
const graceOffset = 100;
// Avoid pointless scrolling by establishing a threshold under
// which scrolling should be skipped;
const epsilon = 10;
const delta = caretTop - graceOffset;
if (Math.abs(delta) > epsilon) {
scrollToRect( rect ) {
const { top: caretTop } = rect;
const container = getScrollContainer( this.editor.getBody() );
if ( ! container ) {
return;
}
// When scrolling, avoid positioning the caret at the very top of
// the viewport, providing some "air" and some textual context for
// the user, and avoiding toolbars.
const graceOffset = 100;
// Avoid pointless scrolling by establishing a threshold under
// which scrolling should be skipped;
const epsilon = 10;
const delta = caretTop - graceOffset;
if ( Math.abs( delta ) > epsilon ) {
const { height } = contentSize;
if ( sticky ) {
let topEl = anchorRef;
let bottomEl = anchorRef;
if ( typeof sticky === 'string' ) {
const elements = document.querySelectorAll( sticky );
if ( elements.length ) {
topEl = elements[ 0 ];
bottomEl = elements[ elements.length - 1 ];
}
}
const scrollContainerEl = getScrollContainer( topEl ) || document.body;
const scrollRect = scrollContainerEl.getBoundingClientRect();
const topRect = topEl.getBoundingClientRect();
const bottomRect = bottomEl.getBoundingClientRect();
if ( topRect.top - height <= scrollRect.top ) {
return {
yAxis,
popoverTop: Math.min( bottomRect.bottom, scrollRect.top + height ),
};
}
}
// y axis alignment choices
let anchorMidPoint = anchorRect.top + ( anchorRect.height / 2 );
if ( corner === 'bottom' ) {
scrollToRect( rect ) {
const { top: caretTop } = rect;
const container = getScrollContainer( this.editor.getBody() );
if ( ! container ) {
return;
}
// When scrolling, avoid positioning the caret at the very top of
// the viewport, providing some "air" and some textual context for
// the user, and avoiding toolbars.
const graceOffset = 100;
// Avoid pointless scrolling by establishing a threshold under
// which scrolling should be skipped;
const epsilon = 10;
const delta = caretTop - graceOffset;
if ( Math.abs( delta ) > epsilon ) {
restorePreviousOffset( offset ) {
const { selectionStart } = this.props;
const blockNode = getBlockDOMNode( selectionStart );
if ( blockNode ) {
const scrollContainer = getScrollContainer( blockNode );
if ( scrollContainer ) {
scrollContainer.scrollTop = scrollContainer.scrollTop +
blockNode.getBoundingClientRect().top - offset;
}
}
}
restorePreviousOffset( offset ) {
const { selectionStart } = this.props;
const blockNode = getBlockDOMNode( selectionStart );
if ( blockNode ) {
const scrollContainer = getScrollContainer( blockNode );
if ( scrollContainer ) {
scrollContainer.scrollTop = scrollContainer.scrollTop +
blockNode.getBoundingClientRect().top - offset;
}
}
}