2
\$\begingroup\$

Recently I started learning Electron - so I wrote a new javascript library for my personal use in the app - MyText.js - to implement a code-editor like interface to my App like auto indent, auto-open-close parenthesis,braces .

As full source cannot be inserted here (due to question restrictions) - so I have provided a link to my project 1.

var text = text || (function(id){

    var textarea = document.getElementById(id);

    textarea.onkeydown = function(e){
        if(window.event.shiftKey){
            if(e.key == "("){
                e.preventDefault();
                var start = this.selectionStart;
                var end = this.selectionEnd;
                var val = this.value;
                this.value = val.slice(0,start) + "()" + val.slice(end);
                this.selectionEnd=start+1;
            }

            // similar logic for other characters

        }

        if(e.keyCode == 13){
            e.preventDefault();
            var start = this.selectionStart;
            var end = this.selectionEnd;
            var val = this.value;
            var indent = this.value.substring(0,start);
            indent = indent.match(/(^|\n)([ ]*)[^\n]*$/);
            indent = indent[2];
            if(val[start-1] == "{" || val[start-1] == "[" || val[start-1] == "("){
                var previous_indent=indent;
                indent+="    ";
                indent = "\n" + indent + "\n" + previous_indent;
                this.value = val.slice(0,start) + indent + val.slice(end);
                this.selectionEnd = start+5+previous_indent.length;
            }

            else if(val[start-1] == ":"){
                indent = "\n" + indent + "    ";
                this.value = val.slice(0,start) + indent + val.slice(end);
            }

            else{
                var previous_indent=indent;
                indent = "\n" + indent;
                this.value = val.slice(0,start) + indent + val.slice(end);
                this.selectionEnd = start+1+previous_indent.length;
            }
        }

        if(e.keyCode == 8){
            var start = this.selectionStart;
            var end = this.selectionEnd;
            var val = this.value;
            if((val[start-1] == "{" && val[start] == "}") || (val[start-1] == "[" && val[start] == "]") || (val[start-1] == "(" && val[start] == ")") || (val[start-1] == "\"" && val[start] == "\"") || (val[start-1] == "\'" && val[start] == "\'")){
                e.preventDefault();
                this.value = val.slice(0,start-1) + val.slice(start+1);
                this.selectionEnd = start-1;
            }
            else{

            }
        }

        if(e.keyCode == 9){
            e.preventDefault();
            var start = this.selectionStart;
            var end = this.selectionEnd;
            var val = this.value;
            var indent = "    ";
            this.value = val.slice(0,start) + indent + val.slice(end);
            this.selectionEnd = start + 4;
        }
    };
});

It is working for me in electron (its not working in firefox - I think due to window.event) - So I want a general code review of my code - Thanks

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.