如何为React中的文本选择提供超链接

发布于 2025-02-04 12:20:22 字数 1530 浏览 3 评论 0原文

我正在尝试构建Wysyyg编辑器,并且可以选择与文本选择的超链接。我一直在搜寻互联网,但我无法找出为什么这不起作用。

我的理解是,一旦他们单击超链接按钮,我们就将选择的背景颜色更改为选择颜色,然后一旦进入链接,我们就会超链接并恢复背景颜色。

这是我的代码:

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);
        }

一旦他们进入超链接,我就会执行此操作:

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));
            
        }
   
    }

但是,当我更改选择的背景颜色时,范围对象就会丢失。而且我无法使用它来表现超级链接。我尝试将其存储并重复使用该变量,但它不起作用。解决此问题的正确方法是什么?

提前致谢 :)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨凡馨 2025-02-11 12:20:22

我意识到背景颜色在您选择中给出了跨度标签。我要做的就是根据新的跨度文本节点创建一个范围对象。我使用此功能跟踪文本节点:

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}
}

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}
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文