【问题标题】:Autocomplete lists in CKEditor [closed]CKEditor中的自动完成列表[关闭]
【发布时间】:2015-02-07 02:27:08
【问题描述】:

我需要向我的 CKEditor 添加一个功能,以便在用户在文档中键入“#”时向用户提供建议,建议可以根据页面上的其他一些字段即时更改。请帮忙

【问题讨论】:

标签: jquery twitter autocomplete ckeditor search-suggestion


【解决方案1】:

为了制作建议框,您必须使您的自定义插件使用上下文菜单作为建议框,请从此处查看链接以获取制作ckeditor插件的基本知识a link

将此添加到您的 config.js,其中自动完成是插件的名称

config.extraPlugins = 'autocomplete';

然后在ckeditor文件夹中创建如下目录结构/文件

ckeditor->plugins->autocomplete->plugin.js

将以下内容放入你的 plugin.js 文件中

CKEDITOR.plugins.add('autocomplete',
            {
                init : function(editor) {

                     var autocompleteCommand = editor.addCommand('autocomplete', {
                        exec : function(editor) {

我们需要在文档中创建一个虚拟 span 来计算要显示的菜单的当前位置

                            var dummyElement = editor.document
                                    .createElement('span');
                            editor.insertElement(dummyElement);

                            var x = 0;
                            var y = 0;

                            var obj = dummyElement.$;

                            while (obj.offsetParent) {
                                x += obj.offsetLeft;
                                y += obj.offsetTop;
                                obj = obj.offsetParent;
                            }
                            x += obj.offsetLeft;
                            y += obj.offsetTop;

                            dummyElement.remove();

计算完位置后,我们移除元素并调用方法显示建议(放置在上下文菜单中,在下一个函数中配置)

                            editor.contextMenu.show(editor.document
                                    .getBody(), null, x, y);
                        }
                    });
                },

这里是编辑器上的监听器绑定,用于检查当前键是否为#,CKEDITOR.SHIFT + 51 是#的组合键

                afterInit : function(editor) {
                    editor.on('key', function(evt) {
                        if (evt.data.keyCode == CKEDITOR.SHIFT + 51) {
                            editor.execCommand('autocomplete');
                        }
                    });

reloadSuggetionBox 命令将在 ckeditor 准备就绪后从您的外部 jquery 调用以生成菜单

                    var firstExecution = true;
                    var dataElement = {};

                     editor.addCommand('reloadSuggetionBox', {
                            exec : function(editor) {
                                if (editor.contextMenu) {
                                    dataElement = {};
                                    editor.addMenuGroup('suggestionBoxGroup');

                            $.each(Suggestions,function(i, suggestion)
                            {
                                    var suggestionBoxItem = "suggestionBoxItem"+ i; 
                                    dataElement[suggestionBoxItem] = CKEDITOR.TRISTATE_OFF;
                                    editor.addMenuItem(suggestionBoxItem,
                                                                        {
                                        id : suggestion.id,
                                        label : suggestion.label,
                                        group : 'suggestionBoxGroup',
                                        icon  : null,
                                        onClick : function() {
                                            var data = editor.getData();
                                            var selection = editor.getSelection();
                                            var element = selection.getStartElement();
                                            var ranges = selection.getRanges();
                                            ranges[0].setStart(element.getFirst(), 0);
                                            ranges[0].setEnd(element.getFirst(),0);
                                            editor.insertHtml(this.id + ' ');
                                            },
                                            });
                                    });

                                    if(firstExecution == true)
                                        {
                                            editor.contextMenu.addListener(function(element) {
                                                return dataElement;
                                            });
                                        firstExecution = false;
                                        }
                                }
                            }
                     });

                    delete editor._.menuItems.paste;
                },
            });

这里的“建议”是页面上某处存在的变量,其中包含要在建议中显示的具有“id”和“标签”的对象列表。

现在为了配置这些建议,请执行以下jquery代码,之后,每当按下'#'时,都会显示建议

$('textarea').ckeditor();
CKEDITOR.on( 'instanceReady', function( evt ) {
        CKEDITOR.instances.contractData.execCommand('reloadSuggetionBox');
    });

这将加载 ckeditor(contractData 是我的 ckeditor 实例的名称)并配置插件以显示“建议”变量中当前存在的建议,只要您需要刷新/更改建议,您只需在之后调用此函数重新加载“建议”变量

 CKEDITOR.instances.contractData.execCommand('reloadSuggetionBox');

如果您在执行此操作时遇到任何问题,请告诉我。

在我的仓库中找到可下载的插件

http://navalgandhi1989.github.io/ckeditor-autocomplete-suggestions-plugin/

【讨论】:

  • 它确实有效,我为此尝试了您的代码 +1。准确地说,当任何人在其中键入“#”(或根据“Key”事件侦听器中使用的键代码的任何其他内容)时,它会触发自定义上下文菜单打开。此解决方案的唯一缺点是使用一长串建议。需要对此进行更多自定义,以便在他在“#”键后键入更多字符时过滤掉结果。很高兴知道包括您在内的任何人是否已经进行了此自定义。
  • 嗨 Suraj,对不起,我没有进一步开发插件,但我们肯定可以一起努力,看看它的可能性。
  • 你能给个建议变量的样本吗?
  • 我收到以下错误,如何解决? 未捕获的类型错误:无法读取未定义的属性“execCommand”
  • 嗨,示例建议 json 文件位于 github 存储库中,地址为 github.com/navalgandhi1989/…
猜你喜欢
  • 2014-10-05
  • 2018-06-28
  • 2018-01-13
  • 2010-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-18
  • 2012-06-17
相关资源
最近更新 更多