Skip to content

Commit af092fe

Browse files
committed
add keyboard shortcut (Meta + E) to execute code
1 parent 4b7dc8b commit af092fe

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Playground.res

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,11 +1149,38 @@ module ControlPanel = {
11491149
| _ => false
11501150
}
11511151

1152+
let onKeyDown = event => {
1153+
switch (
1154+
event->ReactEvent.Keyboard.metaKey || event->ReactEvent.Keyboard.ctrlKey,
1155+
event->ReactEvent.Keyboard.key,
1156+
) {
1157+
| (true, "e") => dispatch(RunCode)
1158+
| _ => ()
1159+
}
1160+
}
1161+
1162+
React.useEffect(() => {
1163+
Webapi.Window.addEventListener("keydown", onKeyDown)
1164+
Some(() => Webapi.Window.removeEventListener("keydown", onKeyDown))
1165+
}, [])
1166+
1167+
let runButtonText = {
1168+
let userAgent = Webapi.Window.Navigator.userAgent
1169+
let run = "Run"
1170+
if userAgent->String.includes("iPhone") || userAgent->String.includes("Android") {
1171+
run
1172+
} else if userAgent->String.includes("Mac") {
1173+
`${run} (⌘ + E)`
1174+
} else {
1175+
`${run} (Ctrl + E)`
1176+
}
1177+
}
1178+
11521179
<div className="flex flex-row gap-x-2">
11531180
<ToggleButton checked=autoRun onChange={_ => dispatch(ToggleAutoRun)}>
11541181
{React.string("Auto-run")}
11551182
</ToggleButton>
1156-
<Button onClick={_ => dispatch(RunCode)}> {React.string("Run")} </Button>
1183+
<Button onClick={_ => dispatch(RunCode)}> {React.string(runButtonText)} </Button>
11571184
<Button onClick=onFormatClick> {React.string("Format")} </Button>
11581185
<ShareButton actionIndicatorKey />
11591186
</div>

src/bindings/Webapi.res

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ module Window = {
6262
module Location = {
6363
@scope(("window", "location")) @val external href: string = "href"
6464
}
65+
66+
module Navigator = {
67+
@scope(("window", "navigator")) @val external userAgent: string = "userAgent"
68+
}
6569
}
6670

6771
module Fetch = {

0 commit comments

Comments
 (0)