suneditor icon indicating copy to clipboard operation
suneditor copied to clipboard

Get current Caret position

Open gopal1996 opened this issue 4 years ago • 2 comments

Hi @JiHong88 ,

It just a question, I wrote a function to get the current caret position (top, left). I am trying to trigger a modal when the user hits {{.

I inserted the below block of code in the onChange event

var range = core.getSelection().getRangeAt(0);
let text;
if (range.collapsed) {
    text = range.startContainer.textContent.substring(0, range.startOffset);
}
if(text.slice(-2) === "{{") {
    const {left, top} = range.getBoundingClientRect();
    updatePosition(top, left)
}

is it the correct way to retrieve the caret position? Sometimes, I am getting the wrong position.

Thank you in Advance.

gopal1996 avatar May 02 '21 14:05 gopal1996

@gopal1996 Refer this code. https://github.com/JiHong88/SunEditor/blob/master/src/lib/core.js#L5722 This function is set position of the balloon toolbar.

JiHong88 avatar May 02 '21 15:05 JiHong88

it seems that the link no longer points to the proper text line - could you please provide a function name or s.th. similar?

rozek avatar Aug 14 '24 08:08 rozek