How to restrict LaTex commands in MathQuill?

Viewed 304

I am using MathQuill lib for the formula input. I need to restrict all commands but +, -, * and /.

I've achieved this in the following way:

var previous = '';
        var answerMathField = MQ.MathField(answerSpan, {
          handlers: {
            edit: function(mathField) {
              var enteredMath = mathField.latex(); // Get entered math in LaTeX format
              let mathSplit = enteredMath.split('\\');

                //restrict commands by triming '\' in 'enteredMath'
                if(mathSplit.length > 1 && enteredMath !== previous){
                  let formatted = mathSplit.reduce((a, b)=>{
                        if(b.indexOf('cdot') !== -1
                                    ||  b.indexOf('frac') !== -1
                                    ||  b === ' '){
                            b = "\\" + b;
                        }
                        return a + b;
                  }, '');

                  //save current math for exiting this loop
                  previous = formatted;

                  answerMathField.latex("");
                  answerMathField.write(formatted);
               }
              }
            }
          }

This restricts all the commands, just like I want, but answeredMathField.write() moves cursor to the end of the line. Is there any way to move cursor to a certain position?

Besides that, it would be great if somebody could suggest more elegant solution for restricting the commands in MathQuill

Any help will be appreciated!

0 Answers
Related