javascript - Move caret to the end of a word in a tinyMCE editor -
i'm trying build function tinymce editor wraps words span tag type, far have i'm using in init_instance_callback
option while initializing tinymce instance:
function(ed) { ed.on('keyup', function () { var editor = tinymce.get('content'), regex = new regexp('test|abc', 'gi'), oldcontent = editor.getcontent(), newcontent = oldcontent.replace(regex, '<span style="background-color: yellow;">$&</span>'); // save cursor position var marker = editor.selection.getbookmark(2); // set new content in editor editor.setcontent(newcontent); // move cursor editor.selection.movetobookmark(marker); }); }
here's fiddle working example: http://fiddle.tinymce.com/jjeaab
it works fine while you're typing enter 1 of matched words (in case "test" or "abc") caret moves start of word, believe because i'm adding characters span element bookmark isn't setting after word wrapped, there way fix this?
perhaps can insert new node instead of replacing content. manage positionning you.
you have remove matched string before (i let check how correctly).
here example :
ed.on('keyup', function () { var editor = tinymce.get('content'), regex = new regexp('test|abc', 'i'); var match = editor.getcontent().match(regex); if (match) { // here remove "match[1].length" last character // add new node, set cursor after editor.selection.setnode(editor.dom.create('span', {style : 'background-color: yellow;'}, match[1])); } });
you may have @ regexp prevent matching node created (you want match string typed user -> not surrounded span tag).
Comments
Post a Comment