diff --git a/web/src/components/WikipediaBrowser.css b/web/src/components/WikipediaBrowser.css index 8ada466..baca317 100644 --- a/web/src/components/WikipediaBrowser.css +++ b/web/src/components/WikipediaBrowser.css @@ -66,3 +66,9 @@ border-radius: 3px; text-decoration: none !important; } + +.wikipedia-content img { + max-width: 100%; + height: auto; +} + diff --git a/web/src/components/WikipediaBrowser.tsx b/web/src/components/WikipediaBrowser.tsx index 0bacad1..c61f57a 100644 --- a/web/src/components/WikipediaBrowser.tsx +++ b/web/src/components/WikipediaBrowser.tsx @@ -1,12 +1,13 @@ "use client"; -import React, { memo, useCallback, useEffect, useState } from "react"; +import React, { memo, useCallback, useEffect, useState, useRef } from "react"; import "./WikipediaBrowser.css"; interface Pane { title: string; content: string; isHomepage: boolean; + width: number; } const SearchBar = memo(({ onSearch }: { onSearch: (term: string, isHomepage: boolean) => void }) => { @@ -42,43 +43,76 @@ const SearchBar = memo(({ onSearch }: { onSearch: (term: string, isHomepage: boo }); SearchBar.displayName = "SearchBar"; -const PaneComponent = memo(({ pane, index, onClose, onClick, clickedLinks }: { +const Resizer = memo(({ onResize }: { onResize: (delta: number) => void }) => { + const startResizeX = useRef(0); + + const handleMouseDown = (e: React.MouseEvent) => { + startResizeX.current = e.clientX; + document.addEventListener('mousemove', handleMouseMove); + document.addEventListener('mouseup', handleMouseUp); + }; + + const handleMouseMove = (e: MouseEvent) => { + const delta = e.clientX - startResizeX.current; + onResize(delta); + startResizeX.current = e.clientX; + }; + + const handleMouseUp = () => { + document.removeEventListener('mousemove', handleMouseMove); + document.removeEventListener('mouseup', handleMouseUp); + }; + + return ( +
+ ); +}); +Resizer.displayName = "Resizer"; + +const PaneComponent = memo(({ pane, index, onClose, onClick, clickedLinks, onResize }: { pane: Pane; index: number; onClose: (index: number) => void; onClick: (e: React.MouseEvent, index: number) => void; clickedLinks: Set; + onResize: (index: number, delta: number) => void; }) => { return ( -
-
-

{pane.title}

- + <> +
+
+

{pane.title}

+ +
+
onClick(e, index)} + dangerouslySetInnerHTML={{ __html: pane.content }} + ref={(node) => { + if (node) { + node.querySelectorAll("a").forEach(a => { + if (clickedLinks.has(a.href)) { + a.classList.add("clicked-link"); + } + }); + } + }} + />
-
onClick(e, index)} - dangerouslySetInnerHTML={{ __html: pane.content }} - ref={(node) => { - if (node) { - node.querySelectorAll("a").forEach(a => { - if (clickedLinks.has(a.href)) { - a.classList.add("clicked-link"); - } - }); - } - }} - /> -
+ onResize(index, delta)} /> + ); }); PaneComponent.displayName = "PaneComponent"; @@ -110,7 +144,7 @@ const WikipediaBrowser: React.FC = () => { const response = await fetch(`https://en.wikipedia.org/w/api.php?action=parse&format=json&page=${encodeURIComponent(title)}&prop=text&formatversion=2&origin=*`); const data = await response.json(); if (data.parse) { - const newPane = { title: data.parse.title, content: data.parse.text, isHomepage }; + const newPane = { title: data.parse.title, content: data.parse.text, isHomepage, width: 720 }; if (panes.length === 0 || (activePane === 0 && panes[0].isHomepage) || isHomepage) { // For the first pane or updating the homepage, just set or update it setPanes([newPane]); @@ -140,13 +174,24 @@ const WikipediaBrowser: React.FC = () => { setActivePane(paneIndex); fetchWikipediaContent(link.title); } - }, [panes, fetchWikipediaContent]); + }, [fetchWikipediaContent]); const closePane = useCallback((index: number) => { setPanes(prevPanes => prevPanes.filter((_, i) => i !== index)); setActivePane(prevActivePane => prevActivePane > index ? prevActivePane - 1 : prevActivePane); }, []); + const handleResize = useCallback((index: number, delta: number) => { + setPanes(prevPanes => { + const newPanes = [...prevPanes]; + newPanes[index] = { ...newPanes[index], width: Math.max(200, newPanes[index].width + delta) }; + if (index < newPanes.length - 1) { + newPanes[index + 1] = { ...newPanes[index + 1], width: Math.max(200, newPanes[index + 1].width - delta) }; + } + return newPanes; + }); + }, []); + return (
@@ -159,6 +204,7 @@ const WikipediaBrowser: React.FC = () => { onClose={closePane} onClick={handleLinkClick} clickedLinks={clickedLinks} + onResize={handleResize} /> ))} {isLoading && (