UNPKG

env-autosuggest

Version:

A React component that provides variable autosuggestion functionality with a rich text editor-like interface. This component allows users to type variables within curly braces and get intelligent suggestions based on predefined variables.

19 lines 639 B
export function getCaretPosition(editableDiv) { const selection = window.getSelection(); if (selection.rangeCount === 0) return null; const range = selection.getRangeAt(0).cloneRange(); const span = document.createElement('span'); span.style.position = 'absolute'; span.style.visibility = 'hidden'; span.style.height = '0px'; range.insertNode(span); const rect = span.getBoundingClientRect(); const caretPosition = { left: rect.left, top: rect.top + 16, right: rect.right, bottom: rect.bottom }; span.parentNode?.removeChild(span); return caretPosition; }