Files
Bkk1234 6f146aef59 feat(android): Capacitor APK build + native save/share bridge
- android/ — Capacitor project (build/, local.properties ignored)
- app/js/native.js — bridge สำหรับ APK: savePhoto/saveZip/shareItems ผ่าน Filesystem+Share
- app/js/csv.js — นำเข้าโฟลเดอร์หลายรายการจาก CSV (site,letter,datetime,name)
- app/js/orient.js — จัดการ orientation ของ preview/กล้อง
- app/manual.html + APK.md — คู่มือใช้งานและวิธี build APK
- app/templates/folders-template.csv — เทมเพลต CSV
- package.json / package-lock.json / capacitor.config.json / scripts/build-apk.sh
- ปรับ ui, camera, exif, archive, db, naming, main, sw + css/README ตามงานข้างบน
2026-11-20 08:50:42 +07:00

112 lines
4.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ── นำเข้าโฟลเดอร์ทีละหลายรายการจาก CSV ─────────────────────
// หัวตาราง: site,letter,datetime,name
// site : คำนำหน้า (เว้นว่าง = "Site")
// letter : ตัวอักษร a-z/aa… (เว้นว่าง = ไล่ต่อจากของเดิมอัตโนมัติ)
// datetime : 2569.10.25.21.45 (รับ – / : ช่องว่าง หรือ ค.ศ. ได้)
// name : ชื่อเต็ม (ถ้าใส่ จะใช้ค่านี้แทนการประกอบชื่อ)
import { SITE_LABEL } from "./config.js";
import { safeFileName, letterForIndex, indexForLetters, nextLetter, normalizeFolderStamp } from "./naming.js";
const DELIMS = [",", ";", "\t"];
function sniffDelimiter(line) {
let best = ",";
let bestCount = -1;
for (const d of DELIMS) {
const n = line.split(d).length - 1;
if (n > bestCount) { best = d; bestCount = n; }
}
return best;
}
/** CSV → array ของ object (คีย์เป็นตัวพิมพ์เล็ก) รองรับ quote/CRLF/BOM */
export function parseCsv(text) {
const src = String(text || "").replace(/^\uFEFF/, "").replace(/\r\n?/g, "\n").trim();
if (!src) return { header: [], rows: [] };
const delim = sniffDelimiter(src.split("\n")[0]);
const rows = [];
let field = "";
let row = [];
let quoted = false;
for (let i = 0; i < src.length; i++) {
const ch = src[i];
if (quoted) {
if (ch === '"') {
if (src[i + 1] === '"') { field += '"'; i++; }
else quoted = false;
} else field += ch;
continue;
}
if (ch === '"') { quoted = true; continue; }
if (ch === delim) { row.push(field); field = ""; continue; }
if (ch === "\n") { row.push(field); rows.push(row); row = []; field = ""; continue; }
field += ch;
}
if (field.length || row.length) { row.push(field); rows.push(row); }
const header = rows.shift().map((h) => h.trim().toLowerCase());
const out = rows
.filter((r) => r.some((c) => String(c).trim() !== ""))
.map((r) => {
const obj = {};
header.forEach((h, idx) => { obj[h] = (r[idx] ?? "").trim(); });
return obj;
});
return { header, rows: out, delimiter: delim };
}
/**
* วางแผนสร้างโฟลเดอร์จากแถว CSV
* คืน { items:[{name,row}], errors:[{row,reason}] }
*/
export function planFolders(rows, existingNames = []) {
const taken = new Set(existingNames.map((n) => String(n)));
const items = [];
const errors = [];
let nextIdx = Math.max(0, indexForLetters(nextLetter(existingNames))); // ตัวอักษรถัดไป (a ถ้ายังไม่มี)
rows.forEach((row, i) => {
const line = i + 2; // +2 = เผื่อหัวตาราง (เริ่มนับที่ 1)
const rawName = (row.name || "").trim();
let site = (row.site || SITE_LABEL).trim() || SITE_LABEL;
site = site.replace(/\s+/g, " ");
let letter = (row.letter || "").trim().toLowerCase();
if (letter && !/^[a-z]+$/.test(letter)) {
errors.push({ row: line, reason: `letter "${row.letter}" ไม่ใช่ a-z` });
return;
}
const stamp = normalizeFolderStamp(row.datetime, null);
if (row.datetime && !stamp) {
errors.push({ row: line, reason: `datetime "${row.datetime}" อ่านไม่ได้ (ต้องเป็น ปี.เดือน.วัน.ชั่วโมง.นาที)` });
return;
}
let name = rawName ? safeFileName(rawName, "") : "";
if (!name) {
if (!stamp) {
errors.push({ row: line, reason: "ไม่มี datetime และไม่มี name" });
return;
}
if (!letter) {
// หา letter ว่างถัดไป (ข้ามชื่อที่มีอยู่แล้ว)
let guard = 0;
do { letter = letterForIndex(nextIdx++); guard++; } while (taken.has(`${site} ${letter} ${stamp}`) && guard < 500);
}
name = `${site} ${letter} ${stamp}`;
}
if (!name) { errors.push({ row: line, reason: "ชื่อว่าง" }); return; }
if (taken.has(name)) { errors.push({ row: line, reason: `ชื่อซ้ำ: ${name}` }); return; }
taken.add(name);
items.push({ name, row: line });
});
return { items, errors };
}