]> git.gir.st - VimFx.git/blob - extension/lib/help.coffee
Fix help dialog font sizing
[VimFx.git] / extension / lib / help.coffee
1 ###
2 # Copyright Simon Lydell 2015.
3 #
4 # This file is part of VimFx.
5 #
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
18 ###
19
20 # This file creates VimFx’s Keyboard Shortcuts help screen.
21
22 translate = require('./l10n')
23 utils = require('./utils')
24
25 CONTAINER_ID = 'VimFxHelpDialogContainer'
26 MAX_FONT_SIZE = 20
27 SEARCH_MATCH_CLASS = 'search-match'
28 SEARCH_NON_MATCH_CLASS = 'search-non-match'
29 SEARCH_HIGHLIGHT_CLASS = 'search-highlight'
30
31 injectHelp = (window, vimfx) ->
32 removeHelp(window)
33
34 {document} = window
35
36 container = utils.createBox(document)
37 container.id = CONTAINER_ID
38
39 wrapper = utils.createBox(document, 'wrapper', container)
40
41 header = createHeader(document, vimfx)
42 wrapper.appendChild(header)
43
44 content = createContent(document, vimfx)
45 wrapper.appendChild(content)
46
47 searchInput = document.createElement('textbox')
48 utils.setAttributes(searchInput, {
49 class: 'search-input'
50 placeholder: translate('help.search')
51 })
52 searchInput.oninput = -> search(content, searchInput.value.trimLeft())
53 container.appendChild(searchInput)
54
55 window.gBrowser.mCurrentBrowser.parentNode.appendChild(container)
56 searchInput.focus()
57
58 # The font size of menu items is used by default, which is usually quite
59 # small. Try to increase it without causing a scrollbar.
60 computedStyle = window.getComputedStyle(container)
61 fontSize = originalFontSize =
62 parseFloat(computedStyle.getPropertyValue('font-size'))
63 while wrapper.scrollTopMax == 0 and fontSize <= MAX_FONT_SIZE
64 fontSize++
65 container.style.fontSize = "#{fontSize}px"
66 container.style.fontSize = "#{Math.max(fontSize - 1, originalFontSize)}px"
67
68 # Uncomment this line if you want to use `gulp help.html`!
69 # utils.writeToClipboard(container.outerHTML)
70
71 removeHelp = (window) ->
72 window.document.getElementById(CONTAINER_ID)?.remove()
73
74 createHeader = (document, vimfx) ->
75 $ = utils.createBox.bind(null, document)
76
77 header = $('header')
78
79 mainHeading = $('heading-main', header)
80 $('logo', mainHeading) # Content is added by CSS.
81 $('title', mainHeading, translate('help.title'))
82
83 closeButton = $('close-button', header, '×')
84 closeButton.onclick = removeHelp.bind(null, document.ownerGlobal)
85
86 return header
87
88 createContent = (document, vimfx) ->
89 $ = utils.createBox.bind(null, document)
90
91 content = $('content')
92
93 for mode in vimfx.getGroupedCommands({enabledOnly: true})
94 modeHeading = $('heading-mode search-item', null, mode.name)
95
96 for category, index in mode.categories
97 categoryContainer = $('category', content)
98 # `data-` attributes are currently unused by VimFx, but provide a great
99 # way to customize the help dialog with custom CSS.
100 utils.setAttributes(categoryContainer, {
101 'data-mode': mode._name
102 'data-category': category._name
103 })
104
105 # Append the mode heading inside the first category container, rather than
106 # before it, for layout purposes.
107 if index == 0
108 categoryContainer.appendChild(modeHeading)
109 categoryContainer.classList.add('first')
110
111 if category.name
112 $('heading-category search-item', categoryContainer, category.name)
113
114 for {command, name, enabledSequences} in category.commands
115 commandContainer = $('command search-item', categoryContainer)
116 utils.setAttributes(commandContainer, {'data-command': command.name})
117 commandContainer.setAttribute('data-command', name)
118 for sequence in enabledSequences
119 keySequence = $('key-sequence', commandContainer)
120 [specialKeys, rest] = splitSequence(sequence, vimfx.SPECIAL_KEYS)
121 $('key-sequence-special-keys', keySequence, specialKeys)
122 $('key-sequence-rest search-text', keySequence, rest)
123 $('description search-text', commandContainer, command.description())
124
125 return content
126
127 splitSequence = (sequence, specialKeys) ->
128 specialKeyEnds = specialKeys.map((key) ->
129 pos = sequence.lastIndexOf(key)
130 return if pos == -1 then 0 else pos + key.length
131 )
132 splitPos = Math.max(specialKeyEnds...)
133 return [sequence[0...splitPos], sequence[splitPos..]]
134
135 search = (content, term) ->
136 document = content.ownerDocument
137 ignoreCase = (term == term.toLowerCase())
138 regex = RegExp("(#{utils.regexEscape(term)})", if ignoreCase then 'i' else '')
139 clear = (term == '')
140
141 for item in content.querySelectorAll('.search-item')
142 texts = item.querySelectorAll('.search-text')
143 texts = [item] if texts.length == 0
144 className = SEARCH_NON_MATCH_CLASS
145
146 for element in texts
147 {textContent} = element
148 # Clear the previous highlighting. This is possible to do for non-matches
149 # as well, but too slow.
150 if item.classList.contains(SEARCH_MATCH_CLASS)
151 element.textContent = textContent
152
153 continue if clear or not regex.test(textContent)
154
155 className = SEARCH_MATCH_CLASS
156 element.textContent = '' # Empty the element.
157 for part, index in textContent.split(regex)
158 # Even indices are surrounding text, odd ones are matches.
159 if index % 2 == 0
160 element.appendChild(document.createTextNode(part))
161 else
162 utils.createBox(document, SEARCH_HIGHLIGHT_CLASS, element, part)
163
164 item.classList.remove(SEARCH_MATCH_CLASS, SEARCH_NON_MATCH_CLASS)
165 item.classList.add(className) unless clear
166
167 return
168
169 module.exports = {
170 injectHelp
171 removeHelp
172 }
Imprint / Impressum