<commit_before>/**
 *
 * @description codeeditor组件，代码编辑器，源代码参考：https://github.com/angular-ui/ui-codemirror
 * @author 吴家荣 <<EMAIL>>
 *
 */

/**
 * Binds a CodeMirror widget to a <textarea> element.
 */

angular.module('matrixui.components.codeeditor', [])
  .constant('muCodeeditorConfig', {
    languageOpts: {
      "CPP": "text/x-c++src",
      "C": "text/x-csrc",
      "Java": "text/x-java",
      "PHP": "text/x-php"
    },
    codemirror: {
      mode: 'c++',
      theme: 'monokai',
      lineNumbers: true,
      mode: 'text/x-c++src',
      keyMap: 'sublime',
      tabSize: 2
    }
  })
  .directive('muCodeeditor', muCodeeditorDirective);

muCodeeditorDirective.$inject = ['$timeout', '$parse', 'muCodeeditorConfig'];

function muCodeeditorDirective($timeout, $parse, muCodeeditorConfig) {

  return {
    restrict: 'EA',
    require: '?ngModel',
    transclude: true,
    scope: {
      uiCodemirror: '=',
      uiCodemirrorOpts: '=',
      contentObj: '='
    },
    template: '' +
      // '<div class="mu-codeeditor-language">' +
      //   '<select ng-model="language">' +
      //     '<option ng-repeat="(language, mode) in languageOpts track by language">{{language}}</option>' +
      //   '</select>' +
      // '</div>' +
      '<div class="editor-instance"></div>',
    compile: function compile() {
      // Require CodeMirror
      if (angular.isUndefined(window.CodeMirror)) {
        throw new Error('ui-codemirror needs CodeMirror to work... (o rly?)');
      }
      return postLink;
    }
  };

  function postLink(scope, iElement, iAttrs, ngModel) {

    var codemirrorOptions = angular.extend(
      { value: iElement.text() },
      muCodeeditorConfig.codemirror || {},
      scope.$eval('uiCodemirror'),
      scope.$eval('uiCodemirrorOpts')
    );

    var codemirror = newCodemirrorEditor(iElement, codemirrorOptions);

    configOptionsWatcher(
      codemirror,
      iAttrs.uiCodemirror || iAttrs.uiCodemirrorOpts,
      scope
    );

    codemirror.on('beforeChange', function (instance, changeObj) {
      var from = changeObj.from,
            to = changeObj.to;
      var text = changeObj.text;
      text = text.map(function(line, index, text) {
        return line.replace(/[\u0009]/g, ' ');
      });
      
      changeObj.update(from, to, text);
    });

    var modelName = iAttrs.ngModel;

    configNgModelLink(codemirror, ngModel, scope, modelName);

    configConverter(codemirror);

    configUiRefreshAttribute(codemirror, iAttrs.uiRefresh, scope);

    //configLanguageOpts(codemirror, scope);

    //configContentBlock(codemirror, scope, modelName);
    // Allow access to the CodeMirror instance through a broadcasted event
    // eg: $broadcast('CodeMirror', function(cm){...});
    scope.$on('CodeMirror', function(event, callback) {
      if (angular.isFunction(callback)) {
        callback(codemirror);
      } else {
        throw new Error('the CodeMirror event requires a callback function');
      }
    });

    // onLoad callback
    if (angular.isFunction(codemirrorOptions.onLoad)) {
      codemirrorOptions.onLoad(codemirror);
    }
  }

  function newCodemirrorEditor(iElement, codemirrorOptions) {
    var codemirrot;
    var editorEle = angular.element(iElement[0].querySelectorAll('.editor-instance'));<commit_msg>修正textarea无法创建子元素的问题<commit_after>/**
 *
 * @description codeeditor组件，代码编辑器，源代码参考：https://github.com/angular-ui/ui-codemirror
 * @author 吴家荣 <<EMAIL>>
 *
 */

/**
 * Binds a CodeMirror widget to a <textarea> element.
 */

angular.module('matrixui.components.codeeditor', [])
  .constant('muCodeeditorConfig', {
    languageOpts: {
      "CPP": "text/x-c++src",
      "C": "text/x-csrc",
      "Java": "text/x-java",
      "PHP": "text/x-php"
    },
    codemirror: {
      mode: 'c++',
      theme: 'monokai',
      lineNumbers: true,
      mode: 'text/x-c++src',
      keyMap: 'sublime',
      tabSize: 2
    }
  })
  .directive('muCodeeditor', muCodeeditorDirective);

muCodeeditorDirective.$inject = ['$timeout', '$parse', 'muCodeeditorConfig'];

function muCodeeditorDirective($timeout, $parse, muCodeeditorConfig) {

  return {
    restrict: 'EA',
    require: '?ngModel',
    transclude: true,
    scope: {
      uiCodemirror: '=',
      uiCodemirrorOpts: '=',
      contentObj: '='
    },
    template: '' +
      // '<div class="mu-codeeditor-language">' +
      //   '<select ng-model="language">' +
      //     '<option ng-repeat="(language, mode) in languageOpts track by language">{{language}}</option>' +
      //   '</select>' +
      // '</div>' +
      '<textarea class="editor-instance"></div>',
    compile: function compile() {
      // Require CodeMirror
      if (angular.isUndefined(window.CodeMirror)) {
        throw new Error('ui-codemirror needs CodeMirror to work... (o rly?)');
      }
      return postLink;
    }
  };

  function postLink(scope, iElement, iAttrs, ngModel) {

    var codemirrorOptions = angular.extend(
      { value: iElement.text() },
      muCodeeditorConfig.codemirror || {},
      scope.$eval('uiCodemirror'),
      scope.$eval('uiCodemirrorOpts')
    );

    var codemirror = newCodemirrorEditor(iElement, codemirrorOptions);

    configOptionsWatcher(
      codemirror,
      iAttrs.uiCodemirror || iAttrs.uiCodemirrorOpts,
      scope
    );

    codemirror.on('beforeChange', function (instance, changeObj) {
      var from = changeObj.from,
            to = changeObj.to;
      var text = changeObj.text;
      text = text.map(function(line, index, text) {
        return line.replace(/[\u0009]/g, ' ');
      });
      
      changeObj.update(from, to, text);
    });

    var modelName = iAttrs.ngModel;

    configNgModelLink(codemirror, ngModel, scope, modelName);

    configConverter(codemirror);

    configUiRefreshAttribute(codemirror, iAttrs.uiRefresh, scope);

    //configLanguageOpts(codemirror, scope);

    //configContentBlock(codemirror, scope, modelName);
    // Allow access to the CodeMirror instance through a broadcasted event
    // eg: $broadcast('CodeMirror', function(cm){...});
    scope.$on('CodeMirror', function(event, callback) {
      if (angular.isFunction(callback)) {
        callback(codemirror);
      } else {
        throw new Error('the CodeMirror event requires a callback function');
      }
    });

    // onLoad callback
    if (angular.isFunction(codemirrorOptions.onLoad)) {
      codemirrorOptions.onLoad(codemirror);
    }
  }

  function newCodemirrorEditor(iElement, codemirrorOptions) {
    var codemirrot;
    var editorEle = angular.element(iElement[0].querySelectorAll('.editor-instance'));
    if (!editorEle[0]) editorEle = iElement;
{'fraction_non_alphanumeric': 0.15228197017623135, 'fraction_numerical': 0.00195812622382889, 'mean_word_length': 2.947681331747919, 'pattern_counts': {'":': 8, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3210928', 'n_tokens_mistral': 2246, 'n_tokens_neox': 2200, 'n_words': 393}