UNPKG

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.64 kB
/*! * froala_editor v5.0.1 (https://www.froala.com/wysiwyg-editor) * License https://froala.com/wysiwyg-editor/terms/ * Copyright 2014-2026 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(("undefined"!=typeof globalThis?globalThis:e||self).FroalaEditor)}(this,function(e){"use strict";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(l){var d,c,f,h=l.$;function p(){return c?c.getValue():d.val()}function g(){f()&&(c&&c.setSize(null,l.opts.height||"auto"),l.opts.heightMin||l.opts.height?(l.$box.find(".CodeMirror-scroll, .CodeMirror-gutters").css("min-height",l.opts.heightMin||l.opts.height),d.css("height",l.opts.height)):l.$box.find(".CodeMirror-scroll, .CodeMirror-gutters").css("min-height",""))}var u,m=!1;function b(){f()&&l.events.trigger("blur")}function v(){f()&&m&&l.events.trigger("focus")}function r(e){d||(d=h('<textarea class="fr-code" tabIndex="-1">'),l.$wp.append(d),d.attr("dir",l.opts.direction),l.$box.hasClass("fr-basic")||(u=h('<a data-cmd="html" title="Code View" class="fr-command fr-btn html-switch'.concat(l.helpers.isMobile()?"":" fr-desktop",'" role="button" tabIndex="-1"><i class="fa fa-code"></i></button>')),l.$box.append(u),l.events.bindClick(l.$box,"a.html-switch",function(){l.events.trigger("commands.before",["html"]),M(!1),l.events.trigger("commands.after",["html"])})),t=function t(){return!f()},l.events.on("buttons.refresh",t),l.events.on("copy",t,!0),l.events.on("cut",t,!0),l.events.on("paste",t,!0),l.events.on("destroy",w,!0),l.events.on("html.set",function(){f()&&M(!0)}),l.events.on("codeView.update",g),l.events.on("codeView.toggle",function(){l.$box.hasClass("fr-code-view")&&M()}),l.events.on("form.submit",function(){f()&&(l.html.set(p()),l.events.trigger("contentChanged",[],!0))},!0),!c&&l.opts.codeMirror?((c=l.opts.codeMirror.fromTextArea(d.get(0),l.opts.codeMirrorOptions)).on("blur",b),c.on("focus",v)):(l.events.$on(d,"keydown keyup change input",function(){l.opts.height?this.removeAttribute("rows"):(this.rows=1,0===this.value.length?this.style.height="auto":this.style.height="".concat(this.scrollHeight,"px"))}),l.events.$on(d,"blur",b),l.events.$on(d,"focus",v))),l.undo.saveStep(),l.html.cleanEmptyTags(),l.html.cleanWhiteTags(!0),l.core.hasFocus()&&!l.core.isEmpty()&&(l.selection.save(),l.$el.find('.fr-marker[data-type="true"]').first().replaceWith('<span class="fr-tmp fr-sm">F</span>'),l.$el.find('.fr-marker[data-type="false"]').last().replaceWith('<span class="fr-tmp fr-em">F</span>'));var t,r,n,o,s,i=l.html.get(!1,!0),a=(l.$el.find("span.fr-tmp").remove(),l.$box.toggleClass("fr-code-view",!0),!1);l.core.hasFocus()&&(a=!0,l.events.disableBlur(),l.$el.blur()),i=(i=i.replace(/<span class="fr-tmp fr-sm">F<\/span>/,"FROALA-SM")).replace(/<span class="fr-tmp fr-em">F<\/span>/,"FROALA-EM"),l.codeBeautifier&&!i.includes("fr-embedly")&&(i=l.codeBeautifier.run(i,l.opts.codeBeautifierOptions)),c?(n=i.indexOf("FROALA-SM"),(o=i.indexOf("FROALA-EM"))<n?n=o:o-=9,s=(i=i.replace(/FROALA-SM/g,"").replace(/FROALA-EM/g,"")).substring(0,n).length-i.substring(0,n).replace(/\n/g,"").length,r=i.substring(0,o).length-i.substring(0,o).replace(/\n/g,"").length,n=i.substring(0,n).length-i.substring(0,i.substring(0,n).lastIndexOf("\n")+1).length,o=i.substring(0,o).length-i.substring(0,i.substring(0,o).lastIndexOf("\n")+1).length,c.setSize(null,l.opts.height||"auto"),l.opts.heightMin&&l.$box.find(".CodeMirror-scroll").css("min-height",l.opts.heightMin),c.setValue(i),m=!a,c.focus(),m=!0,c.setSelection({line:s,ch:n},{line:r,ch:o}),c.refresh(),c.clearHistory(),c.setOption("readOnly",l.edit.isDisabled())):(n=i.indexOf("FROALA-SM"),o=i.indexOf("FROALA-EM")-9,l.opts.heightMin&&d.css("min-height",l.opts.heightMin),l.opts.height&&d.css("height",l.opts.height),l.opts.heightMax&&d.css("max-height",l.opts.height||l.opts.heightMax),d.val(i.replace(/FROALA-SM/g,"").replace(/FROALA-EM/g,"")).trigger("change"),s=h(l.o_doc).scrollTop(),m=!a,d.focus(),m=!0,d.get(0).setSelectionRange(n,o),h(l.o_doc).scrollTop(s)),l.$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 l.opts.codeViewKeepActiveButtons.indexOf(h(this).data("cmd"))<0}).addClass("fr-disabled").attr("aria-disabled",!0),e.addClass("fr-active").attr("aria-pressed",!0),!l.helpers.isMobile()&&l.opts.toolbarInline&&l.toolbar.hide()}function M(e){void 0===e&&(e=!f());var t=l.$tb.find('.fr-command[data-cmd="html"]');e?(l.popups.hideAll(),r(t)):(l.$box.toggleClass("fr-code-view",!1),e=t,t=p(),l.html.set(t),l.$el.blur(),l.edit.isDisabled()||l.$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),(t=h(l.el).children().first()).is("table")?(e=t.find("td").first()).length&&l.selection.setAtStart(e[0]):l.selection.setAtStart(l.el),l.selection.restore(),l.placeholder.refresh(),l.undo.saveStep(),l.events.trigger("codeView.update"))}function w(){f()&&M(!1),c&&c.toTextArea(),d.val("").removeData().remove(),d=null,u&&(u.remove(),u=null)}return{_init:function(){if(l.events.on("focus",function(){var e;l.opts.toolbarContainer&&(e=l.$tb.find('.fr-command[data-cmd="html"]'),(f()?(l.$tb.find(".fr-btn-grp > .fr-command, .fr-more-toolbar > .fr-command").not(e).filter(function(){return l.opts.codeViewKeepActiveButtons.indexOf(h(this).data("cmd"))<0}).addClass("fr-disabled").attr("aria-disabled",!1),e.addClass("fr-active")):(l.$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))}),!l.$wp)return!1},toggle:M,isActive:f=function f(){return l.$box.hasClass("fr-code-view")},get:p}},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"})});