Voog / wysihtml

Open source rich text editor for the modern web
http://wysihtml.com
MIT License
3.36k stars 337 forks source link

Get current caret position #234

Open alessandrofrancesconi opened 9 years ago

alessandrofrancesconi commented 9 years ago

Hello, I see that the editor uses Rangy to work with selection and so on. Assuming this... is it possible to have a public method that returns the current (relative or absolute) position of the caret inside the editable area?

Something like editor.getCaretPosition() => { left; top }

ChristofferJoergensen commented 9 years ago

+1, I would really like to know this as well. My current implementation only gets me this far:

I can identify my currently focussed textbox with Window.selection:

var selection = window.getSelection();

I can identify the caret position with Selection.focusOffset:

var focus = selection.focusOffset;

However, this counter only counts characters on the current line. So if I'm 2 characters in on line 3 focus will be 2. This dosn't tell me anything about how many characters I am into the entire text.

alessandrofrancesconi commented 9 years ago

Just a clarification: I'm talking about position based on pixels, not characters/lines.