From 40f0d176e69f55dc415c75519c2f5617da6a0add Mon Sep 17 00:00:00 2001 From: Steve King Date: Wed, 15 Aug 2018 16:28:45 +1000 Subject: [PATCH 1/2] Added cleanHTML Thanks to @floriansegginger I've added the cleanHTML support. --- src/bootstrap-wysiwyg.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/bootstrap-wysiwyg.js b/src/bootstrap-wysiwyg.js index 8d7f214..2de215b 100644 --- a/src/bootstrap-wysiwyg.js +++ b/src/bootstrap-wysiwyg.js @@ -349,9 +349,14 @@ * @constructor * @param {object} userOptions - The default options selected by the user. */ - - $.fn.wysiwyg = function( userOptions ) { + + $.fn.wysiwyg = function( userOptions ) { var wysiwyg = new Wysiwyg( this, userOptions ); + return this; + }; + + $.fn.cleanHtml = function() { + return Wysiwyg.prototype.cleanHtml.apply(this); }; } )( window, window.jQuery ); From 6b36bc32b01b50c9206025e962c5685d3d7a86c4 Mon Sep 17 00:00:00 2001 From: Steve King Date: Wed, 15 Aug 2018 16:29:58 +1000 Subject: [PATCH 2/2] Update bootstrap-wysiwyg.min.js --- js/bootstrap-wysiwyg.min.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/bootstrap-wysiwyg.min.js b/js/bootstrap-wysiwyg.min.js index 6211a3e..e0c94f3 100644 --- a/js/bootstrap-wysiwyg.min.js +++ b/js/bootstrap-wysiwyg.min.js @@ -3,8 +3,8 @@ * * Name = bootstrap-wysiwyg * Author = Various, see LICENCE - * Version = v2.0.1 + * Version = v2.0.2 * About = A tiny Bootstrap and jQuery based WYSIWYG rich text editor based on the browser function execCommand. */ -!function(a,b){"use strict";function c(c,d){this.selectedRange=null,this.editor=b(c);var e=b(c),f={hotKeys:{"Ctrl+b meta+b":"bold","Ctrl+i meta+i":"italic","Ctrl+u meta+u":"underline","Ctrl+z":"undo","Ctrl+y meta+y meta+shift+z":"redo","Ctrl+l meta+l":"justifyleft","Ctrl+r meta+r":"justifyright","Ctrl+e meta+e":"justifycenter","Ctrl+j meta+j":"justifyfull","Shift+tab":"outdent",tab:"indent"},toolbarSelector:"[data-role=editor-toolbar]",commandRole:"edit",activeToolbarClass:"btn-info",selectionMarker:"edit-focus-marker",selectionColor:"darkgrey",dragAndDropImages:!0,keypressTimeout:200,fileUploadError:function(a,b){console.log("File upload error",a,b)}},g=b.extend(!0,{},f,d),h="a[data-"+g.commandRole+"],button[data-"+g.commandRole+"],input[type=button][data-"+g.commandRole+"]";this.bindHotkeys(e,g,h),g.dragAndDropImages&&this.initFileDrops(e,g,h),this.bindToolbar(e,b(g.toolbarSelector),g,h),e.attr("contenteditable",!0).on("mouseup keyup mouseout",function(){this.saveSelection(),this.updateToolbar(e,h,g)}.bind(this)),b(a).bind("touchend",function(a){var b=e.is(a.target)||e.has(a.target).length>0,c=this.getCurrentRange(),d=c&&c.startContainer===c.endContainer&&c.startOffset===c.endOffset;(!d||b)&&(this.saveSelection(),this.updateToolbar(e,h,g))})}c.prototype.readFileIntoDataUrl=function(a){var c=b.Deferred(),d=new FileReader;return d.onload=function(a){c.resolve(a.target.result)},d.onerror=c.reject,d.onprogress=c.notify,d.readAsDataURL(a),c.promise()},c.prototype.cleanHtml=function(a){var c=this;if(b(c).data("wysiwyg-html-mode")===!0&&(b(c).html(b(c).text()),b(c).attr("contenteditable",!0),b(c).data("wysiwyg-html-mode",!1)),a===!0&&b(c).parent().is("form")){var d=b(c).html;if(b(d).has("img").length){var e=b("img",b(d)),f=[],g=b(c).parent();b.each(e,function(a,c){b(c).attr("src").match(/^data:image\/.*$/)&&(f.push(e[a]),b(g).prepend(""),b(c).attr("src","postedimage/"+a))})}}var h=b(c).html();return h&&h.replace(/(
|\s|

