2 # Copyright Simon Lydell 2015, 2016.
4 # This file is part of VimFx.
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.
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.
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/>.
20 # This file creates VimFx’s Keyboard Shortcuts help screen.
22 translate = require('./l10n')
23 utils = require('./utils')
25 CONTAINER_ID = 'VimFxHelpDialogContainer'
27 SEARCH_MATCH_CLASS = 'search-match'
28 SEARCH_NON_MATCH_CLASS = 'search-non-match'
29 SEARCH_HIGHLIGHT_CLASS = 'search-highlight'
31 shutdownHandlerAdded = false
33 injectHelp = (window, vimfx) ->
38 container = utils.createBox(document)
39 container.id = CONTAINER_ID
41 wrapper = utils.createBox(document, 'wrapper', container)
43 header = createHeader(window, vimfx)
44 wrapper.appendChild(header)
46 content = createContent(window, vimfx)
47 wrapper.appendChild(content)
49 searchInput = document.createElement('textbox')
50 utils.setAttributes(searchInput, {
52 placeholder: translate('help.search')
54 searchInput.oninput = -> search(content, searchInput.value.trimLeft())
55 searchInput.onkeydown = (event) -> searchInput.blur() if event.key == 'Enter'
56 container.appendChild(searchInput)
58 window.gBrowser.selectedBrowser.parentNode.appendChild(container)
60 # The font size of menu items is used by default, which is usually quite
61 # small. Try to increase it without causing a scrollbar.
62 computedStyle = window.getComputedStyle(container)
63 fontSize = originalFontSize =
64 parseFloat(computedStyle.getPropertyValue('font-size'))
65 while wrapper.scrollTopMax == 0 and fontSize <= MAX_FONT_SIZE
67 container.style.fontSize = "#{fontSize}px"
68 container.style.fontSize = "#{Math.max(fontSize - 1, originalFontSize)}px"
70 # Uncomment this line if you want to use `gulp help.html`!
71 # utils.writeToClipboard(container.outerHTML)
73 unless shutdownHandlerAdded
74 module.onShutdown(removeHelp.bind(null, window))
75 shutdownHandlerAdded = true
77 removeHelp = (window) -> getHelp(window)?.remove()
79 getHelp = (window) -> window.document.getElementById(CONTAINER_ID)
81 getSearchInput = (window) -> getHelp(window)?.querySelector('.search-input')
83 createHeader = (window, vimfx) ->
84 $ = utils.createBox.bind(null, window.document)
88 mainHeading = $('heading-main', header)
89 $('logo', mainHeading) # Content is added by CSS.
90 $('title', mainHeading, translate('help.title'))
92 closeButton = $('close-button', header, '×')
93 closeButton.onclick = removeHelp.bind(null, window)
97 createContent = (window, vimfx) ->
98 $ = utils.createBox.bind(null, window.document)
100 content = $('content')
102 for mode in vimfx.getGroupedCommands({enabledOnly: true})
103 modeHeading = $('heading-mode search-item', null, mode.name)
105 for category, index in mode.categories
106 categoryContainer = $('category', content)
107 # `data-` attributes are currently unused by VimFx, but provide a great
108 # way to customize the help dialog with custom CSS.
109 utils.setAttributes(categoryContainer, {
110 'data-mode': mode._name
111 'data-category': category._name
114 # Append the mode heading inside the first category container, rather than
115 # before it, for layout purposes.
117 categoryContainer.appendChild(modeHeading)
118 categoryContainer.classList.add('first')
121 $('heading-category search-item', categoryContainer, category.name)
123 for {command, name, enabledSequences} in category.commands
124 commandContainer = $('command search-item', categoryContainer)
125 commandContainer.setAttribute('data-command', command.name)
126 commandContainer.onclick = goToCommandSetting.bind(
127 null, window, vimfx, command
129 for sequence in enabledSequences
130 keySequence = $('key-sequence', commandContainer)
131 [specialKeys, rest] =
132 splitSequence(sequence, Object.keys(vimfx.SPECIAL_KEYS))
133 $('key-sequence-special-keys', keySequence, specialKeys)
134 $('key-sequence-rest search-text', keySequence, rest)
135 $('description search-text', commandContainer, command.description)
139 splitSequence = (sequence, specialKeys) ->
140 specialKeyEnds = specialKeys.map((key) ->
141 pos = sequence.lastIndexOf(key)
142 return if pos == -1 then 0 else pos + key.length
144 splitPos = Math.max(specialKeyEnds...)
145 return [sequence[0...splitPos], sequence[splitPos..]]
147 goToCommandSetting = (window, vimfx, command) ->
148 vimfx.goToCommand = command
150 # Randomize URI to force a reload of the Add-ons Manager if it’s already open.
151 uri = "addons://detail/#{vimfx.id}/preferences?#{Math.random()}"
152 utils.nextTick(window, ->
153 window.BrowserOpenAddonsMgr(uri)
156 search = (content, term) ->
157 document = content.ownerDocument
158 ignoreCase = (term == term.toLowerCase())
159 regex = RegExp("(#{utils.regexEscape(term)})", if ignoreCase then 'i' else '')
162 for item in content.querySelectorAll('.search-item')
163 texts = item.querySelectorAll('.search-text')
164 texts = [item] if texts.length == 0
165 className = SEARCH_NON_MATCH_CLASS
168 {textContent} = element
169 # Clear the previous highlighting. This is possible to do for non-matches
170 # as well, but too slow.
171 if item.classList.contains(SEARCH_MATCH_CLASS)
172 element.textContent = textContent
174 continue if clear or not regex.test(textContent)
176 className = SEARCH_MATCH_CLASS
177 element.textContent = '' # Empty the element.
178 for part, index in textContent.split(regex)
179 # Even indices are surrounding text, odd ones are matches.
181 element.appendChild(document.createTextNode(part))
183 utils.createBox(document, SEARCH_HIGHLIGHT_CLASS, element, part)
185 item.classList.remove(SEARCH_MATCH_CLASS, SEARCH_NON_MATCH_CLASS)
186 item.classList.add(className) unless clear