How to restrict the user to write only one javascript function (method) inside the ui-ace editor

this is my controller code

$scope.aceOptions = { workerPath: 'http://localhost:50871/Scripts/app/vendor/', useWrapMode: true, showGutter: true, theme: 'chrome', firstLineNumber: 1, onLoad: function (_editor) { $scope.script = {}; $scope.script.scriptCode = "function fieldName_columnName_" + "functionName(){\n\n\n}"; var _session = _editor.getSession(); _session.setMode('ace/mode/javascript'); var _renderer = _editor.renderer; _session.on('changeAnnotation', function () { $scope.annotations = []; $scope.annotations = _editor.getSession().getAnnotations(); }), _editor.$blockScrolling = Infinity; _session.on("change", function (e) { var currentValue = _editor.getSession().getValue(); $scope.script.scriptCode = currentValue; }); }, require: ['ace/ext/language_tools'], advanced: { enableSnippets: true, enableBasicAutocompletion: true, enableLiveAutocompletion: true } } 

I wrote a directive for ui-ace, this is my html code

  <javascript-editor code="script.scriptCode" ace-option="aceOptions"></javascript-editor> 

and directory code

 SCSApp .directive('javascriptEditor', [ function () { return { restrict: 'E', scope: { data: '=code', aceOption: '=' }, templateUrl: '/Scripts/app/shared/directives/javascripEditor/partials/javascriptEditor.html', link: function (scope, element, attrs) { } } }]); 

and this is my javascriptEditor.html

 <div class="e1" ui-ace="aceOption" ng-model="data"></div> 

I just want to restrict the user from writing more than one javascript function in the ui-ace editor

+5
source share
2 answers

You can create your own simple javascript parser or use lib like parser3

Then write logical rules to show success / abandonment of the user interface based on the contents of the ACE editor.

It parses strings and sentences, such as compilers, to create low-level code.

+1
source

You need to attach a js function that will fire on keyDown or onBlur events in a text editor. This function must perform text validation in the editor for function declaration statements using RegExp . If you find more than one matching function declaration, you can process case if more than one function is specified in the text editor.

0
source

Source: https://habr.com/ru/post/1238059/


All Articles