2 # Copyright Simon Lydell 2015.
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'
28 injectHelp = (window, vimfx) ->
33 container = document.createElement('box')
34 container.id = CONTAINER_ID
36 wrapper = document.createElement('box')
37 container.appendChild(wrapper)
39 header = createHeader(document, vimfx)
40 wrapper.appendChild(header)
42 content = createContent(document, vimfx)
43 wrapper.appendChild(content)
45 window.gBrowser.mCurrentBrowser.parentNode.appendChild(container)
47 # The font size of menu items is used by default, which is usually quite
48 # small. Try to increase it without causing a scrollbar.
49 computedStyle = window.getComputedStyle(container)
50 fontSize = originalFontSize =
51 parseFloat(computedStyle.getPropertyValue('font-size'))
52 while wrapper.clientHeight < container.clientHeight and
53 fontSize <= MAX_FONT_SIZE
55 container.style.fontSize = "#{ fontSize }px"
56 container.style.fontSize = "#{ Math.max(fontSize - 1, originalFontSize) }px"
58 # Uncomment this line if you want to use `gulp help.html`!
59 # utils.writeToClipboard(container.outerHTML)
61 removeHelp = (window) ->
62 window.document.getElementById(CONTAINER_ID)?.remove()
64 createHeader = (document, vimfx) ->
65 $ = utils.createBox.bind(null, document)
69 mainHeading = $('heading-main', header)
70 $('logo', mainHeading) # Content is added by CSS.
71 $('title', mainHeading, translate('help.title'))
73 closeButton = $('close-button', header, '×')
74 closeButton.onclick = removeHelp.bind(null, document.defaultView)
78 createContent = (document, vimfx) ->
79 $ = utils.createBox.bind(null, document)
81 content = $('content')
83 for mode in vimfx.getGroupedCommands({enabledOnly: true})
84 modeHeading = $('heading-mode', null, mode.name)
86 for category, index in mode.categories
87 categoryContainer = $('category', content)
89 # Append the mode heading inside the first category container, rather than
90 # before it, for layout purposes.
92 categoryContainer.appendChild(modeHeading)
93 categoryContainer.classList.add('first')
95 $('heading-category', categoryContainer, category.name) if category.name
97 for { command, enabledSequences } in category.commands
98 commandContainer = $('command', categoryContainer)
99 for sequence in enabledSequences
100 keySequence = $('key-sequence', commandContainer)
101 [specialKeys, rest] = splitSequence(sequence, vimfx.SPECIAL_KEYS)
102 $('key-sequence-special-keys', keySequence, specialKeys)
103 $('key-sequence-rest', keySequence, rest)
104 $('description', commandContainer, command.description())
108 splitSequence = (sequence, specialKeys) ->
109 specialKeyEnds = specialKeys.map((key) ->
110 pos = sequence.lastIndexOf(key)
111 return if pos == -1 then 0 else pos + key.length
113 splitPos = Math.max(specialKeyEnds...)
114 return [sequence[0...splitPos], sequence[splitPos..]]