简体   繁体   English

使用textangular在可编辑内容的div中记录和恢复插入符号的位置

[英]Recording and restoring the position of the caret within a contenteditable div using textangular

I've seen a lot of code snippets detailing how to get and set the caret position within a contenteditable div but I cannot get it to work in my case: 我看过很多代码片段,详细介绍了如何在contenteditable div中获取和设置插入符号的位置,但是在我的情况下,我无法使其正常工作:

I am using a WYSIWYG text editor (textAngular). 我正在使用所见即所得的文本编辑器(textAngular)。 I have included functionality for the user to insert a cross-reference at the cursor position. 我提供了一些功能,供用户在光标位置插入交叉引用。 They click a button, which opens up a dialog box for them to make selections from, which in turn will insert a html node at the current cursor position. 他们单击一个按钮,这将打开一个对话框供他们进行选择,然后将在当前光标位置插入一个html节点。

The location of the cursor may be within child nodes within the div such as p tags and/or other formatting tags. 光标的位置可以在div内的子节点内,例如p标签和/或其他格式标签。

My issue is that the current cursor position is lost when the dialog gains focus. 我的问题是,当对话框获得焦点时,当前光标位置会丢失。 I have tried storing the caret position (node and offset) in memory before opening the dialog but whatever I try the caret position always returns to the start of the div. 在打开对话框之前,我曾尝试将插入符号的位置(节点和偏移量)存储在内存中,但是无论我如何尝试,插入符号的位置始终会返回到div的开头。 I've tried all the code snippets I can find on stackoverflow but none of them work. 我已经尝试了所有可以在stackoverflow上找到的代码段,但是它们都不起作用。 I assume I am missing something. 我想我缺少什么。

EDIT: On second thoughts, your issue is probably that when you click the button the content editable looses focus and hence the selection is lost. 编辑:再三考虑,您的问题可能是当您单击按钮时,可编辑的内容失去了焦点,因此选择丢失了。 We handle this as so in textAngular: toolElement.attr('unselectable', 'on'); 我们在textAngular中这样处理: toolElement.attr('unselectable', 'on');

Best solution for this is to use rangy instead of roll-your-own (required in textAngular ^1.3.0). 最好的解决方案是使用rangy而不是roll-your-own(在textAngular ^ 1.3.0中要求)。

You've probably been down voted due to this being very similar to SO questions about "save and restore" caret position in content-editables eg Saving and Restoring caret position for contentEditable div 您可能被否决了,因为这与关于内容可编辑的“保存和恢复”插入符位置的SO问题非常相似,例如,为contentEditable div保存和还原插入符位置

In the case of textAngular we've dealt with this issue multiple times and we have some shortcuts and helpers built into the TA (textAngular) toolbar tools. 对于textAngular,我们已多次处理此问题,并且在TA(textAngular)工具栏工具中内置了一些快捷方式和帮助程序。 Have a look at any of the issues here: https://github.com/fraywing/textAngular/search?q=modal&type=Issues 在这里看看任何问题: https : //github.com/fraywing/textAngular/search?q=modal&type=Issues

If you have TA specific questions it's often best to look through the 500+ issues on the github repository. 如果您有特定于TA的问题,通常最好浏览github存储库中的500多个问题。 Chances are something similar has been attempted before. 以前尝试过类似的机会。

From this answer: Saving and Restoring caret position for contentEditable div 从这个答案: 保存和还原content的插入标记位置

Modified to your use case. 修改为您的用例。 When the combo ctrl + i is pressed a popup appears. 按下ctrl + i组合键时,会出现一个弹出窗口。 You interact with the popup and press the close button, and then the cursor position is returned to it's original. 您与弹出窗口进行交互,然后按关闭按钮,然后光标位置将恢复为原始状态。

Without any other code this is what I had to go on. 没有任何其他代码,这就是我必须继续进行的工作。
I used jquery to make coding the example less verbose, but this also works without jquery. 我使用jquery来使示例编码变得不太冗长,但这在没有jquery的情况下也可以使用。

  function getRestorePosition(context) { var selection = window.getSelection(); var range = selection.getRangeAt(0); range.setStart( context, 0 ); var len = range.toString().length; return function restore(){ var pos = getTextNodeAtPosition(context, len); selection.removeAllRanges(); var range = new Range(); range.setStart(pos.node, pos.position); selection.addRange(range); } } function getTextNodeAtPosition(root, index){ var lastNode = null; var treeWalker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT,function next(elem) { if(index > elem.textContent.length){ index -= elem.textContent.length; lastNode = elem; return NodeFilter.FILTER_REJECT } return NodeFilter.FILTER_ACCEPT; }); var c = treeWalker.nextNode(); return { node: c ? c: root, position: c? index: 0 }; } $('[contenteditable="true"]').on('keydown',function(e) { if(e.ctrlKey && e.which == 73) { var popup = $('#popup'); var restore = getRestorePosition(e.target); popup.show(); popup.find('button').one('click',function() { popup.hide(); restore(); }); e.preventDefault(); return false; } }); 
 #popup { position:absolute; background-color:rgba(100,100,100,0.8); left:0px; right:0px; bottom:0px; top:0px; display:none; color:white; text-align:center; padding:0px;200px; font-size:3em; } [contenteditable="true"] { border:1px solid gray; padding:20px; font-size:2em; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div contenteditable="true"> How much wood could a woodchuck chuck if a woodchuck could chuck wood </div> <div id="popup"> A wood chuck could chuck as much wood as a woodchuck could chuck!<BR/> <button>close</button> </div> 

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

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