Chris@0
|
1 /**
|
Chris@0
|
2 * @file
|
Chris@0
|
3 * CKEditor implementation of {@link Drupal.editors} API.
|
Chris@0
|
4 */
|
Chris@0
|
5
|
Chris@0
|
6 (function (Drupal, debounce, CKEDITOR, $, displace, AjaxCommands) {
|
Chris@0
|
7 /**
|
Chris@0
|
8 * @namespace
|
Chris@0
|
9 */
|
Chris@0
|
10 Drupal.editors.ckeditor = {
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Editor attach callback.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @param {HTMLElement} element
|
Chris@0
|
16 * The element to attach the editor to.
|
Chris@0
|
17 * @param {string} format
|
Chris@0
|
18 * The text format for the editor.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @return {bool}
|
Chris@0
|
21 * Whether the call to `CKEDITOR.replace()` created an editor or not.
|
Chris@0
|
22 */
|
Chris@0
|
23 attach(element, format) {
|
Chris@0
|
24 this._loadExternalPlugins(format);
|
Chris@0
|
25 // Also pass settings that are Drupal-specific.
|
Chris@0
|
26 format.editorSettings.drupal = {
|
Chris@0
|
27 format: format.format,
|
Chris@0
|
28 };
|
Chris@0
|
29
|
Chris@0
|
30 // Set a title on the CKEditor instance that includes the text field's
|
Chris@0
|
31 // label so that screen readers say something that is understandable
|
Chris@0
|
32 // for end users.
|
Chris@0
|
33 const label = $(`label[for=${element.getAttribute('id')}]`).html();
|
Chris@0
|
34 format.editorSettings.title = Drupal.t('Rich Text Editor, !label field', { '!label': label });
|
Chris@0
|
35
|
Chris@0
|
36 return !!CKEDITOR.replace(element, format.editorSettings);
|
Chris@0
|
37 },
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * Editor detach callback.
|
Chris@0
|
41 *
|
Chris@0
|
42 * @param {HTMLElement} element
|
Chris@0
|
43 * The element to detach the editor from.
|
Chris@0
|
44 * @param {string} format
|
Chris@0
|
45 * The text format used for the editor.
|
Chris@0
|
46 * @param {string} trigger
|
Chris@0
|
47 * The event trigger for the detach.
|
Chris@0
|
48 *
|
Chris@0
|
49 * @return {bool}
|
Chris@0
|
50 * Whether the call to `CKEDITOR.dom.element.get(element).getEditor()`
|
Chris@0
|
51 * found an editor or not.
|
Chris@0
|
52 */
|
Chris@0
|
53 detach(element, format, trigger) {
|
Chris@0
|
54 const editor = CKEDITOR.dom.element.get(element).getEditor();
|
Chris@0
|
55 if (editor) {
|
Chris@0
|
56 if (trigger === 'serialize') {
|
Chris@0
|
57 editor.updateElement();
|
Chris@0
|
58 }
|
Chris@0
|
59 else {
|
Chris@0
|
60 editor.destroy();
|
Chris@0
|
61 element.removeAttribute('contentEditable');
|
Chris@0
|
62 }
|
Chris@0
|
63 }
|
Chris@0
|
64 return !!editor;
|
Chris@0
|
65 },
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * Reacts on a change in the editor element.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @param {HTMLElement} element
|
Chris@0
|
71 * The element where the change occured.
|
Chris@0
|
72 * @param {function} callback
|
Chris@0
|
73 * Callback called with the value of the editor.
|
Chris@0
|
74 *
|
Chris@0
|
75 * @return {bool}
|
Chris@0
|
76 * Whether the call to `CKEDITOR.dom.element.get(element).getEditor()`
|
Chris@0
|
77 * found an editor or not.
|
Chris@0
|
78 */
|
Chris@0
|
79 onChange(element, callback) {
|
Chris@0
|
80 const editor = CKEDITOR.dom.element.get(element).getEditor();
|
Chris@0
|
81 if (editor) {
|
Chris@0
|
82 editor.on('change', debounce(() => {
|
Chris@0
|
83 callback(editor.getData());
|
Chris@0
|
84 }, 400));
|
Chris@0
|
85
|
Chris@0
|
86 // A temporary workaround to control scrollbar appearance when using
|
Chris@0
|
87 // autoGrow event to control editor's height.
|
Chris@0
|
88 // @todo Remove when http://dev.ckeditor.com/ticket/12120 is fixed.
|
Chris@0
|
89 editor.on('mode', () => {
|
Chris@0
|
90 const editable = editor.editable();
|
Chris@0
|
91 if (!editable.isInline()) {
|
Chris@0
|
92 editor.on('autoGrow', (evt) => {
|
Chris@0
|
93 const doc = evt.editor.document;
|
Chris@0
|
94 const scrollable = CKEDITOR.env.quirks ? doc.getBody() : doc.getDocumentElement();
|
Chris@0
|
95
|
Chris@0
|
96 if (scrollable.$.scrollHeight < scrollable.$.clientHeight) {
|
Chris@0
|
97 scrollable.setStyle('overflow-y', 'hidden');
|
Chris@0
|
98 }
|
Chris@0
|
99 else {
|
Chris@0
|
100 scrollable.removeStyle('overflow-y');
|
Chris@0
|
101 }
|
Chris@0
|
102 }, null, null, 10000);
|
Chris@0
|
103 }
|
Chris@0
|
104 });
|
Chris@0
|
105 }
|
Chris@0
|
106 return !!editor;
|
Chris@0
|
107 },
|
Chris@0
|
108
|
Chris@0
|
109 /**
|
Chris@0
|
110 * Attaches an inline editor to a DOM element.
|
Chris@0
|
111 *
|
Chris@0
|
112 * @param {HTMLElement} element
|
Chris@0
|
113 * The element to attach the editor to.
|
Chris@0
|
114 * @param {object} format
|
Chris@0
|
115 * The text format used in the editor.
|
Chris@0
|
116 * @param {string} [mainToolbarId]
|
Chris@0
|
117 * The id attribute for the main editor toolbar, if any.
|
Chris@0
|
118 * @param {string} [floatedToolbarId]
|
Chris@0
|
119 * The id attribute for the floated editor toolbar, if any.
|
Chris@0
|
120 *
|
Chris@0
|
121 * @return {bool}
|
Chris@0
|
122 * Whether the call to `CKEDITOR.replace()` created an editor or not.
|
Chris@0
|
123 */
|
Chris@0
|
124 attachInlineEditor(element, format, mainToolbarId, floatedToolbarId) {
|
Chris@0
|
125 this._loadExternalPlugins(format);
|
Chris@0
|
126 // Also pass settings that are Drupal-specific.
|
Chris@0
|
127 format.editorSettings.drupal = {
|
Chris@0
|
128 format: format.format,
|
Chris@0
|
129 };
|
Chris@0
|
130
|
Chris@0
|
131 const settings = $.extend(true, {}, format.editorSettings);
|
Chris@0
|
132
|
Chris@0
|
133 // If a toolbar is already provided for "true WYSIWYG" (in-place editing),
|
Chris@0
|
134 // then use that toolbar instead: override the default settings to render
|
Chris@0
|
135 // CKEditor UI's top toolbar into mainToolbar, and don't render the bottom
|
Chris@0
|
136 // toolbar at all. (CKEditor doesn't need a floated toolbar.)
|
Chris@0
|
137 if (mainToolbarId) {
|
Chris@0
|
138 const settingsOverride = {
|
Chris@0
|
139 extraPlugins: 'sharedspace',
|
Chris@0
|
140 removePlugins: 'floatingspace,elementspath',
|
Chris@0
|
141 sharedSpaces: {
|
Chris@0
|
142 top: mainToolbarId,
|
Chris@0
|
143 },
|
Chris@0
|
144 };
|
Chris@0
|
145
|
Chris@0
|
146 // Find the "Source" button, if any, and replace it with "Sourcedialog".
|
Chris@0
|
147 // (The 'sourcearea' plugin only works in CKEditor's iframe mode.)
|
Chris@0
|
148 let sourceButtonFound = false;
|
Chris@0
|
149 for (let i = 0; !sourceButtonFound && i < settings.toolbar.length; i++) {
|
Chris@0
|
150 if (settings.toolbar[i] !== '/') {
|
Chris@0
|
151 for (let j = 0; !sourceButtonFound && j < settings.toolbar[i].items.length; j++) {
|
Chris@0
|
152 if (settings.toolbar[i].items[j] === 'Source') {
|
Chris@0
|
153 sourceButtonFound = true;
|
Chris@0
|
154 // Swap sourcearea's "Source" button for sourcedialog's.
|
Chris@0
|
155 settings.toolbar[i].items[j] = 'Sourcedialog';
|
Chris@0
|
156 settingsOverride.extraPlugins += ',sourcedialog';
|
Chris@0
|
157 settingsOverride.removePlugins += ',sourcearea';
|
Chris@0
|
158 }
|
Chris@0
|
159 }
|
Chris@0
|
160 }
|
Chris@0
|
161 }
|
Chris@0
|
162
|
Chris@0
|
163 settings.extraPlugins += `,${settingsOverride.extraPlugins}`;
|
Chris@0
|
164 settings.removePlugins += `,${settingsOverride.removePlugins}`;
|
Chris@0
|
165 settings.sharedSpaces = settingsOverride.sharedSpaces;
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 // CKEditor requires an element to already have the contentEditable
|
Chris@0
|
169 // attribute set to "true", otherwise it won't attach an inline editor.
|
Chris@0
|
170 element.setAttribute('contentEditable', 'true');
|
Chris@0
|
171
|
Chris@0
|
172 return !!CKEDITOR.inline(element, settings);
|
Chris@0
|
173 },
|
Chris@0
|
174
|
Chris@0
|
175 /**
|
Chris@0
|
176 * Loads the required external plugins for the editor.
|
Chris@0
|
177 *
|
Chris@0
|
178 * @param {object} format
|
Chris@0
|
179 * The text format used in the editor.
|
Chris@0
|
180 */
|
Chris@0
|
181 _loadExternalPlugins(format) {
|
Chris@0
|
182 const externalPlugins = format.editorSettings.drupalExternalPlugins;
|
Chris@0
|
183 // Register and load additional CKEditor plugins as necessary.
|
Chris@0
|
184 if (externalPlugins) {
|
Chris@0
|
185 Object.keys(externalPlugins || {}).forEach((pluginName) => {
|
Chris@0
|
186 CKEDITOR.plugins.addExternal(pluginName, externalPlugins[pluginName], '');
|
Chris@0
|
187 });
|
Chris@0
|
188 delete format.editorSettings.drupalExternalPlugins;
|
Chris@0
|
189 }
|
Chris@0
|
190 },
|
Chris@0
|
191
|
Chris@0
|
192 };
|
Chris@0
|
193
|
Chris@0
|
194 Drupal.ckeditor = {
|
Chris@0
|
195
|
Chris@0
|
196 /**
|
Chris@0
|
197 * Variable storing the current dialog's save callback.
|
Chris@0
|
198 *
|
Chris@0
|
199 * @type {?function}
|
Chris@0
|
200 */
|
Chris@0
|
201 saveCallback: null,
|
Chris@0
|
202
|
Chris@0
|
203 /**
|
Chris@0
|
204 * Open a dialog for a Drupal-based plugin.
|
Chris@0
|
205 *
|
Chris@0
|
206 * This dynamically loads jQuery UI (if necessary) using the Drupal AJAX
|
Chris@0
|
207 * framework, then opens a dialog at the specified Drupal path.
|
Chris@0
|
208 *
|
Chris@0
|
209 * @param {CKEditor} editor
|
Chris@0
|
210 * The CKEditor instance that is opening the dialog.
|
Chris@0
|
211 * @param {string} url
|
Chris@0
|
212 * The URL that contains the contents of the dialog.
|
Chris@0
|
213 * @param {object} existingValues
|
Chris@0
|
214 * Existing values that will be sent via POST to the url for the dialog
|
Chris@0
|
215 * contents.
|
Chris@0
|
216 * @param {function} saveCallback
|
Chris@0
|
217 * A function to be called upon saving the dialog.
|
Chris@0
|
218 * @param {object} dialogSettings
|
Chris@0
|
219 * An object containing settings to be passed to the jQuery UI.
|
Chris@0
|
220 */
|
Chris@0
|
221 openDialog(editor, url, existingValues, saveCallback, dialogSettings) {
|
Chris@0
|
222 // Locate a suitable place to display our loading indicator.
|
Chris@0
|
223 let $target = $(editor.container.$);
|
Chris@0
|
224 if (editor.elementMode === CKEDITOR.ELEMENT_MODE_REPLACE) {
|
Chris@0
|
225 $target = $target.find('.cke_contents');
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@0
|
228 // Remove any previous loading indicator.
|
Chris@0
|
229 $target.css('position', 'relative').find('.ckeditor-dialog-loading').remove();
|
Chris@0
|
230
|
Chris@0
|
231 // Add a consistent dialog class.
|
Chris@0
|
232 const classes = dialogSettings.dialogClass ? dialogSettings.dialogClass.split(' ') : [];
|
Chris@0
|
233 classes.push('ui-dialog--narrow');
|
Chris@0
|
234 dialogSettings.dialogClass = classes.join(' ');
|
Chris@0
|
235 dialogSettings.autoResize = window.matchMedia('(min-width: 600px)').matches;
|
Chris@0
|
236 dialogSettings.width = 'auto';
|
Chris@0
|
237
|
Chris@0
|
238 // Add a "Loading…" message, hide it underneath the CKEditor toolbar,
|
Chris@0
|
239 // create a Drupal.Ajax instance to load the dialog and trigger it.
|
Chris@0
|
240 const $content = $(`<div class="ckeditor-dialog-loading"><span style="top: -40px;" class="ckeditor-dialog-loading-link">${Drupal.t('Loading...')}</span></div>`);
|
Chris@0
|
241 $content.appendTo($target);
|
Chris@0
|
242
|
Chris@0
|
243 const ckeditorAjaxDialog = Drupal.ajax({
|
Chris@0
|
244 dialog: dialogSettings,
|
Chris@0
|
245 dialogType: 'modal',
|
Chris@0
|
246 selector: '.ckeditor-dialog-loading-link',
|
Chris@0
|
247 url,
|
Chris@0
|
248 progress: { type: 'throbber' },
|
Chris@0
|
249 submit: {
|
Chris@0
|
250 editor_object: existingValues,
|
Chris@0
|
251 },
|
Chris@0
|
252 });
|
Chris@0
|
253 ckeditorAjaxDialog.execute();
|
Chris@0
|
254
|
Chris@0
|
255 // After a short delay, show "Loading…" message.
|
Chris@0
|
256 window.setTimeout(() => {
|
Chris@0
|
257 $content.find('span').animate({ top: '0px' });
|
Chris@0
|
258 }, 1000);
|
Chris@0
|
259
|
Chris@0
|
260 // Store the save callback to be executed when this dialog is closed.
|
Chris@0
|
261 Drupal.ckeditor.saveCallback = saveCallback;
|
Chris@0
|
262 },
|
Chris@0
|
263 };
|
Chris@0
|
264
|
Chris@0
|
265 // Moves the dialog to the top of the CKEDITOR stack.
|
Chris@0
|
266 $(window).on('dialogcreate', (e, dialog, $element, settings) => {
|
Chris@0
|
267 $('.ui-dialog--narrow').css('zIndex', CKEDITOR.config.baseFloatZIndex + 1);
|
Chris@0
|
268 });
|
Chris@0
|
269
|
Chris@0
|
270 // Respond to new dialogs that are opened by CKEditor, closing the AJAX loader.
|
Chris@0
|
271 $(window).on('dialog:beforecreate', (e, dialog, $element, settings) => {
|
Chris@0
|
272 $('.ckeditor-dialog-loading').animate({ top: '-40px' }, function () {
|
Chris@0
|
273 $(this).remove();
|
Chris@0
|
274 });
|
Chris@0
|
275 });
|
Chris@0
|
276
|
Chris@0
|
277 // Respond to dialogs that are saved, sending data back to CKEditor.
|
Chris@0
|
278 $(window).on('editor:dialogsave', (e, values) => {
|
Chris@0
|
279 if (Drupal.ckeditor.saveCallback) {
|
Chris@0
|
280 Drupal.ckeditor.saveCallback(values);
|
Chris@0
|
281 }
|
Chris@0
|
282 });
|
Chris@0
|
283
|
Chris@0
|
284 // Respond to dialogs that are closed, removing the current save handler.
|
Chris@0
|
285 $(window).on('dialog:afterclose', (e, dialog, $element) => {
|
Chris@0
|
286 if (Drupal.ckeditor.saveCallback) {
|
Chris@0
|
287 Drupal.ckeditor.saveCallback = null;
|
Chris@0
|
288 }
|
Chris@0
|
289 });
|
Chris@0
|
290
|
Chris@0
|
291 // Formulate a default formula for the maximum autoGrow height.
|
Chris@0
|
292 $(document).on('drupalViewportOffsetChange', () => {
|
Chris@0
|
293 CKEDITOR.config.autoGrow_maxHeight = 0.7 * (window.innerHeight - displace.offsets.top - displace.offsets.bottom);
|
Chris@0
|
294 });
|
Chris@0
|
295
|
Chris@0
|
296 // Redirect on hash change when the original hash has an associated CKEditor.
|
Chris@0
|
297 function redirectTextareaFragmentToCKEditorInstance() {
|
Chris@0
|
298 const hash = location.hash.substr(1);
|
Chris@0
|
299 const element = document.getElementById(hash);
|
Chris@0
|
300 if (element) {
|
Chris@0
|
301 const editor = CKEDITOR.dom.element.get(element).getEditor();
|
Chris@0
|
302 if (editor) {
|
Chris@0
|
303 const id = editor.container.getAttribute('id');
|
Chris@0
|
304 location.replace(`#${id}`);
|
Chris@0
|
305 }
|
Chris@0
|
306 }
|
Chris@0
|
307 }
|
Chris@0
|
308 $(window).on('hashchange.ckeditor', redirectTextareaFragmentToCKEditorInstance);
|
Chris@0
|
309
|
Chris@0
|
310 // Set autoGrow to make the editor grow the moment it is created.
|
Chris@0
|
311 CKEDITOR.config.autoGrow_onStartup = true;
|
Chris@0
|
312
|
Chris@0
|
313 // Set the CKEditor cache-busting string to the same value as Drupal.
|
Chris@0
|
314 CKEDITOR.timestamp = drupalSettings.ckeditor.timestamp;
|
Chris@0
|
315
|
Chris@0
|
316 if (AjaxCommands) {
|
Chris@0
|
317 /**
|
Chris@0
|
318 * Command to add style sheets to a CKEditor instance.
|
Chris@0
|
319 *
|
Chris@0
|
320 * Works for both iframe and inline CKEditor instances.
|
Chris@0
|
321 *
|
Chris@0
|
322 * @param {Drupal.Ajax} [ajax]
|
Chris@0
|
323 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}.
|
Chris@0
|
324 * @param {object} response
|
Chris@0
|
325 * The response from the Ajax request.
|
Chris@0
|
326 * @param {string} response.editor_id
|
Chris@0
|
327 * The CKEditor instance ID.
|
Chris@0
|
328 * @param {number} [status]
|
Chris@0
|
329 * The XMLHttpRequest status.
|
Chris@0
|
330 *
|
Chris@0
|
331 * @see http://docs.ckeditor.com/#!/api/CKEDITOR.dom.document
|
Chris@0
|
332 */
|
Chris@0
|
333 AjaxCommands.prototype.ckeditor_add_stylesheet = function (ajax, response, status) {
|
Chris@0
|
334 const editor = CKEDITOR.instances[response.editor_id];
|
Chris@0
|
335
|
Chris@0
|
336 if (editor) {
|
Chris@0
|
337 response.stylesheets.forEach((url) => {
|
Chris@0
|
338 editor.document.appendStyleSheet(url);
|
Chris@0
|
339 });
|
Chris@0
|
340 }
|
Chris@0
|
341 };
|
Chris@0
|
342 }
|
Chris@0
|
343 }(Drupal, Drupal.debounce, CKEDITOR, jQuery, Drupal.displace, Drupal.AjaxCommands));
|