Hi guys,
I am using CKEditor 3.x/FF3.x to develop a function, but I met a problem when I need to restore cursor's position, let me elaborate details. Please help to reply if you've any comments, thanks in advance.
The scenarios looks like this. One user open a page, is editing a <p> within CKEditor, let's say the <p> contains the following content
<p>Testing content Hello World!</p>
Assume userA's cursor is currently located after the "H" of "Hello". At the same time, the <p>'s content is updated from Server-update when userA is still editing, let's assume server-update changed the <p>'s content by adding "12345" after "Testing" to <p> as below.
<p>Testing content12345 Hello World!</p>
So I'm writing code to update <p>'s content, as well to keep userA's cursor. Some psedo code like this,
Then comes the problem that userA's original cursor position cannot be restored, the cursor is just not visible. I can see range.startOffset has been added, but the cursor just doesn't work.
I don't know where the problem is, it just seems the range.select() doesn't take effect. Is it because the range object is changed when I am still trying to use it? Don't know much to CkEditor, please help if you have any comments or suggestions. Thanks again.
Regards
Steve
I am using CKEditor 3.x/FF3.x to develop a function, but I met a problem when I need to restore cursor's position, let me elaborate details. Please help to reply if you've any comments, thanks in advance.
The scenarios looks like this. One user open a page, is editing a <p> within CKEditor, let's say the <p> contains the following content
<p>Testing content Hello World!</p>
Assume userA's cursor is currently located after the "H" of "Hello". At the same time, the <p>'s content is updated from Server-update when userA is still editing, let's assume server-update changed the <p>'s content by adding "12345" after "Testing" to <p> as below.
<p>Testing content12345 Hello World!</p>
So I'm writing code to update <p>'s content, as well to keep userA's cursor. Some psedo code like this,
var pEle = getpElement... var pText=pEle.innerHTML; var pos = pText.indexOf("Testing"); pEle.innerHtml=pText.substr(0,pos) + ""12345"+ pText.substr(pos); var selection= CKEDITOR.instances.editor1.getSelection(); var range = selection.getRanges()[0]; range.startOffset += "12345".length; range.select() selection.selectRanges([range])
Then comes the problem that userA's original cursor position cannot be restored, the cursor is just not visible. I can see range.startOffset has been added, but the cursor just doesn't work.
I don't know where the problem is, it just seems the range.select() doesn't take effect. Is it because the range object is changed when I am still trying to use it? Don't know much to CkEditor, please help if you have any comments or suggestions. Thanks again.
Regards
Steve
Re: How to restore the original cursor's position?
Re: How to restore the original cursor's position?
But when I try to set it to expected position within the paragraph with the following code, it failed when newRange.select() is executed, viewing the stacktrace, I saw the error is located at CKeditor plugin of "selection/plugins.js", the function is CKEDITOR.dom.range.prototype.select().
I am wondering is it a bug of ckeditor? Could anyone please help on that? Thanks a lot!
Re: How to restore the original cursor's position?
From a design point, based on your description, I dont think it would be a good idea to restore the cursor position after the server updated. That is, what if after the update the text is totally different from what the user was looking at? where would you set the cursor?
That is, picture this scenario:
lets say the original text is "Hello World!" and the cursor is at "W". After the update, the new text becomes "Hello". Where would you set your cursor?
Re: How to restore the original cursor's position?
Adico, many thanks for your feedback.
You are right that when the updated text from server override or conflict with the position that the user is currently editing. There might be some very complex scenarios to explain here, but I do have some algorithm to transform the position to an doable position when that happened. Take your scenario as example, when I find the original position is no more valid, I will change the position to the nearest position, here to after "o" of "Hello".
So my problem continues, I cannot move the cursor to expected position technically, could you let me know how to do that, if some script or examples, that will be best to me. Thanks again.
Re: How to restore the original cursor's position?
if that is the case, take a look at one of my previous post: http://cksource.com/forums/viewtopic.php?f=11&t=16658&p=42575#p42575
you'll have to do something similar to the following:
Re: How to restore the original cursor's position?
Re: How to restore the original cursor's position?
Adico, thank you all the same.