UNPKG

zwave-js-ui

Version:

Z-Wave Control Panel and MQTT Gateway

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