import{V as I}from"./index-Ms_1iFoB.js";function p(){return p=Object.assign||function(o){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(o[i]=e[i])}return o},p.apply(this,arguments)}var M=13,z=9,B=8,W=89,E=90,R=77,w=57,x=219,L=222,T=192,j=27,Y=100,q=3e3,Q=typeof window<"u"&&navigator&&/Win/i.test(navigator.platform),k=typeof window<"u"&&navigator&&/(Mac|iPhone|iPod|iPad)/i.test(navigator.platform),F=I.extend({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(t){t?this.codeData=t:this.codeData=""}},content:{immediate:!0,handler:function(){var t=this;this.lineNumbers&&this.$nextTick(function(){t.setLineNumbersHeight()})}},lineNumbers:function(){var t=this;this.$nextTick(function(){t.styleLineNumbers(),t.setLineNumbersHeight()})}},computed:{isEmpty:function(){return this.codeData.length===0},content:function(){var t=this.highlight(this.codeData)+"<br />";return t},lineNumbersCount:function(){var t=this.codeData.split(/\r\n|\n/).length;return t}},mounted:function(){this._recordCurrentState(),this.styleLineNumbers()},methods:{setLineNumbersHeight:function(){this.lineNumbersHeight=getComputedStyle(this.$refs.pre).height},styleLineNumbers:function(){if(!(!this.lineNumbers||!this.autoStyleLineNumbers)){var t=this.$refs.pre,e=this.$el.querySelector(".prism-editor__line-numbers"),i=window.getComputedStyle(t);this.$nextTick(function(){var l="border-top-left-radius",u="border-bottom-left-radius";if(e){e.style[l]=i[l],e.style[u]=i[u],t.style[l]="0",t.style[u]="0";var r=["background-color","margin-top","padding-top","font-family","font-size","line-height"];r.forEach(function(s){e.style[s]=i[s]}),e.style["margin-bottom"]="-"+i["padding-top"]}})}},_recordCurrentState:function(){var t=this.$refs.textarea;if(t){var e=t.value,i=t.selectionStart,l=t.selectionEnd;this._recordChange({value:e,selectionStart:i,selectionEnd:l})}},_getLines:function(t,e){return t.substring(0,e).split(`
`)},_applyEdits:function(t){var e=this.$refs.textarea,i=this.history.stack[this.history.offset];i&&e&&(this.history.stack[this.history.offset]=p({},i,{selectionStart:e.selectionStart,selectionEnd:e.selectionEnd})),this._recordChange(t),this._updateInput(t)},_recordChange:function(t,e){e===void 0&&(e=!1);var i=this.history,l=i.stack,u=i.offset;if(l.length&&u>-1){this.history.stack=l.slice(0,u+1);var r=this.history.stack.length;if(r>Y){var s=r-Y;this.history.stack=l.slice(s,r),this.history.offset=Math.max(this.history.offset-s,0)}}var a=Date.now();if(e){var n=this.history.stack[this.history.offset];if(n&&a-n.timestamp<q){var f,d,m=/[^a-z0-9]([a-z0-9]+)$/i,c=(f=this._getLines(n.value,n.selectionStart).pop())===null||f===void 0?void 0:f.match(m),g=(d=this._getLines(t.value,t.selectionStart).pop())===null||d===void 0?void 0:d.match(m);if(c&&g&&g[1].startsWith(c[1])){this.history.stack[this.history.offset]=p({},t,{timestamp:a});return}}}this.history.stack.push(p({},t,{timestamp:a})),this.history.offset++},_updateInput:function(t){var e=this.$refs.textarea;e&&(e.value=t.value,e.selectionStart=t.selectionStart,e.selectionEnd=t.selectionEnd,this.$emit("input",t.value))},handleChange:function(t){var e=t.target,i=e.value,l=e.selectionStart,u=e.selectionEnd;this._recordChange({value:i,selectionStart:l,selectionEnd:u},!0),this.$emit("input",i)},_undoEdit:function(){var t=this.history,e=t.stack,i=t.offset,l=e[i-1];l&&(this._updateInput(l),this.history.offset=Math.max(i-1,0))},_redoEdit:function(){var t=this.history,e=t.stack,i=t.offset,l=e[i+1];l&&(this._updateInput(l),this.history.offset=Math.min(i+1,e.length-1))},handleKeyDown:function(t){var e=this.tabSize,i=this.insertSpaces,l=this.ignoreTabKey;if(!(this.$listeners.keydown&&(this.$emit("keydown",t),t.defaultPrevented))){t.keyCode===j&&(t.target.blur(),this.$emit("blur",t));var u=t.target,r=u.value,s=u.selectionStart,a=u.selectionEnd,n=(i?" ":"	").repeat(e);if(t.keyCode===z&&!l&&this.capture)if(t.preventDefault(),t.shiftKey){var f=this._getLines(r,s),d=f.length-1,m=this._getLines(r,a).length-1,c=r.split(`
`).map(function(v,y){return y>=d&&y<=m&&v.startsWith(n)?v.substring(n.length):v}).join(`
`);if(r!==c){var g=f[d];this._applyEdits({value:c,selectionStart:g.startsWith(n)?s-n.length:s,selectionEnd:a-(r.length-c.length)})}}else if(s!==a){var S=this._getLines(r,s),b=S.length-1,K=this._getLines(r,a).length-1,P=S[b];this._applyEdits({value:r.split(`
`).map(function(v,y){return y>=b&&y<=K?n+v:v}).join(`
`),selectionStart:/\S/.test(P)?s+n.length:s,selectionEnd:a+n.length*(K-b+1)})}else{var D=s+n.length;this._applyEdits({value:r.substring(0,s)+n+r.substring(a),selectionStart:D,selectionEnd:D})}else if(t.keyCode===B){var A=s!==a,H=r.substring(0,s);if(H.endsWith(n)&&!A){t.preventDefault();var N=s-n.length;this._applyEdits({value:r.substring(0,s-n.length)+r.substring(a),selectionStart:N,selectionEnd:N})}}else if(t.keyCode===M){if(s===a){var _=this._getLines(r,s).pop(),C=_==null?void 0:_.match(/^\s+/);if(C&&C[0]){t.preventDefault();var O=`
`+C[0],$=s+O.length;this._applyEdits({value:r.substring(0,s)+O+r.substring(a),selectionStart:$,selectionEnd:$})}}}else if(t.keyCode===w||t.keyCode===x||t.keyCode===L||t.keyCode===T){var h;t.keyCode===w&&t.shiftKey?h=["(",")"]:t.keyCode===x?t.shiftKey?h=["{","}"]:h=["[","]"]:t.keyCode===L?t.shiftKey?h=['"','"']:h=["'","'"]:t.keyCode===T&&!t.shiftKey&&(h=["`","`"]),s!==a&&h&&(t.preventDefault(),this._applyEdits({value:r.substring(0,s)+h[0]+r.substring(s,a)+h[1]+r.substring(a),selectionStart:s,selectionEnd:a+2}))}else(k?t.metaKey&&t.keyCode===E:t.ctrlKey&&t.keyCode===E)&&!t.shiftKey&&!t.altKey?(t.preventDefault(),this._undoEdit()):(k?t.metaKey&&t.keyCode===E&&t.shiftKey:Q?t.ctrlKey&&t.keyCode===W:t.ctrlKey&&t.keyCode===E&&t.shiftKey)&&!t.altKey?(t.preventDefault(),this._redoEdit()):t.keyCode===R&&t.ctrlKey&&(!k||t.shiftKey)&&(t.preventDefault(),this.capture=!this.capture)}}},render:function(t){var e=this,i=t("div",{attrs:{class:"prism-editor__line-width-calc",style:"height: 0px; visibility: hidden; pointer-events: none;"}},"999"),l=t("div",{staticClass:"prism-editor__line-numbers",style:{"min-height":this.lineNumbersHeight},attrs:{"aria-hidden":"true"}},[i,Array.from(Array(this.lineNumbersCount).keys()).map(function(a,n){return t("div",{attrs:{class:"prism-editor__line-number token comment"}},""+ ++n)})]),u=t("textarea",{ref:"textarea",on:{input:this.handleChange,keydown:this.handleKeyDown,click:function(n){e.$emit("click",n)},keyup:function(n){e.$emit("keyup",n)},focus:function(n){e.$emit("focus",n)},blur:function(n){e.$emit("blur",n)}},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}}),r=t("pre",{ref:"pre",staticClass:"prism-editor__editor",attrs:{"data-testid":"preview"},domProps:{innerHTML:this.content}}),s=t("div",{staticClass:"prism-editor__container"},[u,r]);return t("div",{staticClass:"prism-editor-wrapper"},[this.lineNumbers&&l,s])}});export{F as PrismEditor};
