Skip to content

Commit

Permalink
feat(image): use left/right arrow keys to switch between images (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lanfei authored Jan 10, 2025
1 parent ea3928a commit 06a0d80
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/pages/home/previews/image.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
import { Error, FullLoading, ImageWithError } from "~/components"
import { useT } from "~/hooks"
import { useRouter, useT } from "~/hooks"
import { objStore } from "~/store"
import { ObjType } from "~/types"
import { onCleanup, onMount } from "solid-js"

const Preview = () => {
const t = useT()
const { replace } = useRouter()
let images = objStore.objs.filter((obj) => obj.type === ObjType.IMAGE)
if (images.length === 0) {
images = [objStore.obj]
}
const onKeydown = (e: KeyboardEvent) => {
const index = images.findIndex((f) => f.name === objStore.obj.name)
if (e.key === "ArrowLeft" && index > 0) {
replace(images[index - 1].name)
} else if (e.key === "ArrowRight" && index < images.length - 1) {
replace(images[index + 1].name)
}
}
onMount(() => {
window.addEventListener("keydown", onKeydown)
})
onCleanup(() => {
window.removeEventListener("keydown", onKeydown)
})
return (
<ImageWithError
maxH="75vh"
Expand Down

0 comments on commit 06a0d80

Please sign in to comment.