TinyMCE将光标返回到预先保存位置 - API 4

Jon*_*onJ 4 ajax tinymce cursor

我正在开发一个新安装的TinyMCE 4.编辑器是一个简单的内联DIV,只允许斜体和粗体(清理设置为删除大多数其他标签).

tinymce.init({
    inline: true,

    // we only want simple bold and italic controls
    toolbar: "undo redo | bold italic", 

    invalid_elements : "span,u,a,ul,ol,li,h1,h2,h3,h4,h5,h6,blockquote,strikethrough",
    ...
Run Code Online (Sandbox Code Playgroud)

我的问题:我想通过ajax每隔几次击键将我的编辑器的内容保存回我的数据库.

为了运行清理,我首先保存内容:

tinyMCE.triggerSave();  
Run Code Online (Sandbox Code Playgroud)

然后将清理后的内容发送回我的数据库 - 当然,它适当地捕获代码炸弹等:)

var strData = tinymce.activeEditor.getContent() ;
Run Code Online (Sandbox Code Playgroud)

这一切都很好,除了在编辑器中,光标返回到内容的开头.

我想我可以通过在保存之前设置书签来返回正确的位置:

// set bookmark
var bm = tinymce.activeEditor.selection.getBookmark();

// save and cleanup, then ajax
tinyMCE.triggerSave(); 
...ajax stuff here

// Restore the selection bookmark
tinymce.activeEditor.selection.moveToBookmark(bm);
Run Code Online (Sandbox Code Playgroud)

但这似乎不起作用.

我还以为我可以在保存之前捕获当前节点和偏移量:

var nd = tinymce.activeEditor.selection.getNode();
var os = tinymce.activeEditor.selection.getRng().startOffset;
Run Code Online (Sandbox Code Playgroud)

并通过以下方式返回正确的点:

tinymce.activeEditor.setCursorLocation(nd, os);
Run Code Online (Sandbox Code Playgroud)

但这会产生错误,因为nd显然不是有效的节点对象.

这是因为我的'选择'实际上是一个零长度的字符串(它是一个光标点,而不是一个选择)?

我担心我对API 4的官方文档有一些困难,这不像API 3的文档那样"充实"(我猜因为它是最近的更新).

我可以在Stackoverflow(以及一般的谷歌搜索)上找到的大多数帮助和示例与API 3(它看起来是完全不同的野兽)有关.

任何人都可以告诉我,我想要的是可能的吗?

Tha*_*ama 6

嗯,你可以使用非html书签:

// set bookmark
var bm = tinymce.activeEditor.selection.getBookmark(2,true);

// save and cleanup, then ajax
tinyMCE.triggerSave(); 
...ajax stuff here

// Restore the selection bookmark
tinymce.activeEditor.selection.moveToBookmark(bm);
Run Code Online (Sandbox Code Playgroud)

  • 你是英雄!这很完美!但这突出了我对API文档的困难,它将getBookmark'type'参数解释为:`Number如果书签应该是简单的可选状态.默认是复杂的.没有列出哪些数字对应于哪些选项,以及可以使用哪些不同的选项.像我这样的新手很难! (3认同)