限制用户输入/删除任何字母

时间:2014-04-29 07:49:41

标签: javascript angularjs angular-ui ace-editor

我正在尝试使用AngularJS的ACE编辑器(使用UI-Ace)。我有一个问题。 我可以限制用户

  1. 除了预定义的字符(比如说)
  2. 之外,不要输入任何字符
  3. 不删除除预定义字符或空格外的任何字符
  4. 角度上的UI-Ace暴露了ACE编辑器的onChange事件,如下所示:

    $scope.aceChanged = function(e) {
    //
    };
    

    在php中我们刚检查输入的char与指定的char列表/ array / expression / ascii不匹配,但是如何在此更改/输入/删除字符。

    请指导。

    编辑: 我按照用户的问题尝试:但是我收到的错误就像编辑器变量undefined或行undefined

    $scope.aceLoaded = function(_editor) {
        // Options
        //_editor.setReadOnly(true);
        //editor.setOption("showPrintMargin", false)        
        _editor.setOption("showPrintMargin", false);
        //_editor.setShowInvisibles(true);
        var _session = _editor.getSession();
    
        //_session.setUseSoftTabs(false);
    
        //editor.on("change", onchange)
        _editor.commands.on("afterExec", afterExec);
    
      };
    
      var revertScheduled = false;
    
      $scope.aceChanged = function(e) {
        if($scope.startt ==0){ $scope.startt = new Date().getTime()};
        if (!revertScheduled && _editor.curOp) {
            var text = e.data.lines
                ? e.data.lines.join("\n")
                : e.data.text;
    
            var shouldRevert = false;
            var isInsertion = e.data.action[0] == "i";
            if (isInsertion && !/^[;]+$/.test(text)) {
                shouldRevert = true;
            } else if (!isInsertion && !/^[; ]+$/.test(text)) {
                shouldRevert = true;
            }
    
            if (shouldRevert) {
                editor.session.mergeUndoDeltas = false
                editor.session.markUndoGroup()
                revertScheduled = true
                console.log(e.data.text, editor.curOp)
            }
        }
          //
        $http({
            url: global.apiurl+"splitrec",//"http://medline.dev/api/splitrec",
            method: "POST",
            data: {'record': $scope.record.name}
        }).success(function (data, status, headers, config) {
                 // assign  $scope.persons here as promise is resolved here
                //$log.log(data);
            $scope.res = data;
            $scope.prova();
    
            }).error(function (data, status, headers, config) {
                $scope.status = status;
                $log.log(status);
            });
      };
    
    
    var onchange = function(e) {
    
    }
    
    var afterExec = function() {
        if (revertScheduled) {
            editor.undo();
            revertScheduled = false
        }
    }
    

    我的日志输出e :( curOP为空)

    [Object, y]
     0: Object
        data: Object
        action: "insertText"
        range: i
        text: "r"
        __proto__: Object
        __proto__: Object
     1: y
        $blockScrolling: 0
        $enableMultiselect: true
        $highlightPending: false
        $historyTracker: function () { [native code] }
        $isFocused: false
        $mouseHandler: l
        $multiselectOnSessionChange: function () { [native code] }
        $onAddRange: function () { [native code] }
        $onChangeAnnotation: function () { [native code] }
        $onChangeBackMarker: function () { [native code] }
        $onChangeBreakpoint: function () { [native code] }
        $onChangeFold: function () { [native code] }
        $onChangeFrontMarker: function () { [native code] }
        $onChangeMode: function () { [native code] }
        $onChangeTabSize: function () { [native code] }
        $onChangeWrapLimit: function () { [native code] }
        $onChangeWrapMode: function () { [native code] }
        $onCursorChange: function () { [native code] }
        $onDocumentChange: function () { [native code] }
        $onMultiSelect: function () { [native code] }
        $onRemoveRange: function () { [native code] }
        $onScrollLeftChange: function () { [native code] }
        $onScrollTopChange: function () { [native code] }
        $onSelectionChange: function () { [native code] }
        $onSingleSelect: function () { [native code] }
        $onTokenizerUpdate: function () { [native code] }
        $opResetTimer: function (e){n==null&&(n=setTimeout(r,e||t))}
        $search: o
        _$emitInputEvent: function (e){n==null&&(n=setTimeout(r,e||t))}
        _defaultHandlers: Object
        _eventRegistry: Object
        commands: o
        container: div.ng-valid.ace_editor.ace-chrome.ng-dirty
        curOp: null
        env: Object
        keyBinding: s
       lastFileJumpPos: null
       mergeNextCommand: true
       multiSelect: u
       prevOp: Object
       previousCommand: null
       renderer: g
       selection: u
       selections: Array[5]
       sequenceStartTime: 1399493671829
       session: p
       textInput: f
    

    请指导

1 个答案:

答案 0 :(得分:1)

如果您可以访问真实的ace实例,则可以使用以下方法

var revertScheduled = false
var onchange = function(e) {
    if (!revertScheduled && editor.curOp) {
        var text = e.data.lines
            ? e.data.lines.join("\n")
            : e.data.text;

        var shouldRevert = false;
        var isInsertion = e.data.action[0] == "i";
        if (isInsertion && !/^[;]+$/.test(text)) {
            shouldRevert = true;
        } else if (!isInsertion && !/^[; ]+$/.test(text)) {
            shouldRevert = true;
        }

        if (shouldRevert) {
            editor.session.mergeUndoDeltas = false
            editor.session.markUndoGroup()
            revertScheduled = true
            console.log(e.data.text, editor.curOp)
        }
    }
}

var afterExec = function() {
    if (revertScheduled) {
        editor.undo();
        revertScheduled = false
    }
}
editor.on("change", onchange)
editor.commands.on("afterExec", afterExec)

测试是否有不允许的更改,并将其还原。