<\/div>| )*$/,"")},c.prototype.updateToolbar=function(a,c,d){d.activeToolbarClass&&b(d.toolbarSelector).find(c).each(function(){var a=b(this),c=a.data(d.commandRole).split(" "),e=c[0];c.length>1&&document.queryCommandEnabled(e)&&document.queryCommandValue(e)===c[1]?a.addClass(d.activeToolbarClass):1===c.length&&document.queryCommandEnabled(e)&&document.queryCommandState(e)?a.addClass(d.activeToolbarClass):a.removeClass(d.activeToolbarClass)})},c.prototype.execCommand=function(a,b,c,d,e){var f=a.split(" "),g=f.shift(),h=f.join(" ")+(b||""),i=a.split("-");1===i.length?document.execCommand(g,!1,h):"format"===i[0]&&2===i.length&&document.execCommand("formatBlock",!1,i[1]),c.trigger("change"),this.updateToolbar(c,e,d)},c.prototype.bindHotkeys=function(a,c,d){var e=this;b.each(c.hotKeys,function(f,g){b(a).keydown(f,function(f){a.attr("contenteditable")&&b(a).is(":visible")&&(f.preventDefault(),f.stopPropagation(),e.execCommand(g,null,a,c,d))}).keyup(f,function(c){a.attr("contenteditable")&&b(a).is(":visible")&&(c.preventDefault(),c.stopPropagation())})}),a.keyup(function(){a.trigger("change")})},c.prototype.getCurrentRange=function(){var b,c;return a.getSelection?(b=a.getSelection(),b.getRangeAt&&b.rangeCount&&(c=b.getRangeAt(0))):document.selection&&(c=document.selection.createRange()),c},c.prototype.saveSelection=function(){this.selectedRange=this.getCurrentRange()},c.prototype.restoreSelection=function(){var b;if(a.getSelection||document.createRange){if(b=a.getSelection(),this.selectedRange){try{b.removeAllRanges()}catch(c){document.body.createTextRange().select(),document.selection.empty()}b.addRange(this.selectedRange)}}else document.selection&&this.selectedRange&&this.selectedRange.select()},c.prototype.toggleHtmlEdit=function(a){if(a.data("wysiwyg-html-mode")!==!0){var c=a.html(),d=b("
");b(d).append(document.createTextNode(c)),b(d).attr("contenteditable",!0),b(a).html(" "),b(a).append(b(d)),b(a).attr("contenteditable",!1),b(a).data("wysiwyg-html-mode",!0),b(d).focus()}else b(a).html(b(a).text()),b(a).attr("contenteditable",!0),b(a).data("wysiwyg-html-mode",!1),b(a).focus()},c.prototype.insertFiles=function(a,c,d,e){var f=this;d.focus(),b.each(a,function(a,g){/^image\//.test(g.type)?b.when(f.readFileIntoDataUrl(g)).done(function(a){f.execCommand("insertimage",a,d,c,e),d.trigger("image-inserted")}).fail(function(a){c.fileUploadError("file-reader",a)}):c.fileUploadError("unsupported-file-type",g.type)})},c.prototype.markSelection=function(a,b,c){this.restoreSelection(),document.queryCommandSupported("hiliteColor")&&document.execCommand("hiliteColor",!1,b||"transparent"),this.saveSelection(),a.data(c.selectionMarker,b)},c.prototype.bindToolbar=function(a,c,d,e){var f=this;c.find(e).click(function(){f.restoreSelection(),a.focus(),"html"===a.data(d.commandRole)?f.toggleHtmlEdit(a):f.execCommand(b(this).data(d.commandRole),null,a,d,e),f.saveSelection()}),c.find("[data-toggle=dropdown]").click(this.restoreSelection()),c.find("input[type=text][data-"+d.commandRole+"]").on("webkitspeechchange change",function(){var c=this.value;this.value="",f.restoreSelection(),c&&(a.focus(),f.execCommand(b(this).data(d.commandRole),c,a,d,e)),f.saveSelection()}).on("focus",function(){var a=b(this);a.data(d.selectionMarker)||(f.markSelection(a,d.selectionColor,d),a.focus())}).on("blur",function(){var a=b(this);a.data(d.selectionMarker)&&f.markSelection(a,!1,d)}),c.find("input[type=file][data-"+d.commandRole+"]").change(function(){f.restoreSelection(),"file"===this.type&&this.files&&this.files.length>0&&f.insertFiles(this.files,d,a,e),f.saveSelection(),this.value=""})},c.prototype.initFileDrops=function(a,b,c){var d=this;a.on("dragenter dragover",!1).on("drop",function(e){var f=e.originalEvent.dataTransfer;e.stopPropagation(),e.preventDefault(),f&&f.files&&f.files.length>0&&d.insertFiles(f.files,b,a,c)})},b.fn.wysiwyg=function(a){new c(this,a)}}(window,window.jQuery);
\ No newline at end of file
+!function(e,t){"use strict";function n(n,o){this.selectedRange=null,this.editor=t(n);var a=t(n),i={hotKeys:{"Ctrl+b meta+b":"bold","Ctrl+i meta+i":"italic","Ctrl+u meta+u":"underline","Ctrl+z":"undo","Ctrl+y meta+y meta+shift+z":"redo","Ctrl+l meta+l":"justifyleft","Ctrl+r meta+r":"justifyright","Ctrl+e meta+e":"justifycenter","Ctrl+j meta+j":"justifyfull","Shift+tab":"outdent",tab:"indent"},toolbarSelector:"[data-role=editor-toolbar]",commandRole:"edit",activeToolbarClass:"btn-info",selectionMarker:"edit-focus-marker",selectionColor:"darkgrey",dragAndDropImages:!0,keypressTimeout:200,fileUploadError:function(e,t){console.log("File upload error",e,t)}},r=t.extend(!0,{},i,o),l="a[data-"+r.commandRole+"],button[data-"+r.commandRole+"],input[type=button][data-"+r.commandRole+"]";this.bindHotkeys(a,r,l),r.dragAndDropImages&&this.initFileDrops(a,r,l),this.bindToolbar(a,t(r.toolbarSelector),r,l),a.attr("contenteditable",!0).on("mouseup keyup mouseout",function(){this.saveSelection(),this.updateToolbar(a,l,r)}.bind(this)),t(e).bind("touchend",function(e){if(this.getCurrentRange){var t=a.is(e.target)||a.has(e.target).length>0,n=this.getCurrentRange();n&&n.startContainer===n.endContainer&&n.startOffset===n.endOffset&&!t||(this.saveSelection(),this.updateToolbar(a,l,r))}})}n.prototype.readFileIntoDataUrl=function(e){var n=t.Deferred(),o=new FileReader;return o.onload=function(e){n.resolve(e.target.result)},o.onerror=n.reject,o.onprogress=n.notify,o.readAsDataURL(e),n.promise()},n.prototype.cleanHtml=function(e){var n=this;if(!0===t(n).data("wysiwyg-html-mode")&&(t(n).html(t(n).text()),t(n).attr("contenteditable",!0),t(n).data("wysiwyg-html-mode",!1)),!0===e&&t(n).parent().is("form")){var o=t(n).html;if(t(o).has("img").length){var a=t("img",t(o)),i=[],r=t(n).parent();t.each(a,function(e,n){t(n).attr("src").match(/^data:image\/.*$/)&&(i.push(a[e]),t(r).prepend(""),t(n).attr("src","postedimage/"+e))})}}var l=t(n).html();return l&&l.replace(/(
|\s|

<\/div>| )*$/,"")},n.prototype.updateToolbar=function(e,n,o){o.activeToolbarClass&&t(o.toolbarSelector).find(n).each(function(){var e=t(this),n=e.data(o.commandRole).split(" "),a=n[0];n.length>1&&document.queryCommandEnabled(a)&&document.queryCommandValue(a)===n[1]?e.addClass(o.activeToolbarClass):1===n.length&&document.queryCommandEnabled(a)&&document.queryCommandState(a)?e.addClass(o.activeToolbarClass):e.removeClass(o.activeToolbarClass)})},n.prototype.execCommand=function(e,t,n,o,a){var i=e.split(" "),r=i.shift(),l=i.join(" ")+(t||""),s=e.split("-");1===s.length?document.execCommand(r,!1,l):"format"===s[0]&&2===s.length&&document.execCommand("formatBlock",!1,s[1]),n.trigger("change"),this.updateToolbar(n,a,o)},n.prototype.bindHotkeys=function(e,n,o){var a=this;t.each(n.hotKeys,function(i,r){r&&t(e).keydown(i,function(i){e.attr("contenteditable")&&t(e).is(":visible")&&(i.preventDefault(),i.stopPropagation(),a.execCommand(r,null,e,n,o))}).keyup(i,function(n){e.attr("contenteditable")&&t(e).is(":visible")&&(n.preventDefault(),n.stopPropagation())})}),e.keyup(function(){e.trigger("change")})},n.prototype.getCurrentRange=function(){var t,n;return e.getSelection?(t=e.getSelection()).getRangeAt&&t.rangeCount&&(n=t.getRangeAt(0)):document.selection&&(n=document.selection.createRange()),n},n.prototype.saveSelection=function(){this.selectedRange=this.getCurrentRange()},n.prototype.restoreSelection=function(){var t;if(e.getSelection||document.createRange){if(t=e.getSelection(),this.selectedRange){try{t.removeAllRanges()}catch(e){document.body.createTextRange().select(),document.selection.empty()}t.addRange(this.selectedRange)}}else document.selection&&this.selectedRange&&this.selectedRange.select()},n.prototype.toggleHtmlEdit=function(e){if(!0!==e.data("wysiwyg-html-mode")){var n=e.html(),o=t("
");t(o).append(document.createTextNode(n)),t(o).attr("contenteditable",!0),t(e).html(" "),t(e).append(t(o)),t(e).attr("contenteditable",!1),t(e).data("wysiwyg-html-mode",!0),t(o).focus()}else t(e).html(t(e).text()),t(e).attr("contenteditable",!0),t(e).data("wysiwyg-html-mode",!1),t(e).focus()},n.prototype.insertFiles=function(e,n,o,a){var i=this;o.focus(),t.each(e,function(e,r){/^image\//.test(r.type)||/.+\.(bmp|gif|jpg|jpeg|png)$/.test(r.name.toLowerCase())?t.when(i.readFileIntoDataUrl(r)).done(function(e){i.execCommand("insertimage",e,o,n,a),o.trigger("image-inserted")}).fail(function(e){n.fileUploadError("file-reader",e)}):n.fileUploadError("unsupported-file-type",r.type)})},n.prototype.markSelection=function(e,t){this.restoreSelection(),document.queryCommandSupported("hiliteColor")&&document.execCommand("hiliteColor",!1,e||"transparent"),this.saveSelection()},n.prototype.bindToolbar=function(n,o,a,i){var r=this;o.find(i).click(function(){r.restoreSelection(),n.focus(),"html"===n.data(a.commandRole)?r.toggleHtmlEdit(n):r.execCommand(t(this).data(a.commandRole),null,n,a,i),r.saveSelection()}),o.find("[data-toggle=dropdown]").on("click",function(){r.markSelection(a.selectionColor,a)}),o.on("hide.bs.dropdown",function(){r.markSelection(!1,a)}),o.find("input[type=text][data-"+a.commandRole+"]").on("webkitspeechchange change",function(){var o=this.value;this.value="",r.restoreSelection(),""===e.getSelection().toString().trim()&&o&&(r.editor.append(""+o+""),function(t){if(e.getSelection&&document.createRange){var n=e.getSelection(),o=document.createRange();o.selectNodeContents(t),n.removeAllRanges(),n.addRange(o)}else if(document.selection&&document.body.createTextRange){var a=document.body.createTextRange();a.moveToElementText(t),a.select()}}(t("span:last",r.editor)[0])),o&&(n.focus(),r.execCommand(t(this).data(a.commandRole),o,n,a,i)),r.saveSelection()}).on("blur",function(){t(this);r.markSelection(!1,a)}),o.find("input[type=file][data-"+a.commandRole+"]").change(function(){r.restoreSelection(),"file"===this.type&&this.files&&this.files.length>0&&r.insertFiles(this.files,a,n,i),r.saveSelection(),this.value=""})},n.prototype.initFileDrops=function(e,t,n){var o=this;e.on("dragenter dragover",!1).on("drop",function(a){var i=a.originalEvent.dataTransfer;a.stopPropagation(),a.preventDefault(),i&&i.files&&i.files.length>0&&o.insertFiles(i.files,t,e,n)})},t.fn.wysiwyg=function(e){new n(this,e);return this},t.fn.cleanHtml=function(){return n.prototype.cleanHtml.apply(this)}}(window,window.jQuery);