Retain cursor position after reloading the page in CKEditor

房东的猫 提交于 2019-12-30 10:07:48

问题


I am using CKEditor(4.1) in my project.I would like to retain the cursor position in editor after user reloading the page. CKEditor provides

var bookmark = editor.selection.createBookmarks();

to store the cursor position.However, if i use

var data = editor.getData()

returns the following content

<p>one</p>

<p>two<span style="display:none">&nbsp;</span></p>

<p>three</p>

instead of the following

<p>one</p>

<p>two<span data-cke-bookmakrs="1" style="display:none">&nbsp;</span></p>

<p>three</p>

In config.js, I did the following thing

config.extraAllowedContent = "span[data-cke-bookmark]"

What am i missing here?

Thanks in advance for your answers and suggestions...


回答1:


I found a workaround to resolve the problem. I won't say this is a direct solution.(I haven't check for IE)

Once I create bookmark, It will return JSON object as follows

{collapsed: true,
endNode: undefined,
serializable: undefined,
startNode: CKEDITOR.dom.element}

And you can get the reference element by

var spanRef =  object.startNode.$;

And a custom attribute.

$(spanRef).attr('data-selection-bookmark','1')//here value '1' doesn't mean anything

And do the following thing in config.js

config.extraAllowedContent = "span[data-selection-bookmark]"

When you ask the editor content by using editor.getData(), It will return the following

<p>one</p>

<p>two<span data-selection-bookmakr="1" style="display:none">&nbsp;</span></p>

<p>three</p>

The Next Half ( After Reload or Reinit)

var editor = CKEDITOR.replace( 'editor_textarea');
editor.on( 'contentDom', function(){
   var ifrWin = getIframeWindow(); //You need write a code to get iframe window of CKEditor


        var range = document.createRange();

        var sel = ifrWin.getSelection();

        var doc = editor.document.$;

        var $span = $( doc.body ).find( 'span[data-selection-bookmark]' );

        range.selectNode( $span[ 0 ] );// To move the cursor before

        range.collapse(true);

        sel.addRange(range);

        $span.remove();


        ifrWin.document.getElementsByTagName('body')[0].focus();
});


来源:https://stackoverflow.com/questions/27524002/retain-cursor-position-after-reloading-the-page-in-ckeditor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!