Dotclear

source: plugins/dcCKEditor/_post_config.php @ 4024:e70d750792f7

Revision 4024:e70d750792f7, 9.7 KB checked in by franck <carnet.franck.paul@…>, 6 years ago (diff)

Avoid destroying current CKEDitor instance before changing content language - will introduce small regression (ie no more spellcheck on foreign language content)

RevLine 
[2738]1<?php
[3731]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 */
[2738]11
[3730]12if (!defined('DC_CONTEXT_ADMIN')) {return;}
[3367]13
[2738]14header('Content-type: text/javascript');
[3367]15
[2761]16if (!empty($_GET['context'])) {
[3730]17    $context = $_GET['context'];
[2761]18} else {
[3730]19    $context = '';
[2761]20}
[3367]21
[2738]22$__extraPlugins = new ArrayObject();
[3730]23$core->callBehavior('ckeditorExtraPlugins', $__extraPlugins, $context);
[2738]24$extraPlugins = $__extraPlugins->getArrayCopy();
[3367]25
[2738]26?>
[3367]27
[2738]28(function($) {
[3730]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        }
[2962]36
[3730]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        }
[2835]43
[3730]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    };
[2738]48
[3730]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        }
[2751]56
[3730]57        return url;
58    };
[2738]59
[3730]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        }
[2816]69
[3730]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    });
[2738]77})(jQuery);
78
79$(function() {
[3730]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     */
[2738]83
[3730]84    CKEDITOR.timestamp = '';
[3367]85
86<?php if (!isset($dcckeditor_disable_native_spellchecker) || $dcckeditor_disable_native_spellchecker): ?>
[3730]87    CKEDITOR.config.disableNativeSpellChecker = true;
[3367]88<?php else: ?>
[3730]89    CKEDITOR.config.disableNativeSpellChecker = false;
90<?php endif;?>
[3367]91
[3730]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';
[2738]95
[3367]96<?php if (!empty($dcckeditor_cancollapse_button)): ?>
[3730]97    CKEDITOR.config.toolbarCanCollapse = true;
98<?php endif;?>
[2751]99
[3730]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/');
[3367]104
105<?php if (!empty($dcckeditor_textcolor_button) || !empty($dcckeditor_background_textcolor_button)): ?>
[3730]106    // button add "More Colors..." can be added if colordialog plugin is enabled
107    CKEDITOR.config.colorButton_enableMore = true;
108<?php endif;?>
[2738]109
[3897]110    CKEDITOR.config.defaultLanguage = dotclear.user_language;
111    CKEDITOR.config.language = dotclear.user_language;
112    CKEDITOR.config.contentsLanguage = dotclear.user_language;
113
[3367]114<?php
[3730]115if (!empty($extraPlugins) && count($extraPlugins) > 0) {
116    foreach ($extraPlugins as $plugin) {
117        printf("\tCKEDITOR.plugins.addExternal('%s','%s');\n", $plugin['name'], $plugin['url']);
118    }
119}
[2738]120?>
[3730]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({
[3367]125
[2738]126<?php
[3730]127$defautExtraPlugins = 'entrylink,dclink,media,justify,colorbutton,format,img';
128if (!empty($extraPlugins) && count($extraPlugins) > 0) {
129    foreach ($extraPlugins as $plugin) {
130        $defautExtraPlugins .= ',' . $plugin['name'];
131    }
132}
[2738]133?>
[3730]134        extraPlugins: '<?php echo $defautExtraPlugins; ?>',
[2738]135
[3730]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        ],
[3362]142
[3367]143<?php if (!empty($dcckeditor_format_select)): ?>
[3730]144        // format tags
[3367]145
[3730]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;?>
[3367]151
[3730]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;?>
[2738]163
[3730]164        entities: false,
165        removeButtons: '',
166        allowedContent: true,
167        toolbar: [
168            {
169                name: 'basicstyles',
170                items: [
[3367]171
172<?php if (!empty($dcckeditor_format_select)): ?>
[3730]173                    'Format',
[2738]174<?php endif;?>
[3367]175
[3730]176                    'Bold','Italic','Underline','Strike','Subscript','Superscript','Code','Blockquote',
[2751]177
[3367]178<?php if (!empty($dcckeditor_list_buttons)): ?>
[3730]179                    'NumberedList','BulletedList',
180<?php endif;?>
[3367]181
[3730]182                    'RemoveFormat'
183                ]
184            },
[3367]185
186<?php if (!empty($dcckeditor_clipboard_buttons)): ?>
[3730]187            {
188                name: 'clipoard',
189                items: ['Cut','Copy','Paste','PasteText','PasteFromWord']
190            },
191<?php endif;?>
[3367]192
[3955]193<?php if (!empty($dcckeditor_action_buttons)): ?>
194            {
195                name: 'action',
196                items: ['Undo','Redo']
197            },
198<?php endif;?>
199
[3367]200<?php if (!empty($dcckeditor_alignment_buttons)): ?>
[3730]201            {
202                name: 'paragraph',
203                items: ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock']
204            },
205<?php endif;?>
[3367]206
207<?php if (!empty($dcckeditor_table_button)): ?>
[3730]208            {
209                name: 'table',
210                items: ['Table']
211            },
212<?php endif;?>
[3367]213
[3730]214            {
215                name: 'custom',
216                items: [
217                    'EntryLink','dcLink','Media','img','-',
218                    'Source'
[3367]219
220<?php if (!empty($dcckeditor_textcolor_button)): ?>
[3730]221                    ,'TextColor'
222<?php endif;?>
[3367]223
224<?php if (!empty($dcckeditor_background_textcolor_button)): ?>
[3730]225                    ,'BGColor'
226<?php endif;?>
227                ]
228            },
229            {
230                name: 'special',
231                items: [
232                    'Maximize'
233                ]
234            },
[3367]235
236<?php // add extra buttons comming from dotclear plugins
[3730]237if (!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}
[3367]245?>
[3730]246        ]
247    });
[2738]248
[3730]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.
[3367]252
[3730]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    });
[3670]263
[3730]264    CKEDITOR.on('instanceReady',function(e) {
[3915]265        if (dotclear && dotclear.data && dotclear.data.htmlFontSize) {
266            e.editor.document.$.documentElement.style.setProperty('--html-font-size',dotclear.data.htmlFontSize);
[3730]267        }
[3367]268
[3917]269        if ($('label[for="post_excerpt"] button').attr('aria-label') == dotclear.img_minus_alt) {
[3730]270            $('#cke_post_excerpt').removeClass('hide');
271        } else {
272            $('#cke_post_excerpt').addClass('hide');
273        }
[2751]274
[3730]275        $('#excerpt-area label').click(function() {
276            $('#cke_post_excerpt').toggleClass('hide',$('#post_excerpt').hasClass('hide'));
277        });
[3640]278
[3897]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;
[4024]284//                e.editor.destroy();
[3897]285                CKEDITOR.replace(e.editor.name, config);
286            }
287        }
[3730]288    });
[2751]289
[3730]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    }
[2738]296});
Note: See TracBrowser for help on using the repository browser.

Sites map