fix: Keyboard.keyToString, keyFromString

This commit is contained in:
orion 2023-12-12 11:33:09 -06:00
parent e8dcb23aff
commit f299c7d467
Signed by: orion
GPG Key ID: 6D4165AE4C928719

View File

@ -1,11 +1,16 @@
module Puppeteer.Keyboard (module X, KeyMod(..), Key(..), prepareKey, up, down, doType, press) where module Puppeteer.Keyboard (module X, KeyMod(..), Key(..), prepareKey, keyModToString, keyModFromString, keyToString, keyFromString, up, down, doType, press) where
import Prelude import Prelude
import Control.Promise (Promise) import Control.Promise (Promise)
import Control.Promise as Promise import Control.Promise as Promise
import Data.Bounded.Generic (genericBottom, genericTop)
import Data.Enum (class BoundedEnum, class Enum, upFrom)
import Data.Enum.Generic (genericCardinality, genericFromEnum, genericPred, genericSucc, genericToEnum)
import Data.Eq.Generic (genericEq) import Data.Eq.Generic (genericEq)
import Data.Foldable (find)
import Data.Generic.Rep (class Generic) import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe)
import Data.Nullable (Nullable) import Data.Nullable (Nullable)
import Data.Nullable as Nullable import Data.Nullable as Nullable
import Data.Ord.Generic (genericCompare) import Data.Ord.Generic (genericCompare)
@ -55,11 +60,25 @@ data KeyMod
| KeyModControl | KeyModControl
| KeyModAlt | KeyModAlt
derive instance genericKeyMod :: Generic KeyMod _ derive instance Generic KeyMod _
instance ordKeyMod :: Ord KeyMod where
instance Enum KeyMod where
pred = genericPred
succ = genericSucc
instance Bounded KeyMod where
top = genericTop
bottom = genericBottom
instance BoundedEnum KeyMod where
cardinality = genericCardinality
toEnum = genericToEnum
fromEnum = genericFromEnum
instance Ord KeyMod where
compare = genericCompare compare = genericCompare
instance eqKeyMod :: Eq KeyMod where instance Eq KeyMod where
eq = genericEq eq = genericEq
data Key data Key
@ -164,128 +183,148 @@ data Key
| KeyNumpad9 | KeyNumpad9
| KeyNumpadDecimal | KeyNumpadDecimal
derive instance genericKey :: Generic Key _ derive instance Generic Key _
instance ordKey :: Ord Key where
instance Enum Key where
pred = genericPred
succ = genericSucc
instance Bounded Key where
top = genericTop
bottom = genericBottom
instance BoundedEnum Key where
cardinality = genericCardinality
toEnum = genericToEnum
fromEnum = genericFromEnum
instance Ord Key where
compare = genericCompare compare = genericCompare
instance eqKey :: Eq Key where instance Eq Key where
eq = genericEq eq = genericEq
prepareKey :: Key -> Foreign prepareKey :: Key -> Foreign
prepareKey = writeImpl <<< prepareKey_ prepareKey = writeImpl <<< keyToString
prepareKeyMod_ :: KeyMod -> String keyModFromString :: String -> Maybe KeyMod
prepareKeyMod_ KeyModMetaLeft = "MetaLeft" keyModFromString s = find (eq s <<< keyModToString) (upFrom bottom :: Array KeyMod)
prepareKeyMod_ KeyModMetaRight = "MetaRight"
prepareKeyMod_ KeyModMeta = "Meta"
prepareKeyMod_ KeyModShiftLeft = "ShiftLeft"
prepareKeyMod_ KeyModShiftRight = "ShiftRight"
prepareKeyMod_ KeyModControlLeft = "ControlLeft"
prepareKeyMod_ KeyModControlRight = "ControlRight"
prepareKeyMod_ KeyModAltLeft = "AltLeft"
prepareKeyMod_ KeyModAltRight = "AltRight"
prepareKeyMod_ KeyModShift = "Shift"
prepareKeyMod_ KeyModControl = "Control"
prepareKeyMod_ KeyModAlt = "Alt"
prepareKey_ :: Key -> String keyFromString :: String -> Maybe Key
prepareKey_ (KeyChar c) = String.fromCodePointArray [ CodePoint.codePointFromChar c ] keyFromString s = find (eq s <<< keyToString) (upFrom bottom :: Array Key)
prepareKey_ (KeyMod mod) = prepareKeyMod_ mod
prepareKey_ KeyPower = "Power" keyModToString :: KeyMod -> String
prepareKey_ KeyEject = "Eject" keyModToString KeyModMetaLeft = "MetaLeft"
prepareKey_ KeyAbort = "Abort" keyModToString KeyModMetaRight = "MetaRight"
prepareKey_ KeyHelp = "Help" keyModToString KeyModMeta = "Meta"
prepareKey_ KeyBackspace = "Backspace" keyModToString KeyModShiftLeft = "ShiftLeft"
prepareKey_ KeyTab = "Tab" keyModToString KeyModShiftRight = "ShiftRight"
prepareKey_ KeyEnter = "Enter" keyModToString KeyModControlLeft = "ControlLeft"
prepareKey_ KeyCarriageReturn = "CarriageReturn" keyModToString KeyModControlRight = "ControlRight"
prepareKey_ KeyNewline = "Newline" keyModToString KeyModAltLeft = "AltLeft"
prepareKey_ KeyPause = "Pause" keyModToString KeyModAltRight = "AltRight"
prepareKey_ KeyCapsLock = "CapsLock" keyModToString KeyModShift = "Shift"
prepareKey_ KeyEscape = "Escape" keyModToString KeyModControl = "Control"
prepareKey_ KeyConvert = "Convert" keyModToString KeyModAlt = "Alt"
prepareKey_ KeyNonConvert = "NonConvert"
prepareKey_ KeySpace = "Space" keyToString :: Key -> String
prepareKey_ KeyPageUp = "PageUp" keyToString (KeyChar c) = String.fromCodePointArray [ CodePoint.codePointFromChar c ]
prepareKey_ KeyPageDown = "PageDown" keyToString (KeyMod mod) = keyModToString mod
prepareKey_ KeyEnd = "End" keyToString KeyPower = "Power"
prepareKey_ KeyHome = "Home" keyToString KeyEject = "Eject"
prepareKey_ KeyArrowLeft = "ArrowLeft" keyToString KeyAbort = "Abort"
prepareKey_ KeyArrowUp = "ArrowUp" keyToString KeyHelp = "Help"
prepareKey_ KeyArrowRight = "ArrowRight" keyToString KeyBackspace = "Backspace"
prepareKey_ KeyArrowDown = "ArrowDown" keyToString KeyTab = "Tab"
prepareKey_ KeySelect = "Select" keyToString KeyEnter = "Enter"
prepareKey_ KeyOpen = "Open" keyToString KeyCarriageReturn = "CarriageReturn"
prepareKey_ KeyPrintScreen = "PrintScreen" keyToString KeyNewline = "Newline"
prepareKey_ KeyInsert = "Insert" keyToString KeyPause = "Pause"
prepareKey_ KeyDelete = "Delete" keyToString KeyCapsLock = "CapsLock"
prepareKey_ KeyContextMenu = "ContextMenu" keyToString KeyEscape = "Escape"
prepareKey_ KeyF1 = "F1" keyToString KeyConvert = "Convert"
prepareKey_ KeyF2 = "F2" keyToString KeyNonConvert = "NonConvert"
prepareKey_ KeyF3 = "F3" keyToString KeySpace = "Space"
prepareKey_ KeyF4 = "F4" keyToString KeyPageUp = "PageUp"
prepareKey_ KeyF5 = "F5" keyToString KeyPageDown = "PageDown"
prepareKey_ KeyF6 = "F6" keyToString KeyEnd = "End"
prepareKey_ KeyF7 = "F7" keyToString KeyHome = "Home"
prepareKey_ KeyF8 = "F8" keyToString KeyArrowLeft = "ArrowLeft"
prepareKey_ KeyF9 = "F9" keyToString KeyArrowUp = "ArrowUp"
prepareKey_ KeyF10 = "F10" keyToString KeyArrowRight = "ArrowRight"
prepareKey_ KeyF11 = "F11" keyToString KeyArrowDown = "ArrowDown"
prepareKey_ KeyF12 = "F12" keyToString KeySelect = "Select"
prepareKey_ KeyF13 = "F13" keyToString KeyOpen = "Open"
prepareKey_ KeyF14 = "F14" keyToString KeyPrintScreen = "PrintScreen"
prepareKey_ KeyF15 = "F15" keyToString KeyInsert = "Insert"
prepareKey_ KeyF16 = "F16" keyToString KeyDelete = "Delete"
prepareKey_ KeyF17 = "F17" keyToString KeyContextMenu = "ContextMenu"
prepareKey_ KeyF18 = "F18" keyToString KeyF1 = "F1"
prepareKey_ KeyF19 = "F19" keyToString KeyF2 = "F2"
prepareKey_ KeyF20 = "F20" keyToString KeyF3 = "F3"
prepareKey_ KeyF21 = "F21" keyToString KeyF4 = "F4"
prepareKey_ KeyF22 = "F22" keyToString KeyF5 = "F5"
prepareKey_ KeyF23 = "F23" keyToString KeyF6 = "F6"
prepareKey_ KeyF24 = "F24" keyToString KeyF7 = "F7"
prepareKey_ KeyNumLock = "NumLock" keyToString KeyF8 = "F8"
prepareKey_ KeyScrollLock = "ScrollLock" keyToString KeyF9 = "F9"
prepareKey_ KeyAudioVolumeMute = "AudioVolumeMute" keyToString KeyF10 = "F10"
prepareKey_ KeyAudioVolumeDown = "AudioVolumeDown" keyToString KeyF11 = "F11"
prepareKey_ KeyAudioVolumeUp = "AudioVolumeUp" keyToString KeyF12 = "F12"
prepareKey_ KeyMediaTrackNext = "MediaTrackNext" keyToString KeyF13 = "F13"
prepareKey_ KeyMediaTrackPrevious = "MediaTrackPrevious" keyToString KeyF14 = "F14"
prepareKey_ KeyMediaStop = "MediaStop" keyToString KeyF15 = "F15"
prepareKey_ KeyMediaPlayPause = "MediaPlayPause" keyToString KeyF16 = "F16"
prepareKey_ KeyAltGraph = "AltGraph" keyToString KeyF17 = "F17"
prepareKey_ KeyProps = "Props" keyToString KeyF18 = "F18"
prepareKey_ KeyCancel = "Cancel" keyToString KeyF19 = "F19"
prepareKey_ KeyClear = "Clear" keyToString KeyF20 = "F20"
prepareKey_ KeyAccept = "Accept" keyToString KeyF21 = "F21"
prepareKey_ KeyModeChange = "ModeChange" keyToString KeyF22 = "F22"
prepareKey_ KeyAttn = "Attn" keyToString KeyF23 = "F23"
prepareKey_ KeyCrSel = "CrSel" keyToString KeyF24 = "F24"
prepareKey_ KeyExSel = "ExSel" keyToString KeyNumLock = "NumLock"
prepareKey_ KeyEraseEof = "EraseEof" keyToString KeyScrollLock = "ScrollLock"
prepareKey_ KeyPlay = "Play" keyToString KeyAudioVolumeMute = "AudioVolumeMute"
prepareKey_ KeyZoomOut = "ZoomOut" keyToString KeyAudioVolumeDown = "AudioVolumeDown"
prepareKey_ KeySoftLeft = "SoftLeft" keyToString KeyAudioVolumeUp = "AudioVolumeUp"
prepareKey_ KeySoftRight = "SoftRight" keyToString KeyMediaTrackNext = "MediaTrackNext"
prepareKey_ KeyCamera = "Camera" keyToString KeyMediaTrackPrevious = "MediaTrackPrevious"
prepareKey_ KeyCall = "Call" keyToString KeyMediaStop = "MediaStop"
prepareKey_ KeyEndCall = "EndCall" keyToString KeyMediaPlayPause = "MediaPlayPause"
prepareKey_ KeyVolumeDown = "VolumeDown" keyToString KeyAltGraph = "AltGraph"
prepareKey_ KeyVolumeUp = "VolumeUp" keyToString KeyProps = "Props"
prepareKey_ KeyNumpadEnter = "NumpadEnter" keyToString KeyCancel = "Cancel"
prepareKey_ KeyNumpadMultiply = "NumpadMultiply" keyToString KeyClear = "Clear"
prepareKey_ KeyNumpadAdd = "NumpadAdd" keyToString KeyAccept = "Accept"
prepareKey_ KeyNumpadSubtract = "NumpadSubtract" keyToString KeyModeChange = "ModeChange"
prepareKey_ KeyNumpadDivide = "NumpadDivide" keyToString KeyAttn = "Attn"
prepareKey_ KeyNumpadEqual = "NumpadEqual" keyToString KeyCrSel = "CrSel"
prepareKey_ KeyNumpad0 = "Numpad0" keyToString KeyExSel = "ExSel"
prepareKey_ KeyNumpad1 = "Numpad1" keyToString KeyEraseEof = "EraseEof"
prepareKey_ KeyNumpad2 = "Numpad2" keyToString KeyPlay = "Play"
prepareKey_ KeyNumpad3 = "Numpad3" keyToString KeyZoomOut = "ZoomOut"
prepareKey_ KeyNumpad4 = "Numpad4" keyToString KeySoftLeft = "SoftLeft"
prepareKey_ KeyNumpad5 = "Numpad5" keyToString KeySoftRight = "SoftRight"
prepareKey_ KeyNumpad6 = "Numpad6" keyToString KeyCamera = "Camera"
prepareKey_ KeyNumpad7 = "Numpad7" keyToString KeyCall = "Call"
prepareKey_ KeyNumpad8 = "Numpad8" keyToString KeyEndCall = "EndCall"
prepareKey_ KeyNumpad9 = "Numpad9" keyToString KeyVolumeDown = "VolumeDown"
prepareKey_ KeyNumpadDecimal = "NumpadDecimal" keyToString KeyVolumeUp = "VolumeUp"
keyToString KeyNumpadEnter = "NumpadEnter"
keyToString KeyNumpadMultiply = "NumpadMultiply"
keyToString KeyNumpadAdd = "NumpadAdd"
keyToString KeyNumpadSubtract = "NumpadSubtract"
keyToString KeyNumpadDivide = "NumpadDivide"
keyToString KeyNumpadEqual = "NumpadEqual"
keyToString KeyNumpad0 = "Numpad0"
keyToString KeyNumpad1 = "Numpad1"
keyToString KeyNumpad2 = "Numpad2"
keyToString KeyNumpad3 = "Numpad3"
keyToString KeyNumpad4 = "Numpad4"
keyToString KeyNumpad5 = "Numpad5"
keyToString KeyNumpad6 = "Numpad6"
keyToString KeyNumpad7 = "Numpad7"
keyToString KeyNumpad8 = "Numpad8"
keyToString KeyNumpad9 = "Numpad9"
keyToString KeyNumpadDecimal = "NumpadDecimal"