-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui-monaco.js
82 lines (68 loc) · 2.49 KB
/
ui-monaco.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict';
angular.module('ui.monaco', [])
.constant('uiMonacoConfig', {})
.directive('uiMonaco', uiMonacoDirective);
function uiMonacoDirective($timeout, uiMonacoConfig) {
return {
restrict: 'EA',
require: '?ngModel',
compile: function compile() {
return postLink;
}
};
function postLink(scope, iElement, iAttrs, ngModel) {
var monacoOptions = angular.extend(
{ value: iElement.text() },
uiMonacoConfig.monaco || {},
scope.$eval(iAttrs.uiMonaco),
scope.$eval(iAttrs.uiMonacoOpts)
);
ngModel.$render = function () {
monacoOptions.value = ngModel.$viewValue;
newMonacoEditor(iElement, monacoOptions, function (editor) {
configNgModelLink(editor, ngModel, scope);
scope.$watch(iAttrs.ngShow, function () {
editor.layout();
});
scope.$on('Monaco', function (event, callback) {
if (angular.isFunction(callback)) {
callback(editor);
} else {
throw new Error('the Monaco event requires a callback function');
}
});
if (angular.isFunction(monacoOptions.onLoad)) {
monacoOptions.onLoad(editor);
}
});
};
}
function newMonacoEditor(iElement, monacoOptions, cb) {
iElement.html('');
require(['vs/editor/editor.main'], function () {
$(iElement).css("height", "100%");
$(iElement).html("<div style='height: 100%; width: 100%;'></div>")
var div = $(iElement).find("div")[0];
var editor = monaco.editor.create(div, monacoOptions);
window.onresize = function () {
editor.layout();
};
cb(editor);
});
}
function configNgModelLink(editor, ngModel, scope) {
if (!ngModel) { return; }
ngModel.$render = function () {
var safeViewValue = ngModel.$viewValue || '';
editor.getModel().setValue(safeViewValue);
};
editor.getModel().onDidChangeContent(function (event) {
var newValue = editor.getValue();
if (newValue !== ngModel.$viewValue) {
scope.$evalAsync(function () {
ngModel.$setViewValue(newValue);
});
}
});
}
}