47 lines
2.9 KiB
TypeScript
47 lines
2.9 KiB
TypeScript
import { actionIcon } from "../../src/lib/ui/actionIcons.ts";
|
||
|
||
export function createFormulaTools(finish: () => void, measure: () => void) {
|
||
const formulaTools = document.createElement("div");
|
||
formulaTools.className = "table-formula-tools";
|
||
const formulaHelp = document.createElement("details");
|
||
formulaHelp.className = "table-formula-help";
|
||
const summary = document.createElement("summary");
|
||
summary.append(actionIcon("math"), document.createTextNode("Formulas"));
|
||
summary.title = "How to calculate with table cells";
|
||
// Committing a wrapped formula can shrink its row. Keep the click target in
|
||
// place until mouseup, then finish editing and give keyboard focus to help.
|
||
summary.addEventListener("mousedown", event => { if (event.button === 0) { event.preventDefault(); event.stopPropagation(); } });
|
||
summary.addEventListener("click", () => { finish(); summary.focus(); });
|
||
const instructions = document.createElement("div");
|
||
instructions.className = "table-formula-instructions";
|
||
const intro = document.createElement("p");
|
||
intro.textContent = "Start a cell with =, then press Enter or Tab. Columns are A, B, C…; the header is row 1 and the first data row is 2.";
|
||
const examples = document.createElement("dl");
|
||
for (const [formula, description] of [
|
||
["=AVERAGE(B2,C2)", "Average two values: 9 and 10 give 9.5."],
|
||
["=AVERAGE(B2:C2)", "Average every cell in the range B2 through C2."],
|
||
["=SUM(B2:B10)", "Add the values in a range."],
|
||
["=(B2+C2)/2", "Add two values and divide by two."],
|
||
["=ROUND(B2/3,2)", "Round to two decimal places."],
|
||
]) {
|
||
const term = document.createElement("dt"), code = document.createElement("code"), descriptionElement = document.createElement("dd");
|
||
code.textContent = formula; term.append(code); descriptionElement.textContent = description;
|
||
examples.append(term, descriptionElement);
|
||
}
|
||
const functions = document.createElement("p");
|
||
functions.textContent = "Functions: SUM, AVERAGE, MIN, MAX, COUNT, ABS, ROUND. Operators: + − * / ^ and %. Use commas between function arguments; use a colon for a cell range.";
|
||
const references = document.createElement("p");
|
||
references.textContent = "References stay at their written addresses when copying, inserting, deleting, sorting, or moving cells. Code-formatted expressions are literal text; formulas must start with = outside code formatting.";
|
||
instructions.append(intro, examples, functions, references);
|
||
formulaHelp.append(summary, instructions);
|
||
formulaHelp.addEventListener("toggle", measure);
|
||
const formulaStatus = document.createElement("span");
|
||
formulaStatus.className = "table-formula-status";
|
||
formulaStatus.textContent = "Type = in a cell to calculate";
|
||
const formulaHint = document.createElement("div");
|
||
formulaHint.className = "table-formula-hint";
|
||
formulaHint.hidden = true;
|
||
formulaTools.append(formulaHelp, formulaStatus);
|
||
return { formulaTools, formulaStatus, formulaHint };
|
||
}
|