-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyBindingShell.coffee
111 lines (74 loc) · 2.9 KB
/
keyBindingShell.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# @ts-check
import $filter from './filter'
import $forEach from './forEach'
import $formatHotkey from './formatHotkey'
import $noop from './noop'
import $push from './push'
import $replace from './replace'
import $split from './split'
class KeyBindingShell
constructor: ->
###* @type import('../type/keyBindingShell').KeyBindingShell['mapBound'] ###
@mapBound = {}
###* @type import('../type/keyBindingShell').KeyBindingShell['mapCallback'] ###
@mapCallback = {}
###* @type import('../type/keyBindingShell').KeyBindingShell['mapPrevented'] ###
@mapPrevented = {}
###* @type import('../type/keyBindingShell').KeyBindingShell['add'] ###
add: (keyMixed, callback) ->
[$key, $name] = $split ($replace keyMixed, ':down', ''), '.'
@register $key
# Item: [Name, Fn]
$push @mapCallback[$key], [$name, callback]
return
###* @type import('../type/keyBindingShell').KeyBindingShell['fire'] ###
fire: (keyMixed) -> $forEach (@getListItem keyMixed), (it) -> it[1]()
###* @type import('../type/keyBindingShell').KeyBindingShell['formatKey'] ###
formatKey: (key, prefix = '') ->
$key = $formatHotkey $replace key, ':down', ''
unless prefix then return $key
return "#{prefix}#{$key}"
###* @type import('../type/keyBindingShell').KeyBindingShell['getListItem'] ###
getListItem: (keyMixed) ->
[$key, $name] = $split ($replace keyMixed, ':down', ''), '.'
$list = @mapCallback[$key]
if $name then $list = $filter $list, (it) -> it[0] == $name
return $list
###* @type import('../type/keyBindingShell').KeyBindingShell['isPrevented'] ###
isPrevented: (key) -> @mapPrevented[key] == true
###* @type import('../type/keyBindingShell').KeyBindingShell['prepare'] ###
prepare: (key) ->
if @mapCallback[key] then return
@mapBound[key] = => @fire key
@mapCallback[key] = []
@mapPrevented[key] = false
return
###* @type import('../type/keyBindingShell').KeyBindingShell['prevent'] ###
prevent: (key, isPrevented) ->
@prepare key
@mapPrevented[key] = isPrevented
$callback = @mapBound[key]
$key = @formatKey key, '~'
if isPrevented then $key = @formatKey key
$noop $callback, $key
Native 'Hotkey, % $key, % $callback, On'
return
###* @type import('../type/keyBindingShell').KeyBindingShell['register'] ###
register: (key) ->
@prepare key
$callback = @mapBound[key]
$key = @formatKey key, '~'
if @mapPrevented[key] then $key = @formatKey key
$noop $callback, $key
Native 'Hotkey, % $key, % $callback, On'
return
###* @type import('../type/keyBindingShell').KeyBindingShell['remove'] ###
remove: (keyMixed) ->
[$key, $name] = $split ($replace keyMixed, ':down', ''), '.'
unless $name
@mapCallback[$key] = []
return
$listNew = $filter @mapCallback[$key], ($item) -> $item[0] != $name
@mapCallback[$key] = $listNew
return
$noop KeyBindingShell