简体   繁体   English

CKEditor的文件浏览器上的“自定义”按钮

[英]Custom button on CKEditor's File Browser

CKeditor has a built-in File Browse and Upload. CKeditor具有内置的文件浏览和上传。 It can be integrated with many external plugins including CKFinder or even KCFinder (free alternative). 它可以与许多外部插件集成,包括CKFinder甚至KCFinder(免费替代)。

在此处输入图片说明

How can I add a custom button on the existing default panel? 如何在现有默认面板上添加自定义按钮? (For example, a compress image button under preview which would call my an external PHP script). (例如,预览下的“压缩图像”按钮将调用我的外部PHP脚本)。

OK, so I read code written in another answer, but still wasn't clear and had to dive into documentation. 好的,所以我阅读了用另一个答案编写的代码,但是仍然不清楚,不得不深入文档。 I finally managed to settle for this fix, didn't know where to add it, so I put it into my $(document).ready() function. 我终于设法解决了这个问题,不知道在何处添加它,因此将其放入$(document).ready()函数中。 Open to suggestions on a better place to put it but it works: 可以建议在一个更好的地方放置它,但它可以起作用:

CKEDITOR.on( 'dialogDefinition', function( evt ) {
    var dialog = evt.data;
    if ( dialog.name == 'image' ) {
        // Get dialog we want
        var def = evt.data.definition;

        //Get The Desired Tab Element
        var infoTab = def.getContents( 'info' );

        //Add Our Button
        infoTab.add( {
            type: 'button',
            id: 'buttonId',
            label: 'Compress Image',
            title: 'My title',
            onClick: function() {
                //Here define what to do when button is clicked.
                //In my case, I traverse and get the inputs (dirty way).
                  var url = $(".cke_dialog_ui_vbox_child .cke_dialog_ui_text .cke_dialog_ui_labeled_content .cke_dialog_ui_input_text .cke_dialog_ui_input_text").eq(0).val();
                  var width = $(".cke_dialog_ui_vbox_child .cke_dialog_ui_text .cke_dialog_ui_labeled_content .cke_dialog_ui_input_text .cke_dialog_ui_input_text").eq(2).val();
                  var height = $(".cke_dialog_ui_vbox_child .cke_dialog_ui_text .cke_dialog_ui_labeled_content .cke_dialog_ui_input_text .cke_dialog_ui_input_text").eq(3).val();

                //Then I perform an ajax call to a Php file                             
                  $.ajax({ 
                    url: 'path/to/compress.php',
                    data: { 
                        url: url,
                        width: width,
                        height: height,
                        },
                    type: 'post',
                    success: function(output) {
                        alert(output);
                        }
                    });
            }
        });
    }
} );

EDIT: 编辑:

So, in the end, I created a separate file called KCFinderHelper.js which I import and use wherever I need it. 因此,最后,我创建了一个名为KCFinderHelper.js的单独文件,该文件可以在我需要的任何位置导入和使用。 (my final code is actually much longer as I added many more functions and buttons) (我的最终代码实际上要长得多,因为我添加了更多的功能和按钮)

Extend the dialog on dialogDefinition event. dialogDefinition事件上扩展对话框。 See my previous answer to know more. 请参阅我以前的答案以了解更多信息。 Also see the existing implementation of the Image dialog to find out how the definition looks like. 另请参阅“图像”对话框的现有实现 ,以了解定义的外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM