1 | <?php |
---|
2 | /** |
---|
3 | * @brief dcCKEditor, a plugin for Dotclear 2 |
---|
4 | * |
---|
5 | * @package Dotclear |
---|
6 | * @subpackage Plugins |
---|
7 | * |
---|
8 | * @copyright Olivier Meunier & Association Dotclear |
---|
9 | * @copyright GPL-2.0-only |
---|
10 | */ |
---|
11 | |
---|
12 | if (!defined('DC_CONTEXT_ADMIN')) {return;} |
---|
13 | |
---|
14 | header('Content-type: text/javascript'); |
---|
15 | |
---|
16 | if (!empty($_GET['context'])) { |
---|
17 | $context = $_GET['context']; |
---|
18 | } else { |
---|
19 | $context = ''; |
---|
20 | } |
---|
21 | |
---|
22 | $__extraPlugins = new ArrayObject(); |
---|
23 | $core->callBehavior('ckeditorExtraPlugins', $__extraPlugins, $context); |
---|
24 | $extraPlugins = $__extraPlugins->getArrayCopy(); |
---|
25 | |
---|
26 | ?> |
---|
27 | |
---|
28 | (function($) { |
---|
29 | $.toolbarPopup = function toolbarPopup(url) { |
---|
30 | if (dotclear.admin_base_url != '') { |
---|
31 | var pos = url.indexOf(dotclear.admin_base_url); |
---|
32 | if (pos == -1) { |
---|
33 | url = dotclear.admin_base_url + url; |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | var args = Array.prototype.slice.call(arguments); |
---|
38 | var width = 520, height = 420; |
---|
39 | if (args[1]!==undefined) { |
---|
40 | width = args[1].width || width; |
---|
41 | height = args[1].height || height; |
---|
42 | } |
---|
43 | |
---|
44 | var popup_params = 'alwaysRaised=yes,dependent=yes,toolbar=yes,'; |
---|
45 | popup_params += 'height='+height+',width='+width+',menubar=no,resizable=yes,scrollbars=yes,status=no'; |
---|
46 | var popup_link = window.open(url,'dc_popup', popup_params); |
---|
47 | }; |
---|
48 | |
---|
49 | $.stripBaseURL = function stripBaseURL(url) { |
---|
50 | if (dotclear.base_url != '') { |
---|
51 | var pos = url.indexOf(dotclear.base_url); |
---|
52 | if (pos == 0) { |
---|
53 | url = url.substr(dotclear.base_url.length); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | return url; |
---|
58 | }; |
---|
59 | |
---|
60 | /* Retrieve editor from popup */ |
---|
61 | $.active_editor = null; |
---|
62 | $.getEditorName = function getEditorName() { |
---|
63 | return $.active_editor; |
---|
64 | } |
---|
65 | chainHandler(window,'onbeforeunload',function(e) { |
---|
66 | if (e == undefined && window.event) { |
---|
67 | e = window.event; |
---|
68 | } |
---|
69 | |
---|
70 | var editor = CKEDITOR.instances[$.getEditorName()]; |
---|
71 | if (editor !== undefined && !confirmClosePage.formSubmit && editor.checkDirty()) { |
---|
72 | e.returnValue = confirmClosePage.prompt; |
---|
73 | return confirmClosePage.prompt; |
---|
74 | } |
---|
75 | return false; |
---|
76 | }); |
---|
77 | })(jQuery); |
---|
78 | |
---|
79 | $(function() { |
---|
80 | /* By default ckeditor load related resources with a timestamp to avoid cache problem when upgrading itself |
---|
81 | * load_plugin_file.php does not allow other param that file to load (pf param), so remove timestamp |
---|
82 | */ |
---|
83 | |
---|
84 | CKEDITOR.timestamp = ''; |
---|
85 | |
---|
86 | <?php if (!isset($dcckeditor_disable_native_spellchecker) || $dcckeditor_disable_native_spellchecker): ?> |
---|
87 | CKEDITOR.config.disableNativeSpellChecker = true; |
---|
88 | <?php else: ?> |
---|
89 | CKEDITOR.config.disableNativeSpellChecker = false; |
---|
90 | <?php endif;?> |
---|
91 | |
---|
92 | CKEDITOR.config.skin = 'dotclear,'+dotclear.dcckeditor_plugin_url+'/js/ckeditor-skins/dotclear/'; |
---|
93 | CKEDITOR.config.baseHref = dotclear.base_url; |
---|
94 | CKEDITOR.config.height = '<?php echo $core->auth->getOption('edit_size') * 14; ?>px'; |
---|
95 | |
---|
96 | <?php if (!empty($dcckeditor_cancollapse_button)): ?> |
---|
97 | CKEDITOR.config.toolbarCanCollapse = true; |
---|
98 | <?php endif;?> |
---|
99 | |
---|
100 | CKEDITOR.plugins.addExternal('entrylink',dotclear.dcckeditor_plugin_url+'/js/ckeditor-plugins/entrylink/'); |
---|
101 | CKEDITOR.plugins.addExternal('dclink',dotclear.dcckeditor_plugin_url+'/js/ckeditor-plugins/dclink/'); |
---|
102 | CKEDITOR.plugins.addExternal('media',dotclear.dcckeditor_plugin_url+'/js/ckeditor-plugins/media/'); |
---|
103 | CKEDITOR.plugins.addExternal('img',dotclear.dcckeditor_plugin_url+'/js/ckeditor-plugins/img/'); |
---|
104 | |
---|
105 | <?php if (!empty($dcckeditor_textcolor_button) || !empty($dcckeditor_background_textcolor_button)): ?> |
---|
106 | // button add "More Colors..." can be added if colordialog plugin is enabled |
---|
107 | CKEDITOR.config.colorButton_enableMore = true; |
---|
108 | <?php endif;?> |
---|
109 | |
---|
110 | CKEDITOR.config.defaultLanguage = dotclear.user_language; |
---|
111 | CKEDITOR.config.language = dotclear.user_language; |
---|
112 | CKEDITOR.config.contentsLanguage = dotclear.user_language; |
---|
113 | |
---|
114 | <?php |
---|
115 | if (!empty($extraPlugins) && count($extraPlugins) > 0) { |
---|
116 | foreach ($extraPlugins as $plugin) { |
---|
117 | printf("\tCKEDITOR.plugins.addExternal('%s','%s');\n", $plugin['name'], $plugin['url']); |
---|
118 | } |
---|
119 | } |
---|
120 | ?> |
---|
121 | if (dotclear.ckeditor_context === undefined || dotclear.ckeditor_tags_context[dotclear.ckeditor_context] === undefined) { |
---|
122 | return; |
---|
123 | } |
---|
124 | $(dotclear.ckeditor_tags_context[dotclear.ckeditor_context].join(',')).ckeditor({ |
---|
125 | |
---|
126 | <?php |
---|
127 | $defautExtraPlugins = 'entrylink,dclink,media,justify,colorbutton,format,img'; |
---|
128 | if (!empty($extraPlugins) && count($extraPlugins) > 0) { |
---|
129 | foreach ($extraPlugins as $plugin) { |
---|
130 | $defautExtraPlugins .= ',' . $plugin['name']; |
---|
131 | } |
---|
132 | } |
---|
133 | ?> |
---|
134 | extraPlugins: '<?php echo $defautExtraPlugins; ?>', |
---|
135 | |
---|
136 | keystrokes: [ |
---|
137 | [ CKEDITOR.CTRL + (CKEDITOR.env.mac ? CKEDITOR.ALT : CKEDITOR.SHIFT) + |
---|
138 | dotclear.msg.link_accesskey.toUpperCase().charCodeAt(0),'dcLinkCommand' ], // Ctrl+Alt+l |
---|
139 | [ CKEDITOR.CTRL + (CKEDITOR.env.mac ? CKEDITOR.ALT : CKEDITOR.SHIFT) + |
---|
140 | dotclear.msg.img_select_accesskey.toUpperCase().charCodeAt(0),'mediaCommand' ], // Ctrl+Alt+m |
---|
141 | ], |
---|
142 | |
---|
143 | <?php if (!empty($dcckeditor_format_select)): ?> |
---|
144 | // format tags |
---|
145 | |
---|
146 | <?php if (!empty($dcckeditor_format_tags)): ?> |
---|
147 | format_tags: '<?php echo $dcckeditor_format_tags; ?>', |
---|
148 | <?php else: ?> |
---|
149 | format_tags: 'p;h1;h2;h3;h4;h5;h6;pre;address', |
---|
150 | <?php endif;?> |
---|
151 | |
---|
152 | // following definition are needed to be specialized |
---|
153 | format_p: { element: 'p' }, |
---|
154 | format_h1: { element: 'h1' }, |
---|
155 | format_h2: { element: 'h2' }, |
---|
156 | format_h3: { element: 'h3' }, |
---|
157 | format_h4: { element: 'h4' }, |
---|
158 | format_h5: { element: 'h5' }, |
---|
159 | format_h6: { element: 'h6' }, |
---|
160 | format_pre: { element: 'pre' }, |
---|
161 | format_address: { element: 'address' }, |
---|
162 | <?php endif;?> |
---|
163 | |
---|
164 | entities: false, |
---|
165 | removeButtons: '', |
---|
166 | allowedContent: true, |
---|
167 | toolbar: [ |
---|
168 | { |
---|
169 | name: 'basicstyles', |
---|
170 | items: [ |
---|
171 | |
---|
172 | <?php if (!empty($dcckeditor_format_select)): ?> |
---|
173 | 'Format', |
---|
174 | <?php endif;?> |
---|
175 | |
---|
176 | 'Bold','Italic','Underline','Strike','Subscript','Superscript','Code','Blockquote', |
---|
177 | |
---|
178 | <?php if (!empty($dcckeditor_list_buttons)): ?> |
---|
179 | 'NumberedList','BulletedList', |
---|
180 | <?php endif;?> |
---|
181 | |
---|
182 | 'RemoveFormat' |
---|
183 | ] |
---|
184 | }, |
---|
185 | |
---|
186 | <?php if (!empty($dcckeditor_clipboard_buttons)): ?> |
---|
187 | { |
---|
188 | name: 'clipoard', |
---|
189 | items: ['Cut','Copy','Paste','PasteText','PasteFromWord'] |
---|
190 | }, |
---|
191 | <?php endif;?> |
---|
192 | |
---|
193 | <?php if (!empty($dcckeditor_action_buttons)): ?> |
---|
194 | { |
---|
195 | name: 'action', |
---|
196 | items: ['Undo','Redo'] |
---|
197 | }, |
---|
198 | <?php endif;?> |
---|
199 | |
---|
200 | <?php if (!empty($dcckeditor_alignment_buttons)): ?> |
---|
201 | { |
---|
202 | name: 'paragraph', |
---|
203 | items: ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'] |
---|
204 | }, |
---|
205 | <?php endif;?> |
---|
206 | |
---|
207 | <?php if (!empty($dcckeditor_table_button)): ?> |
---|
208 | { |
---|
209 | name: 'table', |
---|
210 | items: ['Table'] |
---|
211 | }, |
---|
212 | <?php endif;?> |
---|
213 | |
---|
214 | { |
---|
215 | name: 'custom', |
---|
216 | items: [ |
---|
217 | 'EntryLink','dcLink','Media','img','-', |
---|
218 | 'Source' |
---|
219 | |
---|
220 | <?php if (!empty($dcckeditor_textcolor_button)): ?> |
---|
221 | ,'TextColor' |
---|
222 | <?php endif;?> |
---|
223 | |
---|
224 | <?php if (!empty($dcckeditor_background_textcolor_button)): ?> |
---|
225 | ,'BGColor' |
---|
226 | <?php endif;?> |
---|
227 | ] |
---|
228 | }, |
---|
229 | { |
---|
230 | name: 'special', |
---|
231 | items: [ |
---|
232 | 'Maximize' |
---|
233 | ] |
---|
234 | }, |
---|
235 | |
---|
236 | <?php // add extra buttons comming from dotclear plugins |
---|
237 | if (!empty($extraPlugins) && count($extraPlugins) > 0) { |
---|
238 | $extraPlugins_str = "{name: 'extra', items: [%s]},\n"; |
---|
239 | $extra_icons = ''; |
---|
240 | foreach ($extraPlugins as $plugin) { |
---|
241 | $extra_icons .= sprintf("'%s',", $plugin['button']); |
---|
242 | } |
---|
243 | printf($extraPlugins_str, $extra_icons); |
---|
244 | } |
---|
245 | ?> |
---|
246 | ] |
---|
247 | }); |
---|
248 | |
---|
249 | CKEDITOR.on('instanceLoaded',function(e) { |
---|
250 | // Retrieve textarea element of the instance, then its line-height (in px) and rows values, |
---|
251 | // then apply line-height * rows (min = 6) to the inner height of the instance. |
---|
252 | |
---|
253 | var ta = document.getElementById(e.editor.name); |
---|
254 | if (ta !== undefined) { |
---|
255 | var ta_rows = ta.rows; |
---|
256 | var ta_line_height = parseFloat(window.getComputedStyle(ta,null).getPropertyValue('line-height')); |
---|
257 | if (ta_rows > 0 && ta_line_height > 0) { |
---|
258 | var ta_height = String(Math.max(ta_rows,6) * ta_line_height); |
---|
259 | e.editor.resize('100%',ta_height,true); |
---|
260 | } |
---|
261 | } |
---|
262 | }); |
---|
263 | |
---|
264 | CKEDITOR.on('instanceReady',function(e) { |
---|
265 | if (dotclear && dotclear.data && dotclear.data.htmlFontSize) { |
---|
266 | e.editor.document.$.documentElement.style.setProperty('--html-font-size',dotclear.data.htmlFontSize); |
---|
267 | } |
---|
268 | |
---|
269 | if ($('label[for="post_excerpt"] button').attr('aria-label') == dotclear.img_minus_alt) { |
---|
270 | $('#cke_post_excerpt').removeClass('hide'); |
---|
271 | } else { |
---|
272 | $('#cke_post_excerpt').addClass('hide'); |
---|
273 | } |
---|
274 | |
---|
275 | $('#excerpt-area label').click(function() { |
---|
276 | $('#cke_post_excerpt').toggleClass('hide',$('#post_excerpt').hasClass('hide')); |
---|
277 | }); |
---|
278 | |
---|
279 | const ta = document.getElementById(e.editor.name); |
---|
280 | if (ta !== undefined) { |
---|
281 | if (ta.lang && e.editor.config.contentsLanguage !== ta.lang) { |
---|
282 | let config = e.editor.config; |
---|
283 | config.contentsLanguage = ta.lang; |
---|
284 | // e.editor.destroy(); |
---|
285 | CKEDITOR.replace(e.editor.name, config); |
---|
286 | } |
---|
287 | } |
---|
288 | }); |
---|
289 | |
---|
290 | // @TODO: find a better way to retrieve active editor |
---|
291 | for (var id in CKEDITOR.instances) { |
---|
292 | CKEDITOR.instances[id].on('focus',function(e) { |
---|
293 | $.active_editor = e.editor.name; |
---|
294 | }); |
---|
295 | } |
---|
296 | }); |
---|