contiai / apps /web /src /components /studio /CopyEditor.tsx
aliabozahra315's picture
Deploy 53b4bcf
5614636
Raw History Blame Contribute Delete
8.39 kB
import { inputClass } from "@/components/ui";
import { countWords, type Copy, type StudioType } from "@/lib/studio";
/* The copy, field by field, as the type is made of it - with the word count
against the type's limits always in view. The Copywriter's draft lands
here to be read and changed; or the user writes it here from nothing. */
const LABELS: Record<string, string> = {
quote: "الاقتباس", source: "المصدر", headline: "العنوان", offer: "العرض", cta: "زر الدعوة",
line: "الجملة", title: "العنوان", subtitle: "سطر تمهيدي", points: "النقاط", slides: "الشرائح",
sections: "الأقسام", number: "الرقم", label: "ماذا يقيس", description: "الوصف",
steps: "الخطوات", left: "العمود الأول", right: "العمود الثاني", name: "اسم المناسبة",
tagline: "سطر تعريفي", date: "التاريخ والوقت", place: "المكان", highlights: "المزايا",
audience: "الفئة",
};
const LONG = new Set(["quote", "offer", "description", "line", "tagline"]);
const ITEM_LISTS = new Set(["slides", "steps", "sections"]);
export function emptyCopy(t: StudioType): Copy {
const out: Copy = {};
for (const f of t.fields) {
if (f === "points" || f === "highlights") out[f] = Array(Math.max(t.items[0], 1)).fill("");
else if (ITEM_LISTS.has(f)) {
out[f] = Array.from({ length: Math.max(t.items[0], 1) }, () =>
f === "sections" ? { title: "", text: "", figure: "" } : { title: "", text: "" });
} else if (f === "left" || f === "right") out[f] = { label: "", items: ["", ""] };
else out[f] = "";
}
return out;
}
export function WordMeter({ t, copy }: { t: StudioType; copy: Copy }) {
const [lo, hi] = t.words;
if (t.per_slide) {
const slides = (copy.slides as { title?: string; text?: string }[] | undefined) ?? [];
return (
<div className="flex flex-wrap gap-1.5 text-micro">
{slides.map((s, i) => {
const n = countWords([s.title, s.text]);
const tone = n > hi ? "err" : n < lo ? "warn" : "ok";
return (
<span key={i} className="rounded-full border px-2 py-0.5"
style={{ background: `var(--${tone}-bg)`, borderColor: `var(--${tone}-bd)`,
color: `var(--${tone}-fg)` }}>
شريحة {i + 1}: {n}/{hi}
</span>
);
})}
</div>
);
}
const n = countWords(copy);
const tone = n > hi ? "err" : n < lo ? "warn" : "ok";
return (
<span className="inline-flex items-center gap-1 rounded-full border px-2.5 py-0.5 text-caption font-bold"
style={{ background: `var(--${tone}-bg)`, borderColor: `var(--${tone}-bd)`,
color: `var(--${tone}-fg)` }}>
{n} كلمة · الحد {lo}–{hi}
</span>
);
}
export default function CopyEditor({ t, copy, onChange, dir }: {
t: StudioType;
copy: Copy;
onChange: (c: Copy) => void;
dir: "rtl" | "ltr";
}) {
const set = (k: string, v: unknown) => onChange({ ...copy, [k]: v });
return (
<div className="flex flex-col gap-3" dir={dir}>
{t.fields.map((f) => {
const v = copy[f];
if (f === "points" || f === "highlights") {
const list = (v as string[]) ?? [];
return (
<ListField key={f} label={LABELS[f] ?? f} max={t.items[1]} count={list.length}
onAdd={() => set(f, [...list, ""])}>
{list.map((x, i) => (
<Row key={i} onRemove={list.length > 1 ? () => set(f, list.filter((_, j) => j !== i))
: undefined}>
<input className={inputClass} value={x} placeholder={`${i + 1}`}
onChange={(e) => set(f, list.map((y, j) => (j === i ? e.target.value : y)))} />
</Row>
))}
</ListField>
);
}
if (ITEM_LISTS.has(f)) {
const list = (v as { title: string; text: string; figure?: string }[]) ?? [];
const blank = f === "sections" ? { title: "", text: "", figure: "" } : { title: "", text: "" };
return (
<ListField key={f} label={LABELS[f] ?? f} max={t.items[1]} count={list.length}
onAdd={() => set(f, [...list, blank])}>
{list.map((x, i) => {
const upd = (patch: object) =>
set(f, list.map((y, j) => (j === i ? { ...y, ...patch } : y)));
return (
<Row key={i} onRemove={list.length > 1 ? () => set(f, list.filter((_, j) => j !== i))
: undefined}>
<div className="grid w-full gap-1.5">
<div className="flex gap-1.5">
<input className={inputClass} value={x.title} placeholder={`عنوان ${i + 1}`}
onChange={(e) => upd({ title: e.target.value })} />
{f === "sections" && (
<input className={`${inputClass} max-w-[7rem]`} value={x.figure ?? ""}
placeholder="رقم/نسبة" onChange={(e) => upd({ figure: e.target.value })} />
)}
</div>
<textarea className={inputClass} rows={2} value={x.text} placeholder="النص"
onChange={(e) => upd({ text: e.target.value })} />
</div>
</Row>
);
})}
</ListField>
);
}
if (f === "left" || f === "right") {
const col = (v as { label: string; items: string[] }) ?? { label: "", items: [] };
return (
<ListField key={f} label={LABELS[f]} max={t.items[1]} count={col.items.length}
onAdd={() => set(f, { ...col, items: [...col.items, ""] })}>
<input className={inputClass} value={col.label} placeholder="عنوان العمود"
onChange={(e) => set(f, { ...col, label: e.target.value })} />
{col.items.map((x, i) => (
<Row key={i} onRemove={col.items.length > 1
? () => set(f, { ...col, items: col.items.filter((_, j) => j !== i) }) : undefined}>
<input className={inputClass} value={x}
onChange={(e) => set(f, { ...col, items: col.items.map((y, j) =>
(j === i ? e.target.value : y)) })} />
</Row>
))}
</ListField>
);
}
return (
<label key={f} className="block">
<span className="mb-1 block text-caption font-semibold text-[var(--text-strong)]">
{LABELS[f] ?? f}
</span>
{LONG.has(f) ? (
<textarea className={inputClass} rows={3} value={String(v ?? "")}
onChange={(e) => set(f, e.target.value)} />
) : (
<input className={inputClass} value={String(v ?? "")}
onChange={(e) => set(f, e.target.value)} />
)}
</label>
);
})}
</div>
);
}
function ListField({ label, children, onAdd, max, count }: {
label: string; children: React.ReactNode; onAdd: () => void; max: number; count: number;
}) {
return (
<div className="flex flex-col gap-1.5">
<span className="text-caption font-semibold text-[var(--text-strong)]">
{label} {max > 0 && <span className="text-[var(--text-muted)]">({count}/{max})</span>}
</span>
{children}
{(!max || count < max) && (
<button type="button" onClick={onAdd}
className="self-start text-caption font-semibold text-[var(--brand-text)]">
+ إضافة
</button>
)}
</div>
);
}
function Row({ children, onRemove }: { children: React.ReactNode; onRemove?: () => void }) {
return (
<div className="flex items-start gap-1.5">
{children}
{onRemove && (
<button type="button" onClick={onRemove} aria-label="حذف"
className="mt-2 shrink-0 px-1 text-[var(--text-muted)] hover:text-[var(--err-fg)]">
×
</button>
)}
</div>
);
}