UNPKG

react-web-code-editor

Version:

A Simple and code-style Customizable web code editor with React

2 lines (1 loc) 3.02 kB
import{checkBracketIsPaired as t,BACKTICK as e,DOUBLE_QUOTE as r,QUOTE as n,CHEVRON_OPEN as a,BRACKET_OPEN as i,PARENTHESIS_OPEN as c,BRACE_OPEN as s}from"./bracketMap.js";var o=function(){function o(t,e,r,n,a){this.textArea=t,this.currentText=e,this.caretStart=r,this.caretEnd=n,this.indent=a}return o.prototype.executeTextInput=function(t){var e=this.caretStart,r=this.caretEnd,n=this.getNewText(t);return this.setNewText(n,e+1,r+1),n},o.prototype.executeEnterAction=function(){var t=this.getCurrentLineIndentation(),e=this.indent,r=this.caretStart;if(this.isCaretSurroundedByBracket()){var n=this.getNewText("\n".concat(" ".repeat(t+e),"\n").concat(" ".repeat(t))),a=r+1+t+e;return this.setNewText(n,a,a),n}var i=this.getNewText("\n".concat(" ".repeat(t))),c=r+1+t;return this.setNewText(i,c,c),i},o.prototype.executeSpaceAction=function(){var t=this.caretStart,e=this.caretEnd,r=this.getNewText(" ");return this.setNewText(this.getNewText(" "),t+1,e+1),r},o.prototype.executeTabAction=function(){var t=this,e=t.indent,r=t.caretStart,n=t.caretEnd,a=this.getNewText(" ".repeat(e));return this.setNewText(a,r+e,n+e),a},o.prototype.executeBracketCloseAction=function(){var t=this.caretStart,e=this.caretEnd;this.setCaretPosition(t+1,e+1)},o.prototype.executeBracketOpenAction=function(t){var e=this.caretStart,r=this.caretEnd,n=this.getParenthesis(t),a=this.getNewText(n);return this.setNewText(a,e+1,r+1),a},o.prototype.refreshTextAreaHeight=function(){this.textArea.style.height="auto",this.textArea.style.height="".concat(this.textArea.scrollHeight,"px")},o.prototype.setNewText=function(t,e,r){void 0===e&&(e=this.caretStart+1),void 0===r&&(r=this.caretEnd+1),this.setValue(t),this.setCaretPosition(e,r)},o.prototype.isParenthesisPaired=function(t){var e=this,r=e.currentText,n=e.caretStart,a=e.caretEnd;return r[n-1]&&r[a]&&this.isCaretSurroundedByBracket(t)||r[n-1]===t},o.prototype.setValue=function(t){this.textArea.value=t},o.prototype.getCurrentLineIndentation=function(){for(var t=this.caretStart-1,e=0;t>=0;){var r=this.currentText[t];if("\n"===r)break;" "===r?e+=1:e=0,t-=1}return e},o.prototype.getNewText=function(t){var e=this.currentText.substring(0,this.caretStart),r=this.currentText.substring(this.caretEnd);return"".concat(e).concat(t).concat(r)},o.prototype.setCaretPosition=function(t,e){this.textArea.selectionStart=t,this.textArea.selectionEnd=e},o.prototype.getTextInBracket=function(){return this.currentText.substring(this.caretStart,this.caretEnd)},o.prototype.isCaretSurroundedByBracket=function(e){void 0===e&&(e=this.currentText[this.caretEnd]);var r=this.currentText,n=this.caretStart;return t(r[n-1],e)},o.prototype.getParenthesis=function(t){var o="";switch(this.caretStart!==this.caretEnd&&(o=this.getTextInBracket()),t){case s:o="{".concat(o,"}");break;case c:o="(".concat(o,")");break;case i:o="[".concat(o,"]");break;case a:o="<".concat(o,">");break;case n:o="'".concat(o,"'");break;case r:o='"'.concat(o,'"');break;case e:o="`".concat(o,"`")}return o},o}();export{o as default};