Files
Bkk1234 c00bc62272 feat: hobby project landing page (PWA + SPA) with admin backoffice
- landing page: welcome hero, live search, project card grid (10 projects from owner list)
- warm brown/white theme (fastwork-inspired layout), embedded K2D Thai font
- PWA: manifest, PIL-generated icons, versioned service worker with auto-update
- backoffice at #/admin: edit name/description/url, reorder, delete, publish
- server/publish.mjs: zero-dependency static server + PUT api/projects with
  validation, JSON write, git commit and push; loopback-only unless ADMIN_TOKEN
- docs: README, DESIGN.md (color/type contract), AGENTS.md
2026-11-19 01:03:13 +07:00

123 lines
5.4 KiB
JavaScript

/* ui.js — ตัวช่วยกลาง: ไอคอน SVG, escaping, toast, ตรวจ URL */
const ICON_PATHS = {
search: '<circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/>',
close: '<path d="M18 6 6 18M6 6l12 12"/>',
settings:
'<path d="M4 7h10M18 7h2M4 17h2M10 17h10"/><circle cx="16" cy="7" r="2.5"/><circle cx="8" cy="17" r="2.5"/>',
arrowRight: '<path d="M5 12h14M13 5l7 7-7 7"/>',
arrowUp: '<path d="M12 19V5M5 12l7-7 7 7"/>',
arrowDown: '<path d="M12 5v14M19 12l-7 7-7-7"/>',
plus: '<path d="M12 5v14M5 12h14"/>',
trash:
'<path d="M4 7h16M9 7V5a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v2M6 7l1 13h10l1-13"/><path d="M10 11v6M14 11v6"/>',
save: '<path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2Z"/><path d="M17 21v-8H7v8M7 3v5h8"/>',
download: '<path d="M12 3v12M7 11l5 5 5-5M4 21h16"/>',
refresh:
'<path d="M20 11a8 8 0 1 0-2.3 5.7"/><path d="M20 4v7h-7"/>',
check: '<path d="m4 12 5 5L20 6"/>',
alert: '<path d="M12 3 2.5 20h19L12 3Z"/><path d="M12 9v5M12 17.5v.5"/>',
shield: '<path d="M12 3 5 6v6c0 4.4 3 7.6 7 9 4-1.4 7-4.6 7-9V6l-7-3Z"/><path d="m9 12 2 2 4-4"/>',
external:
'<path d="M14 4h6v6M20 4l-8.5 8.5"/><path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>',
folder: '<path d="M3 7a2 2 0 0 1 2-2h4l2 2h6a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V7Z"/>',
film: '<rect x="3" y="4" width="18" height="16" rx="2"/><path d="M7 4v16M17 4v16M3 9h4M3 15h4M17 9h4M17 15h4"/>',
bus: '<rect x="4" y="4" width="16" height="12" rx="2"/><path d="M4 10h16M7 20v-2M17 20v-2"/><circle cx="8" cy="13.5" r=".8"/><circle cx="16" cy="13.5" r=".8"/>',
droplet: '<path d="M12 3s6 6.2 6 10a6 6 0 0 1-12 0c0-3.8 6-10 6-10Z"/>',
cpu: '<rect x="7" y="7" width="10" height="10" rx="2"/><path d="M10 3v2M14 3v2M10 19v2M14 19v2M3 10h2M3 14h2M19 10h2M19 14h2"/>',
disc: '<circle cx="12" cy="12" r="8"/><circle cx="12" cy="12" r="2.4"/>',
book: '<path d="M5 4h9a3 3 0 0 1 3 3v13H8a3 3 0 0 1-3-3V4Z"/><path d="M8 20a3 3 0 0 1 0-6h9"/>',
wrench: '<path d="M14.5 4.5a4.5 4.5 0 0 0 5.7 5.7L11 19.5a3.5 3.5 0 1 1-5-5l9-9Z"/>',
dashboard: '<rect x="3" y="3" width="7" height="9" rx="1.6"/><rect x="14" y="3" width="7" height="5" rx="1.6"/><rect x="14" y="12" width="7" height="9" rx="1.6"/><rect x="3" y="16" width="7" height="5" rx="1.6"/>',
palette: '<circle cx="12" cy="12" r="9"/><circle cx="9" cy="9.5" r="1.2"/><circle cx="15" cy="9.5" r="1.2"/><circle cx="12" cy="15" r="1.2"/>',
list: '<path d="M8 6h13M8 12h13M8 18h13M3.5 6h.5M3.5 12h.5M3.5 18h.5"/>',
spark: '<path d="M12 3l1.8 5.2L19 10l-5.2 1.8L12 17l-1.8-5.2L5 10l5.2-1.8L12 3Z"/>',
};
export const ICON_NAMES = Object.keys(ICON_PATHS);
/** คืนสตริง SVG (ใช้ innerHTML — ชื่อไอคอนมาจากลิสต์คงที่ ไม่ใช่ข้อมูลผู้ใช้) */
export function icon(name, extraClass = "") {
const d = ICON_PATHS[name] || ICON_PATHS.folder;
return `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7"
stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false"
${extraClass ? `class="${extraClass}"` : ""}>${d}</svg>`;
}
/** แทนที่ทุกจุดที่ใส่ [data-icon="name"] ด้วย SVG จริง (สำหรับ markup ใน index.html) */
export function hydrateIcons(root = document) {
root.querySelectorAll("[data-icon]").forEach((node) => {
const name = node.getAttribute("data-icon");
if (!name || node.dataset.iconReady === "1") return;
node.innerHTML = icon(name);
node.dataset.iconReady = "1";
});
}
export function escapeHtml(value) {
return String(value ?? "")
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#39;");
}
/** เติม https:// ถ้าไม่มี scheme และคืน null ถ้าไม่ใช่ http(s) */
export function normalizeUrl(raw) {
const value = String(raw ?? "").trim();
if (!value) return null;
const withScheme = /^https?:\/\//i.test(value) ? value : `https://${value.replace(/^\/+/, "")}`;
try {
const parsed = new URL(withScheme);
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return null;
if (!parsed.hostname.includes(".")) return null;
return parsed.toString().replace(/\/$/, "");
} catch {
return null;
}
}
export function hostOf(url) {
const normalized = normalizeUrl(url);
if (!normalized) return "";
try {
return new URL(normalized).hostname;
} catch {
return "";
}
}
export function isEmail(raw) {
return /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(String(raw ?? "").trim());
}
let toastTimer = null;
export function toast(message, kind = "info") {
const stack = document.getElementById("toast-stack");
if (!stack) return;
stack.replaceChildren();
const node = document.createElement("div");
node.className = `toast${kind === "error" ? " toast--error" : ""}`;
node.textContent = message;
stack.appendChild(node);
clearTimeout(toastTimer);
toastTimer = setTimeout(() => stack.replaceChildren(), 3600);
}
export function debounce(fn, wait = 120) {
let timer = null;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), wait);
};
}
export function debounceImmediate(fn, wait = 400) {
let timer = null;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), wait);
};
}