最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How does the cursor work in Ace - Stack Overflow

programmeradmin4浏览0评论

I am pretty intrigued by how Ace works.

I am curious how the cursor is able to move between each character in the divs/spans either by click or by pressing the arrow keys.

I am also intrigued by how the highlight on text works when you select it(kind of turns greyish in the demo)

Would appreciate it if anybody can shed some light on these matters.

Thanks in advance.

I am pretty intrigued by how Ace works.

I am curious how the cursor is able to move between each character in the divs/spans either by click or by pressing the arrow keys.

I am also intrigued by how the highlight on text works when you select it(kind of turns greyish in the demo)

Would appreciate it if anybody can shed some light on these matters.

Thanks in advance.

Share Improve this question edited Jan 24, 2012 at 14:48 Charles 51.4k13 gold badges106 silver badges144 bronze badges asked Jun 13, 2011 at 22:39 JulioJulio 6,37811 gold badges54 silver badges65 bronze badges 1
  • I have customized ACE to work with a UTF-8 characters of Indian language Tamil - and this has multi-glyph symbols for letters, and exacerbates this cursor-point offset problem. – Arcturus Commented Oct 30, 2013 at 5:30
Add a ment  | 

2 Answers 2

Reset to default 15

In Ace I basically treat the DOM as a drawing API. Everything you see is "drawn" using absolutely positioned DIV and SPAN elements. Text is drawn using SPANs, lines (e.g. the cursor or selections) are DIVs, etc.

To position everything correctly I first mesure the height and width of a character. That's aslo the reason Ace only works with mono spaced fonts.

When you click inside of Ace I calculate the relative position of the mouse inside of the editor using getBoundingClientRect and then translate this into character positions using the measured character size. Navigating with the keyboard is similar.

When I inspect the demo page and move the cursor by hitting the arrow keys the style.left attribute of the cursor div moves by 7px in the appropiate direction. For up and down it's 15px, the exact size of the spans used to display the text.

For the selection they are using a div with the class name ace_selection which then gets placed with an absolute position.

I suggest inspecting the demo with chrome or firefox to get a closer look at it. You can see how they create new divs and modify the div's attributes with every action you take.

发布评论

评论列表(0)

  1. 暂无评论