javascript - 如何在页面内调用CKEditor 4插件功能?

标签 javascript jquery ckeditor

假设我已经安装了 CKEditor 4.3(我确实这样做了)和 jquery 加载器。我已经加载了一个插件(我们称之为“foobar”),它几乎可以完成我想要的功能。插件的plugin.js 文件中有一个函数,我想在页面内手动调用该函数。对于此示例,我们调用函数 hello()。

我想在单击特定链接时调用它。

如何引用 ckeditor 插件中的函数?

这是plugin.js结构:

(function() {
    if (!supportsLocalStorage()) {
        CKEDITOR.plugins.add("foobar", {}); //register a dummy plugin to pass CKEditor plugin initialization process
        return;
    }

    CKEDITOR.plugins.add("foobar", {
        lang: 'de,en,jp,pl,pt-br,sv,zh,zh-cn', // %REMOVE_LINE_CORE%
        version: 0.10,
        init: function(editor) {
            CKEDITOR.document.appendStyleSheet(CKEDITOR.getUrl(CKEDITOR.plugins.getPath('foobar') + 'css/foobar.min.css'));

            if (typeof (jQuery) === 'undefined') {
                CKEDITOR.scriptLoader.load('//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', function() {
                    jQuery.noConflict();

                    loadPlugin(editor);
                });

            } else {
                CKEDITOR.scriptLoader.load(CKEDITOR.getUrl(CKEDITOR.plugins.getPath('foobar') + 'js/extensions.min.js'), function() {
                    loadPlugin(editor);
                });
            }
        }
    });

    function loadPlugin(editorInstance) {
        /*
        load of random stuff that isn't important for this example..
         */
    }



    var hello = function() {
        alert('hello!');
    };
}

最佳答案

插件的正确定义应该如下:

( function() {
    // Definition of the plugin.
    CKEDITOR.plugins.add( 'foo', {
        init: function( editor ) {
            ...
        }
    } );

    // Methods and variables related to the plugin but exposed to the public.
    CKEDITOR.plugins.foo = {
        hello: function() {
            ...
        }
    };
} )();

然后只需CKEDITOR.plugins.foo.hello()。这是 CKEditor 核心插件之间最常见的共享模式。

关于javascript - 如何在页面内调用CKEditor 4插件功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24432494/

相关文章:

javascript - 自关闭 .js 切换菜单

javascript - 如何更改@media 打印查询中的 css?

javascript - 你如何在 Rails 中改变背景?

php - MySQL "UPDATE"语法在列中输入 0

javascript - ReactJs 正确导入 ckeditor 配置 - CORS

javascript - 如何在jquery中访问iframe dom元素

javascript - 将项目返回到数组中

javascript - 如何记录短时间内的按键/键盘事件?

javascript - 如何在 jQuery 中使用变量?

javascript - Laravel ckeditor 数据未插入数据库