deshiknaves / caret-pos

Locate the current position of the caret. A plain JavaScript version of Caret.js.
MIT License
117 stars 13 forks source link

Wrong position for scaled textarea #6

Closed nvlled closed 5 years ago

nvlled commented 5 years ago

The position is wrong, or understated, when the textarea the font size is scaled. I could just do a workaround myself if only I could find a way to know the if the font is scaled in the textarea.

deshiknaves commented 5 years ago

I'm not entirely sure exactly what you mean. Can you please provide an example?

ZiedHf commented 5 years ago

Hey, maybe I get his idea. I think I'm facing the same issue. Let me explain.

Let's start from this use case : image As you notice here I have an editor as a long editable iframe. Everything is fine for this snippet :

      const iframe = document.getElementsByTagName('iframe')[0];
      const body = iframe.contentDocument.body;
      const off = offset(body, { iframe: iframe });
      const frameOffset = getOffset(iframe);
      let offsetoff = cloneDeep(off);
      offsetoff.left += frameOffset.left;
      offsetoff.top += frameOffset.top;
      let iframeDim = cloneDeep(offsetoff);
      iframeDim.top += 25;
      iframeDim.left += 15;

I'm able here to get the right position I need inside iframeDim. However, the editor I have is scalable, so for long text it will display a scroll bar. And also I'm working on a page with height:100%. Every time I scale my editor and write long text I expect to get the caret position in regard to the window but I get the caret position in regard to the iframe itself. Is there any option I forgot to include or a tip about this ?

deshiknaves commented 5 years ago

I don’t think that is supported at the moment as in iFrame mode it will come in relation to the iFrame. You can do some calculation as to where that is in regards to the window from the position of the iFrame in the window.

I’ll have a look in more detail. Do you have a working example I can look at? Could you put something on codesandbox?

I’m away for a few days, but I’ll have a look when I’m back.

nvlled commented 5 years ago

I found a way to fix my problem: I add the css rule to the textarea to prevent font scaling:

textarea {
    text-size-adjust: 100%;
}

I don't know if this will help ZiedHf's issue.

ZiedHf commented 5 years ago

Hey Guys, Solved by using scrollTop attribute. Thank you @deshiknaves & @nvlled