-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (129 loc) · 4.45 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
angular.module('fng.ckeditor',[]).directive('ckeditor5', editorDirective);
function editorDirective () {
return {
restrict: 'A',
require: 'ngModel',
scope: {
custom: '=',
config: '=',
plugins: '=',
removePlugins: '=',
replaceConfig: '=',
replaceToolbar: '=',
replacePlugins: '=',
toolbar: '='
},
link: function(scope, element, attrs, ngModel) {
'ngInject';
var constructConfigArray = function(customItems, defaultItems, replace) {
if (!customItems) return defaultItems;
if (customItems && replace) return customItems;
return defaultItems.concat(customItems);
};
var constructConfig = function() {
var config = scope.config;
var plugins = scope.plugins;
var removePlugins = scope.removePlugins;
var replaceConfig = scope.replaceConfig;
var replacePlugins = scope.replacePlugins;
var replaceToolbar = scope.replaceToolbar;
var toolbar = scope.toolbar;
if (!config && !toolbar && !plugins) return;
if (config && replaceConfig) return config;
// construct the plugins, if replacePlugins is truthy, we only provide the given plugins
var defaultPlugins = classicEditor.build.plugins.map(function(plugin) {return plugin.pluginName});
var customPlugins = constructConfigArray(plugins, defaultPlugins, replacePlugins);
// construct the toolbar, if replaceToolbar is truthy, we only provide the given toolbar config
var defaultToolbar = classicEditor.build.config.toolbar.items;
var customToolbar = constructConfigArray(toolbar, defaultToolbar, replaceToolbar);
return Object.assign({}, {
plugins: customPlugins,
removePlugins: removePlugins,
toolbar: customToolbar
}, config);
};
let isReadOnly = attrs.disabled || false;
const textarea = element[0];
if (textarea.tagName.toLowerCase() !== "textarea") {
throw new Error("element is not a textarea");
}
if (textarea.id) {
const disabledFunc = scope.$root.isSecurelyDisabled;
if (disabledFunc && disabledFunc(textarea.id)) {
isReadOnly = true;
}
}
const customConfig = {
toolbar: {
items: [
'heading',
'|',
'bold',
'italic',
'underline',
'bulletedList',
'numberedList',
'|',
'fontBackgroundColor',
'fontColor',
'outdent',
'indent',
'|',
'link',
'imageUpload',
'blockQuote',
'insertTable',
'mediaEmbed',
'undo',
'redo',
'horizontalLine',
'removeFormat',
'htmlEmbed'
]
},
language: 'en',
image: {
toolbar: [
'imageTextAlternative',
'imageStyle:full',
'imageStyle:side'
]
},
table: {
contentToolbar: [
'tableColumn',
'tableRow',
'mergeTableCells',
'tableCellProperties',
'tableProperties'
]
},
licenseKey: '',
};
ClassicEditor.create(
element[0],
attrs.custom !== undefined ? customConfig : constructConfig()
).then(function(instance) {
instance.isReadOnly = isReadOnly;
if (isReadOnly) {
const toolbarElement = instance.ui.view.toolbar.element;
toolbarElement.style.display = 'none';
}
var setData = function() {
var data = instance.getData();
var value = ngModel.$viewValue || '';
if (data !== value) {
instance.setData(value);
}
};
var setModel = function() {
var data = instance.getData();
ngModel.$setViewValue(data);
ngModel.$render();
};
scope.$watch(function() {return ngModel.$viewValue}, setData);
instance.model.document.on('change', setModel);
});
}
};
}