|
40 | 40 | module KeyboardShortcut.KeyboardEvent exposing |
41 | 41 | ( KeyboardEvent |
42 | 42 | , KeyboardEventType(..) |
| 43 | + , attach |
43 | 44 | , decode |
44 | 45 | , decodeToMsg |
45 | 46 | , isHoldingModifier |
46 | 47 | , modifiersHeld |
47 | 48 | , on |
48 | | - , stopPropagationOn |
| 49 | + , preventDefault |
| 50 | + , preventDefaultWhen |
| 51 | + , stopPropagation |
| 52 | + , stopPropagationWhen |
49 | 53 | , subscribe |
50 | 54 | ) |
51 | 55 |
|
@@ -116,26 +120,60 @@ modifiersHeld { altKey, ctrlKey, metaKey, shiftKey } = |
116 | 120 | -- EVENTS |
117 | 121 |
|
118 | 122 |
|
119 | | -stopPropagationOn : KeyboardEventType -> (KeyboardEvent -> msg) -> Html.Attribute msg |
120 | | -stopPropagationOn keyboardEventType toMsg = |
121 | | - Html.Events.custom (keyboardEventTypeToEventString keyboardEventType) |
122 | | - (decodeToMsg toMsg |
123 | | - |> Decode.andThen |
124 | | - (\msg -> |
125 | | - Decode.succeed |
126 | | - { message = msg |
127 | | - , stopPropagation = True |
128 | | - , preventDefault = False |
129 | | - } |
130 | | - ) |
131 | | - ) |
132 | | - |
133 | | - |
134 | | -on : KeyboardEventType -> (KeyboardEvent -> msg) -> Html.Attribute msg |
135 | | -on eventType toMsg = |
136 | | - Html.Events.on |
137 | | - (keyboardEventTypeToEventString eventType) |
138 | | - (Decode.map toMsg decode) |
| 123 | +type alias KeyboardListener msg = |
| 124 | + { keyboardEventType : KeyboardEventType |
| 125 | + , toMsg : KeyboardEvent -> msg |
| 126 | + , stopPropagation : KeyboardEvent -> Bool |
| 127 | + , preventDefault : KeyboardEvent -> Bool |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | +on : KeyboardEventType -> (KeyboardEvent -> msg) -> KeyboardListener msg |
| 132 | +on keyboardEventType toMsg = |
| 133 | + { keyboardEventType = keyboardEventType |
| 134 | + , toMsg = toMsg |
| 135 | + , stopPropagation = always False |
| 136 | + , preventDefault = always False |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | +stopPropagation : KeyboardListener msg -> KeyboardListener msg |
| 141 | +stopPropagation listener = |
| 142 | + { listener | stopPropagation = always True } |
| 143 | + |
| 144 | + |
| 145 | +stopPropagationWhen : (KeyboardEvent -> Bool) -> KeyboardListener msg -> KeyboardListener msg |
| 146 | +stopPropagationWhen when listener = |
| 147 | + { listener | stopPropagation = when } |
| 148 | + |
| 149 | + |
| 150 | +preventDefault : KeyboardListener msg -> KeyboardListener msg |
| 151 | +preventDefault listener = |
| 152 | + { listener | preventDefault = always True } |
| 153 | + |
| 154 | + |
| 155 | +preventDefaultWhen : (KeyboardEvent -> Bool) -> KeyboardListener msg -> KeyboardListener msg |
| 156 | +preventDefaultWhen when listener = |
| 157 | + { listener | preventDefault = when } |
| 158 | + |
| 159 | + |
| 160 | +attach : KeyboardListener msg -> Html.Attribute msg |
| 161 | +attach listener = |
| 162 | + let |
| 163 | + toEventConfig event = |
| 164 | + { message = listener.toMsg event |
| 165 | + , stopPropagation = listener.stopPropagation event |
| 166 | + , preventDefault = listener.preventDefault event |
| 167 | + } |
| 168 | + in |
| 169 | + Html.Events.custom (keyboardEventTypeToEventString listener.keyboardEventType) |
| 170 | + (Decode.map toEventConfig decode) |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | +{--| Note that there's a limitation to Browser.Event in that it does not support |
| 175 | +stopPropagation and preventDefault: https://github.com/elm/browser/issues/77, |
| 176 | +https://github.com/elm/browser/issues/89 --} |
139 | 177 |
|
140 | 178 |
|
141 | 179 | subscribe : KeyboardEventType -> (KeyboardEvent -> msg) -> Sub msg |
|
0 commit comments