Something went wrong. Try again.
a tool for shared writing and social publishing
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249"use client";
import { useEffect, useRef, useState } from "react";import { useParams } from "next/navigation";import * as Slider from "@radix-ui/react-slider";import { useEntity, useReplicache } from "src/replicache";import { addImage } from "src/utils/addImage";import { Radio } from "components/Checkbox";import { Input } from "components/Input";import { SpeedyLink } from "components/SpeedyLink";import { BlockImageSmall } from "components/Icons/BlockImageSmall";
const DEFAULT_WORDMARK_WIDTH = 200;const MIN_WORDMARK_WIDTH = 40;
// Wordmark editing backed by theme/wordmark-* facts on the draft leaflet root.// Edits persist as draft state and go live on the next publish (see// extractThemeFromFacts), mirroring how the background image is handled.export function useDraftWordmark() { let { rep, undoManager, rootEntity } = useReplicache(); let imageFact = useEntity(rootEntity, "theme/wordmark-image"); let widthFact = useEntity(rootEntity, "theme/wordmark-width"); let pageWidth = useEntity(rootEntity, "theme/page-width")?.data.value || 624;
let src = imageFact?.data.src ?? null; let width = widthFact?.data.value ?? null;
let setImage = async (file: File) => { if (!rep) return; await undoManager.withUndoGroup(async () => { await addImage(file, rep, { entityID: rootEntity, attribute: "theme/wordmark-image", }); // Give a sane starting max width so the slider and preview agree. if (width == null) await rep.mutate.assertFact({ entity: rootEntity, attribute: "theme/wordmark-width", data: { type: "number", value: DEFAULT_WORDMARK_WIDTH }, }); }); };
let setWidth = (w: number) => { rep?.mutate.assertFact({ entity: rootEntity, attribute: "theme/wordmark-width", data: { type: "number", value: w }, }); };
let remove = async () => { await rep?.mutate.retractAttribute({ entity: rootEntity, attribute: ["theme/wordmark-image", "theme/wordmark-width"], }); };
return { src, width, pageWidth, setImage, setWidth, remove };}
// Popover body for choosing between the logo and an uploaded wordmark image,// adjusting its max width, and removing it. Lives inside the draft editor.export function WordmarkEditor() { let { src, width, pageWidth, setImage, setWidth, remove } = useDraftWordmark(); let params = useParams<{ did: string; publication: string }>(); let mode: "logo" | "wordmark" = src ? "wordmark" : "logo"; let fileInputRef = useRef<HTMLInputElement>(null);
let sliderWidth = width ?? DEFAULT_WORDMARK_WIDTH; // Interim value keeps the number input and slider in sync while editing; // committed back to draft state on slider release / input blur. let [interimWidth, setInterimWidth] = useState(sliderWidth); useEffect(() => setInterimWidth(sliderWidth), [sliderWidth]);
let commitWidth = (value: number) => { let clamped = isNaN(value) ? sliderWidth : Math.max(MIN_WORDMARK_WIDTH, Math.min(pageWidth, value)); setInterimWidth(clamped); setWidth(clamped); };
return ( <div className="wordmarkEditor flex flex-col gap-3 w-xs text-primary"> <h3 className="font-bold text-primary">Header Options</h3>
<div className="flex flex-col gap-1"> <Radio type="radio" id="wordmark-mode-logo" name="wordmark-mode" value="logo" checked={mode === "logo"} onChange={(e) => { if (!e.currentTarget.checked) return; remove(); }} > <div className="flex flex-col leading-snug"> <div className="font-bold text-primary"> Use Logo and Publication Name </div> {mode === "wordmark" && ( <div className="text-sm font-normal text-tertiary"> Still appears in links and previews of this publication </div> )} </div> </Radio> {/* Outside the Radio: it renders a label, so a link inside it would also flip the radio. */} <div className="pl-5 text-sm font-normal text-tertiary"> Change the logo and name in{" "} <SpeedyLink className="text-accent-contrast" href={`/lish/${params.did}/${params.publication}/dashboard/settings`} > settings </SpeedyLink> </div> </div>
<hr className="border-0 border-t border-border-light" />
<div className="flex flex-col gap-1"> <div className="flex items-start justify-between gap-2"> <Radio type="radio" id="wordmark-mode-wordmark" name="wordmark-mode" value="wordmark" checked={mode === "wordmark"} onChange={(e) => { if (!e.currentTarget.checked) return; fileInputRef.current?.click(); }} > <div className="font-bold text-primary flex flex-col w-full"> Use Wordmark <div className="text-sm font-normal text-tertiary!"> A styled image of your publication name to show on the homepage </div> </div> </Radio> {src && ( <button type="button" className="shrink-0 text-accent-contrast" onClick={remove} > Remove </button> )} </div>
<div className="flex flex-col gap-2 pl-5"> {src ? ( <> <div className="flex items-center justify-center rounded-md border border-border-light bg-bg-page p-2"> <img src={src} alt="Wordmark preview" className="h-auto object-contain" style={{ width: `${interimWidth}px`, maxWidth: "100%" }} /> </div>
<div className="flex flex-col "> <div className="flex justify-between items-center text-secondary"> Max width <div className="flex font-normal text-tertiary"> <Input type="number" className="w-10 text-right appearance-none bg-transparent" max={pageWidth} min={MIN_WORDMARK_WIDTH} value={interimWidth} onChange={(e) => setInterimWidth(parseInt(e.currentTarget.value)) } onKeyDown={(e) => { if (e.key === "Enter" || e.key === "Escape") { e.preventDefault(); commitWidth(interimWidth); } }} onBlur={() => commitWidth(interimWidth)} /> px </div> </div> <Slider.Root className="relative flex items-center select-none touch-none w-full h-fit px-1" value={[interimWidth]} max={pageWidth} min={MIN_WORDMARK_WIDTH} step={5} onValueChange={(v) => setInterimWidth(v[0])} onValueCommit={(v) => setWidth(v[0])} > <Slider.Track className="bg-border relative grow rounded-full h-[3px] my-2"> <Slider.Range className="absolute bg-accent-contrast rounded-full h-full" /> </Slider.Track> <Slider.Thumb className="flex w-4 h-4 rounded-full border-2 border-white bg-accent-contrast shadow-[0_0_0_1px_rgb(var(--border))] cursor-pointer" aria-label="Wordmark max width" /> </Slider.Root> </div> </> ) : ( <label className="group/wordmark-upload flex items-center justify-center gap-2 h-[104px] w-full rounded-lg border border-dashed border-border bg-[var(--accent-light)] cursor-pointer text-tertiary hover:font-bold hover:text-accent-contrast hover:border-accent-contrast" onMouseDown={(e) => e.preventDefault()} onDragOver={(e) => { e.preventDefault(); e.stopPropagation(); }} onDrop={(e) => { e.preventDefault(); e.stopPropagation(); const file = e.dataTransfer.files?.[0]; if (file?.type.startsWith("image/")) setImage(file); }} > <BlockImageSmall className="shrink-0 text-border group-hover/wordmark-upload:text-accent-contrast" /> Upload Wordmark <input ref={fileInputRef} type="file" accept="image/*" className="hidden" onChange={(e) => { const file = e.target.files?.[0]; if (file) setImage(file); e.currentTarget.value = ""; }} /> </label> )} </div> </div> </div> );}