4
votes

I'm trying to create a couple of methods to let tinyMCE move the cursor to some SPANS in the text, and this works ok if all the spans are in the visible part of text, but for long documents, when a span isn't visible (have to scroll to view it), it move the caret, but the text is not scrolled:

This question shows how to move the caret, but it doesn't scroll. How can I force the editor to scroll to the caret position?.

2

2 Answers

6
votes

I've found the solution, just after setting the caret position, add some text, and the editor will scroll automatically to the new position:

ed.execCommand('mceInsertContent', false, "");
0
votes

@leonardorame 's answer works, but it scrolls the text into view only at the bottom of the page like so:

......

......

|

If you would rather have the caret at the top...

|

......

......

then you need a bit extra:

//save the current position
var bm = ed.selection.getBookmark(2, true);

//scroll to the end
ed.selection.select(ed.getBody(), true);
ed.selection.collapse(false);
ed.execCommand('mceInsertContent', false, ");

//scroll back up, so that the caret is now at the top
ed.selection.moveToBookmark(bm);
ed.selection.collapse(false);
ed.execCommand('mceInsertContent', false, "");