Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
dom.documentHasSelection();
// $ExpectType Element[]
dom.focus.focusable.find(element);
// $ExpectType Element[]
dom.focus.tabbable.find(element);
// $ExpectType boolean
dom.focus.tabbable.isTabbableIndex(element);
// $ExpectType Element | null
dom.getOffsetParent(node);
// $ExpectType DOMRect
dom.getRectangleFromRange(range);
// $ExpectType Element | undefined
dom.getScrollContainer(element);
// $ExpectType void
dom.insertAfter(node, node);
// $ExpectType boolean
dom.isEntirelySelected(element);
// $ExpectType boolean
dom.isHorizontalEdge(element, true);
// $ExpectType boolean
dom.isTextField(element);
function getCurrentCaretPositionStyle() {
const selection = window.getSelection();
// Unlikely, but in the case there is no selection, return empty styles so
// as to avoid a thrown error by `Selection#getRangeAt` on invalid index.
if ( selection.rangeCount === 0 ) {
return {};
}
// Get position relative viewport.
const rect = getRectangleFromRange( selection.getRangeAt( 0 ) );
let top = rect.top + rect.height;
let left = rect.left + ( rect.width / 2 );
// Offset by positioned parent, if one exists.
const offsetParent = getOffsetParent( selection.anchorNode );
if ( offsetParent ) {
const parentRect = offsetParent.getBoundingClientRect();
top -= parentRect.top;
left -= parentRect.left;
}
return { top, left };
}
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,
};
}
onKeyUp ({ keyCode }) {
// The input event does not fire when the whole field is selected and
// BACKSPACE is pressed.
if (keyCode === BACKSPACE) {
this.onChange();
}
// `scrollToRect` is called on `nodechange`, whereas calling it on
// `keyup` *when* moving to a new RichText element results in incorrect
// scrolling. Though the following allows false positives, it results
// in much smoother scrolling.
if (this.props.isViewportSmall && keyCode !== BACKSPACE && keyCode !== ENTER) {
this.scrollToRect(getRectangleFromRange(this.editor.selection.getRng()));
}
}
const anchorRect = useMemo( () => {
const selection = window.getSelection();
const range = selection.rangeCount > 0 ? selection.getRangeAt( 0 ) : null;
if ( ! range ) {
return;
}
if ( addingLink ) {
return getRectangleFromRange( range );
}
let element = range.startContainer;
// If the caret is right before the element, select the next element.
element = element.nextElementSibling || element;
while ( element.nodeType !== window.Node.ELEMENT_NODE ) {
element = element.parentNode;
}
const closest = element.closest( 'a' );
if ( closest ) {
return closest.getBoundingClientRect();
}
}, [ isActive, addingLink, value.start, value.end ] );
};
return accFormats;
}, {} );
this.setState( { formats, selectedNodeId: this.state.selectedNodeId + 1 } );
if ( this.props.isViewportSmall ) {
let rect;
const selectedAnchor = find( parents, ( node ) => node.tagName === 'A' );
if ( selectedAnchor ) {
// If we selected a link, position the Link UI below the link
rect = selectedAnchor.getBoundingClientRect();
} else {
// Otherwise, position the Link UI below the cursor or text selection
rect = getRectangleFromRange( this.editor.selection.getRng() );
}
// Originally called on `focusin`, that hook turned out to be
// premature. On `nodechange` we can work with the finalized TinyMCE
// instance and scroll to proper position.
this.scrollToRect( rect );
}
}
const anchorRect = useMemo( () => {
const selection = window.getSelection();
const range = selection.rangeCount > 0 ? selection.getRangeAt( 0 ) : null;
if ( ! range ) {
return;
}
if ( addingColor ) {
return getRectangleFromRange( range );
}
let element = range.startContainer;
// If the caret is right before the element, select the next element.
element = element.nextElementSibling || element;
while ( element.nodeType !== window.Node.ELEMENT_NODE ) {
element = element.parentNode;
}
const closest = element.closest( 'span' );
if ( closest ) {
return closest.getBoundingClientRect();
}
}, [ isActive, addingColor, value.start, value.end ] );
onKeyUp( { keyCode } ) {
// The input event does not fire when the whole field is selected and
// BACKSPACE is pressed.
if ( keyCode === BACKSPACE ) {
this.onChange();
}
// `scrollToRect` is called on `nodechange`, whereas calling it on
// `keyup` *when* moving to a new RichText element results in incorrect
// scrolling. Though the following allows false positives, it results
// in much smoother scrolling.
if ( this.props.isViewportSmall && keyCode !== BACKSPACE && keyCode !== ENTER ) {
this.scrollToRect( getRectangleFromRange( this.editor.selection.getRng() ) );
}
}
if ( getAnchorRect ) {
if ( ! anchorRefFallback.current ) {
return;
}
return getAnchorRect( anchorRefFallback.current );
}
if ( anchorRef !== false ) {
if ( ! anchorRef ) {
return;
}
if ( anchorRef instanceof window.Range ) {
return getRectangleFromRange( anchorRef );
}
const rect = anchorRef.getBoundingClientRect();
if ( shouldAnchorIncludePadding ) {
return rect;
}
return withoutPadding( rect, anchorRef );
}
if ( ! anchorRefFallback.current ) {
return;
}
const { parentNode } = anchorRefFallback.current;