Skip to content
This repository has been archived by the owner on May 25, 2019. It is now read-only.

<textarea ui-codemirror> feedback: cursor error with ng-model #113

Open
bdedardel opened this issue Sep 23, 2015 · 1 comment
Open

<textarea ui-codemirror> feedback: cursor error with ng-model #113

bdedardel opened this issue Sep 23, 2015 · 1 comment

Comments

@bdedardel
Copy link

Hi,

Using textarea with ng-model, cursor returns at the position 0 if I press 'space' or 'enter' keys.
It is OK without ng-model !

<textarea 
   id="mytextarea" 
   ng-model="myModel" 
   ui-codemirror="{
     lineNumbers: true,
     theme:'default',
     mode: 'xml'
   }">
 </textarea>
@bdedardel
Copy link
Author

I worked on a patch that may help you for the next release:
Let me know if you fix it.

Regards,
Benjamin

// Keep the ngModel in sync with changes from CodeMirror
    codemirror.on('change', function(instance) {
      var modeDebug = false;
      var newValue = instance.getValue();

      // patch for enter key
      if (newValue.endsWith('\n')) {
        if (modeDebug) console.log('patch: last char is eol');
        var cursor = codemirror.getCursor();
        cursor.line += 1;
        cursor.ch = 0;
        codemirror.setCursor(cursor); // /!\ need to be improve with smart indent !
      }
      // patch for space key
      else if (newValue.endsWith(' ')) {
        if (modeDebug) console.log('patch: last char is a space');
        var cursor = codemirror.getCursor();
        cursor.ch += 1;
        // ngModel.$setViewValue(newValue); // DO NOT SET new value (trim it !)
        codemirror.setCursor(cursor);
      }       
      else if (newValue !== ngModel.$viewValue) {
        // BUG: cursor returns at position  0 using keys 'space' or 'enter'
        // => see previous patches
        scope.$evalAsync(function() {
            if (modeDebug) console.log('sync ng model');
            ngModel.$setViewValue(newValue);
        }); 
      }
    });
  }

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

No branches or pull requests

1 participant