javascript - CKEditor Get Preceding Text on the same line -
i use instance of ckeditor , able insert text within ckeditor @ point of cursor. want able grab text left of cursor start of line.
i've tried finding x,y coordinates of cursor , didn't help. i'm confident of ckeditor's tools, can done, have not been able figure out tools use make work.
var theeditor = ckeditor.instances.editorfull; sel = theeditor.getselection(); obj = sel.getstartelement().$; range = theeditor.getselection().getranges(); container = range[0].startcontainer.$; textlen = typeof obj.textcontent === "undefined" ? obj.innertext.length : obj.textcontent.length; offset = range[0].startoffset; if(container.nodetype === 3) { while (container.previoussibling) { container = container.previoussibling; if(container.length) { offset += container.length; }else{ offset += container.textcontent ? container.textcontent.length : container.innertext.length; } } } var pct = textlen > 0 ? offset / textlen : 0; cursor = math.floor(obj.offsetheight * pct); while (obj.offsetparent) { cursor += obj.offsettop; obj = obj.offsetparent; } cursor += obj.offsettop; alert(cursor);
this attempt figure out cursor located , works, i'm not sure information i'm getting in alert it's not useable me.
what want hypothetical. in below text uses | cursor , found inside cdeditor instance:
lorem ipsum dolor sit amet, consectetur adipiscing elit. nullam ex nunc, tincidunt id |arcu in, semper volutpat nulla. nunc efficitur egestas magna vitae consequat.
notice | in "id |arcu". want able grab text "tincidunt id " based upon cursor position. not want grab end of line above text. basically, stop @ word wrap.
any ideas? thanks.
allen
it's quite tricky term "line" used. if geometry of editor changes (resize) , text wraps @ different point? if selection in table cell, line equals table row then? if <p>
split <br>
, there 2 "virtual paragraphs"?
there's no such thing line in html. there elements, inline (like <span>
or <strong>
) , blocks (like <p>
or <table>
). html content beginning of closest ancestor block position of caret, need is:
var e = ckeditor.instances.editor1; var r = e.getselection().getranges()[ 0 ]; r.collapse( 1 ); r.setstartat( ( r.startpath().block || r.startpath().blocklimit ).getfirst(), ckeditor.position_after_start ); var docfr = r.clonecontents(); console.log( docfr.gethtml() ); console.log( docfr.$.textcontent );
Comments
Post a Comment