Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/Components/ZoomableImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,12 @@ const ZoomableImage: Component<{
}
}

function ifEnabled<T>(func: (event: T) => void): (event: T) => void {
function ifEnabled<T extends Event>(
func: (event: T) => void,
): (event: T) => void {
return (event) => {
if (enabled) {
event.stopPropagation();
func(event);
}
};
Expand Down Expand Up @@ -390,13 +393,17 @@ const ZoomableImage: Component<{
};
}

function onClick() {
if (movedDuringClick) {
return;
function handleKeyDown(event: KeyboardEvent) {
if (event.key === "Escape") {
clickDeactivate();
}
}

function onClick() {
if (enabled) {
clickDeactivate();
if (!movedDuringClick) {
clickDeactivate();
}
} else {
clickActivate();
}
Expand All @@ -409,6 +416,7 @@ const ZoomableImage: Component<{
updateNeutralImageDimensions();

window.addEventListener("resize", updateNeutralImageDimensions);
window.addEventListener("keydown", ifEnabled(handleKeyDown));

// When image is in disabled state container is exactly the size of
// image and catches propagated events. When enabled container
Expand Down Expand Up @@ -438,6 +446,7 @@ const ZoomableImage: Component<{

onCleanup(() => {
window.removeEventListener("resize", updateNeutralImageDimensions);
window.removeEventListener("keydown", handleKeyDown);
});

return (
Expand Down
14 changes: 12 additions & 2 deletions src/Pages/PhotoDetailed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
createEffect,
on,
} from "solid-js";
import { useLocation, useParams } from "@solidjs/router";
import { useLocation, useNavigate, useParams } from "@solidjs/router";

import style from "./PhotoDetailed.module.css";
import getDB from "../Data/Database";
Expand All @@ -30,6 +30,7 @@ import Card from "../Components/Card";
import classList from "../Util/Classes";
import Tag from "../Components/Tag";
import Arrays from "../Util/Arrays";
import swipe from "../Util/Swipe";

const Toolbar: Component<{
selfId: string;
Expand Down Expand Up @@ -108,10 +109,12 @@ const Tags: Component<{ class: string; tags?: string[] }> = (props) => {

const PhotoDetailed: Component = () => {
const params = useParams();
const location = useLocation();
const navigate = useNavigate();
const searchParams: {
search?: string;
tags?: string | string[];
} = useLocation().query;
} = location.query;

const [info, { refetch }] = createResource(async () => {
const db = await getDB();
Expand Down Expand Up @@ -170,6 +173,13 @@ const PhotoDetailed: Component = () => {
),
);

swipe((dir) => {
if (dir === "right" && info()?.prevURL !== undefined)
navigate(`/photo/${info()!.prevURL}${location.search}`);
else if (dir === "left" && info()?.nextURL !== undefined)
navigate(`/photo/${info()!.nextURL}${location.search}`);
});

return (
<div class={style.article}>
<Toolbar
Expand Down
59 changes: 59 additions & 0 deletions src/Util/Swipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { onCleanup } from "solid-js";

type SwipeDirection = "left" | "right";

type SwipeHandler = (direction: SwipeDirection) => void;

// number: startX of active single-finger gesture
// "scrolled": gesture was invalidated by a scroll
// undefined: no active gesture
type SwipeState = number | "scrolled" | undefined;

const THRESHOLD = 50;

function swipe(onSwipe: SwipeHandler) {
let state: SwipeState;

function onScroll() {
state = "scrolled";
}

function onTouchStart(e: TouchEvent) {
if (e.touches.length !== 1) {
state = undefined;
return;
}
state = e.touches[0].clientX;
// detect if scroll happened during swipe
window.addEventListener("scroll", onScroll, { once: true });
}

function onTouchEnd(e: TouchEvent) {
window.removeEventListener("scroll", onScroll);

if (typeof state !== "number") {
return;
}

const deltaX = e.changedTouches[0].clientX - state;
state = undefined;

if (deltaX > THRESHOLD) {
onSwipe("right");
} else if (deltaX < -THRESHOLD) {
onSwipe("left");
}
}

document.addEventListener("touchstart", onTouchStart);
document.addEventListener("touchend", onTouchEnd);

onCleanup(() => {
console.log("onCleanup");
document.removeEventListener("touchstart", onTouchStart);
document.removeEventListener("touchend", onTouchEnd);
window.removeEventListener("scroll", onScroll);
});
}

export default swipe;
Loading