10 lines
16 KiB
JavaScript
10 lines
16 KiB
JavaScript
/*!
|
|
* Tiny Spelling Autocorrect 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=r=e,(o=String).prototype.isPrototypeOf(n)||r.constructor?.name===o.name)?"string":t;var n,r,o})(t)===e,t=e("string"),n=e("array"),r=e=>null==e,o=e=>!r(e),s=e=>"function"==typeof e;const a=()=>{},c=e=>()=>e;class i{tag;value;static singletonNone=new i(!1);constructor(e,t){this.tag=e,this.value=t}static some(e){return new i(!0,e)}static none(){return i.singletonNone}fold(e,t){return this.tag?t(this.value):e()}isSome(){return this.tag}isNone(){return!this.tag}map(e){return this.tag?i.some(e(this.value)):i.none()}bind(e){return this.tag?e(this.value):i.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:i.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 o(e)?i.some(e):i.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 u=Array.prototype.push,l=(e,t)=>{for(let n=0,r=e.length;n<r;n++)if(t(e[n],n))return!0;return!1},d=(e,t)=>{for(let n=0,r=e.length;n<r;n++)t(e[n],n)},p=Object.keys,g=(e,t)=>{const n=p(e);for(let r=0,o=n.length;r<o;r++){const o=n[r];t(e[o],o)}},m=(e,t)=>h(e,((e,n)=>({k:n,v:t(e,n)}))),h=(e,t)=>{const n={};return g(e,((e,r)=>{const o=t(e,r);n[o.k]=o.v})),n},f=e=>{let t=e;return{get:()=>t,set:e=>{t=e}}},y="undefined"!=typeof window?window:Function("return this;")(),v=e=>""===e?"":e.charAt(0).toUpperCase()+e.substring(1),S=(e,t)=>{const{type:n,message:r}=t;e.notificationManager.open({type:n,text:r})},C=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:r=[],blockList:s=[]}=null!=e?e:{};let a;return a=o(n)?n:r.length>0||s.length>0||t,{protect:a,allowList:r,blockList:s}},x=e=>{const{protect:t,allowList:n,blockList:r}=null!=e?e:{};return e=>{const o=e.toLowerCase();return n.length>0?!l(n,(e=>e.toLowerCase()===o)):r.length>0?l(r,(e=>e.toLowerCase()===o)):t}},$=e=>{const t=x(e);return(e,n)=>{const r=((e,t)=>`${e}:${t}`)(n,e.toLowerCase());return t(r)}},P=(e,t,n,r)=>{const o=Reflect.get(e,"on"),s={protectedEventHandlers:[],cleanupScheduled:!1},c=(e,t)=>{r&&console.log(`[EventsProxy] ${e}`,t||"")},i=t=>{"invalid"===t&&s.protectedEventHandlers.length>0&&(c("Removing event handlers",s.protectedEventHandlers),d(s.protectedEventHandlers,(t=>{e.off(t.name,t.callback)})),s.protectedEventHandlers=[])},u=t.onStatusChange(i);return e.on("remove",u),(r,u,l)=>{c(`Adding event listener: ${r}`,{shouldProtect:n(r)});const d=((e,r)=>{const o=n(e),c=t.getStatus(),i=o&&"invalid"===c?a:r;return o&&s.protectedEventHandlers.push({name:e,callback:i}),i})(r,u);return s.cleanupScheduled||(s.cleanupScheduled=!0,window.setTimeout((()=>{i(t.getStatus()),s.cleanupScheduled=!1}),0)),Reflect.apply(o,e,[r,d,l])}},R=(e,t,n)=>r=>{const s="invalid"!==t.getStatus()&&o(n)?n(r):a,c=((e,t,n)=>{const r=e=>{"invalid"===e&&n.setEnabled(!1)},o=()=>r(t.getStatus());r(t.getStatus()),e.on("NodeChange",o);const s=t.onStatusChange(r);return()=>{e.off("NodeChange",o),s()}})(e,t,r);return e=>{null==s||s(e),c()}},b=(e,t,n)=>(...r)=>{var o;return"invalid"===e.getStatus()?n:null!==(o=null==t?void 0:t(...r))&&void 0!==o?o:n},k=(e,t)=>b(e,t,Promise.resolve([])),E=(e,t)=>b(e,t,[]),A=(e,t)=>b(e,t,!1),L=(e,n,r,o)=>{const s=n=>{const{command:s,value:a}=n,c=i.from(a).filter(t).map((e=>e.toLowerCase())),u=s.toLowerCase()===o.toLowerCase(),l=c.exists((e=>r.has(e))),d=c.exists((t=>t===e.queryCommandValue(o)));u&&l&&!d&&n.preventDefault()},a=n.onStatusChange((t=>{if("invalid"===t){e.on("BeforeExecCommand",s);const t=()=>{const t=e.queryCommandValue(o);r.has(t.toLowerCase())&&e.execCommand(o,!1,t)};e.initialized?t():e.on("init",t)}}));e.on("remove",(()=>{a(),e.off("BeforeExecCommand",s)}))},z=(e,n,r,o,s,a=!1)=>{const c=new Set,i=new Set,u=(e,t)=>{a&&console.log(`[UiProxy] ${e}`,t||"")};((e,t,n)=>{L(e,t,n,"ToggleView")})(e,r,c),((e,t,n)=>{L(e,t,n,"ToggleSidebar")})(e,r,i);const l=(t,n,a,c)=>{const i=s(t,a);return u(`${c}: ${t}`,{spec:n,context:o,shouldProtect:i}),i?{...n,onSetup:R(e,r,n.onSetup)}:n};return new Proxy(n,{get:(e,n,a)=>{const d=Reflect.get(e,n,a);if(!t(n))return d;switch(n){case"addButton":case"addGroupToolbarButton":case"addToggleButton":case"addMenuButton":case"addSplitButton":return(...t)=>{const[r,o,...s]=t,a=l(r,o,"button",String(n));return Reflect.apply(d,e,[r,a,...s])};case"addMenuItem":case"addToggleMenuItem":case"addNestedMenuItem":return(...t)=>{const[r,o,...s]=t,a=l(r,o,"menuitem",String(n));return Reflect.apply(d,e,[r,a,...s])};case"addAutocompleter":return(...t)=>{const[a,c,...i]=t,l=s(a,"autocompleter");if(u(`${String(n)}: ${a}`,{spec:c,context:o,shouldProtect:l}),l){const t={...c,fetch:k(r,c.fetch)};return Reflect.apply(d,e,[a,t,...i])}return Reflect.apply(d,e,[a,c,...i])};case"addSidebar":return(...t)=>{const[r,a,...c]=t,l=s(r,"sidebar");if(u(`${String(n)}: ${r}`,{spec:a,context:o,shouldProtect:l}),l){i.add(r.toLowerCase());const t={...a};return Reflect.apply(d,e,[r,t,...c])}return Reflect.apply(d,e,[r,a,...c])};case"addView":return(...t)=>{const[r,a,...i]=t,l=s(r,"view");if(u(`${String(n)}: ${r}`,{spec:a,context:o,shouldProtect:l}),l){c.add(r.toLowerCase());const t={...a};return Reflect.apply(d,e,[r,t,...i])}return Reflect.apply(d,e,[r,a,...i])};case"addContextMenu":return(...t)=>{const[a,c,...i]=t,l=s(a,"contextmenu");if(u(`${String(n)}: ${a}`,{spec:c,context:o,shouldProtect:l}),l){const t={...c,update:E(r,c.update)};return Reflect.apply(d,e,[a,t,...i])}return Reflect.apply(d,e,[a,c,...i])};case"addContextToolbar":case"addContextForm":return(...t)=>{const[a,c,...i]=t,l=s(a,"contexttoolbar");if(u(`${String(n)}: ${a}`,{spec:c,context:o,shouldProtect:l}),l){const t={...c,predicate:A(r,c.predicate)};return Reflect.apply(d,e,[a,t,...i])}return Reflect.apply(d,e,[a,c,...i])};case"addIcon":case"getAll":case"addContext":return d}}})},_=new Set(["addCommand","addCommands","addQueryStateHandler","addQueryValueHandler"]),I=e=>(t,n)=>{e&&console.log(`[EditorProxy] ${t}`,n||"")},O=(e,t)=>{const{statusProvider:n,uiContext:r,protection:o,debug:c=!1}=t,{events:i,commands:u,ui:l}=o;I(c)("Creating enhanced editor with options:",{hasStatusProvider:!!n,uiContext:r,debug:c,protection:o});const{editorUiProxy:p,editorCommandsProxy:g,editorEventListenerProxy:h}=((e,t,n,r,o,s,c)=>{const i=s.protect?{...e.ui,registry:z(e,e.ui.registry,t,n,$(s),c)}:e.ui,u=r.protect?((e,t,n,r,o)=>{const s={protectedCommands:[],cleanupScheduled:!1},c=(e,t)=>{o&&console.log(`[CommandsProxy] ${e}`,t||"")},i=(e,t)=>{const o=r(e),c=n.getStatus();return o&&s.protectedCommands.push(e),o&&"invalid"===c?a:t},u=t=>{"invalid"===t&&s.protectedCommands.length>0&&(c("Removing commands",s.protectedCommands),d(s.protectedCommands,(t=>{e.editorCommands.removeCommand(t)})),s.protectedCommands=[])},l=()=>{s.cleanupScheduled||(s.cleanupScheduled=!0,window.setTimeout((()=>{u(n.getStatus()),s.cleanupScheduled=!1}),0))},p=n.onStatusChange(u);return e.on("remove",p),new Proxy(t,{get:(e,n,o)=>{if(c(`Accessing property: ${String(n)}`),"addCommand"===n){const s=Reflect.get(e,n,o);return(e,n,...o)=>{c(`Adding command: ${e}`,{shouldProtect:r(e)});const a=i(e,n);return l(),Reflect.apply(s,t,[e,a,...o])}}if("addCommands"===n){const r=Reflect.get(e,n,o);return(e,n="exec")=>{c(`Adding commands (${n})`,{commandCount:Object.keys(e).length,willApplyEnforcement:"exec"===n});const o="exec"!==n?e:m(e,((e,t)=>i(t,e)));return l(),Reflect.apply(r,t,[o,n])}}return Reflect.get(e,n,o)}})})(e,e.editorCommands,t,x(r),c):e.editorCommands;return{editorUiProxy:i,editorCommandsProxy:u,editorEventListenerProxy:o.protect?P(e,t,x(o),c):e.on.bind(e)}})(e,n,r||"",u,i,l,c),f=((e,t,n,r)=>(o,a,c)=>{const i=String(a);if("on"===a)return I(r)("on intercepted"),n;if(_.has(i))return I(r)(`${i} intercepted`),t[i].bind(t);if("editorCommands"===a)return I(r)("editorCommands accessed"),t;if("ui"===a)return I(r)("ui accessed"),e;const u=Reflect.get(o,a,c);return s(u)?u.bind(o):u})(p,g,h,c);return new Proxy(e,{get:f,set:(e,t,n,r)=>Reflect.set(e,t,n,r),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)})},T=e=>parseInt(e,10),j=(e,t)=>{const n=e-t;return 0===n?0:n>0?1:-1},M=(e,t,n)=>({major:e,minor:t,patch:n}),N=e=>{const t=/([0-9]+)\.([0-9]+)\.([0-9]+)(?:(\-.+)?)/.exec(e);return t?M(T(t[1]),T(t[2]),T(t[3])):M(0,0,0)},H=(e,t)=>!!e&&-1===((e,t)=>{const n=j(e.major,t.major);if(0!==n)return n;const r=j(e.minor,t.minor);if(0!==r)return r;const o=j(e.patch,t.patch);return 0!==o?o:0})((e=>N((e=>[e.majorVersion,e.minorVersion].join(".").split(".").slice(0,3).join("."))(e)))(e),N(t)),B=N("8.3.0"),V=`${B.major}.${B.minor}.0`,q=`${B.major+1}.0.0`,D=(e,t)=>{const{setup:n,protection:a}=t;return(c,i)=>{if(!((e,t)=>H(e,V)?(console.error(`The "${t}" plugin requires at least version ${V} of TinyMCE.`),!1):!!H(e,q)||(console.error(`The "${t}" plugin requires at least version ${V} of TinyMCE but less than ${q}.`),!1))(tinymce,e)||((e,t)=>!!(e=>{var t;const n=null===(t=e.options.get("license_key"))||void 0===t?void 0:t.toLowerCase(),o=e.options.get("online_license_key");return r(o)&&"gpl"===n})(e)&&(((e,t)=>{const{console:n,editor:r}=t;o(r)&&(e._skinLoaded?S(e,r):e.on("SkinLoaded",(()=>{S(e,r)}))),o(n)&&C(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))(c,e))return{};const u=(e=>{const{protect:t=!0,events:n,commands:r,ui:o,api:s}=null!=e?e:{};return{protect:t,events:w(n,false),commands:w(r,true),ui:w(o,true),api:w(s,true)}})(a),l=((e="pre-validation")=>{const t=f(e),n=new Set;let r=!1;const{enableLogging:o=!1}={},s=(e,...t)=>{o&&console.log(`[StatusProvider] ${e}`,...t)};return{setStatus:e=>{const r=t.get();if(r===e)return s("Status unchanged:",e),!1;t.set(e),s("Status changed:",r,"->",e);const o={currentStatus:e,previousStatus:r},a=Array.from(n);return d(a,(e=>{e(o.currentStatus,o.previousStatus)})),!0},onStatusChange:e=>(n.add(e),s("Callback registered, total listeners:",n.size),()=>{n.delete(e)&&s("Callback unregistered, remaining listeners:",n.size)}),getStatus:t.get,hasListeners:()=>n.size>0,destroy:()=>{r||(s("Destroying StatusProvider, clearing",n.size,"listeners"),n.clear(),r=!0)}}})(),p=((e,t,n)=>{const r=`commercial-license-${t}`;return e.ui.registry.addContext(r,(()=>"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:r}}})(c,e,l),g=u.protect?O(c,{statusProvider:l,uiContext:p.ui.context,debug:t.debug||!1,protection:u}):c,h=((e,t)=>({pluginCode:e,enforcer:{isInvalid:()=>"invalid"===t.getStatus(),onInvalid:e=>t.onStatusChange(((t,n)=>{"invalid"===t&&e(t,n)}))}}))(e,l);return((e,t,n,r)=>{if(t.protect&&t.api.protect&&o(e)){const c=x(t.api);return(a=e=>"invalid"===n.getStatus()&&c(e),(e,t)=>m(e,((e,n)=>s(e)?(...r)=>{if(a(n.toLowerCase())){const e=`Method "${o(t)?`${t}.`:""}${n}" is not available due to license key restrictions.`,r=new Error(e);throw console.error(e),r}return e(...r)}:e)))(e,r)}var a;return e})(n(g,i,h),u,l,e)}},U=e=>t=>t.options.get(e),W=U("autocorrect_lookup_base_url"),F=U("autocorrect_capitalize"),K=U("autocorrect_autocorrect"),G="AutocorrectStart",J="AutocorrectEnd",Q=G+" "+J,Z="CapitalizeStart",X="CapitalizeEnd",Y=Z+" "+X,ee=e=>{e.dispatch(G)},te=e=>{e.dispatch(J)},ne=e=>e.dispatch(Z),re=e=>e.dispatch(X),oe=(e,t,n)=>{(n.cache?t.autocorrect().setAndCache:t.autocorrect().set)(n.on),(n.on?ee:te)(e)},se=(e,t,n)=>{(n.cache?t.capitalize().setAndCache:t.capitalize().set)(n.on),(n.on?ne:re)(e)},ae=(e,t)=>{const n=f({});return((e,t)=>tinymce.Resource.load(`autocorrect/${t}`,((e,t)=>`${e.endsWith("/")?e.slice(0,-1):e}/${t.startsWith("/")?t.slice(1):t}`)(e,`${t}.js`)))(e,t).then((e=>{const t=((e,t)=>{const n={};var r;return((e,t,n,r)=>{g(e,((e,o)=>{(t(e,o)?n:r)(e,o)}))})(e,t,(r=n,(e,t)=>{r[t]=e}),a),n})(e,((e,t)=>!(1===e.length&&t.length>1)));n.set(t)}),(e=>{console.error("Failed to load autocorrect words.",e)})),{lookup:e=>i.from(n.get()[e])}},ce={normalisation:[new RegExp(["([A-Z][A-Z]+)","([a-z]+)","(\\s*)","$"].join(""))],capitalisation:[new RegExp(["([.?!]\\s+)","([a-z][^\\s]+)","(\\s*)","$"].join("")),new RegExp(["^()","([a-z][^\\s]*)","(\\s*)","$"].join(""))],autocorrect:[new RegExp(["(^|\\W)","(\\w+)","(\\W*)","(\\s*)","$"].join(""))]},ie=e=>{const t=["all","start","word","endOfWord","space"];return{regexes:ce.autocorrect,identify:n=>{if(n&&n.length>=t.length){const t=n[2],r=n[3]+n[4],o=t+r;return e.lookup(t).map((e=>({start:o,replacement:e+r})))}return i.none()}}},ue=(e,t)=>(e=>{const t=[];for(let r=0,o=e.length;r<o;++r){if(!n(e[r]))throw new Error("Arr.flatten item "+r+" was not an array, input: "+e);u.apply(t,e[r])}return t})(((e,t)=>{const n=e.length,r=new Array(n);for(let o=0;o<n;o++){const n=e[o];r[o]=t(n,o)}return r})(t,(t=>((e,t)=>{for(let n=0;n<e.length;n++){const r=t(e[n]);if(r.isSome())return r}return i.none()})(t.regexes,(n=>{const r=e.match(n);return null!==r?t.identify(r):i.none()})).toArray()))),le=(()=>{try{const e=y.localStorage,t="__storage_test__";return e.setItem(t,t),e.removeItem(t),i.some(e)}catch(e){return i.none()}})().fold((()=>{const e={};return{getItem:t=>e[t],setItem:(t,n)=>{e[t]=n}}}),(e=>({getItem:t=>e.getItem("mce."+t),setItem:(t,n)=>{e.setItem("mce."+t,n)}}))),de=(e,t)=>{const n=((e,t)=>{const n=(e=>i.from(le.getItem(e)))(e).map(JSON.parse);return n.getOr(t)})(e,t),r=f(n),o=e=>{r.set(e)};return{get:()=>r.get(),set:o,setAndCache:t=>{o(t),((e,t)=>{const n=JSON.stringify(t);le.setItem(e,n)})(e,t)}}};((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,D(e,t))})("autocorrect",{setup:(e,t,n)=>{((e,t)=>{const n=e.options.register;n("autocorrect_lookup_base_url",{processor:"string",default:`${t}/js/`}),n("autocorrect_capitalize",{processor:"boolean",default:!1}),n("autocorrect_autocorrect",{processor:"boolean",default:!0})})(e,t);const r=(e=>{const t=de("autocorrect",e.autocorrect),n=de("capitalize",e.capitalize);return{autocorrect:c(t),capitalize:c(n)}})({autocorrect:K(e),capitalize:F(e)});((e,t,n)=>{const r=()=>{t.autocorrect().get()&&oe(e,t,{on:!1,cache:!1}),t.capitalize().get()&&se(e,t,{on:!1,cache:!1})};n.onInvalid(r),e.on("SpellcheckerLanguageChanged",(e=>{return n="en",void((t=e.language).length>=2&&t.substr(0,2)===n||r());var t,n}))})(e,r,n.enforcer),((e,t)=>{e.ui.registry.addToggleMenuItem("autocorrect",{text:"Autocorrect",onAction:()=>{((e,t)=>{const n=t.autocorrect().get();oe(e,t,{on:!n,cache:!0})})(e,t)},onSetup:n=>{const r=()=>n.setActive(t.autocorrect().get());return r(),e.on(Q,r),()=>e.off(Q,r)}}),e.ui.registry.addToggleMenuItem("capitalization",{text:"Capitalization",onAction:()=>{((e,t)=>{const n=t.capitalize().get();se(e,t,{on:!n,cache:!0})})(e,t)},onSetup:n=>{const r=()=>n.setActive(t.capitalize().get());return r(),e.on(Y,r),()=>e.off(Y,r)}})})(e,r);const o=((e,t)=>({lookupPatterns:n=>{const r=[...t.autocorrect().get()?[ie(e)]:[],...t.capitalize().get()?[{regexes:ce.capitalisation,identify:e=>{var t;if(e.length>=4){const n=e[2]+e[3],r=v(e[2])+(null!==(t=e[3])&&void 0!==t?t:"");return i.some({start:n,replacement:r})}return i.none()}},{regexes:ce.normalisation,identify:e=>{var t;if(e.length>=4){const n=e[1]+e[2]+(null!==(t=e[3])&&void 0!==t?t:""),r=v(n.toLowerCase());return i.some({start:n,replacement:r})}return i.none()}}]:[]];return ue(n,r)}}))(ae(W(e),"en"),r),s=(e=>e.options.get("text_patterns_lookup"))(e);e.options.set("text_patterns_lookup",(e=>{const t=o.lookupPatterns(e.text);return[...s(e),...t]}))}})}(); |