Files
RuneStone/src/lib/formulas/tools.ts
T
Knotty Wyvern 5e1533c371 Make Table Formulas a built-in feature
The formula engine, the rendered results, and the formula help now
come from src/lib/formulas/. The editor and the renderer call this
module directly. The tableFormulas contribution type is removed.

- Add the tableFormulas general setting (default on) and a switch in
  Settings > General. When it is off, formulas show as source text.
- Do not load an installed runestone.table-formulas package. Settings
  > Plug-ins marks it as included with RuneStone and tells the user
  that it can be removed. The user's files stay in place.
- Translate the formula help and the error descriptions into French,
  Japanese, and Simplified Chinese.
- Port the plugin unit tests. Test the setting and use a test-only
  contribution for the generic plugin lifecycle tests.
- Smoke tests load only Terminal. The formula and lifecycle tests use
  the built-in feature and its setting.
- Remove scripts/buildPlugins.mjs, the build:plugins script, and the
  plugin-packages requirement of the smoke runner.
- Remove RuneStone_Table_Formulas from the menu audit scope. It had no
  menu surfaces.
- Update the README, the user guides, and the developer references.
2026-10-28 01:05:43 -07:00

49 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { tr } from "../i18n/index.ts";
import { actionIcon } from "../ui/actionIcons.ts";
/** The formula help, status line, and error hint below a table in Writer. */
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(tr("Formulas")));
summary.title = tr("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 = tr("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)", tr("Average two values: 9 and 10 give 9.5.")],
["=AVERAGE(B2:C2)", tr("Average every cell in the range B2 through C2.")],
["=SUM(B2:B10)", tr("Add the values in a range.")],
["=(B2+C2)/2", tr("Add two values and divide by two.")],
["=ROUND(B2/3,2)", tr("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 = tr("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 = tr("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 = tr("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 };
}