I'm having trouble in developing CKEditor plugins that insert uneditable contents into the text flow. I've been trying to utilize the range functions, but with little success as the documentation is less than stellar. So, given some text, lets say the plugin inserts "[[uneditable stuff]]" and then upon WYSIWYG display wraps that in a span so it can be styled in a color:
<p>This is some text[[uneditable stuff here]]</p>
When first inserting the uneditable stuff, we want the user to then be able to continue on typing or hitting Enter for a new line. The following code (which I got here: How to set cursor position to end of text in CKEditor?) works in Firefox but (naturally) not in IE9, 8, or 7:
var s = editor.getSelection();
editor.insertElement(e); // element 'e'= a span created earlier
var p = e.getParent();
s.selectElement(p);
var selected_ranges = s.getRanges();
selected_ranges[0].collapse(false); // false = to the end of the selected node
s.selectRanges(selected_ranges); // putting the current selection there
So what I want to happen is that the cursor goes at position "^":
<p>This is some text<span>[[uneditable stuff here]]</span>^</p>
If the new element is not at the end of the line, then after creating it, the cursor should go to here:
<p>This is some text<span>[[uneditable stuff here]]</span>^ with more text after the new element</p>
In FF, I can get the cursor at the end of the line though not at position after the new element. In IE, the cursor is still inside the new SPAN, which I see when I type and it is still in the span's css color, and when switching to SOURCE view, the text is gone (because it's an uneditable span).
I know there's a range.setStartAfter method, but have been totally unable to make it work even in FF/Chrome.
Does anybody have a really good handle on using range and selection methods in CKEditor? I know I don't!
Starting to think that just using editor.insertElement is wrong, and I should learn about the FakeElement (insertBogus?) functions, which I don't understand, yet. Stock plugins for such as links and images don't seem to have this problem.
I had to do some sneaky stuff to solve this, but it did get solved: After creating the non-editable item (a span with attribute content-editable: false) I had to create a "dummy" span with text consisting of one space. So, I insert the real span, then the dummy. But only when creating a new item.
So this goes in the "if not dealing with editing a selected item" section. Here, 'a' is the editor instance, 'e' is the desired non-editable item, 'f' is the dummy span.
I have to admit that I don't entirely understand my own code. I got there through hours of trial and error. Oh, and I had to add an htmlFilter Rule to get rid of left-over 'f' elements:
I also have to add that I don't remember why I had to replace "nbsp" entities before deleting the span. But it works. And I don't know why to delete you use "s.name" instead of just 's'.
Hope that helps someone.