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=c997bc792f12b6da36a579b5380ffdebc6042eac;hp=78930c3719df687e0fbed76eefe7636596f8a583;hb=e3d69ca8c9599c06df8fb182c9e61964688969dc;hpb=4c9d14f02af670cc51c5314248fd1101c5725e05 diff --git a/chloro/phyll/static/js/chloro.js b/chloro/phyll/static/js/chloro.js index 78930c3..c997bc7 100644 --- a/chloro/phyll/static/js/chloro.js +++ b/chloro/phyll/static/js/chloro.js @@ -37,8 +37,47 @@ $(function() { action = 'insertHTML'; param = $('', {text: window.getSelection().toString()})[0].outerHTML; } + if (action == 'createLink') { + var sel = window.getSelection(); + 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); + } + } + $input.addClass('shown'); + $input.focus(); + return; + } document.execCommand(action, false, param); } + function validate_link(ev) { + var charCode = typeof ev.which == "number" ? ev.which : ev.keyCode; + if (ev.key == "Enter") { + var $input = $(this); + var range = this._range; + var url = $input.val(); + $input.removeClass('shown'); + var sel = window.getSelection(); + sel.addRange(this._range); + if (url) { + document.execCommand('createLink', false, url); + } else { + document.execCommand('unlink', false, null); + } + sel.empty(); + $input.val(''); + } + } + function focusout_link(ev) { + var $input = $(this); + $input.removeClass('shown'); + var range = this._range; + var sel = window.getSelection(); + sel.addRange(this._range); + } function show_style_popup(sel) { if (style_popup === null) { @@ -47,18 +86,24 @@ $(function() { '' + '' + '' + + '' + + '' + ''); style_popup.hide(); - style_popup.insertAfter($('div[contenteditable]')); + style_popup.insertAfter($('.actions')); style_popup.find('button').on('click', update_style); + style_popup.find('[name=link-target]').on('keypress', validate_link).on('focusout', focusout_link); } style_popup.css('position', 'absolute'); var pos = sel.getRangeAt(0).getClientRects()[0]; - style_popup.css('top', pos.top + window.scrollY - $('main').offset().top - 33); - style_popup.css('left', pos.left + window.scrollX - $('main').offset().left); + style_popup.css('top', pos.top + window.scrollY - 33); + style_popup.css('left', pos.left + window.scrollX); style_popup.show(); }; $(document).on('selectionchange', function(event) { + if ($('input[name=link-target].shown').length) { + return; + } var sel = window.getSelection(); if ($(sel.anchorNode).parents('div[contenteditable]').length && sel.toString()) { show_style_popup(sel);