Files
fnx_web/svelte/src/filesystem/viewers/Image.svelte

124 lines
2.4 KiB
Svelte
Raw Normal View History

<script lang="ts">
import { swipe_nav } from "lib/SwipeNavigate";
import { fs_path_url } from "lib/FilesystemAPI.svelte";
import type { FSNavigator } from "filesystem/FSNavigator";
2025-10-14 00:03:48 +02:00
import { loading_finish, loading_start } from "lib/Loading";
2020-11-17 23:39:27 +01:00
2025-10-13 16:05:50 +02:00
let { nav }: {
nav: FSNavigator;
} = $props();
let container: HTMLDivElement = $state()
let zoom = $state(false)
let x = 0, y = 0
let dragging = false
2025-10-13 16:05:50 +02:00
let swipe_prev = $state(true)
let swipe_next = $state(true)
export const update = async () => {
2025-10-14 00:03:48 +02:00
loading_start()
// Figure out if there are previous or next files. If not then we disable
// swiping controls in that direction
const siblings = await nav.get_siblings()
for (let i = 0; i < siblings.length; i++) {
if (siblings[i].name === nav.base.name) {
swipe_prev = i > 0
swipe_next = i < siblings.length-1
break
}
}
}
2025-10-14 00:03:48 +02:00
const on_load = () => loading_finish()
const mousedown = (e: MouseEvent) => {
if (!dragging && e.which === 1 && zoom) {
x = e.pageX
y = e.pageY
dragging = true
e.preventDefault()
e.stopPropagation()
return false
}
}
const mousemove = (e: MouseEvent) => {
if (dragging) {
container.scrollLeft = container.scrollLeft - (e.pageX - x)
container.scrollTop = container.scrollTop - (e.pageY - y)
x = e.pageX
y = e.pageY
e.preventDefault()
e.stopPropagation()
return false
}
}
const mouseup = (e: MouseEvent) => {
if (dragging) {
dragging = false
e.preventDefault()
e.stopPropagation()
return false
}
}
</script>
2025-10-13 16:05:50 +02:00
<svelte:window onmousemove={mousemove} onmouseup={mouseup} />
<div
bind:this={container}
class="container"
class:zoom
use:swipe_nav={{
enabled: !zoom,
prev: swipe_prev,
next: swipe_next,
on_prev: () => nav.open_sibling(-1),
on_next: () => nav.open_sibling(1),
}}
>
2025-10-13 16:05:50 +02:00
<!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
<img
2025-10-13 16:05:50 +02:00
ondblclick={() => {zoom = !zoom}}
onmousedown={mousedown}
onload={on_load}
onerror={on_load}
class="image"
class:zoom
src={fs_path_url($nav.base.path)}
alt="no description available"
/>
</div>
<style>
.container {
display: flex;
justify-content: center;
height: 100%;
width: 100%;
overflow: hidden;
}
.container.zoom {
overflow: auto;
justify-content: unset;
}
.image {
position: relative;
display: block;
margin: auto;
max-width: 100%;
max-height: 100%;
cursor: pointer;
2024-02-16 14:50:34 +01:00
box-shadow: 1px 1px 4px 0px var(--shadow_color);
}
.image.zoom {
max-width: none;
max-height: none;
cursor: move;
}
</style>