From 152757ceccba0d0c40d69a8e1844fa724cb69a5d Mon Sep 17 00:00:00 2001 From: Davi Ferreira Date: Sat, 5 Oct 2013 19:07:01 -0300 Subject: [PATCH] Fixes #33 --- dist/js/medium-editor.js | 20 ++++++++++++++------ dist/js/medium-editor.min.js | 2 +- src/js/medium-editor.js | 20 ++++++++++++++------ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/dist/js/medium-editor.js b/dist/js/medium-editor.js index 0501e6279..d1cac3087 100644 --- a/dist/js/medium-editor.js +++ b/dist/js/medium-editor.js @@ -2,6 +2,7 @@ function MediumEditor(elements, options) { 'use strict'; return this.init(elements, options); } + (function (window, document) { 'use strict'; @@ -114,6 +115,10 @@ function MediumEditor(elements, options) { if (e.which === 13 && !e.shiftKey) { if (!(self.options.disableReturn || this.getAttribute('data-disable-return'))) { document.execCommand('formatBlock', false, 'p'); + node = getSelectionStart(); + if (node.tagName.toLowerCase() === 'a') { + document.execCommand('unlink', null, false); + } } } }); @@ -389,17 +394,18 @@ function MediumEditor(elements, options) { showToolbarActions: function () { var self = this, + timeoutWrapper = function () { + self.keepToolbarAlive = false; + self.toolbar.style.display = 'none'; + document.removeEventListener('click', timeoutWrapper); + }, timer; this.anchorForm.style.display = 'none'; this.toolbarActions.style.display = 'block'; this.keepToolbarAlive = false; clearTimeout(timer); timer = setTimeout(function () { - document.addEventListener('click', function () { - self.keepToolbarAlive = false; - self.toolbar.style.display = 'none'; - document.removeEventListener('click', this); - }); + document.addEventListener('click', timeoutWrapper); }, 300); }, @@ -436,7 +442,7 @@ function MediumEditor(elements, options) { createLink: function (input) { restoreSelection(this.savedSelection); - document.execCommand('CreateLink', false, input.value); + document.execCommand('createLink', false, input.value); this.showToolbarActions(); input.value = ''; }, @@ -517,5 +523,7 @@ function MediumEditor(elements, options) { } return this; } + }; + }(window, document)); diff --git a/dist/js/medium-editor.min.js b/dist/js/medium-editor.min.js index 908455633..fb315d486 100644 --- a/dist/js/medium-editor.min.js +++ b/dist/js/medium-editor.min.js @@ -1 +1 @@ -function MediumEditor(a,b){"use strict";return this.init(a,b)}!function(a,b){"use strict";function c(a,b){var c;if(void 0===a)return b;for(c in b)b.hasOwnProperty(c)&&a.hasOwnProperty(c)===!1&&(a[c]=b[c]);return a}function d(){var b,c,d,e=a.getSelection();if(e.getRangeAt&&e.rangeCount){for(d=[],b=0,c=e.rangeCount;c>b;b+=1)d.push(e.getRangeAt(b));return d}return null}function e(b){var c,d,e=a.getSelection();if(b)for(e.removeAllRanges(),c=0,d=b.length;d>c;c+=1)e.addRange(b[c])}function f(){var a=b.getSelection().anchorNode,c=a&&3===a.nodeType?a.parentNode:a;return c}MediumEditor.prototype={defaults:{anchorInputPlaceholder:"Paste or type a link",delay:0,diffLeft:0,diffTop:-10,disableReturn:!1,disableToolbar:!1,excludedActions:[],firstHeader:"h3",forcePlainText:!0,placeholder:"Type your text",secondHeader:"h4"},init:function(a,d){return this.elements="string"==typeof a?b.querySelectorAll(a):a,0!==this.elements.length?(this.isActive=!0,this.parentElements=["p","h1","h2","h3","h4","h5","h6","blockquote"],this.id=b.querySelectorAll(".medium-editor-toolbar").length+1,this.options=c(d,this.defaults),this.initElements().bindPaste().setPlaceholders().bindWindowActions()):void 0},initElements:function(){var a;for(a=0;a
  • '+'
  • '+'
  • '+""+'
    '+' ×'+"
    "},initToolbar:function(){return this.toolbar=this.createToolbar(),this.keepToolbarAlive=!1,this.anchorForm=this.toolbar.querySelector(".medium-editor-toolbar-form-anchor"),this.toolbarActions=this.toolbar.querySelector(".medium-editor-toolbar-actions"),this},createToolbar:function(){var a=b.createElement("div");return a.id="medium-editor-toolbar-"+this.id,a.className="medium-editor-toolbar",a.innerHTML=this.toolbarTemplate(),b.getElementsByTagName("body")[0].appendChild(a),a},bindSelect:function(){var a,b=this,c="";for(this.checkSelectionWrapper=function(a){clearTimeout(c),setTimeout(function(){b.checkSelection(a)},b.options.delay)},a=0;ag?f+h+"px":a.innerWidth-g-1?"none":"block"},checkActiveButtons:function(){var a=this.selection.anchorNode;for(a.tagName||(a=this.selection.anchorNode.parentNode);void 0!==a.tagName&&-1===this.parentElements.indexOf(a.tagName);)this.activateButton(a.tagName.toLowerCase()),a=a.parentNode},activateButton:function(a){var b=this.toolbar.querySelector('[data-element="'+a+'"]');null!==b&&-1===b.className.indexOf("medium-editor-button-active")&&(b.className+=" medium-editor-button-active")},bindButtons:function(){var a,b=this.toolbar.querySelectorAll("button"),c=this,d=function(a){a.preventDefault(),a.stopPropagation(),void 0===c.selection&&c.checkSelection(a),this.className.indexOf("medium-editor-button-active")>-1?this.classList.remove("medium-editor-button-active"):this.className+=" medium-editor-button-active",c.execAction(this.getAttribute("data-action"),a)};for(a=0;a-1?(this.execFormatBlock(a.replace("append-","")),this.setToolbarPosition(),this.setToolbarButtonStates()):"anchor"===a?this.triggerAnchorAction(c):(b.execCommand(a,null,!1),this.setToolbarPosition())},triggerAnchorAction:function(){return"a"===this.selection.anchorNode.parentNode.tagName.toLowerCase()?b.execCommand("unlink",null,!1):"block"===this.anchorForm.style.display?this.showToolbarActions():this.showAnchorForm(),this},execFormatBlock:function(a){var c=this.getSelectionData(this.selection.anchorNode);return"blockquote"===a&&"blockquote"===c.el.parentNode.tagName.toLowerCase()?b.execCommand("outdent",!1,null):(c.tagName===a&&(a="p"),b.execCommand("formatBlock",!1,a))},getSelectionData:function(a){var b;for(a&&a.tagName&&(b=a.tagName.toLowerCase());a&&-1===this.parentElements.indexOf(b);)a=a.parentNode,a&&a.tagName&&(b=a.tagName.toLowerCase());return{el:a,tagName:b}},getFirstChild:function(a){for(var b=a.firstChild;null!==b&&1!==b.nodeType;)b=b.nextSibling;return b},bindElementToolbarEvents:function(a){var b=this;a.addEventListener("mouseup",function(a){b.checkSelection(a)}),a.addEventListener("keyup",function(a){b.checkSelection(a)})},showToolbarActions:function(){var a,c=this;this.anchorForm.style.display="none",this.toolbarActions.style.display="block",this.keepToolbarAlive=!1,clearTimeout(a),a=setTimeout(function(){b.addEventListener("click",function(){c.keepToolbarAlive=!1,c.toolbar.style.display="none",b.removeEventListener("click",this)})},300)},showAnchorForm:function(){var a=this.anchorForm.querySelector("input");this.toolbarActions.style.display="none",this.savedSelection=d(),this.anchorForm.style.display="block",this.keepToolbarAlive=!0,a.focus(),a.value=""},bindAnchorForm:function(){var a=this.anchorForm.querySelector("input"),b=this.anchorForm.querySelector("a"),c=this;return this.anchorForm.addEventListener("click",function(a){a.stopPropagation()}),a.addEventListener("keyup",function(a){13===a.keyCode&&(a.preventDefault(),c.createLink(this))}),b.addEventListener("click",function(a){a.preventDefault(),c.showToolbarActions(),e(c.savedSelection)}),this},createLink:function(a){e(this.savedSelection),b.execCommand("CreateLink",!1,a.value),this.showToolbarActions(),a.value=""},bindWindowActions:function(){var b,c=this;return a.addEventListener("resize",function(){clearTimeout(b),b=setTimeout(function(){c.setToolbarPosition()},100)}),this},activate:function(){var a;if(!this.isActive){for(this.isActive=!0,a=0;a")))};for(a=0;ab;b+=1)d.push(e.getRangeAt(b));return d}return null}function e(b){var c,d,e=a.getSelection();if(b)for(e.removeAllRanges(),c=0,d=b.length;d>c;c+=1)e.addRange(b[c])}function f(){var a=b.getSelection().anchorNode,c=a&&3===a.nodeType?a.parentNode:a;return c}MediumEditor.prototype={defaults:{anchorInputPlaceholder:"Paste or type a link",delay:0,diffLeft:0,diffTop:-10,disableReturn:!1,disableToolbar:!1,excludedActions:[],firstHeader:"h3",forcePlainText:!0,placeholder:"Type your text",secondHeader:"h4"},init:function(a,d){return this.elements="string"==typeof a?b.querySelectorAll(a):a,0!==this.elements.length?(this.isActive=!0,this.parentElements=["p","h1","h2","h3","h4","h5","h6","blockquote"],this.id=b.querySelectorAll(".medium-editor-toolbar").length+1,this.options=c(d,this.defaults),this.initElements().bindPaste().setPlaceholders().bindWindowActions()):void 0},initElements:function(){var a;for(a=0;a
  • '+'
  • '+'
  • '+""+'
    '+' ×'+"
    "},initToolbar:function(){return this.toolbar=this.createToolbar(),this.keepToolbarAlive=!1,this.anchorForm=this.toolbar.querySelector(".medium-editor-toolbar-form-anchor"),this.toolbarActions=this.toolbar.querySelector(".medium-editor-toolbar-actions"),this},createToolbar:function(){var a=b.createElement("div");return a.id="medium-editor-toolbar-"+this.id,a.className="medium-editor-toolbar",a.innerHTML=this.toolbarTemplate(),b.getElementsByTagName("body")[0].appendChild(a),a},bindSelect:function(){var a,b=this,c="";for(this.checkSelectionWrapper=function(a){clearTimeout(c),setTimeout(function(){b.checkSelection(a)},b.options.delay)},a=0;ag?f+h+"px":a.innerWidth-g-1?"none":"block"},checkActiveButtons:function(){var a=this.selection.anchorNode;for(a.tagName||(a=this.selection.anchorNode.parentNode);void 0!==a.tagName&&-1===this.parentElements.indexOf(a.tagName);)this.activateButton(a.tagName.toLowerCase()),a=a.parentNode},activateButton:function(a){var b=this.toolbar.querySelector('[data-element="'+a+'"]');null!==b&&-1===b.className.indexOf("medium-editor-button-active")&&(b.className+=" medium-editor-button-active")},bindButtons:function(){var a,b=this.toolbar.querySelectorAll("button"),c=this,d=function(a){a.preventDefault(),a.stopPropagation(),void 0===c.selection&&c.checkSelection(a),this.className.indexOf("medium-editor-button-active")>-1?this.classList.remove("medium-editor-button-active"):this.className+=" medium-editor-button-active",c.execAction(this.getAttribute("data-action"),a)};for(a=0;a-1?(this.execFormatBlock(a.replace("append-","")),this.setToolbarPosition(),this.setToolbarButtonStates()):"anchor"===a?this.triggerAnchorAction(c):(b.execCommand(a,null,!1),this.setToolbarPosition())},triggerAnchorAction:function(){return"a"===this.selection.anchorNode.parentNode.tagName.toLowerCase()?b.execCommand("unlink",null,!1):"block"===this.anchorForm.style.display?this.showToolbarActions():this.showAnchorForm(),this},execFormatBlock:function(a){var c=this.getSelectionData(this.selection.anchorNode);return"blockquote"===a&&"blockquote"===c.el.parentNode.tagName.toLowerCase()?b.execCommand("outdent",!1,null):(c.tagName===a&&(a="p"),b.execCommand("formatBlock",!1,a))},getSelectionData:function(a){var b;for(a&&a.tagName&&(b=a.tagName.toLowerCase());a&&-1===this.parentElements.indexOf(b);)a=a.parentNode,a&&a.tagName&&(b=a.tagName.toLowerCase());return{el:a,tagName:b}},getFirstChild:function(a){for(var b=a.firstChild;null!==b&&1!==b.nodeType;)b=b.nextSibling;return b},bindElementToolbarEvents:function(a){var b=this;a.addEventListener("mouseup",function(a){b.checkSelection(a)}),a.addEventListener("keyup",function(a){b.checkSelection(a)})},showToolbarActions:function(){var a,c=this,d=function(){c.keepToolbarAlive=!1,c.toolbar.style.display="none",b.removeEventListener("click",d)};this.anchorForm.style.display="none",this.toolbarActions.style.display="block",this.keepToolbarAlive=!1,clearTimeout(a),a=setTimeout(function(){b.addEventListener("click",d)},300)},showAnchorForm:function(){var a=this.anchorForm.querySelector("input");this.toolbarActions.style.display="none",this.savedSelection=d(),this.anchorForm.style.display="block",this.keepToolbarAlive=!0,a.focus(),a.value=""},bindAnchorForm:function(){var a=this.anchorForm.querySelector("input"),b=this.anchorForm.querySelector("a"),c=this;return this.anchorForm.addEventListener("click",function(a){a.stopPropagation()}),a.addEventListener("keyup",function(a){13===a.keyCode&&(a.preventDefault(),c.createLink(this))}),b.addEventListener("click",function(a){a.preventDefault(),c.showToolbarActions(),e(c.savedSelection)}),this},createLink:function(a){e(this.savedSelection),b.execCommand("createLink",!1,a.value),this.showToolbarActions(),a.value=""},bindWindowActions:function(){var b,c=this;return a.addEventListener("resize",function(){clearTimeout(b),b=setTimeout(function(){c.setToolbarPosition()},100)}),this},activate:function(){var a;if(!this.isActive){for(this.isActive=!0,a=0;a")))};for(a=0;a