简体   繁体   English

在'html.set'或'snapshot.restore'之后保留Froala编辑器中的插入位置

[英]Retain caret position in Froala editor after 'html.set' or 'snapshot.restore'

I am trying to incorporate the Froala editor in a React component, and am having difficulties retaining the caret position after updating the content in the editor with the 'html.set' or 'snapshot.restore' methods. 我试图将Froala编辑器合并到React组件中,在使用'html.set'或'snapshot.restore'方法更新编辑器中的内容后,我很难保留插入位置。

The goal is to maintain Froala's editor content in React's state (using a Flux store): 目标是在React的状态下维护Froala的编辑器内容(使用Flux商店):

Grabbing the HTML (and subsequently storing it in a Flux store) is not an issue with: 抓取HTML(并随后将其存储在Flux存储中)不是问题:

var content = $('#editor').froalaEditor('html.get', true);

However, when trying to flush the state back to Froala with: 但是,当尝试将状态刷回到Froala时:

$('#editor').froalaEditor('html.set', this.state.content);

the new HTML is updated without issue, but the caret stops blinking. 新的HTML更新没有问题,但插入符号停止闪烁。 The editor still has focus, but any new text entered is inserted outside of any <p> </p> tag content. 编辑器仍然具有焦点,但输入的任何新文本都插入到任何<p> </p>标记内容之外。 This effectively makes the caret move up from the last position. 这有效地使得插入符号从最后位置向上移动。

Alternatively, using the following to take and restore a snapshot: 或者,使用以下内容获取和还原快照:

var snap = $('#editor').froalaEditor('snapshot.get'); $('#editor').froalaEditor('snapshot.restore', snap);

moves the caret to the top left position in the editor. 将插入符号移动到编辑器中的左上角位置。

Is there a way to get the caret position and re-focus on that position, after the HTML is set (or snapshop restored)? 在设置HTML(或snapshop还原)之后,有没有办法获得插入位置并重新关注该位置?

This is using Froala 2.0.0-rc2, but the same behaviour exists in the latest 1.x release. 这是使用Froala 2.0.0-rc2,但最新的1.x版本中存在相同的行为。

Restoring the snapshot should work. 恢复快照应该有效。 If you can make a jsFiddle with it not working, you could report it on the Github repo . 如果你可以使jsFiddle无法正常工作,你可以在Github repo上报告它。

Another way of doing it would be to call selection.save before getting the HTML so that you get the HTML with markers in place. 另一种方法是在获取HTML之前调用selection.save,以便获得带有标记的HTML。 Then if you have the HTML with selection markers, the cursor will be placed at the position where it was before. 然后,如果你有带选择标记的HTML,光标将被放置在之前的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM