mirror of
https://gitlab.com/buildfeed/BuildFeed.git
synced 2024-03-22 21:10:34 +08:00
2 lines
15 KiB
JavaScript
2 lines
15 KiB
JavaScript
/** Trumbowyg v2.0.0-beta.2 - A lightweight WYSIWYG editor - alex-d.github.io/Trumbowyg - License MIT - Author : Alexandre Demode (Alex-D) / alex-d.fr */
|
|
jQuery.trumbowyg={langs:{en:{viewHTML:"View HTML",formatting:"Formatting",p:"Paragraph",blockquote:"Quote",code:"Code",header:"Header",bold:"Bold",italic:"Italic",strikethrough:"Stroke",underline:"Underline",strong:"Strong",em:"Emphasis",del:"Deleted",unorderedList:"Unordered list",orderedList:"Ordered list",insertImage:"Insert Image",insertVideo:"Insert Video",link:"Link",createLink:"Insert link",unlink:"Remove link",justifyLeft:"Align Left",justifyCenter:"Align Center",justifyRight:"Align Right",justifyFull:"Align Justify",horizontalRule:"Insert horizontal rule",removeformat:"Remove format",fullscreen:"fullscreen",close:"Close",submit:"Confirm",reset:"Cancel",required:"Required",description:"Description",title:"Title",text:"Text"}},opts:{},btnsGrps:{design:["bold","italic","underline","strikethrough"],semantic:["strong","em","del"],justify:["justifyLeft","justifyCenter","justifyRight","justifyFull"],lists:["unorderedList","orderedList"]}},function(e,t,n,o,i){"use strict";o.fn.trumbowyg=function(e,t){if(e===Object(e)||!e)return this.each(function(){o(this).data("trumbowyg")||o(this).data("trumbowyg",new r(this,e))});if(1===this.length)try{var n=o(this).data("trumbowyg");switch(e){case"openModal":return n.openModal(t.title,t.content);case"closeModal":return n.closeModal();case"openModalInsert":return n.openModalInsert(t.title,t.fields,t.callback);case"saveSelection":return n.saveSelection();case"getSelection":return n.selection;case"getSelectedText":return n.getSelectedText();case"restoreSelection":return n.restoreSelection();case"destroy":return n.destroy();case"empty":return n.empty();case"lang":return n.lang;case"html":return n.html(t)}}catch(i){}return!1};var r=function(e,t){var i=this;i.doc=e.ownerDocument||n,i.$ta=o(e),i.$c=o(e),t=o.extend(!0,{},t,o.trumbowyg.opts),i.lang="undefined"==typeof t.lang||"undefined"==typeof o.trumbowyg.langs[t.lang]?o.trumbowyg.langs.en:o.extend(!0,{},o.trumbowyg.langs.en,o.trumbowyg.langs[t.lang]);var r=i.lang.header;i.o=o.extend(!0,{},{lang:"en",dir:"ltr",closable:!1,fullscreenable:!0,fixedBtnPane:!1,fixedFullWidth:!1,autogrow:!1,prefix:"trumbowyg-",semantic:!0,resetCss:!1,removeformatPasted:!1,btns:["viewHTML","|","formatting","|","btnGrp-design","|","link","|","insertImage","|","btnGrp-justify","|","btnGrp-lists","|","horizontalRule","|","removeformat"],btnsAdd:[],btnsDef:{viewHTML:{func:"toggle"},p:{func:"formatBlock"},blockquote:{func:"formatBlock"},h1:{func:"formatBlock",title:r+" 1"},h2:{func:"formatBlock",title:r+" 2"},h3:{func:"formatBlock",title:r+" 3"},h4:{func:"formatBlock",title:r+" 4"},bold:{key:"B"},italic:{key:"I"},underline:{},strikethrough:{},strong:{func:"bold",key:"B"},em:{func:"italic",key:"I"},del:{func:"strikethrough"},createLink:{key:"K"},unlink:{},insertImage:{},justifyLeft:{},justifyCenter:{},justifyRight:{},justifyFull:{},unorderedList:{func:"insertUnorderedList"},orderedList:{func:"insertOrderedList"},horizontalRule:{func:"insertHorizontalRule"},removeformat:{},formatting:{dropdown:["p","blockquote","h1","h2","h3","h4"]},link:{dropdown:["createLink","unlink"]}}},t),t.btns?i.o.btns=t.btns:i.o.semantic&&(i.o.btns[4]="btnGrp-semantic"),i.keys=[],i.init()};r.prototype={init:function(){var e=this;e.height=e.$ta.height(),e.buildEditor(),e.buildBtnPane(),e.fixedBtnPaneEvents(),e.buildOverlay()},buildEditor:function(){var e=this,i=e.o.prefix,r="";e.$box=o("<div/>",{"class":i+"box "+i+"editor-visible "+i+e.o.lang+" trumbowyg"}),e.isTextarea=e.$ta.is("textarea"),e.isTextarea?(r=e.$ta.val(),e.$ed=o("<div/>"),e.$box.insertAfter(e.$ta).append(e.$ed,e.$ta)):(e.$ed=e.$ta,r=e.$ed.html(),e.$ta=o("<textarea/>",{name:e.$ta.attr("id"),height:e.height}).val(r),e.$box.insertAfter(e.$ed).append(e.$ta,e.$ed),e.syncCode()),e.$ta.addClass(i+"textarea").attr("tabindex",-1),e.$ed.addClass(i+"editor").attr({contenteditable:!0,dir:e.lang._dir||e.o.dir}).html(r),e.$c.is("[placeholder]")&&e.$ed.attr("placeholder",e.$c.attr("placeholder")),e.o.resetCss&&e.$ed.addClass(i+"reset-css"),e.o.autogrow||e.$ta.add(e.$ed).css({height:e.height,overflow:"auto"}),e.o.semantic&&(e.$ed.html(r.replace("<br>","</p><p>").replace(" "," ")),e.semanticCode()),e._ctrl=!1,e.$ed.on("dblclick","img",function(){var t=o(this);return e.openModalInsert(e.lang.insertImage,{url:{label:"URL",value:t.attr("src"),required:!0},alt:{label:e.lang.description,value:t.attr("alt")}},function(e){return t.attr({src:e.url,alt:e.alt})}),!1}).on("keydown",function(t){if(e._composition=229===t.which,t.ctrlKey){e._ctrl=!0;var n=e.keys[String.fromCharCode(t.which).toUpperCase()];try{return e.execCmd(n.func,n.param),!1}catch(t){}}}).on("keyup",function(t){e._ctrl||17===t.which||e._composition||(e.semanticCode(!1,13===t.which),e.$c.trigger("tbwchange")),setTimeout(function(){e._ctrl=!1},200)}).on("focus",function(){e.$c.trigger("tbwfocus")}).on("blur",function(){e.syncCode(),e.$c.trigger("tbwblur")}).on("paste",function(o){if(e.$c.trigger("tbwpaste",o),e.o.removeformatPasted){o.preventDefault();try{var i=t.clipboardData.getData("Text");try{e.doc.selection.createRange().pasteHTML(i)}catch(r){e.doc.getSelection().getRangeAt(0).insertNode(n.createTextNode(i))}}catch(r){e.execCmd("insertText",(o.originalEvent||o).clipboardData.getData("text/plain"))}}}),o(e.doc).on("keydown",function(t){return 27===t.which?(e.closeModal(),!1):void 0})},buildBtnPane:function(){var e=this,n=e.o.prefix;if(e.o.btns!==!1){e.$btnPane=o("<ul/>",{"class":n+"button-pane"}),o.each(e.o.btns.concat(e.o.btnsAdd),function(t,r){try{var a=r.split("btnGrp-");a[1]!==i&&(r=o.trumbowyg.btnsGrps[a[1]])}catch(s){}o.isArray(r)||(r=[r]),o.each(r,function(t,i){try{var r=o("<li/>");"|"===i?r.addClass(n+"separator"):e.isSupportedBtn(i)&&r.append(e.buildBtn(i)),e.$btnPane.append(r)}catch(a){}})});var r=o("<li/>",{"class":n+"not-disable "+n+"buttons-right"});e.o.fullscreenable&&r.append(e.buildRightBtn("fullscreen").on("click",function(){var i=n+"fullscreen";e.$box.toggleClass(i),e.$box.hasClass(i)?(o("body").addClass(n+"body-fullscreen"),o.each([e.$ta,e.$ed],function(){o(this).css({height:"calc(100% - 35px)",overflow:"auto"})}),e.$btnPane.css("width","100%")):(o("body").removeClass(n+"body-fullscreen"),e.$box.removeAttr("style"),e.o.autogrow||o.each([e.$ta,e.$ed],function(){o(this).css("height",e.height)})),o(t).trigger("scroll")})),e.o.closable&&r.append(e.buildRightBtn("close").on("click",function(){e.$box.hasClass(n+"fullscreen")&&o("body").css("overflow","auto"),e.destroy(),e.$c.trigger("tbwclose")})),r.not(":empty")&&e.$btnPane.append(r),e.$box.prepend(e.$btnPane)}},buildBtn:function(e){var t=this,n=t.o.prefix,i=t.o.btnsDef[e],r=i.dropdown,a=t.lang[e]||e,s=o("<button/>",{type:"button","class":n+e+"-button"+(i.ico?" "+n+i.ico+"-button":""),text:i.text||i.title||a,title:i.title||i.text||a+(i.key?" (Ctrl + "+i.key+")":""),tabindex:-1,mousedown:function(){return(!r||o("."+e+"-"+n+"dropdown",t.$box).is(":hidden"))&&o("body",t.doc).trigger("mousedown"),!t.$btnPane.hasClass(n+"disable")||o(this).hasClass(n+"active")||o(this).parent().hasClass(n+"not-disable")?(t.execCmd((r?"dropdown":!1)||i.func||e,i.param||e),!1):!1}});if(r){s.addClass(n+"open-dropdown");var l=n+"dropdown",c=o("<div/>",{"class":e+"-"+l+" "+l+" "+n+"fixed-top"});o.each(r,function(e,n){t.o.btnsDef[n]&&t.isSupportedBtn(n)&&c.append(t.buildSubBtn(n))}),t.$box.append(c.hide())}else i.key&&(t.keys[i.key]={func:i.func||e,param:i.param||e});return s},buildSubBtn:function(e){var t=this,n=t.o.btnsDef[e];return n.key&&(t.keys[n.key]={func:n.func||e,param:n.param||e}),o("<button/>",{type:"button","class":t.o.prefix+e+"-dropdown-button"+(n.ico?" "+t.o.prefix+n.ico+"-button":""),text:n.text||n.title||t.lang[e]||e,title:n.key?" (Ctrl + "+n.key+")":null,style:n.style||null,mousedown:function(){return o("body",t.doc).trigger("mousedown"),t.execCmd(n.func||e,n.param||e),!1}})},buildRightBtn:function(e){var t=this.lang[e];return o("<button/>",{type:"button","class":this.o.prefix+e+"-button",title:t,text:t,tabindex:-1})},isSupportedBtn:function(e){try{return this.o.btnsDef[e].isSupported()}catch(t){}return!0},buildOverlay:function(){var e=this;return e.$overlay=o("<div/>",{"class":e.o.prefix+"overlay"}).css({top:e.$btnPane.outerHeight(),height:e.$ed.outerHeight()+1+"px"}).appendTo(e.$box),e.$overlay},showOverlay:function(){var e=this;o(t).trigger("scroll"),e.$overlay.fadeIn(200),e.$box.addClass(e.o.prefix+"box-blur")},hideOverlay:function(){var e=this;e.$overlay.fadeOut(50),e.$box.removeClass(e.o.prefix+"box-blur")},fixedBtnPaneEvents:function(){var e=this,n=e.o.fixedFullWidth,i=e.$box;e.o.fixedBtnPane&&(e.isFixed=!1,o(t).on("scroll resize",function(){if(i){e.syncCode();var r=o(t).scrollTop(),a=i.offset().top+1,s=e.$btnPane,l=s.outerHeight();r-a>0&&r-a-e.height<0?(e.isFixed||(e.isFixed=!0,s.css({position:"fixed",top:0,left:n?"0":"auto",zIndex:7}),o([e.$ta,e.$ed]).css({marginTop:s.height()})),s.css({width:n?"100%":i.width()-1+"px"}),o("."+e.o.prefix+"fixed-top",i).css({position:n?"fixed":"absolute",top:n?l:l+(r-a)+"px",zIndex:15})):e.isFixed&&(e.isFixed=!1,s.removeAttr("style"),o([e.$ta,e.$ed]).css({marginTop:0}),o("."+e.o.prefix+"fixed-top",i).css({position:"absolute",top:l}))}}))},destroy:function(){var e=this,t=e.o.prefix,n=e.height,o=e.html();e.isTextarea?e.$box.after(e.$ta.css({height:n}).val(o).removeClass(t+"textarea").show()):e.$box.after(e.$ed.css({height:n}).removeClass(t+"editor").removeAttr("contenteditable").html(o).show()),e.$box.remove(),e.$c.removeData("trumbowyg")},empty:function(){this.$ta.val(""),this.syncCode(!0)},toggle:function(){var e=this,t=e.o.prefix;e.semanticCode(!1,!0),e.$box.toggleClass(t+"editor-hidden "+t+"editor-visible"),e.$btnPane.toggleClass(t+"disable"),o("."+t+"viewHTML-button",e.$btnPane).toggleClass(t+"active"),e.$box.hasClass(t+"editor-visible")?e.$ta.attr("tabindex",-1):e.$ta.removeAttr("tabindex")},dropdown:function(e){var n=this,i=n.doc,r=n.o.prefix,a=o("."+e+"-"+r+"dropdown",n.$box),s=o("."+r+e+"-button",n.$btnPane);if(a.is(":hidden")){var l=s.offset().left;s.addClass(r+"active"),a.css({position:"absolute",top:n.$btnPane.outerHeight(),left:n.o.fixedFullWidth&&n.isFixed?l+"px":l-n.$btnPane.offset().left+"px"}).show(),o(t).trigger("scroll"),o("body",i).on("mousedown",function(){o("."+r+"dropdown",i).hide(),o("."+r+"active",i).removeClass(r+"active"),o("body",i).off("mousedown")})}else o("body",i).trigger("mousedown")},html:function(e){var t=this;return e?(t.$ta.val(e),t.syncCode(!0),t):t.$ta.val()},syncCode:function(e){var t=this;!e&&t.$ed.is(":visible")?t.$ta.val(t.$ed.html()):t.$ed.html(t.$ta.val()),t.o.autogrow&&(t.height=t.$ed.height(),t.height!=t.$ta.css("height")&&(t.$ta.css({height:t.height}),t.$c.trigger("tbwresize")))},semanticCode:function(e,t){var n=this;n.syncCode(e),n.o.semantic&&(n.saveSelection(),n.semanticTag("b","strong"),n.semanticTag("i","em"),n.semanticTag("strike","del"),t&&(n.$ed.contents().filter(function(){return 3===this.nodeType&&o.trim(this.nodeValue).length>0}).wrap("<p></p>").end().filter("br").remove(),n.semanticTag("div","p")),n.$ta.val(n.$ed.html()),n.restoreSelection())},semanticTag:function(e,t){o(e,this.$ed).each(function(){o(this).replaceWith(function(){return"<"+t+">"+o(this).html()+"</"+t+">"})})},createLink:function(){var e=this;e.saveSelection(),e.openModalInsert(e.lang.createLink,{url:{label:"URL",required:!0},title:{label:e.lang.title,value:e.getSelectedText()},text:{label:e.lang.text,value:e.getSelectedText()}},function(t){e.execCmd("createLink",t.url);var n=o('a[href="'+t.url+'"]:not([title])',e.$box);return t.text.length>0&&n.text(t.text),t.title.length>0&&n.attr("title",t.title),!0})},insertImage:function(){var e=this;e.saveSelection(),e.openModalInsert(e.lang.insertImage,{url:{label:"URL",required:!0},alt:{label:e.lang.description,value:e.getSelectedText()}},function(t){return e.execCmd("insertImage",t.url),o('img[src="'+t.url+'"]:not([alt])',e.$box).attr("alt",t.alt),!0})},execCmd:function(t,n){var o=this;"dropdown"!=t&&o.$ed.focus();try{o[t](n)}catch(i){try{t(n,o)}catch(r){"insertHorizontalRule"==t?n=null:"formatBlock"==t&&(-1!==e.userAgent.indexOf("MSIE")||e.appVersion.indexOf("Trident/")>0)&&(n="<"+n+">"),o.doc.execCommand(t,!1,n)}}o.syncCode()},openModal:function(e,n){var i=this,r=i.o.prefix;if(o("."+r+"modal-box",i.$box).length>0)return!1;i.saveSelection(),i.showOverlay(),i.$btnPane.addClass(r+"disable");var a=o("<div/>",{"class":r+"modal "+r+"fixed-top"}).css({top:i.$btnPane.height()+1+"px"}).appendTo(i.$box);i.$overlay.one("click",function(){return a.trigger(r+"cancel"),!1});var s=o("<form/>",{action:"",html:n}).on("submit",function(){return a.trigger(r+"confirm"),!1}).on("reset",function(){return a.trigger(r+"cancel"),!1}),l=o("<div/>",{"class":r+"modal-box",html:s}).css({top:"-"+i.$btnPane.outerHeight()+"px",opacity:0}).appendTo(a).animate({top:0,opacity:1},100);return o("<span/>",{text:e,"class":r+"modal-title"}).prependTo(l),o("input:first",l).focus(),i.buildModalBtn("submit",l),i.buildModalBtn("reset",l),o(t).trigger("scroll"),a},buildModalBtn:function(e,t){var n=this,i=n.o.prefix;return o("<button/>",{"class":i+"modal-button "+i+"modal-"+e,type:e,text:n.lang[e]||e}).appendTo(o("form",t))},closeModal:function(){var e=this,t=e.o.prefix;e.$btnPane.removeClass(t+"disable"),e.$overlay.off();var n=o("."+t+"modal-box",e.$box);n.animate({top:"-"+n.height()},100,function(){n.parent().remove(),e.hideOverlay()}),e.restoreSelection()},openModalInsert:function(e,t,n){var i=this,r=i.o.prefix,a=i.lang,s="";for(var l in t){var c=t[l],d=c.label,u=c.name?c.name:l;s+='<label><input type="'+(c.type||"text")+'" name="'+u+'" value="'+(c.value||"")+'"><span class="'+r+'input-infos"><span>'+(d?a[d]?a[d]:d:a[l]?a[l]:l)+"</span></span></label>"}return i.openModal(e,s).on(r+"confirm",function(){var e=o("form",o(this)),a=!0,s={};for(var l in t){var c=o('input[name="'+l+'"]',e);s[l]=o.trim(c.val()),t[l].required&&""===s[l]?(a=!1,i.addErrorOnModalField(c,i.lang.required)):t[l].pattern&&!t[l].pattern.test(s[l])&&(a=!1,i.addErrorOnModalField(c,t[l].patternError))}a&&(i.restoreSelection(),n(s,t)&&(i.syncCode(),i.closeModal(),o(this).off(r+"confirm")))}).one(r+"cancel",function(){o(this).off(r+"confirm"),i.closeModal()})},addErrorOnModalField:function(e,t){var n=this.o.prefix,i=e.parent();e.on("change keyup",function(){i.removeClass(n+"input-error")}),i.addClass(n+"input-error").find("input+span").append(o("<span/>",{"class":n+"msg-error",text:t}))},saveSelection:function(){var e=this,n=e.doc.selection;if(e.selection=null,t.getSelection){var o=t.getSelection();o.getRangeAt&&o.rangeCount&&(e.selection=o.getRangeAt(0))}else n&&n.createRange&&(e.selection=n.createRange())},restoreSelection:function(){var e=this,n=e.selection;if(n)if(t.getSelection){var o=t.getSelection();o.removeAllRanges(),o.addRange(n)}else e.doc.selection&&n.select&&n.select()},getSelectedText:function(){var e=this.selection;return e.text!==i?e.text:e+""}}}(navigator,window,document,jQuery); |