Update editor, themes, menus, backgrounds, and release tooling

This commit is contained in:
Knotty Wyvern
2026-10-14 16:19:51 -07:00
parent f6eaa52949
commit edef2520af
113 changed files with 5722 additions and 386 deletions
+93 -10
View File
@@ -1,4 +1,6 @@
import { Editor, Mark } from "@tiptap/core";
import { Editor, Extension, Mark, getMarkRange } from "@tiptap/core";
import { Plugin } from "@tiptap/pm/state";
import { Decoration, DecorationSet } from "@tiptap/pm/view";
import Highlight from "@tiptap/extension-highlight";
import StarterKit from "@tiptap/starter-kit";
import Link from "@tiptap/extension-link";
@@ -13,6 +15,10 @@ import { tableDragControls } from "./tableDragControls";
import type { TableContextActions } from "./tableCellActions";
import { actionIcon } from "../ui/actionIcons";
import { pluginHost } from "../plugins/registry.ts";
import { attachTooltip } from "../ui/tooltip";
import { documentLinks } from "./documentLinks";
import { tableLinkLabelPosition } from "./tableLinkSource";
import { renderMarkdownHtml } from "../workspace/noteHtml";
const cellFormats = ["math", "comment"].map(name => Mark.create({
name,
@@ -46,10 +52,13 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
wrapper.append(scroll);
let editor: Editor | undefined;
let active: HTMLTableCellElement | undefined;
let sourceEditing = false;
let sourceImages = new Map<string, string>();
let firstEdit = true;
let selectionControls: ReturnType<typeof tableSelectionControls> | undefined;
let dragControls: ReturnType<typeof tableDragControls> | undefined;
const controller = { source: initialSource, enter, destroy: () => { dragControls?.destroy(); selectionControls?.destroy(); editor?.destroy(); editor = undefined; } };
const tooltipCleanup: (() => void)[] = [];
const controller = { source: initialSource, enter, destroy: () => { tooltipCleanup.forEach(dispose => dispose()); dragControls?.destroy(); selectionControls?.destroy(); editor?.destroy(); editor = undefined; } };
tableEditors.set(root, controller);
function enter(forward: boolean, x?: number) {
@@ -83,7 +92,8 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
for (const row of table!.rows) for (const cell of row.cells) {
const result = results[row.rowIndex]?.[cell.cellIndex];
const address = formulas.cellAddress(row.rowIndex, cell.cellIndex);
cell.title = `${address} · ${result?.formula || "Click to edit · Type = for a formula"}`;
cell.dataset.cellHint = `${address}${result?.formula ? ` · ${result.formula}` : ""}`;
cell.removeAttribute("title");
if (result && cell !== active) cell.innerHTML = formulas.tableFormulaHtml(result);
if (cell === active) {
formulaStatus.textContent = `${address}${result ? ` = ${result.text}` : " · Type = to calculate"}`;
@@ -115,10 +125,16 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
function finish() {
if (!editor || !active) return;
const cell = active;
const html = editor.getHTML();
let html = editor.getHTML();
if (sourceEditing) {
const rendered = document.createElement("div");
rendered.innerHTML = renderMarkdownHtml(controller.source, sourceImages);
html = rendered.querySelector("table")?.rows[(cell.parentElement as HTMLTableRowElement).rowIndex]?.cells[cell.cellIndex]?.innerHTML ?? html;
}
sourceEditing = false;
editor.destroy(); editor = undefined; active = undefined;
cell.innerHTML = html;
cell.classList.remove("is-editing");
cell.classList.remove("is-editing", "is-source-editing");
refreshFormulas();
formulaStatus.textContent = "Type = in a cell to calculate";
formulaHint.hidden = true;
@@ -165,6 +181,22 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
editor = new Editor({
element: cell,
extensions: [
Extension.create({
name: "cellLinkSource",
addProseMirrorPlugins: () => [new Plugin({ props: { decorations(state) {
if (!sourceEditing) return null;
const decorations: Decoration[] = [];
state.doc.descendants((node, position) => {
if (!node.isTextblock) return;
for (const link of documentLinks(node.textBetween(0, node.content.size, "\n", "\n"))) {
decorations.push(Decoration.inline(position + link.from + 1, position + link.to + 1, { class: "writer-link-source", "data-href": link.target, "data-kind": link.kind }));
if (link.targetTo > link.targetFrom) decorations.push(Decoration.inline(position + link.targetFrom + 1, position + link.targetTo + 1, { class: "writer-link-target" }));
}
return false;
});
return DecorationSet.create(state.doc, decorations);
} } })],
}),
StarterKit.configure({ blockquote: false, bulletList: false, orderedList: false, listItem: false, listKeymap: false, codeBlock: false, heading: false, horizontalRule: false, link: false, underline: false, undoRedo: false, trailingNode: false, gapcursor: false, dropcursor: false }),
CellLink.configure({ openOnClick: false, autolink: false }),
CellImage.configure({ inline: true, allowBase64: true }),
@@ -177,6 +209,9 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
attributes: { class: "writer-cell-editor", role: "textbox", "aria-label": cell.getAttribute("aria-label")!, "aria-multiline": "true", spellcheck: "true" },
handleKeyDown(_editorView, event) {
if (event.isComposing) return false;
if (sourceEditing && (event.ctrlKey || event.metaKey) && ["b", "i"].includes(event.key.toLowerCase())) {
event.preventDefault(); event.stopPropagation(); return true;
}
if (["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(event.key)) {
// The nested editor owns cursor movement, including word movement
// and selections. Never let an arrow reach the prose editor.
@@ -237,10 +272,11 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
});
return true;
}
if (event.key === "Escape" && sourceEditing) { event.preventDefault(); finish(); wrapper.focus(); return true; }
if (event.key === "Escape") { event.preventDefault(); leave(true); return true; }
if (event.key === "Enter") {
event.preventDefault();
if (/^=(?!=)/.test((editor?.getText() || "").trim())) { finish(); wrapper.focus(); }
if (sourceEditing || /^=(?!=)/.test((editor?.getText() || "").trim())) { finish(); wrapper.focus(); }
else if (event.ctrlKey || event.metaKey) leave(true);
else editor?.commands.setHardBreak();
return true;
@@ -258,7 +294,13 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
},
},
onUpdate({ editor: current }) {
const text = tableCellMarkdown(current.getJSON());
const text = sourceEditing
? current.getText({ blockSeparator: "<br>" }).replace(/\r?\n/g, "<br>").replace(/\|/g, (_pipe, offset, value) => {
let slashes = 0;
for (let before = offset - 1; before >= 0 && value[before] === "\\"; before--) slashes++;
return slashes % 2 ? "|" : "\\|";
})
: tableCellMarkdown(current.getJSON());
const source = controller.source;
// Track the full edited range, including typed spaces. Re-parsing the
// trimmed cell on each key would gradually turn those spaces into padding.
@@ -269,6 +311,18 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
if (contentFrom !== undefined) contentTo = contentFrom + text.length;
change(insert);
},
onSelectionUpdate({ editor: current }) {
// Read the final mouse/keyboard selection after activation sets its
// temporary caret. Menus and range selections keep their own focus.
requestAnimationFrame(() => {
if (current !== editor || current.isDestroyed || sourceEditing || !current.isFocused || current.view.composing) return;
const { selection } = current.state;
if (!selection.empty) return;
const mark = selection.$from.marks().find(mark => mark.type.name === "link");
const range = mark && getMarkRange(selection.$from, mark.type, mark.attrs);
if (range) editSource(range, selection.from - range.from);
});
},
onBlur() {
// A toolbar click, another cell, or the prose editor can now take focus.
queueMicrotask(() => { if (active === cell && !cell.contains(document.activeElement) && !selectionControls?.keepEditing()) finish(); });
@@ -279,6 +333,32 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
view.requestMeasure();
}
function editSource(range: { from: number; to: number }, labelOffset?: number) {
if (!editor || !active) return;
const row = (active.parentElement as HTMLTableRowElement).rowIndex;
const cell = parseMarkdownTable(controller.source)?.rows[row === 0 ? 0 : row + 1]?.cells[active.cellIndex];
if (!cell) return;
const preceding = new Set<number>();
editor.state.doc.descendants((node, position) => {
const mark = node.marks.find(mark => mark.type.name === "link");
if (mark && position < range.from) {
const found = getMarkRange(editor!.state.doc.resolve(position), mark.type, mark.attrs);
if (found && found.from < range.from) preceding.add(found.from);
}
});
const link = documentLinks(cell.text)[preceding.size];
if (!link) return;
sourceImages = new Map([...root.querySelectorAll<HTMLImageElement>("img[data-md-src]")].map(image => [image.dataset.mdSrc!, image.src]));
sourceEditing = true;
active.classList.add("is-source-editing");
editor.commands.setContent({ type: "doc", content: [{ type: "paragraph", content: [{ type: "text", text: cell.text }] }] }, { emitUpdate: false });
editor.commands.setTextSelection(labelOffset === undefined
? { from: link.targetFrom + 1, to: link.targetTo + 1 }
: tableLinkLabelPosition(cell.text, link, labelOffset) + 1);
editor.view.focus();
view.requestMeasure();
}
function add(kind: "row" | "column", focusNew = false) {
const from = view.posAtDOM(root);
finish();
@@ -299,7 +379,7 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
const help = document.createElement("div");
help.className = "table-edit-help";
help.textContent = "Drag grips: move rows/columns · Drag cells: select · Right-click: actions · Arrows: move through cells · Tab: next cell · Esc: exit table";
help.hidden = true;
const footer = document.createElement("div");
footer.className = "table-footer";
const tools = formulas?.createFormulaTools(finish, () => view.requestMeasure());
@@ -313,7 +393,7 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
const label = kind === "column" ? "Add column to the right" : "Add row below";
button.type = "button";
button.className = `table-add table-add-${kind}`;
button.title = label;
tooltipCleanup.push(attachTooltip(button, () => label));
button.setAttribute("aria-label", label);
const plus = document.createElement("span");
plus.append(actionIcon("plus"));
@@ -332,7 +412,7 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
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);
selectionControls = tableSelectionControls(root, wrapper, table, view, () => controller.source, finish, change, { get: () => editor, activate, editSource }, actions);
dragControls = tableDragControls(root, wrapper, table, scroll, view, () => controller.source, selectionControls.select, change);
table.addEventListener("mousedown", event => {
if (event.button !== 0) return;
@@ -347,4 +427,7 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
}
});
refreshFormulas();
for (const row of table.rows) for (const cell of row.cells) {
tooltipCleanup.push(attachTooltip(cell, () => cell.dataset.cellHint || cell.getAttribute("aria-label") || ""));
}
}