Hi there!
I have made a plugin (based on an iFrame dialog) where a user can insert a link on the selected text.
It works great with text, but with images, nothing happens. Also, if the text has some formatting (e.g. bold), then it looses the formatting.
I have this code:
Does anyone know how to get the HTML instead of the plain text? So it would work with images too? I have looked at the ckeditor source, but couldn't find anything.
I have made a plugin (based on an iFrame dialog) where a user can insert a link on the selected text.
It works great with text, but with images, nothing happens. Also, if the text has some formatting (e.g. bold), then it looses the formatting.
I have this code:
function addLink(){ var CKEDITOR = window.parent.CKEDITOR; var g = CKEDITOR.instances.editor1; if (CKEDITOR.env.ie) { selection = g.getSelection().document.$.selection.createRange().text; } else { selection = g.getSelection().getSelectedText(); } alert(getSelectionHTML(selection)); CKEDITOR.instances.editor1.insertHtml('<a href="'+$("#html_"+$("#parent_id").val()).html()+'">'+selection+'</a>'); CKEDITOR.dialog.getCurrent().hide(); }
Does anyone know how to get the HTML instead of the plain text? So it would work with images too? I have looked at the ckeditor source, but couldn't find anything.
I too have the same issue.
I too have the same issue.
how i fixed it
I was just trying to debug my issue, and somehow it just gave me what I wanted.
You really only need to see the link.setHtml line near the bottom. I tested IE9 (not IE10 as of this post), and then I tested Chrome and both worked. I haven't gone through a full test of Safari, FireFox, Opera, etc, but I'd say it's safe to use. Try it on your end and post a comment.
Beautiful !
Thanks unriveled1 !
I could fix it on my code base too.
Here is my function:
var selection = editor.getSelection();
if (selection.getType() == CKEDITOR.SELECTION_ELEMENT) {
var selectedContent = selection.getSelectedElement().$.outerHTML;
} else if (selection.getType() == CKEDITOR.SELECTION_TEXT) {
if (CKEDITOR.env.ie) {
selection.unlock(true);
selectedContent = selection.getNative().createRange().text;
} else {
selectedContent = selection.getNative();
console.log("The selectedContent is: " + selectedContent);
}
}