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

179
Vistas
How to provide hyperlinking for text selection in React

I am trying to build a WYSISYG editor, and I have an option to provide hyperlinking to a text selection. I have been scourging the internet but I am not able to find out why this isn't working sigh.

My understanding is, once they click on the hyperlink button, we change the background color of the selection to the selection color, then once they enter the link, we hyperlink and revert back the background color.

Here is my code for it:

const selObj = window.getSelection();
        const oRange = selObj.getRangeAt(0);
        rangeObjectReference.current = oRange;
        
        if(isLinked) {
            document.execCommand("unlink", false, false);
            dispatch(formatBlock(blockId));
            toggleLinked();
        } else {
            document.execCommand("backColor",true,'#ece6ff');
            setBlockActive();
            setLinkEnter(true);
        }

Once they enter the hyperlink, I am performing this:

function appendLink() {
        if(activeLink == '' && isLinked) {
            document.execCommand("unlink", false, false);
            dispatch(formatBlock(blockId));
            toggleLinked(false);
            return 
        } else {
                var sel = window.getSelection();
                sel.removeAllRanges();
                sel.addRange(rangeObjectReference.current);
                document.execCommand("backColor",false,'transparent');
                document.execCommand("CreateLink", false, activeLink);
                dispatch(formatBlock(blockId));
            
        }
   
    }

But when I change the background color of the selection, the range object is lost. and I am not able to use it to peform the hyper link. I have tried storing it and reusing the variable but it doesn't work. What is the correct method to fix this?

Thanks in advance :)

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

0

I realised that the background color gives out a span tag in your selection. All I had to do was to create a range object based on the new span text node. I tracked the text node with this function:

export const returnTextNode = (blockId) => {
    var hasNode = false;
    var selectedNode = document.getElementById(blockId).childNodes;

    for(var i = 0; i < selectedNode.length; i++) {
        if(selectedNode[i].nodeName == 'SPAN') {
            selectedNode = selectedNode[i];
            hasNode = true;
            break;
        } else if(selectedNode[i].nodeName == 'A') {
            var node = selectedNode[i];
            var styleAttribute = node.getAttribute("style");
            if(styleAttribute?.startsWith('background-color')) {
                selectedNode = selectedNode[i];
                hasNode = true;
            } 
            else {
                var childSpanNode = selectedNode[i].children[0];
                if(childSpanNode) {
                    selectedNode = childSpanNode
                    hasNode = true;
                    break

                }
            }
        }
    }

    return {selectedNode,hasNode}
}

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