A Přihlásit/Odhlásit button in the top bar replaces logging in via Přidat hru. The dialog has an eye toggle, says when it is checking, and keeps the remember box as it was after a wrong password; nothing is stored until the server accepts it. A remembered password that no longer works is dropped on load. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HA82gVRDXpoukzgMFumoJt
129 lines
5.6 KiB
JavaScript
129 lines
5.6 KiB
JavaScript
import { api, withLogin, getPassword, askPassword, authButton, esc, $ } from "./api.js";
|
|
import { toJpeg, fingerprint } from "./photos.js";
|
|
|
|
let files = []; // [{ file, url, hash }]
|
|
let cfg;
|
|
|
|
const collator = new Intl.Collator("cs", { numeric: true, sensitivity: "base" });
|
|
|
|
// Add picked photos, skipping any that are byte-for-byte the same as one already
|
|
// picked. (Two different photos of the same page are sorted out after transcription.)
|
|
async function addFiles(list) {
|
|
const imgs = [...list].filter((f) => f.type.startsWith("image/") || /\.(heic|heif)$/i.test(f.name));
|
|
const skipped = [];
|
|
for (const file of imgs) {
|
|
const hash = await fingerprint(file);
|
|
const same = files.find((f) => f.hash === hash);
|
|
if (same) { skipped.push(`„${file.name}“ je stejná fotka jako „${same.file.name}“`); continue; }
|
|
files.push({ file, hash, url: URL.createObjectURL(file) });
|
|
}
|
|
files.sort((a, b) => collator.compare(a.file.name, b.file.name));
|
|
const notes = skipped.length ? [`Přeskočeno ${skipped.length === 1 ? "1 opakování" : `${skipped.length} opakování`}: ${skipped.join("; ")}.`] : [];
|
|
if (files.length > cfg.maxPages) {
|
|
notes.push(`Najednou lze nahrát nejvýše ${cfg.maxPages} stran.`);
|
|
files.splice(cfg.maxPages).forEach((f) => URL.revokeObjectURL(f.url));
|
|
}
|
|
$("notice").textContent = notes.join(" ");
|
|
$("notice").hidden = !notes.length;
|
|
renderThumbs();
|
|
}
|
|
|
|
function renderThumbs() {
|
|
$("thumbs").innerHTML = files.map((f, i) => `
|
|
<li class="thumb">
|
|
<img src="${f.url}" alt="">
|
|
<span class="thumb-num">${i + 1}</span>
|
|
<span class="thumb-name" title="${esc(f.file.name)}">${esc(f.file.name)}</span>
|
|
<span class="thumb-tools">
|
|
<button type="button" data-move="-1" data-i="${i}" title="Dříve" ${i === 0 ? "disabled" : ""}>◀</button>
|
|
<button type="button" data-move="1" data-i="${i}" title="Později" ${i === files.length - 1 ? "disabled" : ""}>▶</button>
|
|
<button type="button" data-remove data-i="${i}" title="Odebrat">✕</button>
|
|
</span>
|
|
</li>`).join("");
|
|
$("go").disabled = !files.length;
|
|
}
|
|
|
|
function showError(msg) { $("error").textContent = msg; $("error").hidden = !msg; }
|
|
function setStatus(text, value) {
|
|
$("progress").hidden = false;
|
|
$("status").textContent = text;
|
|
if (value !== undefined) $("bar").value = value;
|
|
}
|
|
|
|
async function waitForTranscription(id) {
|
|
for (;;) {
|
|
const meta = await api(`plays/${id}`);
|
|
if (meta.progress) setStatus(`Přepisuji stranu ${Math.min(meta.progress.done + 1, meta.progress.total)} z ${meta.progress.total}…`, meta.progress.done / meta.progress.total);
|
|
if (meta.status !== "processing") return meta;
|
|
await new Promise((r) => setTimeout(r, 2000));
|
|
}
|
|
}
|
|
|
|
async function submit(e) {
|
|
e.preventDefault();
|
|
showError("");
|
|
if (!files.length) return;
|
|
$("go").disabled = true;
|
|
$("form").classList.add("busy");
|
|
try {
|
|
const meta = await withLogin(() => api("plays", { method: "POST", body: { title: $("title").value, note: $("note").value } }));
|
|
const id = encodeURIComponent(meta.id);
|
|
for (let i = 0; i < files.length; i++) {
|
|
setStatus(`Nahrávám stranu ${i + 1} z ${files.length}…`, i / files.length);
|
|
await api(`plays/${id}/images/${i + 1}`, { method: "PUT", body: await toJpeg(files[i].file), type: "image/jpeg" });
|
|
}
|
|
if (cfg.transcription) {
|
|
setStatus("Začínám přepis…", 0);
|
|
await api(`plays/${id}/transcribe`, { method: "POST" });
|
|
// leaving the page is fine: the server keeps going and the editor shows progress
|
|
window.removeEventListener("beforeunload", warn);
|
|
await waitForTranscription(id);
|
|
}
|
|
location.href = `edit.html?id=${id}`;
|
|
} catch (err) {
|
|
showError(err.message);
|
|
$("go").disabled = false;
|
|
} finally {
|
|
$("form").classList.remove("busy");
|
|
}
|
|
}
|
|
|
|
const warn = (e) => { if (files.length && !$("progress").hidden) e.preventDefault(); };
|
|
|
|
async function main() {
|
|
cfg = await api("config");
|
|
if (!cfg.uploads) {
|
|
$("disabled").textContent = "Přidávání her je na tomto serveru vypnuté (správce nenastavil UPLOAD_PASSWORD).";
|
|
$("disabled").hidden = false;
|
|
$("form").hidden = true;
|
|
return;
|
|
}
|
|
if (!cfg.transcription) {
|
|
$("ai-note").textContent = "Automatický přepis není na serveru zapnutý (chybí klíč OPENROUTER_API_KEY nebo ANTHROPIC_API_KEY). Fotky se nahrají a text pak napíšete v editoru ručně.";
|
|
$("go").textContent = "Nahrát";
|
|
}
|
|
await authButton($("auth"), { uploads: true, onChange: (inn) => { if (!inn) location.replace("./"); } });
|
|
if (!getPassword() && !(await askPassword("Přidávat hry může jen ten, kdo zná heslo."))) { location.replace("./"); return; }
|
|
|
|
$("files").addEventListener("change", (e) => { addFiles(e.target.files); e.target.value = ""; });
|
|
const drop = $("drop");
|
|
drop.addEventListener("dragover", (e) => { e.preventDefault(); drop.classList.add("over"); });
|
|
drop.addEventListener("dragleave", () => drop.classList.remove("over"));
|
|
drop.addEventListener("drop", (e) => { e.preventDefault(); drop.classList.remove("over"); addFiles(e.dataTransfer.files); });
|
|
$("thumbs").addEventListener("click", (e) => {
|
|
const b = e.target.closest("button");
|
|
if (!b) return;
|
|
const i = Number(b.dataset.i);
|
|
if (b.hasAttribute("data-remove")) URL.revokeObjectURL(files.splice(i, 1)[0].url);
|
|
else {
|
|
const j = i + Number(b.dataset.move);
|
|
[files[i], files[j]] = [files[j], files[i]];
|
|
}
|
|
renderThumbs();
|
|
});
|
|
$("form").addEventListener("submit", submit);
|
|
window.addEventListener("beforeunload", warn);
|
|
renderThumbs();
|
|
}
|
|
|
|
main().catch((err) => showError(err.message));
|