actuel-inactuel/site/plugins/code-editor/index.js

15 lines
39 KiB
JavaScript
Raw Normal View History

2025-02-18 10:16:52 +01:00
(()=>{(function(){"use strict";function D(){return D=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var d in i)Object.prototype.hasOwnProperty.call(i,d)&&(r[d]=i[d])}return r},D.apply(this,arguments)}var ae=13,re=9,ie=8,se=89,B=90,oe=77,Y=57,G=219,U=222,Q=192,le=27,X=100,ue=3e3,de=typeof window!="undefined"&&navigator&&/Win/i.test(navigator.platform),M=typeof window!="undefined"&&navigator&&/(Mac|iPhone|iPod|iPad)/i.test(navigator.platform),ce={props:{lineNumbers:{type:Boolean,default:!1},autoStyleLineNumbers:{type:Boolean,default:!0},readonly:{type:Boolean,default:!1},value:{type:String,default:""},highlight:{type:Function,required:!0},tabSize:{type:Number,default:2},insertSpaces:{type:Boolean,default:!0},ignoreTabKey:{type:Boolean,default:!1},placeholder:{type:String,default:""}},data:function(){return{capture:!0,history:{stack:[],offset:-1},lineNumbersHeight:"20px",codeData:""}},watch:{value:{immediate:!0,handler:function(e){e?this.codeData=e:this.codeData=""}},content:{immediate:!0,handler:function(){var e=this;this.lineNumbers&&this.$nextTick(function(){e.setLineNumbersHeight()})}},lineNumbers:function(){var e=this;this.$nextTick(function(){e.styleLineNumbers(),e.setLineNumbersHeight()})}},computed:{isEmpty:function(){return this.codeData.length===0},content:function(){var e=this.highlight(this.codeData)+"<br />";return e},lineNumbersCount:function(){var e=this.codeData.split(/\r\n|\n/).length;return e}},mounted:function(){this._recordCurrentState(),this.styleLineNumbers()},methods:{setLineNumbersHeight:function(){this.lineNumbersHeight=getComputedStyle(this.$refs.pre).height},styleLineNumbers:function(){if(!(!this.lineNumbers||!this.autoStyleLineNumbers)){var e=this.$refs.pre,i=this.$el.querySelector(".prism-editor__line-numbers"),d=window.getComputedStyle(e);this.$nextTick(function(){var h="border-top-left-radius",b="border-bottom-left-radius";if(!!i){i.style[h]=d[h],i.style[b]=d[b],e.style[h]="0",e.style[b]="0";var p=["background-color","margin-top","padding-top","font-family","font-size","line-height"];p.forEach(function(a){i.style[a]=d[a]}),i.style["margin-bottom"]="-"+d["padding-top"]}})}},_recordCurrentState:function(){var e=this.$refs.textarea;if(!!e){var i=e.value,d=e.selectionStart,h=e.selectionEnd;this._recordChange({value:i,selectionStart:d,selectionEnd:h})}},_getLines:function(e,i){return e.substring(0,i).split(`
`)},_applyEdits:function(e){var i=this.$refs.textarea,d=this.history.stack[this.history.offset];d&&i&&(this.history.stack[this.history.offset]=D({},d,{selectionStart:i.selectionStart,selectionEnd:i.selectionEnd})),this._recordChange(e),this._updateInput(e)},_recordChange:function(e,i){i===void 0&&(i=!1);var d=this.history,h=d.stack,b=d.offset;if(h.length&&b>-1){this.history.stack=h.slice(0,b+1);var p=this.history.stack.length;if(p>X){var a=p-X;this.history.stack=h.slice(a,p),this.history.offset=Math.max(this.history.offset-a,0)}}var u=Date.now();if(i){var c=this.history.stack[this.history.offset];if(c&&u-c.timestamp<ue){var x,$,v=/[^a-z0-9]([a-z0-9]+)$/i,S=(x=this._getLines(c.value,c.selectionStart).pop())===null||x===void 0?void 0:x.match(v),F=($=this._getLines(e.value,e.selectionStart).pop())===null||$===void 0?void 0:$.match(v);if(S&&F&&F[1].startsWith(S[1])){this.history.stack[this.history.offset]=D({},e,{timestamp:u});return}}}this.history.stack.push(D({},e,{timestamp:u})),this.history.offset++},_updateInput:function(e){var i=this.$refs.textarea;!i||(i.value=e.value,i.selectionStart=e.selectionStart,i.selectionEnd=e.selectionEnd,this.$emit("input",e.value))},handleChange:function(e){var i=e.target,d=i.value,h=i.selectionStart,b=i.selectionEnd;this._recordChange({value:d,selectionStart:h,selectionEnd:b},!0),this.$emit("input",d)},_undoEdit:function(){var e=this.history,i=e.stack,d=e.offset,h=i[d-1];h&&(this._updateInput(h),this.history.offset=Math.max(d-1,0))},_redoEdit:function(){var e=this.history,i=e.stack,d=e.offset,h=i[d+1];h&&(this._updateInput(h),this.history.offset=Math.min(d+1,i.length-1))},handleKeyDown:function(e){var i=this.tabSize,d=this.insertSpaces,h=this.ignoreTabKey;if(!(this.$listeners.keydown&&(this.$emit("keydown",e),e.defaultPrevented))){e.keyCode===le&&(e.target.blur(),this.$emit("blur",e));var b=e.target,p=b.value,a=b.selectionStart,u=b.selectionEnd,c=(d?" ":" ").repeat(i);if(e.keyCode===re&&!h&&this.capture)if(e.preventDefault(),e.shiftKey){var x=this._getLines(p,a),$=x.length-1,v=this._getLines(p,u).length-1,S=p.split(`
`).map(function(k,K){return K>=$&&K<=v&&k.startsWith(c)?k.substring(c.length):k}).join(`
`);if(p!==S){var F=x[$];this._applyEdits({value:S,selectionStart:F.startsWith(c)?a-c.length:a,selectionEnd:u-(p.length-S.length)})}}else if(a!==u){var E=this._getLines(p,a),z=E.length-1,P=this._getLines(p,u).length-1,t=E[z];this._applyEdits({value:p.split(`
`).map(function(k,K){return K>=z&&K<=P?c+k:k}).join(`
`),selectionStart:/\S/.test(t)?a+c.length:a,selectionEnd:u+c.length*(P-z+1)})}else{var n=a+c.length;this._applyEdits({value:p.substring(0,a)+c+p.substring(u),selectionStart:n,selectionEnd:n})}else if(e.keyCode===ie){var s=a!==u,o=p.substring(0,a);if(o.endsWith(c)&&!s){e.preventDefault();var l=a-c.length;this._applyEdits({value:p.substring(0,a-c.length)+p.substring(u),selectionStart:l,selectionEnd:l})}}else if(e.keyCode===ae){if(a===u){var f=this._getLines(p,a).pop(),y=f==null?void 0:f.match(/^\s+/);if(y&&y[0]){e.preventDefault();var g=`
`+y[0],w=a+g.length;this._applyEdits({value:p.substring(0,a)+g+p.substring(u),selectionStart:w,selectionEnd:w})}}}else if(e.keyCode===Y||e.keyCode===G||e.keyCode===U||e.keyCode===Q){var m;e.keyCode===Y&&e.shiftKey?m=["(",")"]:e.keyCode===G?e.shiftKey?m=["{","}"]:m=["[","]"]:e.keyCode===U?e.shiftKey?m=['"','"']:m=["'","'"]:e.keyCode===Q&&!e.shiftKey&&(m=["`","`"]),a!==u&&m&&(e.preventDefault(),this._applyEdits({value:p.substring(0,a)+m[0]+p.substring(a,u)+m[1]+p.substring(u),selectionStart:a,selectionEnd:u+2}))}else(M?e.metaKey&&e.keyCode===B:e.ctrlKey&&e.keyCode===B)&&!e.shiftKey&&!e.altKey?(e.preventDefault(),this._undoEdit()):(M?e.metaKey&&e.keyCode===B&&e.shiftKey:de?e.ctrlKey&&e.keyCode===se:e.ctrlKey&&e.keyCode===B&&e.shiftKey)&&!e.altKey?(e.preventDefault(),this._redoEdit()):e.keyCode===oe&&e.ctrlKey&&(M?e.shiftKey:!0)&&(e.preventDefault(),this.capture=!this.capture)}}},render:function(e){var i=this,d=e("div",{attrs:{class:"prism-editor__line-width-calc",style:"height: 0px; visibility: hidden; pointer-events: none;"}},"999"),h=e("div",{staticClass:"prism-editor__line-numbers",style:{"min-height":this.lineNumbersHeight},attrs:{"aria-hidden":"true"}},[d,Array.from(Array(this.lineNumbersCount).keys()).map(function(u,c){return e("div",{attrs:{class:"prism-editor__line-number token comment"}},""+ ++c)})]),b=e("textarea",{ref:"textarea",on:{input:this.handleChange,keydown:this.handleKeyDown,click:function(c){i.$emit("click",c)},keyup:function(c){i.$emit("keyup",c)},focus:function(c){i.$emit("focus",c)},blur:function(c){i.$emit("blur",c)}},staticClass:"prism-editor__textarea",class:{"prism-editor__textarea--empty":this.isEmpty},attrs:{spellCheck:"false",autocapitalize:"off",autocomplete:"off",autocorrect:"off","data-gramm":"false",placeholder:this.placeholder,"data-testid":"textarea",readonly:this.readonly},domProps:{value:this.codeData}}),p=e("pre",{ref:"pre",staticClass:"prism-editor__editor",attrs:{"data-testid":"preview"},domProps:{innerHTML:this.content}}),a=e("div",{staticClass:"prism-editor__container"},[b,p]);return e("div",{staticClass:"prism-editor-wrapper"},[this.lineNumbers&&h,a])}},xe="",J=typeof globalThis!="undefined"?globalThis:typeof window!="undefined"?window:typeof global!="undefined"?global:typeof self!="undefined"?self:{},R={exports:{}};(function(r){var e=typeof window!="undefined"?window:typeof WorkerGlobalScope!="undefined"&&self instanceof WorkerGlobalScope?self:{};/**
* Prism: Lightweight, robust, elegant syntax highlighting
*
* @license MIT <https://opensource.org/licenses/MIT>
* @author Lea Verou <https://lea.verou.me>
* @namespace
* @public
*/var i=function(d){var h=/\blang(?:uage)?-([\w-]+)\b/i,b=0,p={},a={manual:d.Prism&&d.Prism.manual,disableWorkerMessageHandler:d.Prism&&d.Prism.disableWorkerMessageHandler,util:{encode:function t(n){return n instanceof u?new u(n.type,t(n.content),n.alias):Array.isArray(n)?n.map(t):n.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/\u00a0/g," ")},type:function(t){return Object.prototype.toString.call(t).slice(8,-1)},objId:function(t){return t.__id||Object.defineProperty(t,"__id",{value:++b}),t.__id},clone:function t(n,s){s=s||{};var o,l;switch(a.util.type(n)){case"Object":if(l=a.util.objId(n),s[l])return s[l];o={},s[l]=o;for(var f in n)n.hasOwnProperty(f)&&(o[f]=t(n[f],s));return o;case"Array":return l=a.util.objId(n),s[l]?s[l]:(o=[],s[l]=o,n.forEach(function(y,g){o[g]=t(y,s)}),o);default:return n}},getLanguage:function(t){for(;t&&!h.test(t.className);)t=t.parentElement;return t?(t.className.match(h)||[,"none"])[1].toLowerCase():"none"},currentScript:function(){if(typeof document=="undefined")return null;if("currentScript"in document&&1<2)return document.currentScript;try{throw new Error}catch(o){var t=(/at [^(\r\n]*\((.*):[^:]+:[^:]+\)$/i.exec(o.stack)||[])[1];if(t){var n=document.getElementsByTagName("script");for(var s in n)if(n[s].src==t)return n[s]}return null}},isActive:function(t,n,s){for(var o="no-"+n;t;){var l=t.classList;if(l.contains(n))return!0;if(l.contains(o))return!1;t=t.parentElement}return!!s}},languages:{plain:p,plaintext:p,text:p,txt:p,extend:function(t,n){var s=a.util.clone(a.languages[t]);for(var o in n)s[o]=n[o];return s},insertBefore:function(t,n,s,o){o=o||a.languages;var l=o[t],f={};for(var y in l)if(l.hasOwnProperty(y)){if(y==n)for(var g in s)s.hasOwnProperty(g)&&(f[g]=s[g]);s.hasOwnProperty(y)||(f[y]=l[y])}var w=o[t];return o[t]=f,a.languages.DFS(a.languages,function(m,k){k===w&&m!=t&&(this[m]=f)}),f},DFS:function t(n,s,o,l){l=l||{};var f=a.util.objId;for(var y in n)if(n.hasOwnProperty(y)){s.call(n,y,n[y],o||y);var g=n[y],w=a.util.type(g);w==="Object"&&!l[f(g)]?(l[f(g)]=!0,t(g,s,null,l)):w==="Array"&&!l[f(g)]&&(l[f(g)]=!0,t(g,s,y,l))}}},plugins:{},highlightAll:function(t,n){a.highlightAllUnder(document,t,n)},highlightAllUnder:function(t,n,s){var o={callback:s,container:t,selector:'code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code'};a.hooks.run("before-highlightall",o),o.elements=Array.prototype.slice.apply(o.container.querySelectorAll(o.selector)),a.hooks.run("before-all-elements-highlight",o);for(var l=0,f;f=o.elements[l++];)a.highlightElement(f,n===!0,o.callback)},highlightElement:function(t,n,s){var o=a.util.getLanguage(t),l=a.languages[o];t.className=t.className.replace(h,"").replace(/\s+/g," ")+" language-"+o;var f=t.parentElement;f&&f.nodeName.toLowerCase()==="pre"&&(f.className=f.className.replace(h,"").replace(/\s+/g," ")+" language-"+o);var y=t.textContent,g={element:t,language:o,grammar:l,code:y};function w(k){g.highlightedCode=k,a.hooks.run("before-insert",g),g.element.innerHTML=g.highlightedCode,a.hooks.run("after-highlight",g),a.hooks.run("complete",g),s&&s.call(g.element)}if(a.hooks.run("before-sanity-check",g),f=g.element.parentElement,f&&f.nodeName.toLowerCase()==="pre"&&!f.hasAttribute("tabindex")&&f.setAttribute("tabindex","0"),!g.code){a.hooks.run("complete",g),s&&s.call(g.element);return}if(a.hooks.run("before-highlight",g),!g.grammar){w(a.util.encode(g.code));return}if(n&&d.Worker){var m=new Worker(a.filename);m.onmessage=function(k){w(k.data)},m.postMessage(JSON.stringify({language:g.language,code:g.code,immediateClose:!0}))}else w(a.highlight(g.code,g.grammar,g.language))},highlight:function(t,n,s){var o={code:t,grammar:n,language:s};return a.hooks.run("before-tokenize",o),o.tokens=a.tokenize(o.code,o.grammar),a.hooks.run("after-tokenize",o),u.stringify(a.util.encode(o.tokens),o.language)},tokenize:function(t,n){var s=n.rest;if(s){for(var o in s)n[o]=s[o];delete n.rest}var l=new $;return v(l,l.head,t),x(t,l,n,l.head,0),F(l)},hooks:{all:{},add:function(t,n){var s=a.hooks.all;s[t]=s[t]||[],s[t].push(n)},run:function(t,n){var