]> git.gir.st - VimFx.git/blob - extension/packages/marker.coffee
While in the hints mode the extension will no longer consume all the keystrokes....
[VimFx.git] / extension / packages / marker.coffee
1 { interfaces: Ci } = Components
2 XPathResult = Ci.nsIDOMXPathResult
3
4 { getPref } = require 'prefs'
5
6 # All elements that have one or more of the following properties
7 # qualify for their own marker in hints mode
8 MARKABLE_ELEMENT_PROPERTIES = [
9 "@tabindex"
10 "@onclick"
11 "@onmousedown"
12 "@onmouseup"
13 "@oncommand"
14 "@role='link'"
15 "@role='button'"
16 "contains(@class, 'button')"
17 "@contenteditable='' or translate(@contenteditable, 'TRUE', 'true')='true'"
18 ]
19
20 # All the following elements qualify for their own marker in hints mode
21 MARKABLE_ELEMENTS = [
22 "a"
23 "area[@href]"
24 "textarea"
25 "button"
26 "select"
27 "input[not(@type='hidden' or @disabled or @readonly)]"
28 ]
29
30
31 # Marker class wraps the markable element and provides
32 # methods to manipulate the markers
33 class Marker
34 # Creates the marker DOM node
35 constructor: (@element) ->
36 document = @element.ownerDocument
37 window = document.defaultView
38 @markerElement = document.createElement 'div'
39 @markerElement.className = 'VimFxReset VimFxHintMarker'
40
41 # Shows the marker
42 show: -> @markerElement.className = 'VimFxReset VimFxHintMarker'
43
44 # Hides the marker
45 hide: -> @markerElement.className = 'VimFxReset VimFxHiddenHintMarker'
46
47 # Positions the marker on the page. The positioning is absulute
48 setPosition: (rect) ->
49 @markerElement.style.left = rect.left + 'px'
50 @markerElement.style.top = rect.top + 'px'
51
52 # Assigns hint string to the marker
53 setHint: (@hintChars) ->
54 # number of hint chars that have been matched so far
55 @enteredHintChars = ''
56
57 document = @element.ownerDocument
58
59 while @markerElement.hasChildNodes()
60 @markerElement.removeChild @markedElement.firstChild
61
62 fragment = document.createDocumentFragment()
63 for char in @hintChars
64 span = document.createElement 'span'
65 span.className = 'VimFxReset'
66 span.textContent = char.toUpperCase()
67
68 fragment.appendChild span
69
70 @markerElement.appendChild fragment
71
72 # Add another char to the `enteredHintString`,
73 # see if it still matches `hintString`, apply classes to
74 # the distinct hint characters and show/hide marker when
75 # the entered string partially (not) matches the hint string
76 matchHintChar: (char) ->
77 # Handle backspace key by removing a previously entered hint char
78 # and resetting its class
79 if char == 'backspace'
80 if @enteredHintChars.length > 0
81 @enteredHintChars = @enteredHintChars.slice(0, -1)
82 @markerElement.children[@enteredHintChars.length]?.className = 'VimFxReset'
83 # Otherwise append hint char and change hint class
84 else
85 @markerElement.children[@enteredHintChars.length]?.className = 'VimFxReset VimFxCharMatch'
86 @enteredHintChars += char
87
88 # If entered hint chars no longer partially match the hint chars
89 # then hide the marker. Othersie show it back
90 if @hintChars.search(@enteredHintChars) == 0 then @show() else @hide()
91
92 # Checks if the marker will be matched if the next character entered is `char`
93 willMatch: (char) ->
94 char == 'backspace' or @hintChars.search(@enteredHintChars + char) == 0
95
96 # Checks if enterd hint chars completely match the hint chars
97 isMatched: ->
98 return @hintChars == @enteredHintChars
99
100
101 # Selects all markable elements on the page, creates markers
102 # for each of them The markers are then positioned on the page
103 #
104 # The array of markers is returned
105 Marker.createMarkers = (document) ->
106 hintChars = getPref('hint_chars').toLowerCase()
107
108 elementsSet = getMarkableElements(document)
109 markers = [];
110 j = 0
111 for i in [0...elementsSet.snapshotLength] by 1
112 element = elementsSet.snapshotItem(i)
113 if rect = getElementRect element
114 hint = indexToHint(j++, hintChars)
115 marker = new Marker(element)
116 marker.setPosition rect
117 marker.setHint hint
118 markers.push(marker)
119
120 return markers
121
122 # Function generator that creates a function that
123 # returns hint string for supplied numeric index.
124 indexToHint = do ->
125 # Helper function that returns a permutation number `i`
126 # of some of the characters in the `chars` agrument
127 f = (i, chars) ->
128 return '' if i < 0
129
130 n = chars.length
131 l = Math.floor(i / n); k = i % n;
132
133 return f(l - 1, chars) + chars[k]
134
135 return (i, chars) ->
136 # split the characters into two groups:
137 #
138 # * left chars are used for the head
139 # * right chars are used to build the tail
140 left = chars[...chars.length / 3]
141 right = chars[chars.length / 3...]
142
143 n = Math.floor(i / left.length)
144 m = i % left.length
145 return f(n - 1, right) + left[m]
146
147
148 # Returns elements that qualify for hint markers in hints mode.
149 # Generates and memoizes an XPath query internally
150 getMarkableElements = do ->
151 # Some preparations done on startup
152 elements = Array.concat \
153 MARKABLE_ELEMENTS,
154 ["*[#{ MARKABLE_ELEMENT_PROPERTIES.join(" or ") }]"]
155
156 xpath = elements.reduce((m, rule) ->
157 m.concat(["//#{ rule }", "//xhtml:#{ rule }"])
158 , []).join(' | ')
159
160 namespaceResolver = (namespace) ->
161 if (namespace == "xhtml") then "http://www.w3.org/1999/xhtml" else null
162
163 # The actual function that will return the desired elements
164 return (document, resultType = XPathResult.ORDERED_NODE_SNAPSHOT_TYPE) ->
165 document.evaluate xpath, document.documentElement, namespaceResolver, resultType, null
166
167 # Checks if the given TextRectangle object qualifies
168 # for its own Marker with respect to the `window` object
169 isRectOk = (rect, window) ->
170 rect.width > 2 and rect.height > 2 and \
171 rect.top > -2 and rect.left > -2 and \
172 rect.top < window.innerHeight - 2 and \
173 rect.left < window.innerWidth - 2
174
175 # Will scan through `element.getClientRects()` and look for
176 # the first visible rectange. If there are no visible rectangles, then
177 # will look at the children of the markable node.
178 #
179 # The logic has been copied over from Vimiun
180 getElementRect = (element) ->
181 document = element.ownerDocument
182 window = document.defaultView
183 docElem = document.documentElement
184 body = document.body
185
186 clientTop = docElem.clientTop || body.clientTop || 0;
187 clientLeft = docElem.clientLeft || body.clientLeft || 0;
188 scrollTop = window.pageYOffset || docElem.scrollTop;
189 scrollLeft = window.pageXOffset || docElem.scrollLeft;
190
191 rects = [rect for rect in element.getClientRects()]
192 rects.push element.getBoundingClientRect()
193
194 for rect in rects
195 if isRectOk rect, window
196 return {
197 top: rect.top + scrollTop - clientTop
198 left: rect.left + scrollLeft - clientLeft
199 width: rect.width
200 height: rect.height
201 }
202
203 # If the element has 0 dimentions then check what's inside.
204 # Floated or absolutely positioned elements are of particular interest
205 for rect in rects
206 if rect.width == 0 or rect.height == 0
207 for childElement in element.children
208 computedStyle = window.getComputedStyle childElement, null
209 if computedStyle.getPropertyValue 'float' != 'none' or \
210 computedStyle.getPropertyValue 'position' == 'absolute'
211
212 childRect if childRect = getElementRect childElement
213
214 return undefined
215
216 exports.Marker = Marker
Imprint / Impressum