Newsletter: every part editable and movable (v1.11.2-newsletter) #24

Merged
ngu-git-admin merged 4 commits from newsletter-layout into main 2026-10-02 19:50:04 +01:00
Showing only changes of commit c81712ef32 - Show all commits

View file

@ -20,8 +20,8 @@
email is read far from this admin. email is read far from this admin.
═══════════════════════════════════════════════════════════════ */ ═══════════════════════════════════════════════════════════════ */
import { Article, Check, Copy, DownloadSimple, FloppyDisk, Trash } from '@phosphor-icons/react' import { Article, Check, Copy, DotsSixVertical, DownloadSimple, FloppyDisk, Trash } from '@phosphor-icons/react'
import { useEffect, useMemo, useState } from 'react' import { useEffect, useMemo, useRef, useState } from 'react'
import { useNavigate } from 'react-router-dom' import { useNavigate } from 'react-router-dom'
import { ApiError, del, get, patch, post } from '../../lib/api.ts' import { ApiError, del, get, patch, post } from '../../lib/api.ts'
@ -496,6 +496,7 @@ export default function AdminNewsletter() {
} }
const empty = parts.length === 0 const empty = parts.length === 0
const split = usePreviewWidth()
return ( return (
<div> <div>
@ -543,8 +544,13 @@ export default function AdminNewsletter() {
)} )}
</div> </div>
{/* The editor takes what the preview (the email's 600px and its margin) leaves. */} {/* The editor takes what the preview leaves; the handle between
<div className="mt-6 grid gap-8 lg:grid-cols-[minmax(0,1fr)_minmax(0,42rem)]"> them sets the preview's width (from lg up). */}
<div
ref={split.ref}
className="mt-6 grid gap-8 lg:grid-cols-[minmax(0,1fr)_auto_var(--preview-width)] lg:gap-3"
style={{ '--preview-width': `${split.width}px` } as React.CSSProperties}
>
<section className="border-l-4 border-brand bg-raised p-5 shadow-sm"> <section className="border-l-4 border-brand bg-raised p-5 shadow-sm">
<Repeater spec={PARTS_SPEC} rows={parts} options={pickOptions} errors={{}} errorPrefix="parts." className="" onChange={setParts} /> <Repeater spec={PARTS_SPEC} rows={parts} options={pickOptions} errors={{}} errorPrefix="parts." className="" onChange={setParts} />
<p className="mt-3 text-xs" style={{ color: MUTED }}> <p className="mt-3 text-xs" style={{ color: MUTED }}>
@ -552,7 +558,25 @@ export default function AdminNewsletter() {
</p> </p>
</section> </section>
<div> <div
role="separator"
aria-orientation="vertical"
aria-label="Resize the parts and the preview"
aria-valuemin={PREVIEW_MIN}
aria-valuemax={split.max}
aria-valuenow={split.width}
tabIndex={0}
onPointerDown={split.start}
onPointerMove={split.drag}
onPointerUp={split.end}
onPointerCancel={split.end}
onKeyDown={split.key}
className="hidden cursor-col-resize touch-none items-start justify-center self-stretch border-x border-brand/20 bg-tint-soft px-0.5 pt-6 text-muted hover:bg-tint hover:text-link focus-visible:bg-tint focus-visible:text-link lg:flex"
>
<DotsSixVertical size={16} weight="bold" aria-hidden="true" className="lg:sticky lg:top-1/2" />
</div>
<div className="min-w-0">
<div className="flex flex-wrap gap-3"> <div className="flex flex-wrap gap-3">
<button type="button" onClick={() => copy('html')} className={button} disabled={empty}> <button type="button" onClick={() => copy('html')} className={button} disabled={empty}>
{copied === 'html' ? <Check weight="bold" aria-hidden="true" /> : <Copy weight="bold" aria-hidden="true" />} {copied === 'html' ? <Check weight="bold" aria-hidden="true" /> : <Copy weight="bold" aria-hidden="true" />}
@ -581,3 +605,71 @@ export default function AdminNewsletter() {
</div> </div>
) )
} }
/* ── 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<HTMLDivElement>(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<HTMLDivElement>) => e.currentTarget.setPointerCapture(e.pointerId),
drag: (e: React.PointerEvent<HTMLDivElement>) => {
if (!e.currentTarget.hasPointerCapture(e.pointerId) || !ref.current) return
set(ref.current.getBoundingClientRect().right - e.clientX - e.currentTarget.offsetWidth / 2)
},
end: (e: React.PointerEvent<HTMLDivElement>) => {
if (e.currentTarget.hasPointerCapture(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId)
},
key: (e: React.KeyboardEvent<HTMLDivElement>) => {
// 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)
},
}
}