Skip to content

Conversation

@solomax
Copy link
Contributor

@solomax solomax commented Apr 27, 2020

Monkey-patching due to original plugin seems to be abandoned

*/

!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("<input value='"+b(c).attr("src")+"' type='hidden' name='postedimage/"+a+"' />"),b(c).attr("src","postedimage/"+a))})}}var h=b(c).html();return h&&h.replace(/(<br>|\s|<div><br><\/div>|&nbsp;)*$/,"")},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("<pre />");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);
(function(window,$){"use strict";function Wysiwyg(element,userOptions){this.selectedRange=null;this.editor=$(element);var editor=$(element);var defaults={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:true,keypressTimeout:200,fileUploadError:function(reason,detail){console.log("File upload error",reason,detail)}};var options=$.extend(true,{},defaults,userOptions);var toolbarBtnSelector="a[data-"+options.commandRole+"],button[data-"+options.commandRole+"],input[type=button][data-"+options.commandRole+"]";this.bindHotkeys(editor,options,toolbarBtnSelector);if(options.dragAndDropImages){this.initFileDrops(editor,options,toolbarBtnSelector)}this.bindToolbar(editor,$(options.toolbarSelector),options,toolbarBtnSelector);editor.attr("contenteditable",true).on("mouseup keyup mouseout",function(){this.saveSelection();this.updateToolbar(editor,toolbarBtnSelector,options)}.bind(this));var self=this;$(window).bind("touchend",function(e){var isInside=editor.is(e.target)||editor.has(e.target).length>0,currentRange=self.getCurrentRange(),clear=currentRange&&(currentRange.startContainer===currentRange.endContainer&&currentRange.startOffset===currentRange.endOffset);if(!clear||isInside){self.saveSelection();self.updateToolbar(editor,toolbarBtnSelector,options)}})}Wysiwyg.prototype.readFileIntoDataUrl=function(fileInfo){var loader=$.Deferred(),fReader=new FileReader;fReader.onload=function(e){loader.resolve(e.target.result)};fReader.onerror=loader.reject;fReader.onprogress=loader.notify;fReader.readAsDataURL(fileInfo);return loader.promise()};Wysiwyg.prototype.cleanHtml=function(o){var self=this;if($(self).data("wysiwyg-html-mode")===true){$(self).html($(self).text());$(self).attr("contenteditable",true);$(self).data("wysiwyg-html-mode",false)}if(o===true&&$(self).parent().is("form")){var gGal=$(self).html;if($(gGal).has("img").length){var gImages=$("img",$(gGal));var gResults=[];var gEditor=$(self).parent();$.each(gImages,function(i,v){if($(v).attr("src").match(/^data:image\/.*$/)){gResults.push(gImages[i]);$(gEditor).prepend("<input value='"+$(v).attr("src")+"' type='hidden' name='postedimage/"+i+"' />");$(v).attr("src","postedimage/"+i)}})}}var html=$(self).html();return html&&html.replace(/(<br>|\s|<div><br><\/div>|&nbsp;)*$/,"")};Wysiwyg.prototype.updateToolbar=function(editor,toolbarBtnSelector,options){if(options.activeToolbarClass){$(options.toolbarSelector).find(toolbarBtnSelector).each(function(){var self=$(this);var commandArr=self.data(options.commandRole).split(" ");var command=commandArr[0];if(commandArr.length>1&&document.queryCommandEnabled(command)&&document.queryCommandValue(command)===commandArr[1]){self.addClass(options.activeToolbarClass)}else if(commandArr.length===1&&document.queryCommandEnabled(command)&&document.queryCommandState(command)){self.addClass(options.activeToolbarClass)}else{self.removeClass(options.activeToolbarClass)}})}};Wysiwyg.prototype.execCommand=function(commandWithArgs,valueArg,editor,options,toolbarBtnSelector){var commandArr=commandWithArgs.split(" "),command=commandArr.shift(),args=commandArr.join(" ")+(valueArg||"");var parts=commandWithArgs.split("-");if(parts.length===1){document.execCommand(command,false,args)}else if(parts[0]==="format"&&parts.length===2){document.execCommand("formatBlock",false,parts[1])}editor.trigger("change");this.updateToolbar(editor,toolbarBtnSelector,options)};Wysiwyg.prototype.bindHotkeys=function(editor,options,toolbarBtnSelector){var self=this;$.each(options.hotKeys,function(hotkey,command){if(!command)return;$(editor).keydown(hotkey,function(e){if(editor.attr("contenteditable")&&$(editor).is(":visible")){e.preventDefault();e.stopPropagation();self.execCommand(command,null,editor,options,toolbarBtnSelector)}}).keyup(hotkey,function(e){if(editor.attr("contenteditable")&&$(editor).is(":visible")){e.preventDefault();e.stopPropagation()}})});editor.keyup(function(){editor.trigger("change")})};Wysiwyg.prototype.getCurrentRange=function(){var sel,range;if(window.getSelection){sel=window.getSelection();if(sel.getRangeAt&&sel.rangeCount){range=sel.getRangeAt(0)}}else if(document.selection){range=document.selection.createRange()}return range};Wysiwyg.prototype.saveSelection=function(){this.selectedRange=this.getCurrentRange()};Wysiwyg.prototype.restoreSelection=function(){var selection;if(window.getSelection||document.createRange){selection=window.getSelection();if(this.selectedRange){try{selection.removeAllRanges()}catch(ex){document.body.createTextRange().select();document.selection.empty()}selection.addRange(this.selectedRange)}}else if(document.selection&&this.selectedRange){this.selectedRange.select()}};Wysiwyg.prototype.toggleHtmlEdit=function(editor){if(editor.data("wysiwyg-html-mode")!==true){var oContent=editor.html();var editorPre=$("<pre />");$(editorPre).append(document.createTextNode(oContent));$(editorPre).attr("contenteditable",true);$(editor).html(" ");$(editor).append($(editorPre));$(editor).attr("contenteditable",false);$(editor).data("wysiwyg-html-mode",true);$(editorPre).focus()}else{$(editor).html($(editor).text());$(editor).attr("contenteditable",true);$(editor).data("wysiwyg-html-mode",false);$(editor).focus()}};Wysiwyg.prototype.insertFiles=function(files,options,editor,toolbarBtnSelector){var self=this;editor.focus();$.each(files,function(idx,fileInfo){if(/^image\//.test(fileInfo.type)){$.when(self.readFileIntoDataUrl(fileInfo)).done(function(dataUrl){self.execCommand("insertimage",dataUrl,editor,options,toolbarBtnSelector);editor.trigger("image-inserted")}).fail(function(e){options.fileUploadError("file-reader",e)})}else{options.fileUploadError("unsupported-file-type",fileInfo.type)}})};Wysiwyg.prototype.markSelection=function(input,color,options){this.restoreSelection();if(document.queryCommandSupported("hiliteColor")){document.execCommand("hiliteColor",false,color||"transparent")}this.saveSelection();input.data(options.selectionMarker,color)};Wysiwyg.prototype.bindToolbar=function(editor,toolbar,options,toolbarBtnSelector){var self=this;toolbar.find(toolbarBtnSelector).click(function(){self.restoreSelection();editor.focus();if(editor.data(options.commandRole)==="html"){self.toggleHtmlEdit(editor)}else{self.execCommand($(this).data(options.commandRole),null,editor,options,toolbarBtnSelector)}self.saveSelection()});toolbar.find("[data-toggle=dropdown]").click(this.restoreSelection());toolbar.find("input[type=text][data-"+options.commandRole+"]").on("webkitspeechchange change",function(){var newValue=this.value;this.value="";self.restoreSelection();if(newValue){editor.focus();self.execCommand($(this).data(options.commandRole),newValue,editor,options,toolbarBtnSelector)}self.saveSelection()}).on("focus",function(){var input=$(this);if(!input.data(options.selectionMarker)){self.markSelection(input,options.selectionColor,options);input.focus()}}).on("blur",function(){var input=$(this);if(input.data(options.selectionMarker)){self.markSelection(input,false,options)}});toolbar.find("input[type=file][data-"+options.commandRole+"]").change(function(){self.restoreSelection();if(this.type==="file"&&this.files&&this.files.length>0){self.insertFiles(this.files,options,editor,toolbarBtnSelector)}self.saveSelection();this.value=""})};Wysiwyg.prototype.initFileDrops=function(editor,options,toolbarBtnSelector){var self=this;editor.on("dragenter dragover",false).on("drop",function(e){var dataTransfer=e.originalEvent.dataTransfer;e.stopPropagation();e.preventDefault();if(dataTransfer&&dataTransfer.files&&dataTransfer.files.length>0){self.insertFiles(dataTransfer.files,options,editor,toolbarBtnSelector)}})};$.fn.wysiwyg=function(userOptions){var wysiwyg=new Wysiwyg(this,userOptions)}})(window,window.jQuery);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you minimize it manually ? :-) (s/\n//g ?!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martin-g I run uglify from command line and add header manually :(
Will create pr to original repo ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sebfz1
Copy link
Owner

sebfz1 commented May 3, 2020

Thanks Maxim and Martin! :)
I'll do the minimized version, merge and release asap...

@solomax
Copy link
Contributor Author

solomax commented May 3, 2020

Thanks @sebfz1 ! :)
(was sure command line uglify should work as expected :(( )

@sebfz1
Copy link
Owner

sebfz1 commented May 3, 2020

uglifyjs bootstrap-wysiwyg.js -cm -o bootstrap-wysiwyg.min.js
I believe the "mangle" option was missing

@solomax
Copy link
Contributor Author

solomax commented May 3, 2020

Always thought it is something from "Harry Potter" ;)
Thanks again :)

@sebfz1
Copy link
Owner

sebfz1 commented May 3, 2020

Always thought it is something from "Harry Potter" ;)

Haha, was my impression too! :)
Merged as 3a9b264

@sebfz1 sebfz1 closed this May 3, 2020
@solomax
Copy link
Contributor Author

solomax commented May 3, 2020

Hello @sebfz1
When do you plan to deploy maven artifacts? :)

@solomax
Copy link
Contributor Author

solomax commented May 3, 2020

BTW, maybe it worth to cherry-pick this fix to 8.x?

@sebfz1
Copy link
Owner

sebfz1 commented May 4, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants