import { Head, Link, router } from '@inertiajs/react'; import AppLayout from '@/Layouts/AppLayout.jsx'; import React, { useEffect, useLayoutEffect, useRef, useState } from 'react'; import { BreadcrumbItem, BreadcrumbLink, BreadcrumbPage, BreadcrumbSeparator } from "@/Components/ui/breadcrumb.jsx"; import { Button } from "@/Components/ui/button.jsx"; import { ChevronFirst, ChevronLast, Rows3, Settings } from "lucide-react"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import PrimaryButton from "@/Components/PrimaryButton.jsx"; import { Switch } from "@/components/ui/switch"; import { Tooltip, TooltipProvider, TooltipTrigger } from "@radix-ui/react-tooltip"; import { TooltipContent } from "@/Components/ui/tooltip.jsx"; import { throttle } from "lodash"; export default function Read({ auth, comic, chapter }) { const [readingMode, setReadingMode] = useState('rtl'); // rtl, utd const [isTwoPagesPerScreen, setIsTwoPagePerScreen] = useState(false); const [currentImage, setCurrentImage] = useState(1); const windowSize = useWindowSize(); const ref = useRef(); const [divDimensions, setDivDimensions] = useState([0, 0]); const [loading, setLoading] = useState(true); function useWindowSize() { const [size, setSize] = useState([0, 0]); useLayoutEffect(() => { function updateSize() { setSize([window.innerWidth, window.innerHeight]); } window.addEventListener('resize', updateSize); updateSize(); return () => window.removeEventListener('resize', updateSize); }, []); return size; } const toggleReadingMode = (e) => { if (e) { setReadingMode('utd'); } else { setReadingMode('rtl'); } } const setViewPort = (e) => { //console.log(e.target.childNodes); //console.log(e.target.naturalHeight); } const ImageForComic = (img) => { const imgRef = useRef(); const resizeImage = () => { if (!imgRef.current || !ref.current) return; const { naturalWidth, naturalHeight } = imgRef.current; const containerWidth = ref.current.clientWidth; const containerHeight = ref.current.clientHeight; let width, height; if (readingMode === "rtl") { // Scale for RTL mode const ratioWidth = naturalWidth / containerWidth; const ratioHeight = naturalHeight / containerHeight; const maxRatio = Math.max(ratioWidth, ratioHeight); width = naturalWidth / maxRatio; height = naturalHeight / maxRatio; } else if (readingMode === "utd") { // Scale for UTD mode const ratio = divDimensions[1] < divDimensions[0] ? 0.33 : 1; // Example logic const scaledWidth = containerWidth * ratio; const scaledRatio = naturalWidth / scaledWidth; width = naturalWidth / scaledRatio; height = naturalHeight / scaledRatio; } // Apply dimensions directly imgRef.current.style.width = `${width}px`; imgRef.current.style.height = `${height}px`; }; useEffect(() => { resizeImage(); }, [readingMode, divDimensions]); // Recalculate when these dependencies change const handleImageClick = (e) => { if (readingMode === "utd") return; const bounds = imgRef.current.getBoundingClientRect(); const percentage = (e.pageX - bounds.left) / imgRef.current.offsetWidth; if (percentage < 0.45) { if (img.innerKey === 0 && chapter.chapter.prev) { router.visit(route('comics.read', [comic.comic.path_word, chapter.chapter.prev])); } else { document.getElementById(`image-${img.innerKey - 1}`)?.scrollIntoView(); } } else if (percentage > 0.55) { if (img.innerKey >= chapter.sorted.length - 1 && chapter.chapter.next) { router.visit(route('comics.read', [comic.comic.path_word, chapter.chapter.next])); } else { document.getElementById(`image-${img.innerKey + 1}`)?.scrollIntoView(); } } }; return (
Content Page
Previous Chapter
Next Chapter