froala-editor
Version:
The next generation Javascript WYSIWYG HTML rich text editor made by devs for devs. High performance and modern design make it easy to use for developers and loved by users.
7 lines (6 loc) • 6.46 kB
JavaScript
/*!
* froala_editor v3.0.0 (https://www.froala.com/wysiwyg-editor)
* License https://froala.com/wysiwyg-editor/terms/
* Copyright 2014-2019 Froala Labs
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(require("froala-editor")):"function"==typeof define&&define.amd?define(["froala-editor"],t):t(e.FroalaEditor)}(this,function(e){"use strict";e=e&&e.hasOwnProperty("default")?e["default"]:e,Object.assign(e.DEFAULTS,{codeMirror:window.CodeMirror,codeMirrorOptions:{lineNumbers:!0,tabMode:"indent",indentWithTabs:!0,lineWrapping:!0,mode:"text/html",tabSize:2},codeBeautifierOptions:{end_with_newline:!0,indent_inner_html:!0,extra_liners:["p","h1","h2","h3","h4","h5","h6","blockquote","pre","ul","ol","table","dl"],brace_style:"expand",indent_char:"\t",indent_size:1,wrap_line_length:0},codeViewKeepActiveButtons:["fullscreen"]}),e.PLUGINS.codeView=function(d){var c,f,h=d.$;function p(){return d.$box.hasClass("fr-code-view")}function u(){return f?f.getValue():c.val()}function g(){p()&&(f&&f.setSize(null,d.opts.height?d.opts.height:"auto"),d.opts.heightMin||d.opts.height?d.$box.find(".CodeMirror-scroll, .CodeMirror-gutters").css("min-height",d.opts.heightMin||d.opts.height):d.$box.find(".CodeMirror-scroll, .CodeMirror-gutters").css("min-height",""))}var m,b=!1;function v(){p()&&d.events.trigger("blur")}function M(){p()&&b&&d.events.trigger("focus")}function o(e){c||(!function l(){c=h('<textarea class="fr-code" tabIndex="-1">'),d.$wp.append(c),c.attr("dir",d.opts.direction),d.$box.hasClass("fr-basic")||(m=h('<a data-cmd="html" title="Code View" class="fr-command fr-btn html-switch'+(d.helpers.isMobile()?"":" fr-desktop")+'" role="button" tabIndex="-1"><i class="fa fa-code"></i></button>'),d.$box.append(m),d.events.bindClick(d.$box,"a.html-switch",function(){d.events.trigger("commands.before",["html"]),w(!1),d.events.trigger("commands.after",["html"])}));var e=function e(){return!p()};d.events.on("buttons.refresh",e),d.events.on("copy",e,!0),d.events.on("cut",e,!0),d.events.on("paste",e,!0),d.events.on("destroy",x,!0),d.events.on("html.set",function(){p()&&w(!0)}),d.events.on("codeView.update",g),d.events.on("codeView.toggle",function(){d.$box.hasClass("fr-code-view")&&w()}),d.events.on("form.submit",function(){p()&&(d.html.set(u()),d.events.trigger("contentChanged",[],!0))},!0)}(),!f&&d.opts.codeMirror?((f=d.opts.codeMirror.fromTextArea(c.get(0),d.opts.codeMirrorOptions)).on("blur",v),f.on("focus",M)):(d.events.$on(c,"keydown keyup change input",function(){d.opts.height?this.removeAttribute("rows"):(this.rows=1,0===this.value.length?this.style.height="auto":this.style.height=this.scrollHeight+"px")}),d.events.$on(c,"blur",v),d.events.$on(c,"focus",M))),d.undo.saveStep(),d.html.cleanEmptyTags(),d.html.cleanWhiteTags(!0),d.core.hasFocus()&&(d.core.isEmpty()||(d.selection.save(),d.$el.find('.fr-marker[data-type="true"]').first().replaceWith('<span class="fr-tmp fr-sm">F</span>'),d.$el.find('.fr-marker[data-type="false"]').last().replaceWith('<span class="fr-tmp fr-em">F</span>')));var t=d.html.get(!1,!0);d.$el.find("span.fr-tmp").remove(),d.$box.toggleClass("fr-code-view",!0);var r,o,n=!1;if(d.core.hasFocus()&&(n=!0,d.events.disableBlur(),d.$el.blur()),t=(t=t.replace(/<span class="fr-tmp fr-sm">F<\/span>/,"FROALA-SM")).replace(/<span class="fr-tmp fr-em">F<\/span>/,"FROALA-EM"),d.codeBeautifier&&(t=d.codeBeautifier.run(t,d.opts.codeBeautifierOptions)),f){r=t.indexOf("FROALA-SM"),(o=t.indexOf("FROALA-EM"))<r?r=o:o-=9;var s=(t=t.replace(/FROALA-SM/g,"").replace(/FROALA-EM/g,"")).substring(0,r).length-t.substring(0,r).replace(/\n/g,"").length,i=t.substring(0,o).length-t.substring(0,o).replace(/\n/g,"").length;r=t.substring(0,r).length-t.substring(0,t.substring(0,r).lastIndexOf("\n")+1).length,o=t.substring(0,o).length-t.substring(0,t.substring(0,o).lastIndexOf("\n")+1).length,f.setSize(null,d.opts.height?d.opts.height:"auto"),d.opts.heightMin&&d.$box.find(".CodeMirror-scroll").css("min-height",d.opts.heightMin),f.setValue(t),b=!n,f.focus(),b=!0,f.setSelection({line:s,ch:r},{line:i,ch:o}),f.refresh(),f.clearHistory()}else{r=t.indexOf("FROALA-SM"),o=t.indexOf("FROALA-EM")-9,d.opts.heightMin&&c.css("min-height",d.opts.heightMin),d.opts.height&&c.css("height",d.opts.height),d.opts.heightMax&&c.css("max-height",d.opts.height||d.opts.heightMax),c.val(t.replace(/FROALA-SM/g,"").replace(/FROALA-EM/g,"")).trigger("change");var a=h(d.o_doc).scrollTop();b=!n,c.focus(),b=!0,c.get(0).setSelectionRange(r,o),h(d.o_doc).scrollTop(a)}d.$tb.find(".fr-btn-grp > .fr-command, .fr-more-toolbar > .fr-command, .fr-btn-grp > .fr-btn-wrap > .fr-command, .fr-more-toolbar > .fr-btn-wrap > .fr-command").not(e).filter(function(){return d.opts.codeViewKeepActiveButtons.indexOf(h(this).data("cmd"))<0}).addClass("fr-disabled").attr("aria-disabled",!0),e.addClass("fr-active").attr("aria-pressed",!0),!d.helpers.isMobile()&&d.opts.toolbarInline&&d.toolbar.hide()}function w(e){void 0===e&&(e=!p());var t=d.$tb.find('.fr-command[data-cmd="html"]');e?(d.popups.hideAll(),o(t)):(d.$box.toggleClass("fr-code-view",!1),function r(e){var t=u();d.html.set(t),d.$el.blur(),d.$tb.find(".fr-btn-grp > .fr-command, .fr-more-toolbar > .fr-command, .fr-btn-grp > .fr-btn-wrap > .fr-command, .fr-more-toolbar > .fr-btn-wrap > .fr-command").not(e).removeClass("fr-disabled").attr("aria-disabled",!1),e.removeClass("fr-active").attr("aria-pressed",!1),d.selection.setAtStart(d.el),d.selection.restore(),d.placeholder.refresh(),d.undo.saveStep()}(t))}function x(){p()&&w(!1),f&&f.toTextArea(),c.val("").removeData().remove(),c=null,m&&(m.remove(),m=null)}return{_init:function e(){if(d.events.on("focus",function(){d.opts.toolbarContainer&&function t(){var e=d.$tb.find('.fr-command[data-cmd="html"]');p()?(d.$tb.find(".fr-btn-grp > .fr-command, .fr-more-toolbar > .fr-command").not(e).filter(function(){return d.opts.codeViewKeepActiveButtons.indexOf(h(this).data("cmd"))<0}).addClass("fr-disabled").attr("aria-disabled",!1),e.addClass("fr-active").attr("aria-pressed",!1)):(d.$tb.find(".fr-btn-grp > .fr-command, .fr-more-toolbar > .fr-command").not(e).removeClass("fr-disabled").attr("aria-disabled",!1),e.removeClass("fr-active").attr("aria-pressed",!1))}()}),!d.$wp)return!1},toggle:w,isActive:p,get:u}},e.RegisterCommand("html",{title:"Code View",undo:!1,focus:!1,forcedRefresh:!0,toggle:!0,callback:function(){this.codeView.toggle()},plugin:"codeView"}),e.DefineIcon("html",{NAME:"code",SVG_KEY:"codeView"})});