Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line numbers sometimes don't load properly using fromTextArea #154

Open
DiMono opened this issue Nov 21, 2019 · 0 comments
Open

Line numbers sometimes don't load properly using fromTextArea #154

DiMono opened this issue Nov 21, 2019 · 0 comments

Comments

@DiMono
Copy link

DiMono commented Nov 21, 2019

I have the following code to load a codemirror text area dynamically, changing the contents and styling based on the file:

var myCodeMirror = {};
function loadpage(filename) {
  $.ajax({
    method: 'GET',
    url: 'path/to/file/'+filename,
    success: function(xhrRequest, responseText, fullResponse) {
      var results = JSON.parse(fullResponse.responseText);
      $('#editor').val(results);
      $('#filename').val(filename);
      if (typeof(myCodeMirror.setOption) == 'undefined') {
        setTimeout(function() {
          myCodeMirror = CodeMirror.fromTextArea($('#editor')[0], {
           mode: 'css',
           lineNumbers: true
          });
        }, 200);
      } else {
        myCodeMirror.setOption('mode',(filename=='stylescss'?'text/css':'application/x-httpd-php'));
        myCodeMirror.getDoc().setValue(results);
      }
    },
    error: function(xhrRequest, textStatus, errorThrown) {
      // Handle errors
    }
  });
}

$(window).on('load', function() {
	loadpage('initialpage');
});

You might notice the setTimeout. That's because for some reason, when I load the editor the first time, there's a chance that the gutters will be almost 1000px wide. This will persist until I load other files a few times, at which point it will eventually correct itself. Delaying the load seems to be the only semi-reliable way I've found to prevent this from happening. Is there a better way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant