forked from massiveart/husky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckeditor-extension.js
305 lines (255 loc) · 11.3 KB
/
ckeditor-extension.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* This file is part of Husky frontend development framework.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*
*/
(function() {
'use strict';
require.config({
paths: {
ckeditor: 'bower_components/ckeditor/ckeditor',
jqueryAdapter: 'bower_components/ckeditor/adapters/jquery'
},
shim: {
jqueryAdapter: {
deps: ['jquery', 'ckeditor']
}
}
});
define(['underscore', 'services/husky/util', 'ckeditor', 'jqueryAdapter'], function(_, Util) {
var getConfig = function() {
return {
format_tags: 'p;h1;h2;h3;h4;h5;h6',
width: '100%',
defaultLanguage: 'en',
removeButtons: '',
removePlugins: 'elementspath,magicline',
removeDialogTabs: 'image:advanced;link:advanced',
allowedContent: true,
resize_enabled: false,
enterMode: 'P',
uiColor: '#ffffff',
skin: 'husky'
};
};
/**
* Builder to extend basic toolbar.
*
* @param toolbar
*
* @constructor
*/
function ToolbarBuilder(toolbar) {
this.toolbar = toolbar;
}
/**
* Add toolbar items.
*
* @param {String} name
* @param {String[]} items
*/
ToolbarBuilder.prototype.add = function(name, items) {
if (!this.toolbar[name]) {
this.toolbar[name] = [];
}
this.toolbar[name] = this.toolbar[name].concat(items);
};
/**
* Remove toolbar items.
*
* @param {String} name
* @param {String[]} items
*/
ToolbarBuilder.prototype.remove = function(name, items) {
if (!this.toolbar[name]) {
this.toolbar[name] = [];
}
this.toolbar[name] = _.difference(this.toolbar[name], items);
};
/**
* Returns toolbar.
*
* @returns {{name, items}[]}
*/
ToolbarBuilder.prototype.get = function() {
return _.map(this.toolbar, function(items, name) {
return {name: name, items: items};
});
};
/**
* Returns raw toolbar data.
*
* @returns {Object}
*/
ToolbarBuilder.getRaw = function() {
return this.toolbar;
};
return {
name: 'ckeditor',
initialize: function(app) {
var availableToolbarSections = {
semantics: ['Format'],
basicstyles: ['Superscript', 'Subscript', 'Italic', 'Bold', 'Underline', 'Strike'],
blockstyles: ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
list: ['NumberedList', 'BulletedList'],
paste: ['PasteFromWord'],
links: ['Link'],
insert: ['Table'],
styles: ['Styles'],
code: ['Source']
},
toolbar,
plugins = ['justify', 'format', 'sourcearea', 'link', 'table', 'pastefromword', 'autogrow'],
icons = {
Format: 'font',
Strike: 'strikethrough',
JustifyLeft: 'align-left',
JustifyCenter: 'align-center',
JustifyRight: 'align-right',
JustifyBlock: 'align-justify',
NumberedList: 'list-ol',
BulletedList: 'list',
PasteFromWord: 'clipboard',
Styles: 'header',
Source: 'code'
},
relations = {
Link: ['Unlink']
},
/**
* Expands a given toolbar. Iterates over all sections and
* all section items in the toolbar. If there is a relation
* defined for a section item it adds all items of the relations
* to that section.
*
* @param {Object} toolbar The toolbar to expand
* @returns {Object} the expanded toolbar
*/
expandToolbar = function(toolbar) {
var expandedToolbar = {}, expandedSection;
Util.each(toolbar, function(sectionName, section) {
expandedSection = [];
Util.foreach(section, function(sectionItem) {
expandedSection.push(sectionItem);
if (!!relations[sectionItem]) {
expandedSection = expandedSection.concat(relations[sectionItem]);
}
}.bind(this));
expandedToolbar[sectionName] = expandedSection;
}.bind(this));
return expandedToolbar;
};
app.sandbox.ckeditor = {
addPlugin: function(name, plugin) {
plugins.push(name);
CKEDITOR.plugins.add(name, plugin);
},
addToolbarButton: function(sectionName, button, icon, buttonRelations) {
if (!availableToolbarSections[sectionName]) {
availableToolbarSections[sectionName] = [];
}
availableToolbarSections[sectionName].push(button);
if (!!icon) {
icons[button] = icon;
}
relations[button] = buttonRelations;
},
setToolbar: function(newToolbar) {
toolbar = Util.deepCopy(newToolbar);
},
getAvailableToolbarSections: function() {
return Util.deepCopy(availableToolbarSections);
},
getToolbar: function() {
if (!toolbar) {
return expandToolbar(this.getAvailableToolbarSections());
}
return expandToolbar(toolbar);
},
getIcon: function(button) {
if (icons.hasOwnProperty(button)) {
return icons[button];
}
return button.toLowerCase();
},
getToolbarBuilder: function() {
return new ToolbarBuilder(this.getToolbar());
},
createToolbarBuilder: function(sections) {
var toolbarSections = {};
for (var i = 0, length = sections.length; i < length; ++i) {
toolbarSections[sections[i].name] = sections[i].items;
}
return new ToolbarBuilder(toolbarSections);
},
// callback when editor is ready
init: function(selector, callback, config) {
var configuration = app.sandbox.util.extend(true, {}, getConfig.call(), config), $editor;
configuration.extraPlugins = plugins.join(',');
if (!!callback && typeof callback === 'function') {
$editor = $(selector).ckeditor(callback, configuration);
} else {
$editor = $(selector).ckeditor(configuration);
}
// customize ckeditor dialog appearance on 'dialogDefinition' (=> open)
// and filter link/target options
CKEDITOR.on('dialogDefinition', function(ev) {
// take the dialog name and its definition from the event
// data.
var dialogName = ev.data.name,
dialogDefinition = ev.data.definition;
// check if the definition is from the dialog we're
// interested in (the "Link" dialog).
if (dialogName == 'link') {
// get a reference to the "Link Info" and "Target" tab.
var infoTab = dialogDefinition.getContents('info'),
targetTab = dialogDefinition.getContents('target'),
// get a reference to the link type
linkOptions = infoTab.get('linkType'),
targetOptions = targetTab.get('linkTargetType'),
// list of included link options
includedLinkOptions = [
'url',
'email'
],
selectedLinkOption = [],
// list of included link target options
includedTargetOptions = [
'notSet',
'_blank',
'_self'
],
selectedTargetOptions = [];
// just show included link options
for (var i = 0; i < linkOptions.items.length; i++) {
if (includedLinkOptions.indexOf(linkOptions.items[i][1]) !== -1) {
selectedLinkOption.push(linkOptions.items[i]);
}
}
// just show included target options
for (var i = 0; i < targetOptions.items.length; i++) {
if (includedTargetOptions.indexOf(targetOptions.items[i][1]) !== -1) {
selectedTargetOptions.push(targetOptions.items[i]);
}
}
linkOptions.items = selectedLinkOption;
targetOptions.items = selectedTargetOptions;
}
});
return $editor.editor;
},
getInstance: function(id) {
var instance = CKEDITOR.instances[id];
if (!!instance) {
return instance;
}
}
};
}
};
});
})();