10 lines
17 KiB
JavaScript

/*!
* Tiny Permanent Pen plugin
*
* Copyright (c) 2023 Ephox Corporation DBA Tiny Technologies, Inc.
* Licensed under the Tiny commercial license. See https://www.tiny.cloud/legal/
*
* Version: 8.3.0-112
*/
!function(){"use strict";const e=e=>t=>(e=>{const t=typeof e;return null===e?"null":"object"===t&&Array.isArray(e)?"array":"object"===t&&(n=o=e,(r=String).prototype.isPrototypeOf(n)||o.constructor?.name===r.name)?"string":t;var n,o,r})(t)===e,t=e("string"),n=e("object"),o=e=>null==e,r=e=>!o(e),a=e=>"function"==typeof e;const s=()=>{},i=e=>e;class c{tag;value;static singletonNone=new c(!1);constructor(e,t){this.tag=e,this.value=t}static some(e){return new c(!0,e)}static none(){return c.singletonNone}fold(e,t){return this.tag?t(this.value):e()}isSome(){return this.tag}isNone(){return!this.tag}map(e){return this.tag?c.some(e(this.value)):c.none()}bind(e){return this.tag?e(this.value):c.none()}exists(e){return this.tag&&e(this.value)}forall(e){return!this.tag||e(this.value)}filter(e){return!this.tag||e(this.value)?this:c.none()}getOr(e){return this.tag?this.value:e}or(e){return this.tag?this:e}getOrThunk(e){return this.tag?this.value:e()}orThunk(e){return this.tag?this:e()}getOrDie(e){if(this.tag)return this.value;throw new Error(e??"Called getOrDie on None")}static from(e){return r(e)?c.some(e):c.none()}getOrNull(){return this.tag?this.value:null}getOrUndefined(){return this.value}each(e){this.tag&&e(this.value)}toArray(){return this.tag?[this.value]:[]}toString(){return this.tag?`some(${this.value})`:"none()"}}const l=Array.prototype.indexOf,u=(e,t)=>{return n=e,o=t,l.call(n,o)>-1;var n,o},d=(e,t)=>{for(let n=0,o=e.length;n<o;n++)if(t(e[n],n))return!0;return!1},p=(e,t)=>{for(let n=0,o=e.length;n<o;n++)t(e[n],n)},m=(e,t,n)=>(p(e,((e,o)=>{n=t(n,e,o)})),n),g=Object.keys,f=(e,t)=>{const n=g(e);for(let o=0,r=n.length;o<r;o++){const r=n[o];t(e[r],r)}},h=(e,t)=>v(e,((e,n)=>({k:n,v:t(e,n)}))),v=(e,t)=>{const n={};return f(e,((e,o)=>{const r=t(e,o);n[r.k]=r.v})),n},y=(e,t)=>{const n=[];return f(e,((e,o)=>{n.push(t(e,o))})),n},C=e=>{let t=e;return{get:()=>t,set:e=>{t=e}}},P=(S=/^\s+|\s+$/g,e=>e.replace(S,""));var S;const x=(e,t)=>{const{type:n,message:o}=t;e.notificationManager.open({type:n,text:o})},b=e=>{const t=(e=>{switch(e){case"error":return console.error;case"info":return console.info;case"warn":return console.warn;default:return console.log}})(e.type);t(e.message)},w=(e,t)=>{const{protect:n,allowList:o=[],blockList:a=[]}=null!=e?e:{};let s;return s=r(n)?n:o.length>0||a.length>0||t,{protect:s,allowList:o,blockList:a}},R=e=>{const{protect:t,allowList:n,blockList:o}=null!=e?e:{};return e=>{const r=e.toLowerCase();return n.length>0?!d(n,(e=>e.toLowerCase()===r)):o.length>0?d(o,(e=>e.toLowerCase()===r)):t}},$=e=>{const t=R(e);return(e,n)=>{const o=((e,t)=>`${e}:${t}`)(n,e.toLowerCase());return t(o)}},k=(e,t,n,o)=>{const r=Reflect.get(e,"on"),a={protectedEventHandlers:[],cleanupScheduled:!1},i=(e,t)=>{o&&console.log(`[EventsProxy] ${e}`,t||"")},c=t=>{"invalid"===t&&a.protectedEventHandlers.length>0&&(i("Removing event handlers",a.protectedEventHandlers),p(a.protectedEventHandlers,(t=>{e.off(t.name,t.callback)})),a.protectedEventHandlers=[])},l=t.onStatusChange(c);return e.on("remove",l),(o,l,u)=>{i(`Adding event listener: ${o}`,{shouldProtect:n(o)});const d=((e,o)=>{const r=n(e),i=t.getStatus(),c=r&&"invalid"===i?s:o;return r&&a.protectedEventHandlers.push({name:e,callback:c}),c})(o,l);return a.cleanupScheduled||(a.cleanupScheduled=!0,window.setTimeout((()=>{c(t.getStatus()),a.cleanupScheduled=!1}),0)),Reflect.apply(r,e,[o,d,u])}},T=(e,t,n)=>o=>{const a="invalid"!==t.getStatus()&&r(n)?n(o):s,i=((e,t,n)=>{const o=e=>{"invalid"===e&&n.setEnabled(!1)},r=()=>o(t.getStatus());o(t.getStatus()),e.on("NodeChange",r);const a=t.onStatusChange(o);return()=>{e.off("NodeChange",r),a()}})(e,t,o);return e=>{null==a||a(e),i()}},E=(e,t,n)=>(...o)=>{var r;return"invalid"===e.getStatus()?n:null!==(r=null==t?void 0:t(...o))&&void 0!==r?r:n},L=(e,t)=>E(e,t,Promise.resolve([])),M=(e,t)=>E(e,t,[]),O=(e,t)=>E(e,t,!1),A=(e,n,o,r)=>{const a=n=>{const{command:a,value:s}=n,i=c.from(s).filter(t).map((e=>e.toLowerCase())),l=a.toLowerCase()===r.toLowerCase(),u=i.exists((e=>o.has(e))),d=i.exists((t=>t===e.queryCommandValue(r)));l&&u&&!d&&n.preventDefault()},s=n.onStatusChange((t=>{if("invalid"===t){e.on("BeforeExecCommand",a);const t=()=>{const t=e.queryCommandValue(r);o.has(t.toLowerCase())&&e.execCommand(r,!1,t)};e.initialized?t():e.on("init",t)}}));e.on("remove",(()=>{s(),e.off("BeforeExecCommand",a)}))},N=(e,n,o,r,a,s=!1)=>{const i=new Set,c=new Set,l=(e,t)=>{s&&console.log(`[UiProxy] ${e}`,t||"")};((e,t,n)=>{A(e,t,n,"ToggleView")})(e,o,i),((e,t,n)=>{A(e,t,n,"ToggleSidebar")})(e,o,c);const u=(t,n,s,i)=>{const c=a(t,s);return l(`${i}: ${t}`,{spec:n,context:r,shouldProtect:c}),c?{...n,onSetup:T(e,o,n.onSetup)}:n};return new Proxy(n,{get:(e,n,s)=>{const d=Reflect.get(e,n,s);if(!t(n))return d;switch(n){case"addButton":case"addGroupToolbarButton":case"addToggleButton":case"addMenuButton":case"addSplitButton":return(...t)=>{const[o,r,...a]=t,s=u(o,r,"button",String(n));return Reflect.apply(d,e,[o,s,...a])};case"addMenuItem":case"addToggleMenuItem":case"addNestedMenuItem":return(...t)=>{const[o,r,...a]=t,s=u(o,r,"menuitem",String(n));return Reflect.apply(d,e,[o,s,...a])};case"addAutocompleter":return(...t)=>{const[s,i,...c]=t,u=a(s,"autocompleter");if(l(`${String(n)}: ${s}`,{spec:i,context:r,shouldProtect:u}),u){const t={...i,fetch:L(o,i.fetch)};return Reflect.apply(d,e,[s,t,...c])}return Reflect.apply(d,e,[s,i,...c])};case"addSidebar":return(...t)=>{const[o,s,...i]=t,u=a(o,"sidebar");if(l(`${String(n)}: ${o}`,{spec:s,context:r,shouldProtect:u}),u){c.add(o.toLowerCase());const t={...s};return Reflect.apply(d,e,[o,t,...i])}return Reflect.apply(d,e,[o,s,...i])};case"addView":return(...t)=>{const[o,s,...c]=t,u=a(o,"view");if(l(`${String(n)}: ${o}`,{spec:s,context:r,shouldProtect:u}),u){i.add(o.toLowerCase());const t={...s};return Reflect.apply(d,e,[o,t,...c])}return Reflect.apply(d,e,[o,s,...c])};case"addContextMenu":return(...t)=>{const[s,i,...c]=t,u=a(s,"contextmenu");if(l(`${String(n)}: ${s}`,{spec:i,context:r,shouldProtect:u}),u){const t={...i,update:M(o,i.update)};return Reflect.apply(d,e,[s,t,...c])}return Reflect.apply(d,e,[s,i,...c])};case"addContextToolbar":case"addContextForm":return(...t)=>{const[s,i,...c]=t,u=a(s,"contexttoolbar");if(l(`${String(n)}: ${s}`,{spec:i,context:r,shouldProtect:u}),u){const t={...i,predicate:O(o,i.predicate)};return Reflect.apply(d,e,[s,t,...c])}return Reflect.apply(d,e,[s,i,...c])};case"addIcon":case"getAll":case"addContext":return d}}})},j=new Set(["addCommand","addCommands","addQueryStateHandler","addQueryValueHandler"]),z=e=>(t,n)=>{e&&console.log(`[EditorProxy] ${t}`,n||"")},B=(e,t)=>{const{statusProvider:n,uiContext:o,protection:r,debug:i=!1}=t,{events:c,commands:l,ui:u}=r;z(i)("Creating enhanced editor with options:",{hasStatusProvider:!!n,uiContext:o,debug:i,protection:r});const{editorUiProxy:d,editorCommandsProxy:m,editorEventListenerProxy:g}=((e,t,n,o,r,a,i)=>{const c=a.protect?{...e.ui,registry:N(e,e.ui.registry,t,n,$(a),i)}:e.ui,l=o.protect?((e,t,n,o,r)=>{const a={protectedCommands:[],cleanupScheduled:!1},i=(e,t)=>{r&&console.log(`[CommandsProxy] ${e}`,t||"")},c=(e,t)=>{const r=o(e),i=n.getStatus();return r&&a.protectedCommands.push(e),r&&"invalid"===i?s:t},l=t=>{"invalid"===t&&a.protectedCommands.length>0&&(i("Removing commands",a.protectedCommands),p(a.protectedCommands,(t=>{e.editorCommands.removeCommand(t)})),a.protectedCommands=[])},u=()=>{a.cleanupScheduled||(a.cleanupScheduled=!0,window.setTimeout((()=>{l(n.getStatus()),a.cleanupScheduled=!1}),0))},d=n.onStatusChange(l);return e.on("remove",d),new Proxy(t,{get:(e,n,r)=>{if(i(`Accessing property: ${String(n)}`),"addCommand"===n){const a=Reflect.get(e,n,r);return(e,n,...r)=>{i(`Adding command: ${e}`,{shouldProtect:o(e)});const s=c(e,n);return u(),Reflect.apply(a,t,[e,s,...r])}}if("addCommands"===n){const o=Reflect.get(e,n,r);return(e,n="exec")=>{i(`Adding commands (${n})`,{commandCount:Object.keys(e).length,willApplyEnforcement:"exec"===n});const r="exec"!==n?e:h(e,((e,t)=>c(t,e)));return u(),Reflect.apply(o,t,[r,n])}}return Reflect.get(e,n,r)}})})(e,e.editorCommands,t,R(o),i):e.editorCommands;return{editorUiProxy:c,editorCommandsProxy:l,editorEventListenerProxy:r.protect?k(e,t,R(r),i):e.on.bind(e)}})(e,n,o||"",l,c,u,i),f=((e,t,n,o)=>(r,s,i)=>{const c=String(s);if("on"===s)return z(o)("on intercepted"),n;if(j.has(c))return z(o)(`${c} intercepted`),t[c].bind(t);if("editorCommands"===s)return z(o)("editorCommands accessed"),t;if("ui"===s)return z(o)("ui accessed"),e;const l=Reflect.get(r,s,i);return a(l)?l.bind(r):l})(d,m,g,i);return new Proxy(e,{get:f,set:(e,t,n,o)=>Reflect.set(e,t,n,o),has:(e,t)=>Reflect.has(e,t),ownKeys:e=>Reflect.ownKeys(e),getOwnPropertyDescriptor:(e,t)=>Reflect.getOwnPropertyDescriptor(e,t),defineProperty:(e,t,n)=>Reflect.defineProperty(e,t,n),deleteProperty:(e,t)=>Reflect.deleteProperty(e,t),isExtensible:e=>Reflect.isExtensible(e),preventExtensions:e=>Reflect.preventExtensions(e),getPrototypeOf:e=>Reflect.getPrototypeOf(e),setPrototypeOf:(e,t)=>Reflect.setPrototypeOf(e,t)})},_=e=>parseInt(e,10),I=(e,t)=>{const n=e-t;return 0===n?0:n>0?1:-1},H=(e,t,n)=>({major:e,minor:t,patch:n}),D=e=>{const t=/([0-9]+)\.([0-9]+)\.([0-9]+)(?:(\-.+)?)/.exec(e);return t?H(_(t[1]),_(t[2]),_(t[3])):H(0,0,0)},V=(e,t)=>!!e&&-1===((e,t)=>{const n=I(e.major,t.major);if(0!==n)return n;const o=I(e.minor,t.minor);if(0!==o)return o;const r=I(e.patch,t.patch);return 0!==r?r:0})((e=>D((e=>[e.majorVersion,e.minorVersion].join(".").split(".").slice(0,3).join("."))(e)))(e),D(t)),q=D("8.3.0"),U=`${q.major}.${q.minor}.0`,K=`${q.major+1}.0.0`,F=(e,t)=>{const{setup:n,protection:s}=t;return(i,c)=>{if(!((e,t)=>V(e,U)?(console.error(`The "${t}" plugin requires at least version ${U} of TinyMCE.`),!1):!!V(e,K)||(console.error(`The "${t}" plugin requires at least version ${U} of TinyMCE but less than ${K}.`),!1))(tinymce,e)||((e,t)=>!!(e=>{var t;const n=null===(t=e.options.get("license_key"))||void 0===t?void 0:t.toLowerCase(),r=e.options.get("online_license_key");return o(r)&&"gpl"===n})(e)&&(((e,t)=>{const{console:n,editor:o}=t;r(o)&&(e._skinLoaded?x(e,o):e.on("SkinLoaded",(()=>{x(e,o)}))),r(n)&&b(n)})(e,{console:{type:"error",message:[`The "${t}" plugin requires a valid TinyMCE license key.`,"Read more: https://www.tiny.cloud/docs/tinymce/latest/license-key/"].join(" ")}}),!0))(i,e))return{};const l=(e=>{const{protect:t=!0,events:n,commands:o,ui:r,api:a}=null!=e?e:{};return{protect:t,events:w(n,false),commands:w(o,true),ui:w(r,true),api:w(a,true)}})(s),u=((e="pre-validation")=>{const t=C(e),n=new Set;let o=!1;const{enableLogging:r=!1}={},a=(e,...t)=>{r&&console.log(`[StatusProvider] ${e}`,...t)};return{setStatus:e=>{const o=t.get();if(o===e)return a("Status unchanged:",e),!1;t.set(e),a("Status changed:",o,"->",e);const r={currentStatus:e,previousStatus:o},s=Array.from(n);return p(s,(e=>{e(r.currentStatus,r.previousStatus)})),!0},onStatusChange:e=>(n.add(e),a("Callback registered, total listeners:",n.size),()=>{n.delete(e)&&a("Callback unregistered, remaining listeners:",n.size)}),getStatus:t.get,hasListeners:()=>n.size>0,destroy:()=>{o||(a("Destroying StatusProvider, clearing",n.size,"listeners"),n.clear(),o=!0)}}})(),d=((e,t,n)=>{const o=`commercial-license-${t}`;return e.ui.registry.addContext(o,(()=>"invalid"!==n.getStatus()&&!e.mode.isReadOnly())),e.licenseKeyManager.validate({plugin:t}).then((e=>{n.setStatus(e?"valid":"invalid")}),(e=>{console.error(e),console.error(`An unknown error occurred when trying to validate "${t}" plugin.`),n.setStatus("invalid")})),{ui:{context:o}}})(i,e,u),m=l.protect?B(i,{statusProvider:u,uiContext:d.ui.context,debug:t.debug||!1,protection:l}):i,g=((e,t)=>({pluginCode:e,enforcer:{isInvalid:()=>"invalid"===t.getStatus(),onInvalid:e=>t.onStatusChange(((t,n)=>{"invalid"===t&&e(t,n)}))}}))(e,u);return((e,t,n,o)=>{if(t.protect&&t.api.protect&&r(e)){const i=R(t.api);return(s=e=>"invalid"===n.getStatus()&&i(e),(e,t)=>h(e,((e,n)=>a(e)?(...o)=>{if(s(n.toLowerCase())){const e=`Method "${r(t)?`${t}.`:""}${n}" is not available due to license key restrictions.`,o=new Error(e);throw console.error(e),o}return e(...o)}:e)))(e,o)}var s;return e})(n(m,c,g),l,u,e)}},G={fontname:"arial,helvetica,sans-serif",forecolor:"#E74C3C",fontsize:"12pt",hilitecolor:"",bold:!0,italic:!1,underline:!1,strikethrough:!1},Q=e=>e.options.get("permanentpen_properties");const W=e=>{var t;return(e=>{const t=e.indexOf("=")>0?e.split(/[;,](?![^=;,]*(?:[;,]|$))/):e.split(",");return m(t,((e,t)=>{const n=t.split("="),o=n[0],r=n.length>1?n[1]:o;return e[P(o)]=P(r),e}),{})})(null!==(t=e.options.get("font_family_formats"))&&void 0!==t?t:"")},J=e=>{var t;return(null!==(t=e.options.get("font_size_formats"))&&void 0!==t?t:"").split(/[, ]/)},X=e=>{const t=(e=>(e=>{const t=e.length,n=new Array(t);for(let r=0;r<t;r++){const t=e[r];n[r]={text:o=t,value:o}}var o;return n})(J(e)))(e),n=(e=>y(W(e),((e,t)=>({text:t,value:e}))))(e);return{type:"panel",items:[{type:"listbox",name:"fontname",label:"Font",items:n},{type:"listbox",name:"fontsize",label:"Size",items:t},{type:"label",label:"Styles",items:[{type:"bar",items:[{type:"checkbox",name:"bold",label:"Bold"},{type:"checkbox",name:"italic",label:"Italic"},{type:"checkbox",name:"strikethrough",label:"Strikethrough"},{type:"checkbox",name:"underline",label:"Underline"}]}]},{name:"forecolor",type:"colorinput",label:"Text color"},{name:"hilitecolor",type:"colorinput",label:"Background color"}]}},Y=e=>"br"===(null==e?void 0:e.nodeName.toLowerCase()),Z=(e,t)=>(e=>u(["bold","italic","underline","strikethrough"],e))(t)&&e?c.some({formatName:t,values:{}}):(e=>u(["forecolor","hilitecolor","fontname","fontsize"],e))(t)&&e.length>0?c.some({formatName:t,values:{value:e}}):c.none(),ee=e=>!e.isComposing&&("insertText"===e.inputType||"compositionend"===e.type)&&null!==e.data&&e.data.length>0,te=(e,t,n,o)=>{e.undoManager.ignore((()=>{"Apply"===o?((e,t,n)=>{if(!((e,t)=>(t=>{for(let o=0,r=t.length;o<r;++o)if(!0!==(n=t[o],e.formatter.match(n.formatName,n.values)))return!1;var n;return!0})(t))(e,t)){const o=e.selection.getRng();o.setStart(o.startContainer,o.startOffset-n),e.selection.setRng(o),e.formatter.remove("removeformat"),((e,t)=>{p(t,(t=>{e.formatter.apply(t.formatName,t.values)}))})(e,t),((e,t)=>{const n=e.getRng().cloneRange();n.setEnd(n.startContainer,n.startOffset+t),n.setStart(n.startContainer,n.startOffset+t),e.setRng(n)})(e.selection,n)}})(e,(e=>(e=>{const t=[],n=e=>{t.push(e)};for(let t=0;t<e.length;t++)e[t].each(n);return t})(y(e,Z)))(t),n):((e,t)=>{const n=0===t,o=e.selection.getRng().cloneRange();if(t>0)o.setStart(o.startContainer,o.startOffset-t);else{const t=(e=>{const t=e.dom.create("span",{id:"_mce_caret","data-mce-bogus":"1","data-mce-type":"format-caret"});return t.appendChild(e.getDoc().createTextNode("\ufeff")),t})(e);o.insertNode(t),o.selectNodeContents(t),Y(t.previousSibling)&&Y(t.nextSibling)&&o.setEndAfter(t.nextSibling)}e.selection.setRng(o),e.formatter.remove("removeformat"),e.selection.collapse(n)})(e,n)}))},ne=(e,t,n)=>{if(!d(t,(e=>e===n))){const o=m(t,((e,t)=>e+`"${t}" `),`TinyMCE PermanentPen permanentpen_properties.${e}: "${n}" not found. Possible values are: `);console.log(o)}};((e,t)=>{const{i18n:n=!0}=t;n&&tinymce.PluginManager.requireLangPack(e,"ar,bg-BG,ca,cs,da,de,el,es,eu,fa,fi,fr-FR,he-IL,hi,hr,hu-HU,id,it,ja,kk,ko-KR,ms,nb-NO,nl,pl,pt-BR,pt-PT,ro,ru,sk,sl-SI,sv-SE,th-TH,tr,uk,vi,zh-CN,zh-TW"),tinymce.PluginManager.add(e,F(e,t))})("permanentpen",{setup:e=>{(e=>{(0,e.options.register)("permanentpen_properties",{processor:e=>n(e)?{value:{...G,...e},valid:!0}:{valid:!1,message:"Must be an object."},default:G})})(e),(e=>{const t=Q(e);var n;ne("fontsize",J(e),t.fontsize),ne("fontname",(n=W(e),y(n,i)),t.fontname)})(e);const t=C(!1),o=C(Q(e)),r=((e,t)=>{const n=n=>{ee(n)&&te(e,t.get(),n.data.length,"Apply")},o=n=>{const a="input"===n.type&&ee(n)?n.data.length:0;te(e,t.get(),a,"Remove"),r(o)},r=t=>{e.off("input compositionend NewBlock",t),t===o&&e.off("nodechange",a)},a=()=>{e.composing||r(o)},s=()=>{r(n),r(o)};return{enterPermaPenMode:()=>{s(),e.on("input compositionend",n)},leavePermaPenMode:()=>{s(),e.once("input compositionend NewBlock",o),e.on("nodechange",a)}}})(e,o);((e,t,n,o)=>{e.addCommand("mceConfigurePermanentPen",(()=>{((e,t)=>{e.windowManager.open({title:"Permanent Pen Properties",size:"normal",body:X(e),onSubmit:n=>{const o=n.getData();t.set(o),((e,t)=>{e.dispatch("PermanentPenProperties",{properties:t})})(e,o),n.close()},buttons:[{type:"cancel",name:"cancel",text:"Cancel"},{type:"submit",name:"save",text:"Save",primary:!0}],initialData:t.get()})})(e,t)})),e.addCommand("mceTogglePermanentPen",(()=>{const t=!n.get();((e,t)=>{e.dispatch("PermanentPenToggle",{state:t})})(e,t),n.set(t),t?o.enterPermaPenMode():o.leavePermaPenMode()}))})(e,o,t,r),(e=>{e.ui.registry.addMenuItem("configurepermanentpen",{icon:"permanent-pen",text:"Permanent pen properties...",onAction:()=>e.execCommand("mceConfigurePermanentPen")}),e.ui.registry.addMenuItem("permanentpen",{icon:"permanent-pen",text:"Permanent pen",onAction:()=>e.execCommand("mceTogglePermanentPen")})})(e),(e=>{e.ui.registry.addToggleButton("permanentpen",{icon:"permanent-pen",tooltip:"Permanent pen",onAction:()=>e.execCommand("mceTogglePermanentPen"),onSetup:t=>{const n=e=>{t.setActive(e.state)};return e.on("PermanentPenToggle",n),()=>e.off("PermanentPenToggle",n)}})})(e),((e,t)=>{e.ui.registry.addContextMenu("configurepermanentpen",{update:()=>t.get()?["configurepermanentpen"]:[]})})(e,t)},i18n:!1})}();