Add scheduled and event-driven printing with a new web UI
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:
+173
@@ -0,0 +1,173 @@
|
||||
// Settings tab: time zone and location, quiet hours, limits, pause, saved sources and images.
|
||||
|
||||
import {assets, call, del, post, put, settings, sources} from "./api.js";
|
||||
import {checkbox, clear, h, input, labelled, select, toast} from "./dom.js";
|
||||
|
||||
export async function settingsView(view) {
|
||||
const conf = await settings(true);
|
||||
const s = structuredClone(conf.settings);
|
||||
const quiet = {on: !!s.quiet_start, start: (s.quiet_start || "22:00").slice(0, 5),
|
||||
end: (s.quiet_end || "07:00").slice(0, 5)};
|
||||
const loc = {latitude: s.location?.latitude ?? "", longitude: s.location?.longitude ?? "",
|
||||
name: s.location?.name ?? ""};
|
||||
const sourcesBox = h("div");
|
||||
const assetsBox = h("div");
|
||||
|
||||
async function save() {
|
||||
const body = {
|
||||
timezone: s.timezone,
|
||||
location: loc.latitude === "" || loc.longitude === "" || loc.latitude === null
|
||||
? null : {latitude: Number(loc.latitude), longitude: Number(loc.longitude),
|
||||
name: loc.name || ""},
|
||||
quiet_start: quiet.on ? quiet.start : null,
|
||||
quiet_end: quiet.on ? quiet.end : null,
|
||||
daily_cap: Number(s.daily_cap) || 0,
|
||||
paused: !!s.paused,
|
||||
paused_until: s.paused && s.paused_until ? s.paused_until : null,
|
||||
};
|
||||
const r = await put("api/settings", body);
|
||||
if (!r.ok) return toast(r.text, "err");
|
||||
await settings(true);
|
||||
toast("Settings saved.");
|
||||
}
|
||||
|
||||
const latInput = input(loc, "latitude", {type: "number", step: "any", class: "narrow",
|
||||
placeholder: "51.5"});
|
||||
const lonInput = input(loc, "longitude", {type: "number", step: "any", class: "narrow",
|
||||
placeholder: "-0.12"});
|
||||
const locate = h("button", {type: "button", class: "ghost small", onclick: () => {
|
||||
if (!navigator.geolocation) return toast("This browser can't share its location.", "err");
|
||||
locate.textContent = "Locating…";
|
||||
navigator.geolocation.getCurrentPosition((pos) => {
|
||||
loc.latitude = Number(pos.coords.latitude.toFixed(4));
|
||||
loc.longitude = Number(pos.coords.longitude.toFixed(4));
|
||||
latInput.value = loc.latitude; lonInput.value = loc.longitude;
|
||||
locate.textContent = "Use my location";
|
||||
}, (err) => { locate.textContent = "Use my location"; toast(err.message, "err"); });
|
||||
}}, "Use my location");
|
||||
|
||||
const tzList = h("datalist", {id: "tz-list"}, conf.timezones.map((z) => h("option", {value: z})));
|
||||
const pauseUntil = labelled("Until (optional)", input(s, "paused_until", {type: "date",
|
||||
parse: (v) => v || null}), "Printing resumes at the start of that day.");
|
||||
|
||||
clear(view,
|
||||
h("div", {class: "toolbar"}, h("h2", {}, "Settings"), h("span", {class: "spacer"}),
|
||||
h("button", {class: "primary", onclick: save}, "Save settings")),
|
||||
h("div", {class: "grid2"},
|
||||
h("section", {class: "card"},
|
||||
h("h3", {}, "Time and place"),
|
||||
labelled("Time zone", input(s, "timezone", {list: "tz-list"}), null), tzList,
|
||||
labelled("Location (for sunrise and sunset)",
|
||||
h("div", {class: "row start"}, latInput, lonInput, locate)),
|
||||
labelled("Place name", input(loc, "name", {placeholder: "(optional)"}))),
|
||||
h("section", {class: "card"},
|
||||
h("h3", {}, "Automatic printing"),
|
||||
checkbox(quiet, "on", "Quiet hours"),
|
||||
h("div", {class: "row start indent"},
|
||||
input(quiet, "start", {type: "time"}), h("span", {}, "to"),
|
||||
input(quiet, "end", {type: "time"})),
|
||||
h("p", {class: "muted small"}, "Each schedule chooses whether to wait, skip or print anyway."),
|
||||
labelled("Most automatic prints per day", input(s, "daily_cap", {type: "number", min: 0,
|
||||
max: 1000, class: "narrow"}), "0 for no limit. Prints by hand don't count."),
|
||||
checkbox(s, "paused", "Pause all schedules (holiday mode)"),
|
||||
pauseUntil)),
|
||||
h("section", {class: "card"},
|
||||
h("h3", {}, "Calendars and feeds"),
|
||||
h("p", {class: "muted small"},
|
||||
"Saved once, used by agenda and headline blocks and calendar reminders. For Google " +
|
||||
"Calendar, use the calendar's secret address in iCal format."),
|
||||
sourcesBox),
|
||||
h("section", {class: "card"}, h("h3", {}, "Images for schedules"), assetsBox));
|
||||
|
||||
renderSources(sourcesBox);
|
||||
renderAssets(assetsBox);
|
||||
}
|
||||
|
||||
function mask(url) {
|
||||
try {
|
||||
const u = new URL(url);
|
||||
return `${u.protocol}//${u.host}${u.pathname.length > 1 ? "/…" : ""}`;
|
||||
} catch (e) { return url; }
|
||||
}
|
||||
|
||||
async function renderSources(box) {
|
||||
const list = await sources(true);
|
||||
const draft = {name: "", kind: "ics", url: ""};
|
||||
const result = h("span", {class: "small muted"});
|
||||
|
||||
async function test(body, out) {
|
||||
out.textContent = "Checking…";
|
||||
const r = await post("api/sources/test", body);
|
||||
out.textContent = r.text;
|
||||
out.className = `small ${r.ok ? "ok" : "err"}`;
|
||||
}
|
||||
|
||||
function item(source) {
|
||||
const out = h("span", {class: "small muted"});
|
||||
return h("div", {class: "list-row"},
|
||||
h("span", {class: "badge"}, source.kind === "ics" ? "Calendar" : "Feed"),
|
||||
h("strong", {}, source.name),
|
||||
h("code", {class: "muted small", title: "The full address is hidden"}, mask(source.url)),
|
||||
h("span", {class: "spacer"}),
|
||||
out,
|
||||
h("button", {class: "ghost small", onclick: () => test(
|
||||
{name: source.name, kind: source.kind, url: source.url}, out)}, "Test"),
|
||||
h("button", {class: "ghost small", onclick: async () => {
|
||||
const url = prompt(`New address for ${source.name}:`, "");
|
||||
if (!url) return;
|
||||
const r = await put(`api/sources/${source.id}`, {...source, url, id: undefined,
|
||||
created_at: undefined});
|
||||
if (!r.ok) return toast(r.text, "err");
|
||||
renderSources(box);
|
||||
}}, "Change URL"),
|
||||
h("button", {class: "ghost small danger", onclick: async () => {
|
||||
if (!confirm(`Delete ${source.name}?`)) return;
|
||||
const r = await del(`api/sources/${source.id}`);
|
||||
if (!r.ok) return toast(r.text, "err");
|
||||
renderSources(box);
|
||||
}}, "Delete"));
|
||||
}
|
||||
|
||||
clear(box,
|
||||
list.length ? list.map(item) : h("p", {class: "muted"}, "None yet."),
|
||||
h("form", {class: "add-source", onsubmit: async (e) => {
|
||||
e.preventDefault();
|
||||
const r = await post("api/sources", draft);
|
||||
if (!r.ok) return toast(r.text, "err");
|
||||
toast(`Added ${draft.name}.`);
|
||||
renderSources(box);
|
||||
}},
|
||||
h("div", {class: "row start"},
|
||||
select(draft, "kind", [["ics", "Calendar (ICS)"], ["rss", "Feed (RSS/Atom)"]]),
|
||||
input(draft, "name", {placeholder: "Name", required: true, maxlength: 100}),
|
||||
input(draft, "url", {placeholder: "https://… or webcal://…", required: true,
|
||||
class: "grow"})),
|
||||
h("div", {class: "row start"},
|
||||
h("button", {type: "button", class: "ghost", onclick: () => test(draft, result)}, "Test"),
|
||||
h("button", {type: "submit"}, "Add"), result)));
|
||||
}
|
||||
|
||||
async function renderAssets(box) {
|
||||
const list = await assets(true);
|
||||
clear(box,
|
||||
list.length ? list.map((a) => h("div", {class: "list-row"},
|
||||
h("strong", {}, a.name), h("span", {class: "muted small"}, `${Math.ceil(a.size / 1024)} KB`),
|
||||
h("span", {class: "spacer"}),
|
||||
h("button", {class: "ghost small danger", onclick: async () => {
|
||||
if (!confirm(`Delete ${a.name}?`)) return;
|
||||
const r = await del(`api/assets/${a.id}`);
|
||||
if (!r.ok) return toast(r.text, "err");
|
||||
renderAssets(box);
|
||||
}}, "Delete")))
|
||||
: h("p", {class: "muted"}, "None yet. Upload one from an image block in a schedule, or here."),
|
||||
h("label", {class: "button ghost"}, "Upload image…",
|
||||
h("input", {type: "file", accept: "image/*", hidden: true, onchange: async (e) => {
|
||||
const file = e.target.files[0];
|
||||
if (!file) return;
|
||||
const data = new FormData();
|
||||
data.append("file", file, file.name || "image.png");
|
||||
const r = await call("api/assets", {method: "POST", body: data});
|
||||
if (!r.ok) return toast(r.text, "err");
|
||||
renderAssets(box);
|
||||
}})));
|
||||
}
|
||||
Reference in New Issue
Block a user