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 injectHelp = (window, vimfx) ->
36 container = utils.createBox(document)
37 container.id = CONTAINER_ID
39 wrapper = utils.createBox(document, 'wrapper', container)
41 header = createHeader(window, vimfx)
42 wrapper.appendChild(header)
44 content = createContent(window, vimfx)
45 wrapper.appendChild(content)
47 searchInput = document.createElement('textbox')
48 utils.setAttributes(searchInput, {
50 placeholder: translate('help.search')
52 searchInput.oninput = -> search(content, searchInput.value.trimLeft())
53 searchInput.onkeydown = (event) -> searchInput.blur() if event.key == 'Enter'
54 container.appendChild(searchInput)
56 window.gBrowser.selectedBrowser.parentNode.appendChild(container)
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
65 container.style.fontSize = "#{fontSize}px"
66 container.style.fontSize = "#{Math.max(fontSize - 1, originalFontSize)}px"
68 # Uncomment this line if you want to use `gulp help.html`!
69 # utils.writeToClipboard(container.outerHTML)
71 removeHelp = (window) -> getHelp(window)?.remove()
73 getHelp = (window) -> window.document.getElementById(CONTAINER_ID)
75 getSearchInput = (window) -> getHelp(window)?.querySelector('.search-input')
77 createHeader = (window, vimfx) ->
78 $ = utils.createBox.bind(null, window.document)
82 mainHeading = $('heading-main', header)
83 $('logo', mainHeading) # Content is added by CSS.
84 $('title', mainHeading, translate('help.title'))
86 closeButton = $('close-button', header, '×')
87 closeButton.onclick = removeHelp.bind(null, window)
91 createContent = (window, vimfx) ->
92 $ = utils.createBox.bind(null, window.document)
94 content = $('content')
96 for mode in vimfx.getGroupedCommands({enabledOnly: true})
97 modeHeading = $('heading-mode search-item', null, mode.name)
99 for category, index in mode.categories
100 categoryContainer = $('category', content)
101 # `data-` attributes are currently unused by VimFx, but provide a great
102 # way to customize the help dialog with custom CSS.
103 utils.setAttributes(categoryContainer, {
104 'data-mode': mode._name
105 'data-category': category._name
108 # Append the mode heading inside the first category container, rather than
109 # before it, for layout purposes.
111 categoryContainer.appendChild(modeHeading)
112 categoryContainer.classList.add('first')
115 $('heading-category search-item', categoryContainer, category.name)
117 for {command, name, enabledSequences} in category.commands
118 commandContainer = $('command search-item', categoryContainer)
119 commandContainer.setAttribute('data-command', command.name)
120 commandContainer.onclick = goToCommandSetting.bind(
121 null, window, vimfx, command
123 for sequence in enabledSequences
124 keySequence = $('key-sequence', commandContainer)
125 [specialKeys, rest] =
126 splitSequence(sequence, Object.keys(vimfx.SPECIAL_KEYS))
127 $('key-sequence-special-keys', keySequence, specialKeys)
128 $('key-sequence-rest search-text', keySequence, rest)
129 $('description search-text', commandContainer, command.description)
133 splitSequence = (sequence, specialKeys) ->
134 specialKeyEnds = specialKeys.map((key) ->
135 pos = sequence.lastIndexOf(key)
136 return if pos == -1 then 0 else pos + key.length
138 splitPos = Math.max(specialKeyEnds...)
139 return [sequence[0...splitPos], sequence[splitPos..]]
141 goToCommandSetting = (window, vimfx, command) ->
142 vimfx.goToCommand = command
144 # Randomize URI to force a reload of the Add-ons Manager if it’s already open.
145 uri = "addons://detail/#{vimfx.id}/preferences?#{Math.random()}"
146 utils.nextTick(window, ->
147 window.BrowserOpenAddonsMgr(uri)
150 search = (content, term) ->
151 document = content.ownerDocument
152 ignoreCase = (term == term.toLowerCase())
153 regex = RegExp("(#{utils.regexEscape(term)})", if ignoreCase then 'i' else '')
156 for item in content.querySelectorAll('.search-item')
157 texts = item.querySelectorAll('.search-text')
158 texts = [item] if texts.length == 0
159 className = SEARCH_NON_MATCH_CLASS
162 {textContent} = element
163 # Clear the previous highlighting. This is possible to do for non-matches
164 # as well, but too slow.
165 if item.classList.contains(SEARCH_MATCH_CLASS)
166 element.textContent = textContent
168 continue if clear or not regex.test(textContent)
170 className = SEARCH_MATCH_CLASS
171 element.textContent = '' # Empty the element.
172 for part, index in textContent.split(regex)
173 # Even indices are surrounding text, odd ones are matches.
175 element.appendChild(document.createTextNode(part))
177 utils.createBox(document, SEARCH_HIGHLIGHT_CLASS, element, part)
179 item.classList.remove(SEARCH_MATCH_CLASS, SEARCH_NON_MATCH_CLASS)
180 item.classList.add(className) unless clear