X-Git-Url: https://git.0d.be/?p=chloro.git;a=blobdiff_plain;f=chloro%2Fphyll%2Fstatic%2Fjs%2Fchloro.js;fp=chloro%2Fphyll%2Fstatic%2Fjs%2Fchloro.js;h=10f91e252e2dddc45052a9b19324fd5cabd1d834;hp=816d0223b08951a7f1c307f5ff71442c0260f309;hb=a892ad4fdfea78076651b645c258411b9ae1704c;hpb=425f34e0cbf80f79f6cf6c7ab7f89ee50b4b2572 diff --git a/chloro/phyll/static/js/chloro.js b/chloro/phyll/static/js/chloro.js index 816d022..10f91e2 100644 --- a/chloro/phyll/static/js/chloro.js +++ b/chloro/phyll/static/js/chloro.js @@ -355,13 +355,17 @@ } if (action == 'createLink') { var sel = window.getSelection(); + var selected_link = get_parent(sel.anchorNode, 'A'); + if (sel.anchorNode.nodeType == Node.TEXT_NODE) { + if (sel.anchorNode.length == sel.anchorOffset && sel.anchorNode.nextSibling.nodeName == 'A') { + selected_link = sel.anchorNode.nextSibling; + } + } var $input = $('input[name=link-target]'); $input[0]._range = sel.getRangeAt(0); - if (sel.anchorNode instanceof Element) { - var elem = sel.anchorNode.childNodes[sel.anchorOffset]; - if (elem.tagName == 'A') { - $input.val(elem.href); - } + if (selected_link) { + $input[0]._selected_link = selected_link; + $input.val(selected_link.href); } $input.addClass('shown'); $input.focus(); @@ -378,13 +382,25 @@ $input.removeClass('shown'); var sel = window.getSelection(); sel.addRange(this._range); + var selected_link = $input[0]._selected_link; if (url) { - document.execCommand('createLink', false, url); + if (selected_link) { + selected_link.href = url; + } else { + var $new_link = $('', {text: sel.toString(), href: url}); + this._range.deleteContents(); + this._range.insertNode($new_link[0]); + sel.empty(); + sel.collapse($new_link[0]); + sel.empty(); + } } else { - document.execCommand('unlink', false, null); + if (selected_link) { + selected_link.replaceWith(document.createTextNode(selected_link.textContent)); + } } - sel.empty(); $input.val(''); + $input[0]._selected_link = null; } } function focusout_link(ev) {