对于我的项目,我很乐意为特定的textarea提供自动完成功能.类似于intellisense/omnicomplete的工作原理.然而,我必须找出绝对光标位置,以便我知道DIV应该出现在哪里.
事实证明:那是(几乎我希望)无法实现的.有没有人有一些巧妙的想法如何解决这个问题?
Version 2 of My Hacky Experiment
This new version works with any font, which can be adjusted on demand, and any textarea size.
After noticing that some of you are still trying to get this to work, I decided to try a new approach. My results are FAR better this time around - at least on google chrome on linux. I no longer have a windows PC available to me, so I can only test on chrome/firefox on Ubuntu. My results work 100% consistently on Chrome, and let's say somewhere around 70 - 80% on Firefox, but I don't imagine it would be incredibly difficult to find the inconsistencies.
This new version relies on a Canvas object. In my example, I actually show that very canvas - just so you can see it in action, but it could very easily be done with a hidden canvas object.
This is most certainly a hack, and I apologize ahead of time for my rather thrown together code. At the very least, in google chrome, it works consistently, no matter what font I set it to, or size of textarea. I used Sam Saffron's example to show cursor coordinates (a gray-background div). I also added a "Randomize" link, so you can see it work in different font/texarea sizes and styles, and watch the cursor position update on the fly. I recommend looking at the full page demo so you can better see the companion canvas play along.
I'll summarize how it works...
The underlying idea is that we're trying to redraw the textarea on a canvas, as closely as possible. Since the browser uses the same font engine for both and texarea, we can use canvas's font measurement functionality to figure out where things are. From there, we can use the canvas methods available to us to figure out our coordinates.
First and foremost, we adjust our canvas to match the dimensions of the textarea. This is entirely for visual purposes since the canvas size doesn't really make a difference in our outcome. Since Canvas doesn't actually provide a means of word wrap, I had to conjure (steal/borrow/munge together) a means of breaking up lines to as-best-as-possible match the textarea. This is where you'll likely find you need to do the most cross-browser tweaking.
After word wrap, everything else is basic math. We split the lines into an array to mimic the word wrap, and now we want to loop through those lines and go all the way down until the point where our current selection ends. In order to do that, we're just counting characters and once we surpass selection.end
, we know we have gone down far enough. Multiply the line count up until that point with the line-height and you have a y
coordinate.
The x
coordinate is very similar, except we're using context.measureText
. As long as we're printing out the right number of characters, that will give us the width of the line that's being drawn to Canvas, which happens to end after the last character written out, which is the character before the currentl selection.end
position.
When trying to debug this for other browsers, the thing to look for is where the lines don't break properly. You'll see in some places that the last word on a line in canvas may have wrapped over on the textarea or vice-versa. This has to do with how the browser handles word wraps. As long as you get the wrapping in the canvas to match the textarea, your cursor should be correct.
I'll paste the source below. You should be able to copy and paste it, but if you do, I ask that you download your own copy of jquery-fieldselection instead of hitting the one on my server.
I've also upped a new demo as well as a fiddle.
Good luck!
Tooltip 2 Randomize
Bug
There's one bug I do recall. If you put the cursor before the first letter on a line, it shows the "position" as the last letter on the previous line. This has to do with how selection.end work. I don't think it should be too difficult to look for that case and fix it accordingly.
Version 1
Leaving this here so you can see the progress without having to dig through the edit history.
It's not perfect and it's most Definitely a hack, but I got it to work pretty well on WinXP IE, FF, Safari, Chrome and Opera.
As far as I can tell there's no way to directly find out the x/y of a cursor on any browser. The IE method, mentioned by Adam Bellaire is interesting, but unfortunately not cross-browser. I figured the next best thing would be to use the characters as a grid.
Unfortunately there's no font metric information built into any of the browsers, which means a monospace font is the only font type that's going to have a consistent measurement. Also, there's no reliable means of figuring out a font-width from the font-height. At first I'd tried using a percentage of the height, which worked great. Then I changed the font-size and everything went to hell.
I tried one method to figure out character width, which was to create a temporary textarea and keep adding characters until the scrollHeight (or scrollWidth) changed. It seems plausable, but about halfway down that road, I realized I could just use the cols attribute on the textarea and figured there are enough hacks in this ordeal to add another one. This means you can't set the width of the textarea via css. You HAVE to use the cols for this to work.
The next problem I ran into is that, even when you set the font via css, the browsers report the font differently. When you don't set a font, mozilla uses monospace
by default, IE uses Courier New
, Opera "Courier New"
(with quotes), Safari, 'Lucida Grand'
(with single quotes). When you do set the font to monospace
, mozilla and ie take what you give them, Safari comes out as -webkit-monospace
and Opera stays with "Courier New"
.
所以现在我们初始化一些变量.确保在css中设置行高.Firefox报告正确的行高,但IE报告"正常",我没有打扰其他浏览器.我只是在我的CSS中设置行高,这解决了差异.我还没有使用ems而不是像素进行测试.字符高度只是字体大小.应该也可以在你的CSS中预先设置它.
另外,在我们开始放置角色之前还有一个预先设置 - 这真让我挠头.对于ie和mozilla,texarea字符是 Now we're going to create an array of first-character positions for every line. We loop through every char in the textarea. If it's a newline, we add a new position to our line array. If it's a space, we try to figure out if the current "word" will fit on the line we're on or if it's going to get pushed to the next line. Punctuation counts as a part of the "word". I haven't tested with tabs, but there's a line there for adding 4 chars for a tab char. Once we have an array of line positions, we loop through and try to find which line the cursor is on. We're using hte "End" of the selection as our cursor. x = (cursor position - first character position of cursor line)*character width y = ((cursor line + 1)*line height) - scroll position I'm using jquery 1.2.6, jquery-fieldselection, and jquery-dimensions The Demo: http://enobrev.info/cursor/ And the code: Here I Am!!