Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

924
Vistas
How to get current cursor's position in draft.js?

For my draftjs project, I want to enable the user to insert a link. To do that, I had created a popup after pressing the shortcut cmk + k.

For better user experience, I am currently trying to smart align the popup.

enter image description here

See the above picture, I am able to position the popup below the current focus line. The bounding rect object of the Editor-container and the current selection's bounding rect would contain enough information for me to finish the calculation.

However, in other cases, when there's an empty line only: enter image description here (The cursor's position before the popup is at the end of the editor) The window.getSelection().getRangeAt(0).getBoundingClientRect() would return the DOMRect object with every value is 0. Is there any way around in dom, or in draftjs that I could attain enough information to smart align the popup? So the popup could pop up around the current cursor position.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

To solve this issue, I used the block index of the cursor multiplied by the line height + some offsets to achieve correct placement from the top when an empty line is selected.

This is code from my "calculate position of popup" function. I've excluded parts for getting the top/left positions when a line with text is selected and just included when I need to account for lines with no text when getBoundingClientRect() returns null.

const singleLineHeightOffset = 28;
const topBlankLineOffset = 15;
let position = getVisibleSelectionRect(window);
// If there is a visible selection area
if (position) {
    // Get ref of editor via prop and location of div if exists
    let editorRef = forwardedRef;
    element = ReactDOM.findDOMNode(editorRef.current) as HTMLElement;
    if (element) {
        // Code here to calculate when a line with text is selected
    } else {
        // Else if no text selected on line
        // Get current block position of cursor
        const currentBlockKey = editorState.getSelection().getStartKey();
        const currentBlockIndex = editorState
          .getCurrentContent()
          .getBlockMap()
          .keySeq()
          .findIndex((k) => k === currentBlockKey);
        let emptyLineTopHeight =
          singleLineHeightOffset * (currentBlockIndex + 1) - 5;

        // If first line of editor, add offset for toolbar/padding
        if (currentBlockIndex === 0) emptyLineTopHeight += topBlankLineOffset;
        setTopPosition(emptyLineTopHeight);
      }

This will detect what line the empty cursor is on, then creates the top offset by multiplying with your line height. You may need to play around with the offsets and edge cases such as initial line like I did.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda