fuck-tiny-cloud/plugins/advcode/plugin.min.js

10 lines
11 KiB
JavaScript

/*!
* Tiny Enhanced Code Editor plugin
*
* Copyright (c) 2023 Ephox Corporation DBA Tiny Technologies, Inc.
* Licensed under the Tiny commercial license. See https://www.tiny.cloud/legal/
*
* Version: 7.0.0-22
*/
!function(){"use strict";const e=e=>parseInt(e,10),t=(e,t)=>{const o=e-t;return 0===o?0:o>0?1:-1},o=(e,t,o)=>({major:e,minor:t,patch:o}),n=t=>{const n=/([0-9]+)\.([0-9]+)\.([0-9]+)(?:(\-.+)?)/.exec(t);return n?o(e(n[1]),e(n[2]),e(n[3])):o(0,0,0)},r=e=>t=>typeof t===e,s=("string",e=>"string"===(e=>{const t=typeof e;return null===e?"null":"object"===t&&Array.isArray(e)?"array":"object"===t&&(o=n=e,(r=String).prototype.isPrototypeOf(o)||(null===(s=n.constructor)||void 0===s?void 0:s.name)===r.name)?"string":t;var o,n,r,s})(e));const i=r("boolean"),c=r("function"),a=r("number");class d{constructor(e,t){this.tag=e,this.value=t}static some(e){return new d(!0,e)}static none(){return d.singletonNone}fold(e,t){return this.tag?t(this.value):e()}isSome(){return this.tag}isNone(){return!this.tag}map(e){return this.tag?d.some(e(this.value)):d.none()}bind(e){return this.tag?e(this.value):d.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:d.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(null!=e?e:"Called getOrDie on None")}static from(e){return null==e?d.none():d.some(e)}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()"}}d.singletonNone=new d(!1);let l=0;const u=e=>{const t=(new Date).getTime(),o=Math.floor(1e9*Math.random());return l++,e+"_"+o+l+String(t)},m=e=>t=>t.options.get(e),g=m("codemirror_script"),h=m("codemirror_linewrapping"),p=m("codemirror_linenumbers"),f=m("codemirror_foldgutter"),v=m("codemirror_gutter"),y=m("advcode_headless"),b=m("advcode_inline"),_=(e,t,o)=>({customEditorScriptUrl:t+"/customeditor.min.js",codeMirrorScriptUrl:g(e),lineWrapping:h(e),lineNumbers:p(e),foldGutter:f(e),gutter:v(e),editorId:e.id,advcodeInstanceId:u("advcode_instance"),currentAdvStatus:o}),S=()=>{},w=()=>!1,C=e=>{if(null==e)throw new Error("Node cannot be null or undefined");return{dom:e}},k=(e,t)=>{const o=(t||document).createElement("div");if(o.innerHTML=e,!o.hasChildNodes()||o.childNodes.length>1){const t="HTML does not have a single root node";throw console.error(t,e),new Error(t)}return C(o.childNodes[0])},x=(e,t)=>{const o=(t||document).createElement(e);return C(o)},z=C;"undefined"!=typeof window?window:Function("return this;")();const A=(e,t,o)=>{let n=e.dom;const r=c(o)?o:w;for(;n.parentNode;){n=n.parentNode;const e=z(n);if(t(e))return d.some(e);if(r(e))break}return d.none()},I=Object.hasOwnProperty,N=(e,t)=>I.call(e,t),T=e=>{const t=e.dom;null!==t.parentNode&&t.parentNode.removeChild(t)},F="\x3c!--mce_cursor--\x3e",M=(e,t)=>{const o=e.dom,n=e.selection,r=n.getBookmark(),s=(e=>N(e,"name"))(r)?o.select(r.name)[r.index]:o.select(`#${r.id}_start`)[0],i=d.from(s).map((t=>{const o=z(e.getBody());return((e,t)=>{const n=k(F),r=z(e),s=((e,t,o)=>((e,t,o,n,r)=>n(o)?d.some(o):c(r)&&r(o)?d.none():t(o,n,r))(0,A,e,t,o))(r,(e=>((e,t)=>{const o=e.dom;return!(!o||!o.hasAttribute)&&o.hasAttribute("data-mce-bogus")})(e)),(e=>{return t=o,e.dom===t.dom;var t})).getOr(r);return((e,t)=>{const o=(e=>d.from(e.dom.parentNode).map(z))(e);o.each((o=>{o.dom.insertBefore(t.dom,e.dom)}))})(s,n),n})(t)})),a=t();return i.each(T),n.moveToBookmark(r),a},E=e=>{const t=(e=>{const t=e.indexOf(F);return-1===t?0:t})(M(e,(()=>e.getContent({source_view:!0}))));return{content:e.getContent({source_view:!0}),cursor:t}},j=e=>{let t=e;return{get:()=>t,set:e=>{t=e}}},D=()=>{const e=(e=>{const t=j(d.none()),o=()=>t.get().each(e);return{clear:()=>{o(),t.set(d.none())},isSet:()=>t.get().isSome(),get:()=>t.get(),set:e=>{o(),t.set(d.some(e))}}})(S);return{...e,on:t=>e.get().each(t)}},O=(e,t)=>{e.focus(),e.undoManager.transact((()=>{e.setContent(t)})),e.selection.setCursorLocation(),e.nodeChanged()},V=e=>{return(t=tinymce,"get",N(t,"get")?d.from(t.get):d.none()).bind((t=>d.from(t.call(tinymce,e))));var t},B=(e,t)=>V(e).bind((e=>d.from(e.plugins.advcode))).map((e=>(e[t]||(e[t]=D()),e[t]))),P=(e,t)=>{V(e).bind((e=>d.from(e.plugins.advcode))).each((e=>{e[t]&&delete e[t]}))},L=e=>({name:"codeview",type:"customeditor",tag:"div",onFocus:e=>{((e,t)=>{const o=e=>{for(let t=0;t<e.childNodes.length;t++){const n=z(e.childNodes[t]);if(n.dom.shadowRoot)return d.some(n);const r=o(e.childNodes[t]);if(r.isSome())return r}return d.none()};return o(e.dom)})(z(e)).each((e=>{var t;null===(t=e.dom.shadowRoot.querySelector(".cm-content"))||void 0===t||t.focus()}))},scriptId:"tinymce.plugins.advcode.customeditor",scriptUrl:e.customEditorScriptUrl,settings:e}),R=(e,t)=>{const o={dark:!1,fullscreen:!1,fontSize:d.none()};e.addCommand("mceCodeEditor",(()=>{b(e)&&!e.inline?"code"===e.mode.get()?e.mode.set("design"):e.mode.set("code"):(()=>{const n=_(e,t,o);((e,t)=>{const o=E(e),n={...t,cursor:o.cursor},r=t.currentAdvStatus,s=B(t.editorId,t.advcodeInstanceId);var i;e.windowManager.open((i=o.content,{title:"Source Code",size:"large",body:{type:"panel",items:[L(n)]},buttons:(()=>{let e=[{type:"togglebutton",name:"dark_theme_toggle",text:"Dark/light mode",active:r.dark,align:"start"},{type:"custom",name:"increase_font_size",text:"Increase font size",icon:"text-size-increase",align:"start"},{type:"custom",name:"decrease_font_size",text:"Decrease font size",icon:"text-size-decrease",align:"start"},{type:"cancel",name:"cancel",text:"Cancel",align:"end"},{type:"submit",name:"save",text:"Save",primary:!0,align:"end"}];return window.isSecureContext?e=[{type:"custom",name:"copy_code",text:"Copy code",align:"start"}].concat(e):console.warn("Copy code is not allowed since window.isSecureContext is false"),e})(),initialData:{codeview:i},onSubmit:o=>{O(e,o.getData().codeview),P(t.editorId,t.advcodeInstanceId),o.close()},onClose:()=>{P(t.editorId,t.advcodeInstanceId)},onAction:(e,t)=>{"copy_code"===t.name&&s.each((t=>t.get().each((t=>{(async()=>{const o=setTimeout((()=>{e.block("copying")}),150);await t.copyCode(),clearTimeout(o),e.unblock()})()})))),"increase_font_size"===t.name&&s.each((e=>e.get().each((e=>{r.fontSize=e.changeFontSize("increase")})))),"decrease_font_size"===t.name&&s.each((e=>e.get().each((e=>{r.fontSize=e.changeFontSize("decrease")})))),"dark_theme_toggle"===t.name&&s.each((e=>e.get().each((e=>{e.setDarkMode(!r.dark),r.dark=!r.dark}))))}}))})(e,n)})()}))},U=(e,t)=>((e,t,o)=>{const n=((e,t)=>{const o=((e,t)=>{const o=e.dom.getAttribute(t);return null===o?void 0:o})(e,t);return void 0===o||""===o?[]:o.split(" ")})(e,t);return((e,t,o)=>{((e,t,o)=>{if(!(s(o)||i(o)||a(o)))throw console.error("Invalid call to Attribute.set. Key ",t,":: Value ",o,":: Element ",e),new Error("Attribute value was not simple");e.setAttribute(t,o+"")})(e.dom,t,o)})(e,t,n.concat([o]).join(" ")),!0})(e,"class",t),q=e=>e.plugins.fullscreen&&e.plugins.fullscreen.isFullscreen(),H=(e,t,o)=>{const n=j({dark:!1,fullscreen:!1,fontSize:d.none()}),r=j(q(e)),s=D(),i=j(d.none()),c={dark:!1,fullscreen:!1,fontSize:d.none()},a=()=>i.get().bind((t=>B(e.id,t))),l=()=>{s.get().each((({destroy:t})=>{t(),n.get().fullscreen!==r.get()&&e.execCommand("mceFullScreen")})),s.clear()};e.ui.registry.addView("code",{buttons:1===o?[{type:"group",buttons:[...e.hasPlugin("fullscreen")?[{type:"togglebutton",icon:"fullscreen",tooltip:"Fullsceen",onAction:t=>{e.execCommand("mceFullScreen",void 0,void 0,{skip_focus:!0});const o=!t.isActive();n.set({...n.get(),fullscreen:o}),t.setActive(o)}}]:[],{type:"togglebutton",icon:"copy",text:"Copy code",onAction:e=>a().each((e=>e.get().each((e=>{(async()=>{await e.copyCode()})()}))))}]},{type:"group",buttons:[{type:"togglebutton",text:"Dark/light mode",onAction:e=>{a().each((t=>t.get().each((t=>{n.set({...n.get(),dark:!e.isActive()}),e.setActive(n.get().dark),t.setDarkMode(n.get().dark)}))))}},{type:"togglebutton",icon:"text-size-increase",tooltip:"Increase font size",onAction:e=>a().each((e=>e.get().each((e=>n.set({...n.get(),fontSize:e.changeFontSize("increase")})))))},{type:"togglebutton",icon:"text-size-decrease",tooltip:"Decrease font size",onAction:e=>a().each((e=>e.get().each((e=>n.set({...n.get(),fontSize:e.changeFontSize("decrease")})))))}]},{type:"group",buttons:[{type:"button",text:"Cancel",onAction:()=>{s.get().each((()=>{l(),e.mode.set("design")}))}},{type:"button",text:"Save code",buttonType:"primary",onAction:()=>{s.get().each((({syncWithEditor:t})=>{t(),l(),e.mode.set("design")}))}}]}]:[],onShow:a=>{const l=a.getContainer(),u=_(e,t,c);i.set(d.some(u.advcodeInstanceId)),r.set(q(e)),e.setProgressState(!0),n.get().fullscreen!==q(e)&&e.execCommand("mceFullScreen"),(async e=>{var t;return(await(t=e,tinymce.Resource.load("tinymce.plugins.advcode.CodeMirror",t)))()})(g(e)).then((t=>{e.setProgressState(!1),s.set(((e,t,o,n,r,s)=>{const i=E(o),c={...r,cursor:i.cursor},a=x("div");var d;d=a,((e,t)=>{for(let t=0,n=e.length;t<n;t++)o=e[t],((e,t)=>{(e=>void 0!==e.dom.classList)(e)?e.dom.classList.add(t):U(e,t)})(d,o);var o})(0===t?["tox-inline-headless-codemirror","mce-codemirror"]:["tox-custom-editor","tox-inline-codemirror","mce-codemirror"]),((e,t)=>{e.dom.appendChild(t.dom)})(z(e),a);const{getValue:l,destroy:u,copyCode:m,changeFontSize:g,setDarkMode:h,setFontSize:p}=n(a.dom,c,i.content,s);B(r.editorId,r.advcodeInstanceId).each((e=>e.set({copyCode:m,changeFontSize:g,setDarkMode:h,setFontSize:p}))),p(s.fontSize);const f=()=>O(o,l());return 0===t&&o.on("BeforeGetContent",f),{syncWithEditor:f,destroy:()=>{0===t&&(o.off("BeforeGetContent",f),f()),T(a),u()}}})(l,o,e,t,u,n.get()))}))},onHide:l})};tinymce.PluginManager.requireLangPack("advcode","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("advcode",((e,o)=>{((e,o)=>!!e&&-1===((e,o)=>{const n=t(e.major,o.major);if(0!==n)return n;const r=t(e.minor,o.minor);if(0!==r)return r;const s=t(e.patch,o.patch);return 0!==s?s:0})((e=>n((e=>[e.majorVersion,e.minorVersion].join(".").split(".").slice(0,3).join("."))(e)))(e),n(o)))(tinymce,"6.8.0")?console.error("The advcode plugin requires at least version 6.8.0 of TinyMCE."):((e,t)=>{((e,t)=>{const o=e.options.register;o("codemirror_script",{processor:"string",default:t+"/codemirror.min.js"}),o("codemirror_linewrapping",{processor:"boolean",default:!0}),o("codemirror_linenumbers",{processor:"boolean",default:!0}),o("codemirror_foldgutter",{processor:"boolean",default:!0}),o("codemirror_gutter",{processor:"boolean",default:!0}),o("advcode_headless",{processor:"boolean",default:!1}),o("advcode_inline",{processor:"boolean",default:!1})})(e,t),R(e,t),(e=>{const t="sourcecode",o=()=>e.execCommand("mceCodeEditor");e.ui.registry.addButton("code",{icon:t,tooltip:"Source code",onAction:o}),e.ui.registry.addMenuItem("code",{icon:t,text:"Source code",onAction:o})})(e),((e,t)=>{H(e,t,y(e)?0:1)})(e,t),(e=>{e.mode.register("code",{activate:()=>{"code"!==e.queryCommandValue("ToggleView")&&e.execCommand("ToggleView",!1,"code")},deactivate:()=>{"code"===e.queryCommandValue("ToggleView")&&e.execCommand("ToggleView",!1,"code")},editorReadOnly:!1})})(e)})(e,o)}))}();