Replace keystrokes in tooltips

This commit is contained in:
Ben Ogle
2013-11-21 16:53:38 -08:00
parent 01c141eec6
commit 18399aa264
2 changed files with 49 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
{View, $$} = require 'atom'
{View, $, $$} = require 'atom'
describe "SpacePen extensions", ->
class TestView extends View
@@ -51,3 +51,17 @@ describe "SpacePen extensions", ->
it "subscribes to the given event emitter and unsubscribes when unsubscribe is called", ->
emitter.trigger "foo"
expect(eventHandler).toHaveBeenCalled()
describe "tooltips", ->
describe "replaceModifiers", ->
replaceModifiers = $.fn.setTooltip.replaceModifiers
it "replaces single keystroke", ->
expect(replaceModifiers('cmd-O')).toEqual '⌘⇧O'
expect(replaceModifiers('cmd-shift-up')).toEqual '⌘⇧↑'
expect(replaceModifiers('cmd-option-down')).toEqual '⌘⌥↓'
expect(replaceModifiers('cmd-option-left')).toEqual '⌘⌥←'
expect(replaceModifiers('cmd-option-right')).toEqual '⌘⌥→'
it "replaces multiple keystroke", ->
expect(replaceModifiers('cmd-o ctrl-2')).toEqual '⌘O ⌃2'

View File

@@ -22,9 +22,39 @@ tooltipDefaults =
container: 'body'
html: true
modifiers =
cmd: ''
option: ''
ctrl: ''
shift: ''
left: ''
right: ''
up: ''
down: ''
replaceKey = (key) ->
if modifiers[key]
modifiers[key]
else if key.length == 1 and key == key.toUpperCase() and key.toUpperCase() != key.toLowerCase()
[modifiers.shift, key.toUpperCase()]
else if key.length == 1
key.toUpperCase()
else
key
replaceModifiersInSingleKeystroke = (keystroke) ->
keys = keystroke.split('-')
keys = _.flatten(replaceKey(key) for key in keys)
keys.join('')
replaceModifiers = (keystroke) ->
keystrokes = keystroke.split(' ')
keystrokes = (replaceModifiersInSingleKeystroke(stroke) for stroke in keystrokes)
keystrokes.join(' ')
getKeystroke = (bindings) ->
if bindings?.length
"<span class=\"keystroke\">#{bindings[0].keystroke}</span>"
"<span class=\"keystroke\">#{replaceModifiers(bindings[0].keystroke)}</span>"
else
''
@@ -38,4 +68,7 @@ jQuery.fn.setTooltip = (title, {command, commandElement}={}) ->
this.tooltip(jQuery.extend(tooltipDefaults, {title: "#{title} #{getKeystroke(bindings)}"}))
jQuery.fn.setTooltip.getKeystroke = getKeystroke
jQuery.fn.setTooltip.replaceModifiers = replaceModifiers
module.exports = spacePen