Autolink URL in ContentEditable Iframe

后端 未结 1 1739

I have a content editable Iframe I want to autolink it, like : \"enter My content editable If

1条回答
  •  感情败类
    2020-12-11 06:59

    Finally its working and thanks to @Tim Down for his code and replies

    This is what i did - Autolink in an content editable Iframe

    autoAppLink: function (Iframe) {
            var saveSelection, restoreSelection;
    
            if (window.getSelection && document.createRange) {
                saveSelection = function (containerEl) {
                    var range = iframe[0].contentWindow.getSelection().getRangeAt(0);
                    var preSelectionRange = range.cloneRange();
                    preSelectionRange.selectNodeContents(containerEl);
                    preSelectionRange.setEnd(range.startContainer, range.startOffset);
                    var start = preSelectionRange.toString().length;
    
                    return {
                        start: start,
                        end: start + range.toString().length
                    }
                };
    
                restoreSelection = function (containerEl, savedSel) {
                    var charIndex = 0, range = document.createRange();
                    range.setStart(containerEl, 0);
                    range.collapse(true);
                    var nodeStack = [containerEl], node, foundStart = false, stop = false;
    
                    while (!stop && (node = nodeStack.pop())) {
                        if (node.nodeType == 3) {
                            var nextCharIndex = charIndex + node.length;
                            if (!foundStart && savedSel.start >= charIndex && savedSel.start <= nextCharIndex) {
                                range.setStart(node, savedSel.start - charIndex);
                                foundStart = true;
                            }
                            if (foundStart && savedSel.end >= charIndex && savedSel.end <= nextCharIndex) {
                                range.setEnd(node, savedSel.end - charIndex);
                                stop = true;
                            }
                            charIndex = nextCharIndex;
                        } else {
                            var i = node.childNodes.length;
                            while (i--) {
                                nodeStack.push(node.childNodes[i]);
                            }
                        }
                    }
    
                    var sel = iframe[0].contentWindow.getSelection();
                    sel.removeAllRanges();
                    sel.addRange(range);
                }
            } else if (document.selection) {
                saveSelection = function (containerEl) {
                    var selectedTextRange = document.selection.createRange();
                    var preSelectionTextRange = document.body.createTextRange();
                    preSelectionTextRange.moveToElementText(containerEl);
                    preSelectionTextRange.setEndPoint("EndToStart", selectedTextRange);
                    var start = preSelectionTextRange.text.length;
    
                    return {
                        start: start,
                        end: start + selectedTextRange.text.length
                    }
                };
    
                restoreSelection = function (containerEl, savedSel) {
                    var textRange = document.body.createTextRange();
                    textRange.moveToElementText(containerEl);
                    textRange.collapse(true);
                    textRange.moveEnd("character", savedSel.end);
                    textRange.moveStart("character", savedSel.start);
                    textRange.select();
                };
            }
    
            function createLink(matchedTextNode) {
                var el = document.createElement("a");
                el.href = matchedTextNode.data;
                el.target = "_blank";
                el.appendChild(matchedTextNode);
                return el;
            }
    
            function shouldLinkifyContents(el) {
                return el.tagName != "A";
            }
    
            function surroundInElement(el, regex, surrounderCreateFunc, shouldSurroundFunc) {
                var child = el.lastChild;
                while (child) {
                    if (child.nodeType == 1 && shouldSurroundFunc(el)) {
                        surroundInElement(child, regex, createLink, shouldSurroundFunc);
                    } else if (child.nodeType == 3) {
                        surroundMatchingText(child, regex, surrounderCreateFunc);
                    }
                    child = child.previousSibling;
                }
            }
    
            function surroundMatchingText(textNode, regex, surrounderCreateFunc) {
                var parent = textNode.parentNode;
                var result, surroundingNode, matchedTextNode, matchLength, matchedText;
                while (textNode && (result = regex.exec(textNode.data))) {
                    matchedTextNode = textNode.splitText(result.index);
                    matchedText = result[0];
                    matchLength = matchedText.length;
                    textNode = (matchedTextNode.length > matchLength) ?
                        matchedTextNode.splitText(matchLength) : null;
                    surroundingNode = surrounderCreateFunc(matchedTextNode.cloneNode(true));
                    parent.insertBefore(surroundingNode, matchedTextNode);
                    parent.removeChild(matchedTextNode);
                }
            }
    
            var iframe = Iframe,
                textbox = iframe.contents().find("body")[0];
            var urlRegex = /http(s?):\/\/($|[^ ]+)/;
    
            function updateLinks() {
                var savedSelection = saveSelection(textbox);
                surroundInElement(textbox, urlRegex, createLink, shouldLinkifyContents);
                restoreSelection(textbox, savedSelection);
            }
    
            var $textbox = $(textbox);
    
    
            $textbox.focus();
    
            var keyTimer = null, keyDelay = 1000;
    
            $textbox.keyup(function () {
                if (keyTimer) {
                    window.clearTimeout(keyTimer);
                }
                keyTimer = window.setTimeout(function () {
    
                    updateLinks();
                    keyTimer = null;
                }, keyDelay);
            });
    
        }
    

    0 讨论(0)
提交回复
热议问题