Add scheduled and event-driven printing with a new web UI
CI / test (3.10) (push) Successful in 1m20s
CI / test (3.12) (push) Successful in 1m49s
CI / test (3.13) (push) Successful in 1m5s

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]>
This commit is contained in:
2026-10-23 23:58:26 +01:00
co-authored by Claude Opus 5.5
parent fa99c3076c
commit ba92f1fbda
29 changed files with 5433 additions and 294 deletions
+154
View File
@@ -0,0 +1,154 @@
// Small DOM and formatting helpers shared by the tabs.
export const $ = (selector, root = document) => root.querySelector(selector);
// h("button", {class: "x", onclick: fn}, "text", child) -> element
export function h(tag, props, ...children) {
const el = document.createElement(tag);
for (const [key, value] of Object.entries(props || {})) {
if (value === null || value === undefined || value === false) continue;
if (key.startsWith("on")) el.addEventListener(key.slice(2), value);
else if (key === "class") el.className = value;
else if (key === "dataset") Object.assign(el.dataset, value);
else if (key === "style") el.style.cssText = value;
else if (key === "value") el.value = value; // a textarea has no value attribute
else if (key in el && typeof value !== "string") el[key] = value;
else el.setAttribute(key, value === true ? "" : value);
}
append(el, children);
return el;
}
function append(el, children) {
for (const child of children.flat(Infinity)) {
if (child === null || child === undefined || child === false) continue;
el.append(child instanceof Node ? child : String(child));
}
}
export function clear(el, ...children) {
el.replaceChildren();
append(el, children);
return el;
}
let toastTimer = null;
export function toast(text, kind = "") {
const el = $("#toast");
el.textContent = text;
el.className = `show ${kind}`;
clearTimeout(toastTimer);
toastTimer = setTimeout(() => { el.className = ""; }, kind === "err" ? 6000 : 3000);
}
export function debounce(fn, ms) {
let timer = null;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), ms);
};
}
export const DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
export const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec"];
export function fmtTime(iso) {
if (!iso) return "";
const d = new Date(iso);
const today = new Date();
const time = d.toLocaleTimeString([], {hour: "2-digit", minute: "2-digit"});
if (d.toDateString() === today.toDateString()) return `Today ${time}`;
const tomorrow = new Date(today); tomorrow.setDate(today.getDate() + 1);
if (d.toDateString() === tomorrow.toDateString()) return `Tomorrow ${time}`;
const opts = {weekday: "short", day: "numeric", month: "short"};
if (d.getFullYear() !== today.getFullYear()) opts.year = "numeric";
return `${d.toLocaleDateString([], opts)} ${time}`;
}
export function relative(iso) {
if (!iso) return "";
const minutes = Math.round((new Date(iso) - Date.now()) / 60000);
const abs = Math.abs(minutes);
let text;
if (abs < 1) return "now";
if (abs < 60) text = `${abs} min`;
else if (abs < 60 * 36) text = `${Math.round(abs / 60)} h`;
else text = `${Math.round(abs / 1440)} days`;
return minutes > 0 ? `in ${text}` : `${text} ago`;
}
// A <label> wrapping a control, with the caption above it.
export function labelled(caption, control, hint) {
return h("label", {class: "field"}, h("span", {class: "caption"}, caption), control,
hint ? h("small", {class: "hint"}, hint) : null);
}
// A checkbox bound to obj[key].
export function checkbox(obj, key, caption, onchange) {
const input = h("input", {type: "checkbox", checked: !!obj[key], onchange: () => {
obj[key] = input.checked; onchange && onchange();
}});
return h("label", {class: "check"}, input, " ", caption);
}
// Text / number / time / date input bound to obj[key].
export function input(obj, key, props = {}, onchange) {
const type = props.type || "text";
const parse = props.parse || (type === "number"
? (v) => (v === "" ? null : Number(v)) : (v) => v);
const el = h(props.multiline ? "textarea" : "input", {
...props, type: props.multiline ? null : type, multiline: null, parse: null,
value: obj[key] ?? "",
oninput: () => { obj[key] = parse(el.value); onchange && onchange(); },
});
return el;
}
// <select> bound to obj[key]; options are [value, label] pairs.
export function select(obj, key, options, onchange, parse = (v) => v) {
const el = h("select", {onchange: () => { obj[key] = parse(el.value); onchange && onchange(); }},
options.map(([value, label]) => h("option", {value: String(value)}, label)));
el.value = String(obj[key] ?? "");
return el;
}
// Toggle chips for a list of numbers (weekdays).
export function chips(list, labels, onchange) {
return h("div", {class: "chips"}, labels.map((label, i) => {
const chip = h("button", {type: "button", class: list.includes(i) ? "chip on" : "chip",
"aria-pressed": String(list.includes(i)),
onclick: () => {
const at = list.indexOf(i);
if (at >= 0) list.splice(at, 1); else { list.push(i); list.sort((a, b) => a - b); }
chip.classList.toggle("on", at < 0);
chip.setAttribute("aria-pressed", String(at < 0));
onchange && onchange();
}}, label);
return chip;
}));
}
// Segmented control: one of options [value, label].
export function segmented(current, options, onpick) {
return h("div", {class: "segmented", role: "tablist"}, options.map(([value, label]) =>
h("button", {type: "button", role: "tab", class: value === current ? "on" : "",
"aria-selected": String(value === current),
onclick: () => onpick(value)}, label)));
}
// Insert text at the cursor of an input/textarea and fire its input event.
export function insertAtCursor(el, text) {
const start = el.selectionStart ?? el.value.length;
const end = el.selectionEnd ?? el.value.length;
el.value = el.value.slice(0, start) + text + el.value.slice(end);
el.selectionStart = el.selectionEnd = start + text.length;
el.focus();
el.dispatchEvent(new Event("input"));
}
export function localDateTimeValue(date) {
const pad = (n) => String(n).padStart(2, "0");
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T` +
`${pad(date.getHours())}:${pad(date.getMinutes())}`;
}