]> git.gir.st - VimFx.git/blob - extension/lib/help.coffee
Rename l10n.coffee into translate.coffee
[VimFx.git] / extension / lib / help.coffee
1 ###
2 # Copyright Simon Lydell 2015, 2016.
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('./translate')
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(window, vimfx)
42 wrapper.appendChild(header)
43
44 content = createContent(window, 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 searchInput.onkeydown = (event) -> searchInput.blur() if event.key == 'Enter'
54 container.appendChild(searchInput)
55
56 window.gBrowser.selectedBrowser.parentNode.appendChild(container)
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 += 1
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) -> getHelp(window)?.remove()
72
73 toggleHelp = (window, vimfx) ->
74 helpContainer = getHelp(window)
75 if helpContainer
76 helpContainer.remove()
77 else
78 injectHelp(window, vimfx)
79
80 getHelp = (window) -> window.document.getElementById(CONTAINER_ID)
81
82 getSearchInput = (window) -> getHelp(window)?.querySelector('.search-input')
83
84 createHeader = (window, vimfx) ->
85 $ = utils.createBox.bind(null, window.document)
86
87 header = $('header')
88
89 mainHeading = $('heading-main', header)
90 $('logo', mainHeading) # Content is added by CSS.
91 $('title', mainHeading, translate('help.title'))
92
93 closeButton = $('close-button', header, '×')
94 closeButton.onclick = -> removeHelp(window)
95
96 return header
97
98 createContent = (window, vimfx) ->
99 $ = utils.createBox.bind(null, window.document)
100
101 content = $('content')
102
103 for mode in vimfx.getGroupedCommands({enabledOnly: true})
104 modeHeading = $('heading-mode search-item', null, mode.name)
105
106 for category, index in mode.categories
107 categoryContainer = $('category', content)
108 utils.setAttributes(categoryContainer, {
109 'data-mode': mode._name
110 'data-category': category._name
111 })
112
113 # Append the mode heading inside the first category container, rather than
114 # before it, for layout purposes.
115 if index == 0
116 categoryContainer.appendChild(modeHeading)
117 categoryContainer.classList.add('first')
118
119 if category.name
120 $('heading-category search-item', categoryContainer, category.name)
121
122 for {command, name, enabledSequences} in category.commands
123 commandContainer = $('command search-item', categoryContainer)
124 commandContainer.setAttribute('data-command', name)
125 commandContainer.onclick = goToCommandSetting.bind(
126 null, window, vimfx, command
127 )
128 for sequence in enabledSequences
129 keySequence = $('key-sequence', commandContainer)
130 [specialKeys, rest] =
131 splitSequence(sequence, Object.keys(vimfx.SPECIAL_KEYS))
132 $('key-sequence-special-keys', keySequence, specialKeys)
133 $('key-sequence-rest search-text', keySequence, rest)
134 $('description search-text', commandContainer, command.description)
135
136 return content
137
138 splitSequence = (sequence, specialKeys) ->
139 specialKeyEnds = specialKeys.map((key) ->
140 pos = sequence.lastIndexOf(key)
141 return if pos == -1 then 0 else pos + key.length
142 )
143 splitPos = Math.max(specialKeyEnds...)
144 return [sequence[0...splitPos], sequence[splitPos..]]
145
146 goToCommandSetting = (window, vimfx, command) ->
147 vimfx.goToCommand = command
148 removeHelp(window)
149 # Randomize URI to force a reload of the Add-ons Manager if it’s already open.
150 uri = "addons://detail/#{vimfx.id}/preferences?#{Math.random()}"
151 utils.nextTick(window, ->
152 window.BrowserOpenAddonsMgr(uri)
153 )
154
155 search = (content, term) ->
156 document = content.ownerDocument
157 ignoreCase = (term == term.toLowerCase())
158 regex = RegExp("(#{utils.regexEscape(term)})", if ignoreCase then 'i' else '')
159 clear = (term == '')
160
161 for item in content.querySelectorAll('.search-item')
162 texts = item.querySelectorAll('.search-text')
163 texts = [item] if texts.length == 0
164 className = SEARCH_NON_MATCH_CLASS
165
166 for element in texts
167 {textContent} = element
168 # Clear the previous highlighting. This is possible to do for non-matches
169 # as well, but too slow.
170 if item.classList.contains(SEARCH_MATCH_CLASS)
171 element.textContent = textContent
172
173 continue if clear or not regex.test(textContent)
174
175 className = SEARCH_MATCH_CLASS
176 element.textContent = '' # Empty the element.
177 for part, index in textContent.split(regex)
178 # Even indices are surrounding text, odd ones are matches.
179 if index % 2 == 0
180 element.appendChild(document.createTextNode(part))
181 else
182 utils.createBox(document, SEARCH_HIGHLIGHT_CLASS, element, part)
183
184 item.classList.remove(SEARCH_MATCH_CLASS, SEARCH_NON_MATCH_CLASS)
185 item.classList.add(className) unless clear
186
187 return
188
189 module.exports = {
190 injectHelp
191 removeHelp
192 toggleHelp
193 getHelp
194 getSearchInput
195 }
Imprint / Impressum