Release RuneStone 0.1.4 with installable plugins and settings
This commit is contained in:
@@ -11,8 +11,7 @@ import { tableSelectionControls } from "./tableSelectionControls";
|
||||
import { tableDragControls } from "./tableDragControls";
|
||||
import type { TableContextActions } from "./tableCellActions";
|
||||
import { actionIcon } from "../ui/actionIcons";
|
||||
import { cellAddress, evaluateTableFormulas, formulaErrors, isTableFormula } from "./tableFormulas";
|
||||
import { tableFormulaHtml } from "../workspace/noteHtml";
|
||||
import { pluginHost } from "../plugins/registry.ts";
|
||||
|
||||
const cellFormats = ["math", "comment"].map(name => Mark.create({
|
||||
name,
|
||||
@@ -36,6 +35,7 @@ const CellImage = Image.extend({
|
||||
export const tableEditors = new WeakMap<HTMLElement, { source: string; enter: (forward: boolean, x?: number) => void; destroy: () => void }>();
|
||||
|
||||
export function tableControls(root: HTMLElement, view: EditorView, initialSource: string, actions: TableContextActions = {}) {
|
||||
const formulas = pluginHost.get("tableFormulas");
|
||||
const scroll = root.querySelector<HTMLElement>(".md-table-scroll");
|
||||
const table = scroll?.querySelector("table");
|
||||
if (!scroll || !table || !parseMarkdownTable(initialSource)) return;
|
||||
@@ -76,17 +76,17 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
|
||||
|
||||
function refreshFormulas() {
|
||||
const parsed = parseMarkdownTable(controller.source);
|
||||
if (!parsed) return;
|
||||
if (!parsed || !formulas) return;
|
||||
const grid = parsed.rows.filter((_, index) => index !== 1).map(row => Array.from({ length: parsed.columns }, (_, c) => row.cells[c]?.text || ""));
|
||||
const results = evaluateTableFormulas(grid);
|
||||
const results = formulas.evaluateTableFormulas(grid);
|
||||
for (const row of table!.rows) for (const cell of row.cells) {
|
||||
const result = results[row.rowIndex]?.[cell.cellIndex];
|
||||
const address = cellAddress(row.rowIndex, cell.cellIndex);
|
||||
const address = formulas.cellAddress(row.rowIndex, cell.cellIndex);
|
||||
cell.title = `${address} · ${result?.formula || "Click to edit · Type = for a formula"}`;
|
||||
if (result && cell !== active) cell.innerHTML = tableFormulaHtml(result);
|
||||
if (result && cell !== active) cell.innerHTML = formulas.tableFormulaHtml(result);
|
||||
if (cell === active) {
|
||||
formulaStatus.textContent = `${address}${result ? ` = ${result.text}` : " · Type = to calculate"}`;
|
||||
formulaHint.textContent = result?.error ? formulaErrors[result.error] : "";
|
||||
formulaHint.textContent = result?.error ? formulas.formulaErrors[result.error] : "";
|
||||
formulaHint.hidden = !formulaHint.textContent;
|
||||
}
|
||||
}
|
||||
@@ -156,9 +156,9 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
|
||||
// leave its previous active line so its Markdown markers render again.
|
||||
view.dispatch({ selection: { anchor: view.posAtDOM(root) + (originalCell?.from ?? 0) }, userEvent: "select.table" });
|
||||
let contentFrom = originalCell?.from, contentTo = originalCell?.to;
|
||||
const formula = originalCell && (isTableFormula(originalCell.text) || /^\\=(?!=)/.test(originalCell.text)) ? originalCell.text : undefined;
|
||||
const formula = originalCell && (/^=(?!=)/.test(originalCell.text.trim()) || /^\\=(?!=)/.test(originalCell.text)) ? originalCell.text : undefined;
|
||||
const html = cell.innerHTML;
|
||||
formulaStatus.textContent = `${cellAddress(row, column)} · Type = to calculate`;
|
||||
formulaStatus.textContent = formulas ? `${formulas.cellAddress(row, column)} · Type = to calculate` : "";
|
||||
cell.innerHTML = "";
|
||||
cell.classList.add("is-editing");
|
||||
editor = new Editor({
|
||||
@@ -239,7 +239,7 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
|
||||
if (event.key === "Escape") { event.preventDefault(); leave(true); return true; }
|
||||
if (event.key === "Enter") {
|
||||
event.preventDefault();
|
||||
if (isTableFormula(editor?.getText() || "")) { finish(); wrapper.focus(); }
|
||||
if (/^=(?!=)/.test((editor?.getText() || "").trim())) { finish(); wrapper.focus(); }
|
||||
else if (event.ctrlKey || event.metaKey) leave(true);
|
||||
else editor?.commands.setHardBreak();
|
||||
return true;
|
||||
@@ -301,48 +301,11 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
|
||||
help.textContent = "Drag grips: move rows/columns · Drag cells: select · Right-click: actions · Arrows: move through cells · Tab: next cell · Esc: exit table";
|
||||
const footer = document.createElement("div");
|
||||
footer.className = "table-footer";
|
||||
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", () => view.requestMeasure());
|
||||
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);
|
||||
footer.append(help, formulaTools, formulaHint);
|
||||
const tools = formulas?.createFormulaTools(finish, () => view.requestMeasure());
|
||||
const formulaStatus = tools?.formulaStatus ?? document.createElement("span");
|
||||
const formulaHint = tools?.formulaHint ?? document.createElement("div");
|
||||
footer.append(help);
|
||||
if (tools) footer.append(tools.formulaTools, formulaHint);
|
||||
wrapper.after(footer);
|
||||
for (const kind of ["column", "row"] as const) {
|
||||
const button = document.createElement("button");
|
||||
@@ -365,7 +328,7 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
|
||||
}
|
||||
for (const row of table.rows) for (const cell of row.cells) {
|
||||
cell.tabIndex = 0;
|
||||
cell.setAttribute("aria-label", `${cellAddress(row.rowIndex, cell.cellIndex)} · Row ${row.rowIndex + 1}, column ${cell.cellIndex + 1}`);
|
||||
cell.setAttribute("aria-label", `${formulas ? formulas.cellAddress(row.rowIndex, cell.cellIndex) + " · " : ""}Row ${row.rowIndex + 1}, column ${cell.cellIndex + 1}`);
|
||||
cell.addEventListener("focus", () => activate(cell));
|
||||
}
|
||||
selectionControls = tableSelectionControls(root, wrapper, table, view, () => controller.source, finish, change, { get: () => editor, activate }, actions);
|
||||
|
||||
Reference in New Issue
Block a user