Autolink URL in contenteditable

后端 未结 1 1374
孤城傲影
孤城傲影 2021-01-03 17:22

When the user finishes to type in a URL in a contenteditable div, I want to autolink it, like Medium does: \"http://d.pr

相关标签:
1条回答
  • 2021-01-03 18:07

    Finally found a workaround, without using execCommand:

    1. delete the range content: range.deleteContents()
    2. create the link node I want to insert
    3. insert the node in the range: range.insertNode(createdLinkNode)
    4. place the caret right after the inserted node:

     

    range.setStartAfter(createdLinkNode);
    range.setEndAfter(createdLinkNode);
    selection.removeAllRanges();
    selection.addRange(range);
    
    0 讨论(0)
提交回复
热议问题