Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect focus of codemirror editor

I am using codemirror in a webapp I am creating and I have several editors on the page. There are undo and redo buttons as well and they do work. However I can only set one editor to undo unless I use an if statement to determine which one is focussed and then apply the undo or redo function to its corresponding textbox. I have tried a few variations in both jQuery and JavaScript but to no avail. This is one of the code blocks I am attempting to get to work. The actual variable that holds the codemirror setup is called "codeeditor1", "#editor1" is the id of the textbox.

if ($("#editor1").is(':focus')) {
  undo.addEventListener('click', function() {
      codeeditor1.undo();
  }, false);
  redo.addEventListener('click', function() {
      codeeditor1.redo();
  }, false);
}

I also tried this according to the documentation of the method "cm.hasFocus()" = boolean.

if (codeeditor1.hasFocus() == true) {
    undo.addEventListener('click', function() {
        codeeditor1.undo();
    }, false);
    redo.addEventListener('click', function() {
        codeeditor1.redo();
    }, false);
}

I have now also tried this for the sake of logical code placement but still no luck with it. Perhaps it is a bug in the method of codemirror?

undo.addEventListener('click', function() {
    if (codeeditor1.hasFocus() == true) {
        codeeditor1.undo();
    }
}, false);

redo.addEventListener('click', function() {
    if (codeeditor1.hasFocus() == true) {
        codeeditor1.redo();
    }
}, false);
like image 798
alphadmon Avatar asked Oct 15 '25 13:10

alphadmon


1 Answers

Try to

  if( editor.hasFocus() == true  ) 
      // This editor is Codemirror object 
      // not jquery element.
  }
like image 96
Hasan Delibaş Avatar answered Oct 18 '25 07:10

Hasan Delibaş



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!