是否可以在 ckeditor 中以某种方式实现 jquery 自动完成?创建一个按钮并不难,但是是否可以将其硬连线以自动完成,以便在再次按下按钮之前键入下一个单词...?
任何远程做过类似事情的人,请告诉我。或者,如果这不可能,则显示一个自动完成搜索的弹出窗口,然后单击/选择时会将所选项目添加到 ckeditor 文本区域/当前光标位置(可能作为链接...)?
当然尽量不要做得太过分:)
最佳答案
为了制作建议框,您必须制作自定义插件以使用上下文菜单作为建议框,请从此处查看链接以了解制作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) {
我们需要在文档中创建一个虚拟范围来计算要显示的菜单的当前位置
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);
}
});
},
这里是编辑器上绑定(bind)的监听器,用于检查当前键是否为 #,CKEDITOR.SHIFT + 51 是 # 的组合键
afterInit : function(editor) {
editor.on('key', function(evt) {
if (evt.data.keyCode == CKEDITOR.SHIFT + 51) {
editor.execCommand('autocomplete');
}
});
reloadSuggetionBox 命令将从外部 jquery 调用以在 ckeditor 准备就绪后生成菜单
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/
关于jquery - CKeditor 有自动完成功能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11385700/