]> git.gir.st - VimFx.git/blob - extension/lib/help.coffee
Style the "VimFx" part in the help dialog more nicely
[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 translate = require('./l10n')
21 utils = require('./utils')
22
23 CONTAINER_ID = 'VimFxHelpDialogContainer'
24 MAX_FONT_SIZE = 20
25
26 injectHelp = (rootWindow, vimfx) ->
27 removeHelp(rootWindow)
28
29 { document } = rootWindow
30
31 container = document.createElement('box')
32 container.id = CONTAINER_ID
33
34 wrapper = document.createElement('box')
35 container.appendChild(wrapper)
36
37 header = createHeader(document, vimfx)
38 wrapper.appendChild(header)
39
40 content = createContent(document, vimfx)
41 wrapper.appendChild(content)
42
43 rootWindow.gBrowser.mCurrentBrowser.parentNode.appendChild(container)
44
45 # The font size of menu items is used by default, which is usually quite
46 # small. Try to increase it without causing a scrollbar.
47 computedStyle = rootWindow.getComputedStyle(container)
48 fontSize = originalFontSize =
49 parseFloat(computedStyle.getPropertyValue('font-size'))
50 while wrapper.clientHeight < container.clientHeight and
51 fontSize <= MAX_FONT_SIZE
52 fontSize++
53 container.style.fontSize = "#{ fontSize }px"
54 container.style.fontSize = "#{ Math.max(fontSize - 1, originalFontSize) }px"
55
56 # Uncomment this line if you want to use `gulp help.html`!
57 # utils.writeToClipboard(container.outerHTML)
58
59 removeHelp = (rootWindow) ->
60 rootWindow.document.getElementById(CONTAINER_ID)?.remove()
61
62 createHeader = (document, vimfx) ->
63 $ = utils.createBox.bind(null, document)
64
65 header = $('header')
66
67 mainHeading = $('heading-main', header)
68 $('logo', mainHeading) # Content is added by CSS.
69 $('title', mainHeading, translate('help.title'))
70
71 closeButton = $('close-button', header, '×')
72 closeButton.onclick = removeHelp.bind(null, document.defaultView)
73
74 return header
75
76 createContent = (document, vimfx) ->
77 $ = utils.createBox.bind(null, document)
78
79 content = $('content')
80
81 for mode in vimfx.getGroupedCommands({enabledOnly: true})
82 modeHeading = $('heading-mode', null, mode.name)
83
84 for category, index in mode.categories
85 categoryContainer = $('category', content)
86
87 # Append the mode heading inside the first category container, rather than
88 # before it, for layout purposes.
89 if index == 0
90 categoryContainer.appendChild(modeHeading)
91 categoryContainer.classList.add('first')
92
93 $('heading-category', categoryContainer, category.name) if category.name
94
95 for { command, enabledSequences } in category.commands
96 commandContainer = $('command', categoryContainer)
97 for sequence in enabledSequences
98 $('key-sequence', commandContainer, sequence)
99 $('description', commandContainer, command.description())
100
101 return content
102
103 module.exports = {
104 injectHelp
105 removeHelp
106 }
Imprint / Impressum