diff --git a/src/pages/admin/AdminNewsletter.tsx b/src/pages/admin/AdminNewsletter.tsx index 5ec552f..ea73605 100644 --- a/src/pages/admin/AdminNewsletter.tsx +++ b/src/pages/admin/AdminNewsletter.tsx @@ -20,8 +20,8 @@ email is read far from this admin. ═══════════════════════════════════════════════════════════════ */ -import { Article, Check, Copy, DownloadSimple, FloppyDisk, Trash } from '@phosphor-icons/react' -import { useEffect, useMemo, useState } from 'react' +import { Article, Check, Copy, DotsSixVertical, DownloadSimple, FloppyDisk, Trash } from '@phosphor-icons/react' +import { useEffect, useMemo, useRef, useState } from 'react' import { useNavigate } from 'react-router-dom' import { ApiError, del, get, patch, post } from '../../lib/api.ts' @@ -496,6 +496,7 @@ export default function AdminNewsletter() { } const empty = parts.length === 0 + const split = usePreviewWidth() return (
@@ -543,8 +544,13 @@ export default function AdminNewsletter() { )}
- {/* The editor takes what the preview (the email's 600px and its margin) leaves. */} -
+ {/* The editor takes what the preview leaves; the handle between + them sets the preview's width (from lg up). */} +

@@ -552,7 +558,25 @@ export default function AdminNewsletter() {

-
+
+
+ +
) } + +/* ── The handle between the parts and the preview ───────────── + Drag it, or focus it and use the arrow keys (Home and End for + the narrowest and widest). Under the email's 600px the preview + shows it as a phone would. The width is remembered in this + browser, a convenience only. */ + +const PREVIEW_MIN = 320 +const PARTS_MIN = 360 // what the parts list keeps, at least +const PREVIEW_DEFAULT = 672 +const PREVIEW_KEY = 'site-newsletter-preview-width' +const STEP = 32 + +function usePreviewWidth() { + const ref = useRef(null) + const [room, setRoom] = useState(0) + const [wanted, setWanted] = useState(() => { + try { + const saved = Number(window.localStorage.getItem(PREVIEW_KEY)) + return saved >= PREVIEW_MIN ? saved : PREVIEW_DEFAULT + } catch { + return PREVIEW_DEFAULT + } + }) + + // The row's width, so the widest the preview can be follows the window. + useEffect(() => { + const row = ref.current + if (!row) return undefined + const observer = new ResizeObserver(() => setRoom(row.clientWidth)) + observer.observe(row) + return () => observer.disconnect() + }, []) + + const max = Math.max(PREVIEW_MIN, room ? room - PARTS_MIN : PREVIEW_DEFAULT) + const width = Math.min(max, Math.max(PREVIEW_MIN, wanted)) + + const set = (next: number) => { + const clamped = Math.round(Math.min(max, Math.max(PREVIEW_MIN, next))) + setWanted(clamped) + try { + window.localStorage.setItem(PREVIEW_KEY, String(clamped)) + } catch { + // Storage off: the width lasts until the page is left. + } + } + + return { + ref, + width, + max, + start: (e: React.PointerEvent) => e.currentTarget.setPointerCapture(e.pointerId), + drag: (e: React.PointerEvent) => { + if (!e.currentTarget.hasPointerCapture(e.pointerId) || !ref.current) return + set(ref.current.getBoundingClientRect().right - e.clientX - e.currentTarget.offsetWidth / 2) + }, + end: (e: React.PointerEvent) => { + if (e.currentTarget.hasPointerCapture(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId) + }, + key: (e: React.KeyboardEvent) => { + // Left widens the preview (the handle moves left), Right narrows it. + const next = { ArrowLeft: width + STEP, ArrowRight: width - STEP, Home: PREVIEW_MIN, End: max }[e.key] + if (next === undefined) return + e.preventDefault() + set(next) + }, + } +}