InsertHtml正在错误的位置添加指向ckeditor的链接



我已经将ckeditor 4.5.1与domino集成在一起,它只处理了一个问题。我添加了自定义按钮,它将打开另一个窗口。子窗口包含html按钮,这些按钮将使用insertHtml或insertElement插入到编辑器的链接。这适用于已保存的文档。但是,如果我将新内容粘贴到编辑器中,并使用自定义按钮窗口插入链接,则添加的链接只在一个位置而不是选定区域。在这两种情况下,光标位置和范围都会返回正确的值。

function openReferenceDialog(field){
//OpenReferenceDialog(field,'DialogReference',500,500);
fieldname=field
var oEditor = eval('CKEDITOR.instances.' + fieldname);
var mySelection = oEditor.getSelection();
if (CKEDITOR.env.ie) {
mySelection.unlock(false);
selectedText = mySelection.getNative().createRange().text;
} else {
selectedText = mySelection.getNative();
}
//oEditor.lockSelection(mySelection)
range = mySelection.getRanges()[0];
var filepath=document.location.protocol+'//'+document.location.host+'/'+document.forms[0].DbName.value;
dialog=window.open(filepath + '/' + 'DialogCreateNewGraph' + '?Openform&field='+field+"&seltext="+selectedText,'win','scrollbars=1,resizable=1,width=370,height=270');
dialog.focus();

}

在子窗口中插入代码(不包含在编辑器中(

var CKEdit = window.opener.CKEDITOR;
var oEditor = eval('CKEdit.instances.' + window.opener.fieldname);
var elementHtml = "<a href="javascript:OpenCkLink('" + url + "')">" + txt + "</a>"                                            
alert(window.opener.range.startOffset+"after window")
//oEditor.insertHtml(elementHtml);
element = CKEdit.dom.element.createFromHtml(elementHtml);
oEditor.insertHtml(elementHtml,window.opener.range);
oEditor.insertHtml("&nbsp;")

是否有其他方法可以使用所选文本的当前位置和长度将文本/链接添加到父ckeditor。请帮助解决问题。如果问题不清楚,请告诉我。

首先,我建议将编辑器升级到最新的4.10.1(或4.11,计划在几天内发布(。

接下来,请参阅CKEditor链接插件代码,并尝试以与此处相同的方式应用链接-https://github.com/ckeditor/ckeditor-dev/blob/major/plugins/link/dialogs/link.js#L24-L70.

为了使用insertElementinsertHtml,您需要尝试解锁选择或聚焦编辑器(或主体(,例如

document.body.focus();
child = window.open(path, '', 'toolbar=no,width=800,height=370,directories=no,status=yes,scrollbars=yes,menubar=no,resizable=yes');
child.creator = self;

然而,这些方法可能在旧的IE中不起作用,因此最好的方法是与链接插件相同的方法。基本上,你需要:

// Get Selection
var selection = CKEDITOR.instances.editor1.getSelection();
// Get Range or Ranges
var range = selection.getRanges( )[0];
// You use the range to insert either text or wrap link around selected text
// Please see Link plugin code for that.
// Set some sample link attributes
var attributes = {};
attributes.target = '_blank';
attributes.href = link_passed_as_ paramater;
// Create new style
var style = new CKEDITOR.style( { element: 'a', attributes: attributes } );
style.type = CKEDITOR.STYLE_INLINE;
// Apply style.
style.applyToRange( range, CKEDITOR.instances.editor1 );

最新更新