The web server now runs a scheduler over schedules kept in SQLite
(~/.local/share/receipt-maker, outside the clone). A schedule is a
trigger, a receipt made of blocks, and options:
- Triggers: once, recurring (rrule-based, DST-safe), sun times,
reminders before calendar (ICS) events, RSS/JSON watchers, webhooks
with per-schedule secrets.
- Blocks, rendered fresh as one job: templated notes ({date},
{days_until:..}, rotating {choice}, trigger vars), QR, saved images,
random image, calendar agenda, feed headlines, dividers.
- Rules: quiet hours (hold/skip/ignore), daily cap, pause, catch-up
window for missed runs (never replayed), retries on connect failure
only; a mid-job drop (new PrinterDropped) is never resent.
receipt.py renders through composable Blocks (render_note/image/qr are
wrappers, output unchanged). Print history persists, with Print again.
The page moves to web/ with Print (note/image/QR + paper preview),
Schedules (presets, editor with live preview and next runs), History
and Settings tabs.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
150 lines
7.1 KiB
JavaScript
150 lines
7.1 KiB
JavaScript
// Print tab: a note, an image or a QR code, right now, with a live preview.
|
|
|
|
import {call, post, printJob} from "./api.js";
|
|
import {h, clear, debounce, segmented, toast} from "./dom.js";
|
|
import {paper} from "./paper.js";
|
|
|
|
const draft = {mode: "note", text: "", timestamp: false, size: 2, caption: "", qr: "",
|
|
qrCaption: "", file: null};
|
|
|
|
export function printView(view) {
|
|
const preview = paper();
|
|
const status = h("p", {class: "status muted"}, "Ready.");
|
|
const formBox = h("div");
|
|
const modes = h("div");
|
|
|
|
function setStatus(text, cls = "muted") { status.textContent = text; status.className = `status ${cls}`; }
|
|
|
|
const refreshPreview = debounce(async () => {
|
|
if (draft.mode === "image") {
|
|
if (!draft.file) return preview.empty("Choose an image to see how it prints.");
|
|
const data = new FormData();
|
|
data.append("file", draft.file, draft.file.name || "pasted.png");
|
|
data.append("message", draft.caption);
|
|
const r = await call("api/preview/image", {method: "POST", body: data});
|
|
return r.ok ? preview.show(r.body) : preview.error(r.text);
|
|
}
|
|
let block;
|
|
if (draft.mode === "note") {
|
|
if (!draft.text.trim()) return preview.empty("Type a note to see how it prints.");
|
|
block = {type: "note", text: draft.text, size: draft.size, timestamp: draft.timestamp};
|
|
} else {
|
|
if (!draft.qr.trim()) return preview.empty("Enter text or a link to encode.");
|
|
block = {type: "qr", content: draft.qr, caption: draft.qrCaption};
|
|
}
|
|
// Plain prints have no template variables: {{ }} keeps braces literal in the preview.
|
|
if (block.type === "note") block.text = block.text.replaceAll("{", "{{").replaceAll("}", "}}");
|
|
if (block.type === "qr") {
|
|
block.content = block.content.replaceAll("{", "{{").replaceAll("}", "}}");
|
|
block.caption = block.caption.replaceAll("{", "{{").replaceAll("}", "}}");
|
|
}
|
|
const r = await post("api/preview", {blocks: [block]});
|
|
r.ok ? preview.show(r.body) : preview.error(r.text);
|
|
}, 350);
|
|
|
|
async function send(request, after) {
|
|
const buttons = view.querySelectorAll("button.primary");
|
|
buttons.forEach((b) => (b.disabled = true));
|
|
const result = await printJob(request, (text) => setStatus(text));
|
|
buttons.forEach((b) => (b.disabled = false));
|
|
setStatus(result.text, result.ok ? "ok" : "err");
|
|
if (result.ok) { toast("Printed."); after && after(); }
|
|
}
|
|
|
|
function noteForm() {
|
|
const text = h("textarea", {maxlength: 4000, placeholder: "Your note", value: draft.text,
|
|
oninput: () => { draft.text = text.value; refreshPreview(); }});
|
|
const size = h("select", {onchange: () => { draft.size = Number(size.value); refreshPreview(); }},
|
|
h("option", {value: "1"}, "Normal (48 per line)"),
|
|
h("option", {value: "2"}, "Large (24 per line)"),
|
|
h("option", {value: "3"}, "Huge (16 per line)"));
|
|
size.value = String(draft.size);
|
|
const stamp = h("input", {type: "checkbox", checked: draft.timestamp,
|
|
onchange: () => { draft.timestamp = stamp.checked; refreshPreview(); }});
|
|
return h("form", {onsubmit: (e) => {
|
|
e.preventDefault();
|
|
if (!draft.text.trim()) return setStatus("Type something first.", "err");
|
|
send(() => post("api/note", {text: draft.text, timestamp: draft.timestamp, size: draft.size}),
|
|
() => { draft.text = ""; text.value = ""; refreshPreview(); });
|
|
}},
|
|
text,
|
|
h("div", {class: "row"},
|
|
h("label", {class: "check"}, stamp, " Date/time header"),
|
|
h("label", {class: "inline"}, "Size ", size),
|
|
h("button", {type: "submit", class: "primary"}, "Print note")));
|
|
}
|
|
|
|
function imageForm() {
|
|
const fileInput = h("input", {type: "file", accept: "image/*", hidden: true,
|
|
onchange: () => pick(fileInput.files[0])});
|
|
const drop = h("label", {class: "drop"}, draft.file ? draft.file.name || "Pasted image"
|
|
: "Choose, drop or paste an image");
|
|
drop.append(fileInput);
|
|
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"); pick(e.dataTransfer.files[0]);
|
|
});
|
|
function pick(file) {
|
|
if (!file || !file.type.startsWith("image/")) return;
|
|
draft.file = file;
|
|
drop.firstChild.textContent = file.name || "Pasted image";
|
|
refreshPreview();
|
|
}
|
|
view._pick = pick;
|
|
const caption = h("input", {type: "text", maxlength: 500, placeholder: "Caption (optional)",
|
|
value: draft.caption, oninput: () => { draft.caption = caption.value; refreshPreview(); }});
|
|
return h("form", {onsubmit: (e) => {
|
|
e.preventDefault();
|
|
if (!draft.file) return setStatus("Choose an image first.", "err");
|
|
const data = new FormData();
|
|
data.append("file", draft.file, draft.file.name || "pasted.png");
|
|
data.append("message", draft.caption);
|
|
send(() => call("api/image", {method: "POST", body: data}));
|
|
}},
|
|
drop, caption,
|
|
h("div", {class: "row"},
|
|
h("span", {class: "muted"}, "Max 16 MB. Printed full width, dithered."),
|
|
h("button", {type: "submit", class: "primary"}, "Print image")));
|
|
}
|
|
|
|
function qrForm() {
|
|
const content = h("input", {type: "text", maxlength: 2000, placeholder: "https://example.com",
|
|
value: draft.qr, oninput: () => { draft.qr = content.value; refreshPreview(); }});
|
|
const caption = h("input", {type: "text", maxlength: 500, placeholder: "Caption (optional)",
|
|
value: draft.qrCaption,
|
|
oninput: () => { draft.qrCaption = caption.value; refreshPreview(); }});
|
|
return h("form", {onsubmit: (e) => {
|
|
e.preventDefault();
|
|
if (!draft.qr.trim()) return setStatus("Enter something to encode.", "err");
|
|
send(() => post("api/qr", {content: draft.qr, caption: draft.qrCaption}));
|
|
}},
|
|
h("label", {class: "field"}, h("span", {class: "caption"}, "Text or link"), content),
|
|
h("label", {class: "field"}, h("span", {class: "caption"}, "Caption"), caption),
|
|
h("div", {class: "row end"}, h("button", {type: "submit", class: "primary"}, "Print QR code")));
|
|
}
|
|
|
|
function render() {
|
|
clear(modes, segmented(draft.mode, [["note", "Note"], ["image", "Image"], ["qr", "QR code"]],
|
|
(mode) => { draft.mode = mode; render(); }));
|
|
clear(formBox, {note: noteForm, image: imageForm, qr: qrForm}[draft.mode]());
|
|
refreshPreview();
|
|
}
|
|
|
|
// Pasting an image anywhere on this tab picks it.
|
|
const onPaste = (e) => {
|
|
const item = [...(e.clipboardData?.items || [])].find((i) => i.type.startsWith("image/"));
|
|
if (!item) return;
|
|
if (draft.mode !== "image") { draft.mode = "image"; render(); }
|
|
view._pick(item.getAsFile());
|
|
};
|
|
document.addEventListener("paste", onPaste);
|
|
|
|
clear(view,
|
|
h("div", {class: "split"},
|
|
h("section", {class: "card"}, h("h2", {}, "Print now"), modes, formBox, status),
|
|
h("aside", {class: "preview-col"}, h("h3", {class: "muted small"}, "Preview"), preview.root)));
|
|
render();
|
|
return () => document.removeEventListener("paste", onPaste);
|
|
}
|