This repository has been archived by the owner on May 9, 2024. It is now read-only.
forked from lemonde/angular-ckeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-ckeditor.js
135 lines (114 loc) · 3.79 KB
/
angular-ckeditor.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
/*! Angular CKEditor v0.1.1 | (c) 2014 Le Monde | License MIT */
(function (root, factory) {
// AMD
if (typeof define === 'function' && define.amd) define(['angular'], factory);
// Global
else factory(angular);
}(this, function (angular) {
angular
.module('ckeditor', [])
.directive('ckeditor', ['$parse', ckeditorDirective]);
// Create setImmediate function.
var setImmediate = window && window.setImmediate ? window.setImmediate : function (fn) {
setTimeout(fn, 0);
};
/**
* CKEditor directive.
*
* @example
* <div ckeditor="options" ng-model="content" ready="onReady()"></div>
*/
function ckeditorDirective($parse) {
return {
restrict: 'A',
require: ['ckeditor', 'ngModel'],
controller: ['$scope', '$element', '$attrs', '$parse', '$q', ckeditorController],
link: function (scope, element, attrs, ctrls) {
var ckeditor = ctrls[0];
var ngModel = ctrls[1];
// Initialize the editor when it is ready.
ckeditor.ready().then(function initialize() {
// Sync view on specific events.
['dataReady', 'change', 'saveSnapshot'].forEach(function (event) {
ckeditor.$on(event, function syncView() {
ngModel.$setViewValue(ckeditor.instance.getData());
});
});
// Put editor out of readonly.
ckeditor.instance.setReadOnly(false);
// Defer the ready handler calling to ensure that the editor is
// completely ready and populated with data.
setImmediate(function () {
$parse(attrs.ready)(scope);
});
});
// Set editor data when view data change.
ngModel.$render = function syncEditor() {
ckeditor.ready().then(function () {
ckeditor.instance.setData(ngModel.$viewValue);
});
};
}
};
}
/**
* CKEditor controller.
*/
function ckeditorController($scope, $element, $attrs, $parse, $q) {
// Create editor instance.
var config = $parse($attrs.ckeditor)($scope) || {};
var editorElement = $element[0];
var instance;
if (editorElement.hasAttribute('contenteditable') &&
editorElement.getAttribute('contenteditable').toLowerCase() == 'true') {
instance = this.instance = CKEDITOR.inline(editorElement, config);
}
else {
instance = this.instance = CKEDITOR.replace(editorElement, config);
}
/**
* Listen on events of a given type.
* This make all event asynchrone and wrapped in $scope.$apply.
*
* @param {String} event
* @param {Function} listener
* @returns {Function} Deregistration function for this listener.
*/
this.$on = function $on(event, listener) {
// Wrap primus event with $rootScope.$apply.
instance.on(event, asyncListener);
function asyncListener() {
var args = arguments;
setImmediate(function () {
applyListener.apply(null, args);
});
}
function applyListener() {
var args = arguments;
$scope.$apply(function () {
listener.apply(null, args);
});
}
// Return the deregistration function
return function $off() {
instance.removeListener(event, applyListener);
};
};
/**
* Check if the editor if ready.
*
* @returns {Promise}
*/
this.ready = function ready() {
if (this.readyDefer) return this.readyDefer.promise;
var readyDefer = this.readyDefer = $q.defer();
if (this.instance.status === 'ready') readyDefer.resolve();
else this.$on('instanceReady', readyDefer.resolve);
return readyDefer.promise;
};
// Destroy editor when the scope is destroyed.
$scope.$on('$destroy', function onDestroy() {
instance.destroy(false);
});
}
}));