mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 04:29:27 +08:00
* feat(web): let the reading companion claim half the window with a focus preset The companion column was drag-resizable only within a hard 640px cap, which left readers with a narrow chat pane on wide monitors; leaving immersive reading for the full chat loses the viewport-grounded context, so widening the pane in place is the better escape hatch. - Raise the drag ceiling to half the window width (the reader track keeps its minmax(360px,1fr) floor; the stored-width validator follows). - Add a focus preset next to the panel toggles: one click collapses the contents navigator and sets the companion to half the window; exiting restores the previous width and navigator state. Ephemeral like the navigator collapse state - only the width persists. - Hide the preset on narrow screens where the companion renders as a sheet. * feat(web): fully resizable reading workspace panels Extend the reading workspace from a companion-only drag handle to fully user-sized panels: - The contents navigator gains its own drag divider (184-400px, persisted in dt.reader.navigatorWidth) instead of a fixed 184-230px track. - The companion's ceiling is now the reader's minimum width, not half the window: with the navigator collapsed the companion can claim all but 360px, so readers on wide monitors can make the chat pane as large as the layout physically allows. - The grid renders both dividers as dedicated 5px tracks so each handle owns one column and auto-placement lines the panels up. - A persisted width that no longer fits (window moved to a smaller screen) is clamped at render time so the reader never drops below its floor. * fix(web): measure the reading grid, not the window, for panel ceilings The app sidebar sits outside the reading grid, so innerWidth overstates the space the panels share and a companion dragged past the container overflowed off-screen. Track the grid container with a ResizeObserver and derive the companion ceiling (drag-time and render-time) plus the focus preset from the measured frame, accounting for the divider tracks. * Adapt reading panel resize and focus to current workspace layout --------- Co-authored-by: Bingxi Zhao (Frank) <13051345080@163.com>
129 lines
4.6 KiB
TypeScript
129 lines
4.6 KiB
TypeScript
'use client'
|
|
|
|
import { useCallback, useEffect, useState } from 'react'
|
|
import { browserStorage } from '@/shared/storage'
|
|
|
|
const NAVIGATOR_MIN = 184
|
|
const NAVIGATOR_MAX = 400
|
|
const COMPANION_MIN = 300
|
|
const READER_MIN = 360
|
|
const HANDLE = 5
|
|
const clamp = (value: number, min: number, max: number) =>
|
|
Math.round(Math.min(max, Math.max(min, value)))
|
|
|
|
/** Reserve the reader first; shrink side-panel floors only if the frame cannot fit them. */
|
|
export function readingPanelBounds(
|
|
frame: number,
|
|
navigatorOpen: boolean,
|
|
companionOpen: boolean,
|
|
preferredNavigator: number,
|
|
preferredCompanion: number
|
|
) {
|
|
const handles = (Number(navigatorOpen) + Number(companionOpen)) * HANDLE
|
|
const available = Math.max(0, Math.floor(frame - handles - Math.min(READER_MIN, frame / 2)))
|
|
const floors = (navigatorOpen ? NAVIGATOR_MIN : 0) + (companionOpen ? COMPANION_MIN : 0)
|
|
const scale = floors ? Math.min(1, available / floors) : 1
|
|
const navigatorMin = navigatorOpen ? Math.floor(NAVIGATOR_MIN * scale) : 0
|
|
const companionMin = companionOpen ? Math.floor(COMPANION_MIN * scale) : 0
|
|
const navigatorMax = navigatorOpen ? Math.min(NAVIGATOR_MAX, available - companionMin) : 0
|
|
const navigatorWidth = navigatorOpen ? clamp(preferredNavigator, navigatorMin, navigatorMax) : 0
|
|
const companionMax = companionOpen ? available - navigatorWidth : 0
|
|
const companionWidth = companionOpen ? clamp(preferredCompanion, companionMin, companionMax) : 0
|
|
return {
|
|
navigatorWidth,
|
|
companionWidth,
|
|
navigatorMin,
|
|
// A drag may consume free reader space, but never displaces the other panel.
|
|
navigatorMax: navigatorOpen ? Math.min(NAVIGATOR_MAX, available - companionWidth) : 0,
|
|
companionMin,
|
|
companionMax,
|
|
}
|
|
}
|
|
|
|
function readWidth(key: string, fallback: number, min: number, max: number) {
|
|
if (typeof window === 'undefined') return fallback
|
|
const value = Number(browserStorage.readRaw('local', key))
|
|
return Number.isFinite(value) && value >= min && value <= max ? value : fallback
|
|
}
|
|
|
|
export function useReadingPanelLayout({
|
|
enabled,
|
|
navigatorOpen: preferredNavigatorOpen,
|
|
companionOpen,
|
|
}: {
|
|
enabled: boolean
|
|
navigatorOpen: boolean
|
|
companionOpen: boolean
|
|
}) {
|
|
const [element, gridRef] = useState<HTMLDivElement | null>(null)
|
|
const [frame, setFrame] = useState(0)
|
|
const [navigatorWidth, setNavigatorWidth] = useState(() =>
|
|
readWidth('dt.reader.navigatorWidth', 210, NAVIGATOR_MIN, NAVIGATOR_MAX)
|
|
)
|
|
const [companionWidth, setCompanionWidth] = useState(() =>
|
|
readWidth('dt.reader.companionWidth', 380, COMPANION_MIN, 2400)
|
|
)
|
|
const [focus, setFocus] = useState<{ width?: number } | null>(null)
|
|
const companionFocus = focus !== null && enabled && companionOpen
|
|
const navigatorOpen = companionFocus ? false : preferredNavigatorOpen
|
|
|
|
// A callback ref also observes a grid that appears after the loading screen.
|
|
useEffect(() => {
|
|
if (!element) return
|
|
const measure = () => setFrame(element.clientWidth)
|
|
const scheduled = requestAnimationFrame(measure)
|
|
const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(measure)
|
|
observer?.observe(element)
|
|
if (!observer) window.addEventListener("resize", measure)
|
|
return () => {
|
|
cancelAnimationFrame(scheduled)
|
|
observer?.disconnect()
|
|
if (!observer) window.removeEventListener("resize", measure)
|
|
}
|
|
}, [element])
|
|
|
|
const bounds = readingPanelBounds(
|
|
frame,
|
|
navigatorOpen,
|
|
companionOpen,
|
|
navigatorWidth,
|
|
companionFocus ? (focus.width ?? frame / 2) : companionWidth
|
|
)
|
|
const exitFocus = useCallback(() => setFocus(null), [])
|
|
const toggleFocus = useCallback(() => setFocus(current => (current ? null : {})), [])
|
|
const resizeNavigator = (width: number) => {
|
|
setNavigatorWidth(width)
|
|
browserStorage.writeRaw('local', 'dt.reader.navigatorWidth', String(width))
|
|
}
|
|
const resizeCompanion = (width: number) => {
|
|
if (companionFocus) {
|
|
setFocus({ width })
|
|
} else {
|
|
setCompanionWidth(width)
|
|
browserStorage.writeRaw('local', 'dt.reader.companionWidth', String(width))
|
|
}
|
|
}
|
|
const measured = enabled && frame > 0
|
|
const gridTemplateColumns = measured
|
|
? [
|
|
...(navigatorOpen ? [`${bounds.navigatorWidth}px`, `${HANDLE}px`] : []),
|
|
'minmax(0,1fr)',
|
|
...(companionOpen ? [`${HANDLE}px`, `${bounds.companionWidth}px`] : []),
|
|
].join(' ')
|
|
: undefined
|
|
|
|
return {
|
|
...bounds,
|
|
gridRef,
|
|
gridStyle: gridTemplateColumns ? { gridTemplateColumns } : undefined,
|
|
navigatorOpen,
|
|
showNavigatorHandle: measured && navigatorOpen,
|
|
showCompanionHandle: measured && companionOpen,
|
|
companionFocus,
|
|
toggleFocus,
|
|
exitFocus,
|
|
resizeNavigator,
|
|
resizeCompanion,
|
|
}
|
|
}
|