mirror of
https://github.com/atom/atom.git
synced 2026-01-14 17:38:03 -05:00
Replace keystrokes in tooltips
This commit is contained in:
@@ -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'
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user