- บทความจากหลังบ้าน: /for-film-lovers, /for-cinemas (ArticlePage + ArticleEditor แบบบล็อกย่อหน้า/รูป) - แชร์: ปุ่มบนการ์ดและท้ายหน้ารายละเอียด · ลิงก์ย่อ /c/<slug> (302) · ธีมแก้วตาม DESIGN.md - PWA: manifest/sw/offline + ถอดการล็อกซูม (WCAG 1.4.4) + touch target ≥44px - แอนิเมชัน reveal: IntersectionObserver + gate ด้วย html.fx-motion · เคารพ prefers-reduced-motion - ค้นหา: normalize/ตัดวรรณยุกต์ + AND หลายคำ + จัดอันดับ · รองรับ /?q= จาก SearchAction - SEO: pageMeta (canonical/OG/Twitter) + JSON-LD (WebSite/Organization/BreadcrumbList/MovieTheater/Article) - ไม่ฝังโดเมนในโค้ด: origin มาจาก request (x-forwarded-host/proto) ทั้ง canonical, sitemap, robots, ลิงก์แชร์ - กัน AI: robots.txt (Nitro route, บล็อก AI 21 ตัว) + ai.txt + OG image 1200x630 - เอกสาร: README สรุปโปรเจกต์, DESIGN.md (motion/PWA/SEO/ข้อมูล), architecture.html, admin-guide - tests: 135 ผ่าน (16 ไฟล์)
216 lines
7.7 KiB
TypeScript
216 lines
7.7 KiB
TypeScript
import { useState } from "react";
|
|
|
|
import { ArticleBlockCard } from "@/components/admin/ArticleBlockCard";
|
|
import {
|
|
inputClass,
|
|
labelClass,
|
|
primaryButtonClass,
|
|
secondaryButtonClass,
|
|
} from "@/components/admin/form-styles";
|
|
import {
|
|
countImages,
|
|
createImageBlock,
|
|
createParagraphBlock,
|
|
moveBlock,
|
|
removeBlock,
|
|
} from "@/lib/article";
|
|
import type { ArticleBlock, ArticleContent } from "@/lib/content-types";
|
|
import type { Locale } from "@/lib/i18n/messages";
|
|
|
|
const LOCALE_TABS: Array<{ code: Locale; label: string }> = [
|
|
{ code: "th", label: "ไทย" },
|
|
{ code: "en", label: "English" },
|
|
];
|
|
|
|
type ArticleEditorProps = {
|
|
value: Record<Locale, ArticleContent>;
|
|
defaults: Record<Locale, ArticleContent>;
|
|
onChange: (next: Record<Locale, ArticleContent>) => void;
|
|
onSave: () => void;
|
|
saving: boolean;
|
|
/** ป้ายปุ่มบันทึก เช่น "บันทึกหน้าสำหรับคนทำโรงหนัง" */
|
|
saveLabel: string;
|
|
};
|
|
|
|
/**
|
|
* ตัวแก้ไขหน้าบทความ (ใช้ทั้ง "สำหรับคนทำโรงหนัง" และ "สำหรับคนรักหนัง")
|
|
*
|
|
* ไฟล์นี้มีหน้าที่เดียว: ถือภาษาที่เลือก + ประกอบฟอร์ม
|
|
* - รายละเอียดของแต่ละบล็อก → `ArticleBlockCard`
|
|
* - สไตล์ฟอร์ม (input/label/ปุ่ม) → `form-styles`
|
|
* ฟิลด์ที่เว้นว่างจะถูกเซิร์ฟเวอร์เติมค่าเริ่มต้น (sanitizeForCinemas / sanitizeForFilmLovers)
|
|
*/
|
|
export function ArticleEditor({
|
|
value,
|
|
defaults,
|
|
onChange,
|
|
onSave,
|
|
saving,
|
|
saveLabel,
|
|
}: ArticleEditorProps) {
|
|
const [locale, setLocale] = useState<Locale>("th");
|
|
const current = value[locale];
|
|
|
|
/** อัปเดตฟิลด์ระดับหน้า (title/lead/contact… หรือทั้งรายการ blocks) */
|
|
function patch(next: Partial<ArticleContent>) {
|
|
onChange({ ...value, [locale]: { ...current, ...next } });
|
|
}
|
|
|
|
/** แทนที่บล็อกตำแหน่งที่ index — ชนิดของบล็อกถูกต้องเสมอ เพราะผู้เรียกส่งมาทั้งก้อน */
|
|
function replaceBlock(index: number, next: ArticleBlock) {
|
|
patch({
|
|
blocks: current.blocks.map((block, i) => (i === index ? next : block)),
|
|
});
|
|
}
|
|
|
|
function restoreDefaults() {
|
|
onChange({ ...value, [locale]: defaults[locale] });
|
|
}
|
|
|
|
const images = countImages(current.blocks);
|
|
const paragraphs = current.blocks.length - images;
|
|
|
|
return (
|
|
<div className="px-6 py-5">
|
|
<div className="mb-4 flex flex-wrap items-center justify-between gap-3">
|
|
<div
|
|
role="tablist"
|
|
aria-label="ภาษา"
|
|
className="flex items-center gap-1 rounded-lg border border-[#242424] bg-[#141414] p-1"
|
|
>
|
|
{LOCALE_TABS.map((tab) => (
|
|
<button
|
|
key={tab.code}
|
|
type="button"
|
|
role="tab"
|
|
aria-selected={locale === tab.code}
|
|
onClick={() => setLocale(tab.code)}
|
|
className={`min-h-9 rounded-md px-4 text-sm transition ${
|
|
locale === tab.code
|
|
? "bg-[#242424] font-semibold text-[#D47272]"
|
|
: "text-[#999] hover:text-white"
|
|
}`}
|
|
>
|
|
{tab.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<span className="text-xs text-[#999]">
|
|
{paragraphs} ย่อหน้า · {images} รูป
|
|
</span>
|
|
<button
|
|
type="button"
|
|
className={secondaryButtonClass}
|
|
onClick={restoreDefaults}
|
|
>
|
|
คืนค่าตั้งต้น ({locale.toUpperCase()})
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className={primaryButtonClass}
|
|
onClick={onSave}
|
|
disabled={saving}
|
|
>
|
|
{saving ? "กำลังบันทึก…" : saveLabel}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid gap-4 lg:grid-cols-2">
|
|
<label className={labelClass}>
|
|
หัวเรื่อง (H1)
|
|
<input
|
|
value={current.title}
|
|
onChange={(event) => patch({ title: event.target.value })}
|
|
placeholder="สำหรับคนทำโรงหนัง"
|
|
className={inputClass}
|
|
/>
|
|
{current.title.trim() === "" ? (
|
|
<span className="text-[#D47272]">เว้นว่างไม่ได้ — ระบบจะใช้ค่าเดิม</span>
|
|
) : null}
|
|
</label>
|
|
|
|
<label className={labelClass}>
|
|
คำเกริ่นนำ (แสดงใต้หัวเรื่อง)
|
|
<textarea
|
|
value={current.lead}
|
|
onChange={(event) => patch({ lead: event.target.value })}
|
|
rows={3}
|
|
className={inputClass}
|
|
/>
|
|
</label>
|
|
</div>
|
|
|
|
<div className="mt-6 flex flex-col gap-3">
|
|
<div className="flex flex-wrap items-center justify-between gap-2">
|
|
<h3 className="text-sm font-semibold text-white">เนื้อหาบทความ</h3>
|
|
<div className="flex flex-wrap gap-2">
|
|
<button
|
|
type="button"
|
|
className={secondaryButtonClass}
|
|
onClick={() =>
|
|
patch({ blocks: [...current.blocks, createParagraphBlock()] })
|
|
}
|
|
>
|
|
+ เพิ่มย่อหน้า
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className={secondaryButtonClass}
|
|
onClick={() =>
|
|
patch({ blocks: [...current.blocks, createImageBlock()] })
|
|
}
|
|
>
|
|
+ เพิ่มรูปภาพ
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{current.blocks.length === 0 ? (
|
|
<p className="rounded-lg border border-dashed border-[#303030] p-6 text-center text-sm text-[#999]">
|
|
ยังไม่มีเนื้อหา — กด “เพิ่มย่อหน้า” หรือ “เพิ่มรูปภาพ” เพื่อเริ่มเขียน
|
|
</p>
|
|
) : null}
|
|
|
|
{current.blocks.map((block, index) => (
|
|
<ArticleBlockCard
|
|
key={`${block.type}-${index}`}
|
|
block={block}
|
|
index={index}
|
|
total={current.blocks.length}
|
|
onChange={(next) => replaceBlock(index, next)}
|
|
onMove={(direction) =>
|
|
patch({ blocks: moveBlock(current.blocks, index, direction) })
|
|
}
|
|
onRemove={() =>
|
|
patch({ blocks: removeBlock(current.blocks, index) })
|
|
}
|
|
/>
|
|
))}
|
|
</div>
|
|
|
|
<div className="mt-6 grid gap-4 lg:grid-cols-2">
|
|
<label className={labelClass}>
|
|
หัวข้อการ์ดติดต่อ
|
|
<input
|
|
value={current.contactHeading}
|
|
onChange={(event) => patch({ contactHeading: event.target.value })}
|
|
className={inputClass}
|
|
/>
|
|
</label>
|
|
<label className={labelClass}>
|
|
ข้อความในการ์ดติดต่อ
|
|
<textarea
|
|
value={current.contactNote}
|
|
onChange={(event) => patch({ contactNote: event.target.value })}
|
|
rows={3}
|
|
className={inputClass}
|
|
/>
|
|
</label>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|