// ── UI: markup + toast + bottom sheet ────────────────────────
import { humanDateTime } from "./naming.js";
import { formatExposure } from "./exif.js";
export const esc = (s) =>
String(s ?? "")
.replace(/&/g, "&").replace(//g, ">")
.replace(/"/g, """).replace(/'/g, "'");
export function fmtBytes(n) {
if (!Number.isFinite(n) || n <= 0) return "0 B";
if (n < 1024) return n + " B";
if (n < 1024 ** 2) return (n / 1024).toFixed(1) + " KB";
if (n < 1024 ** 3) return (n / 1024 ** 2).toFixed(1) + " MB";
return (n / 1024 ** 3).toFixed(2) + " GB";
}
const ICON = {
torch: ``,
flip: ``,
grid: ``,
pencil: ``,
trash: ``,
zip: ``,
share: ``,
camera: ``,
lock: ``,
block: ``,
folder: ``,
image: ``,
};
// ── Toast ──
let toastTimer = null;
export function toast(msg, ms = 2600) {
const el = document.getElementById("toast");
el.textContent = msg;
el.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => el.classList.remove("show"), ms);
}
// ── Bottom sheet ──
export function openSheet(html) {
const overlay = document.getElementById("sheet-overlay");
document.getElementById("sheet-content").innerHTML = html;
overlay.hidden = false;
overlay.setAttribute("aria-hidden", "false");
}
export function closeSheet() {
const overlay = document.getElementById("sheet-overlay");
overlay.hidden = true;
overlay.setAttribute("aria-hidden", "true");
document.getElementById("sheet-content").innerHTML = "";
}
export function isSheetOpen() {
return !document.getElementById("sheet-overlay").hidden;
}
// ── Camera ──
export function cameraShell() {
return `
`;
}
export function overlay(html) {
const el = document.getElementById("cam-overlay");
if (el) el.innerHTML = html;
}
export function hero(kind, { title, text = "", steps = [], action = null, note = "" }) {
const stepsHtml = steps.length
? `${steps.map((s) => `- ${s}
`).join("")}
`
: "";
const actionHtml = action
? ``
: "";
return `
${kind === "denied" ? ICON.block : kind === "insecure" ? ICON.lock : ICON.camera}
${esc(title)}
${text ? `
${esc(text)}
` : ""}
${stepsHtml ? `
${stepsHtml}
` : ""}
${actionHtml}
${note ? `
${note}
` : ""}
`;
}
export function syncCameraControls({ torchAvailable, torchOn, zoomRange, zoomValue, gridOn, count, thumbUrl, ready }) {
const chip = document.getElementById("chip-torch");
if (chip) {
chip.hidden = !torchAvailable;
chip.setAttribute("aria-pressed", torchOn ? "true" : "false");
}
const pills = document.getElementById("zoom-pills");
if (pills) {
const min = zoomRange ? Number(zoomRange.min) : 1;
const max = zoomRange ? Number(zoomRange.max) : 1;
if (zoomRange && max > min) {
const levels = [1, 2, 3].filter((z) => z >= min - 0.05 && z <= max + 0.05);
if (!levels.length) levels.push(min);
const current = zoomValue ?? min;
pills.hidden = false;
pills.innerHTML = levels
.map(
(z) =>
``
)
.join("");
} else {
pills.hidden = true;
pills.innerHTML = "";
}
}
const grid = document.getElementById("grid-lines");
if (grid) grid.classList.toggle("on", !!gridOn);
const cnt = document.getElementById("shot-count");
if (cnt) cnt.textContent = count ? count + " รูป" : "";
const thumb = document.getElementById("last-thumb");
if (thumb && thumbUrl) thumb.innerHTML = `
`;
const sh = document.getElementById("shutter");
if (sh) sh.disabled = !ready;
}
// ── Folders ──
export function renderFolders({ folders, counts, activeId, storage }) {
const total = counts.reduce((a, b) => a + b, 0);
let html = `
โฟลเดอร์
${folders.length} โฟลเดอร์ · ${total} รูป
`;
if (!folders.length) {
html += `${ICON.folder}ยังไม่มีโฟลเดอร์
`;
} else {
html += ``;
folders.forEach((f, i) => {
const active = f.id === activeId;
html += `
${ICON.folder}
${esc(f.name)}
${counts[i]} รูป${active ? " · ใช้งานอยู่" : ""}
`;
});
html += `
`;
}
html += `
พื้นที่เก็บในเครื่อง${storage ? ` · ใช้ ${fmtBytes(storage.usage)} จาก ${fmtBytes(storage.quota)}` : ""}
`;
return html;
}
// ── Gallery ──
export function renderGallery({ folder, images, thumbUrl }) {
const bytes = images.reduce((a, i) => a + (i.size || i.blob?.size || 0), 0);
let html = `
${esc(folder ? folder.name : "ยังไม่เลือกโฟลเดอร์")}
${images.length} รูป · ${fmtBytes(bytes)}
`;
if (!folder) {
return html + `
${ICON.folder}เลือกโฟลเดอร์ก่อน
`;
}
if (!images.length) {
return html + `${ICON.image}ยังไม่มีรูปในโฟลเดอร์นี้
`;
}
html += ``;
images.forEach((img) => {
html += `
`;
});
html += `
`;
html += `
`;
return html;
}
// ── Photo sheet ──
export function photoSheet({ record, url, folderName }) {
const m = record.meta || {};
const rows = [
["ไฟล์", record.name || "-"],
["ขนาด", fmtBytes(record.size || record.blob?.size || 0)],
["ความละเอียด", m.width && m.height ? `${m.width} × ${m.height} (${(m.width * m.height / 1e6).toFixed(1)} MP)` : "—"],
["ถ่ายเมื่อ", humanDateTime(record.createdAt)],
["โฟลเดอร์", folderName || "-"],
["อุปกรณ์", m.device || "—"],
["กล้อง", m.facingLabel || "—"],
["ที่มาไฟล์", m.sourceLabel || "—"],
];
if (m.gps) rows.push(["พิกัด GPS", `${m.gps.lat.toFixed(6)}, ${m.gps.lng.toFixed(6)}${m.gps.accuracy ? ` (±${Math.round(m.gps.accuracy)} ม.)` : ""}`]);
if (m.exif && (m.exif.fNumber || m.exif.iso || m.exif.exposureTime)) rows.push(["ค่าแสง EXIF", formatExposure(m.exif)]);
if (m.exif?.model) {
const make = (m.exif.make || "").trim();
const model = m.exif.model.trim();
const label = make && !model.toLowerCase().startsWith(make.toLowerCase()) ? `${make} ${model}` : model;
rows.push(["รุ่นใน EXIF", label]);
}
if (m.exif?.orientation) rows.push(["Orientation", String(m.exif.orientation)]);
if (m.exif?.dateTimeOriginal) rows.push(["EXIF DateTimeOriginal", m.exif.dateTimeOriginal]);
const rowsHtml = rows
.map(([k, v]) => `${esc(k)}${esc(v)}
`)
.join("");
return `
${esc(record.name || "รูปภาพ")}
${esc(humanDateTime(record.createdAt))}
${rowsHtml}
`;
}
// ── Folder sheet ──
export function folderSheet(mode, { id = "", name = "", suggested = "", count = 0 }) {
if (mode === "delete") {
return `ลบโฟลเดอร์นี้?
"${esc(name)}" · ${count} รูป จะถูกลบทั้งหมดและกู้คืนไม่ได้
`;
}
const isNew = mode === "new";
return `${isNew ? "โฟลเดอร์ใหม่" : "เปลี่ยนชื่อโฟลเดอร์"}
ชื่อเริ่มต้นอิงวันที่ไทย (พ.ศ.) ของวันนี้ — แก้ไขได้
${isNew ? `ถ้าชื่อซ้ำ ระบบจะเติม (2) ให้อัตโนมัติ
` : ""}
`;
}
// ── QR ลิงก์ทดสอบบนมือถือ ────────────────────────────
export function qrSheet(url) {
return `ทดสอบบนมือถือ
สแกนด้วยกล้อง Samsung S23 แล้วเปิดใน Chrome
${esc(url)}
กล้องใช้ได้เฉพาะหน้า HTTPS — ถ้าลิงก์ขึ้นต้น http:// กล้องจะถูกบล็อก
`;
}
// ── Diagnostics ──
export function diagnosticsSheet(rows) {
const body = rows
.map(([k, v]) => `${esc(k)}${esc(v)}
`)
.join("");
return `ข้อมูลอุปกรณ์และกล้อง
ใช้ตรวจว่ารันบน S23 จริงและกล้องได้ความละเอียดเท่าไร
${body}
`;
}
// ── Progress ──
export function progressSheet(label) {
openSheet(``);
}
export function progressUpdate(pct, label) {
const card = document.getElementById("progress-card");
if (!card) return;
const p = Math.max(0, Math.min(100, Math.round(pct)));
card.innerHTML = `${esc(label || "")} ${p}%
`;
}