javascript - CKEditor 文件浏览器中的自定义按钮

标签 javascript jquery ckeditor

CKeditor 有一个内置的文件浏览和上传。它可以与许多外部插件集成,包括 CKFinder 甚至 KCFinder(免费替代品)。

enter image description here

如何在现有的默认面板上添加自定义按钮? (例如,预览下的压缩图像按钮会调用我的外部 PHP 脚本)。

最佳答案

好的,所以我阅读了另一个答案中编写的代码,但仍然不清楚,不得不深入研究文档。我终于设法解决了这个问题,不知道在哪里添加它,所以我把它放在我的 $(document).ready() 函数中。接受有关更好放置它的建议,但它有效:

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);
                        }
                    });
            }
        });
    }
} );

编辑:

因此,最后,我创建了一个名为 KCFinderHelper.js 的单独文件,我将其导入并在需要的任何地方使用。 (我的最终代码实际上要长得多,因为我添加了更多的功能和按钮)

关于javascript - CKEditor 文件浏览器中的自定义按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21381840/

相关文章:

javascript - 检测是否处于全屏模式(3d 动画书)

javascript - Node 骑士调用队列

javascript - 用 jQuery 替换/重新加载输入列表并保留第一个元素

jquery - 如何克隆克隆的表行?

javascript - Angularjs:捕获浏览器中的刷新事件

javascript - CKEDITOR getData() 返回 html 字符实体(unicode),但是如何获得未翻译的字符?

sql-server - 如何使用 CKEditor 在 Grails 中保存和显示 HTML?

javascript - 使 <td> 充当伞 <tr> 内的 <tr>(表格行)

javascript - react CKeditor Jest 错误 : SyntaxError: Cannot use import statement outside a module

javascript - Firefox 与 IE Javascript 问题