Something went wrong. Try again.
Convert CSV banking statements to hledger journals
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181// One rule's fields (M1-UI-008): a name, a condition, and the accounts it// sets. There is no control here that edits the rules file as text, which is// what M1-UI-008 rules out.//// The rule goes in as the `rule` property and comes back out as events, each// carrying the rule as the fields now say it://// rule-edit on every keystroke// rule-commit when a changed field is left// rule-delete when the delete button is pressed// rule-open when the fields are opened//// The element keeps its own copy, so it never changes the draft it was given.// What an edit means for that draft is the page's business.//// The fields fold under one line (§6.5 of DESIGN_M1.md). Folded, it shows the// name, the target account and the condition; open, only the name, since the// fields below it say the rest. Either way it follows the fields as they are// typed. Which rule is open is the page's business too: it hears `rule-open`// and calls `fold()` on the others, and `edit()` opens a rule to be typed in.//// Tab completes a field alias in the condition (§6.4 of DESIGN_M1.md). The// page supplies the `complete` property, a function from the condition's text// up to the cursor to the server's answer, so this element never fetches.//// `extend(clauses)` opens the rule and adds the clauses of a row's selected// cells to the end of its condition (D12, §6.7 of DESIGN_M1.md).
import { appended } from "./clause.js";import { beforeCursor, Offer } from "./completion.js";import { FIELDS, lineOf, shown, valueOf } from "./rule.js";
class RuleEditor extends HTMLElement { #rule = null; #inputs = new Map(); #line = null; #details = document.createElement("details"); // The last completion the server gave, with the text it was for. #offer = null; // Whether text was inserted that no `change` has reported yet. #unreported = false; complete = null;
constructor() { super(); const sheet = document.createElement("link"); sheet.rel = "stylesheet"; sheet.href = "/rule-editor.css";
const box = document.createElement("div"); box.className = "rule";
const remove = document.createElement("button"); remove.type = "button"; remove.textContent = "delete"; remove.addEventListener("click", () => this.#emit("rule-delete"));
box.append(...FIELDS.map((field) => this.#field(field)), remove);
this.#details.append(this.#summary(), box); this.#details.addEventListener("toggle", () => { if (this.#details.open) this.#emit("rule-open"); }); this.attachShadow({ mode: "open" }).append(sheet, this.#details); }
fold() { this.#details.open = false; }
edit() { this.#details.open = true; this.#inputs.get("name").focus(); }
// The added text is left selected, so it shows what was added, one Delete // takes it back, and the left arrow goes to its start. extend(clauses) { this.#details.open = true; const input = this.#inputs.get("match"); input.focus(); const end = input.value.length; input.setRangeText(appended(clauses, input.value), end, end, "select"); // As when Tab completes: say it was typed, and commit it on leaving. this.#unreported = true; input.dispatchEvent(new Event("input")); }
set rule(rule) { this.#rule = { ...rule }; for (const [key, input] of this.#inputs) { input.value = shown(rule[key]); } this.#describe(); }
#summary() { const summary = document.createElement("summary"); const span = (className) => { const part = document.createElement("span"); part.className = className; summary.append(part); return part; }; this.#line = { name: span("name"), target: span("target"), match: span("match") }; return summary; }
#describe() { for (const [part, text] of Object.entries(lineOf(this.#rule))) { this.#line[part].textContent = text; } }
#field(field) { const wrap = document.createElement("label"); const caption = document.createElement("span"); caption.textContent = field.label;
const input = document.createElement(field.multiline ? "textarea" : "input"); if (field.multiline) input.rows = 2; input.addEventListener("input", () => { this.#rule[field.key] = valueOf(field, input.value); this.#describe(); this.#emit("rule-edit"); }); input.addEventListener("change", () => this.#emit("rule-commit")); this.#inputs.set(field.key, input);
wrap.className = "field"; wrap.append(caption, input); if (field.completes) wrap.append(this.#completion(input)); return wrap; }
// Asks ahead of Tab and completes on it (completion.js). #completion(input) { const hint = document.createElement("span"); hint.className = "hint"; hint.hidden = true;
const ask = async () => { if (!this.complete) return; const asked = beforeCursor(input); const offer = new Offer(asked, await this.complete(asked)); if (!offer.isFor(input)) return; this.#offer = offer; hint.textContent = offer.hint; hint.hidden = offer.candidates.length === 0 || this.shadowRoot.activeElement !== input; }; for (const type of ["input", "keyup", "mouseup"]) input.addEventListener(type, ask); // A browser may not count inserted text as the user's change, and then // leaving the field would not commit it. `change` fires before `blur`, // so this commits only what `change` did not. input.addEventListener("change", () => { this.#unreported = false; }); input.addEventListener("blur", () => { hint.hidden = true; if (this.#unreported) this.#emit("rule-commit"); this.#unreported = false; });
input.addEventListener("keydown", (event) => { const offer = this.#offer; if (offer === null || !offer.takesTab(event, input)) return; event.preventDefault(); input.setRangeText(offer.insert, input.selectionStart, input.selectionEnd, "end"); if (offer.insert !== "") this.#unreported = true; // setRangeText is not typing, so say it was: the rule and the offer // both follow the new text. input.dispatchEvent(new Event("input")); }); return hint; }
#emit(type) { this.dispatchEvent(new CustomEvent(type, { detail: { ...this.#rule } })); }}
customElements.define("rule-editor", RuleEditor);