Fix the table cell arrow race and remove the TipTap Writer

Phase 4a. The ProseMirror focus handler starts a 20 ms timer that writes
the ProseMirror selection back to the DOM. Under load, the timer ran
after a native arrow move and before ProseMirror read the
selectionchange event, so one move was lost. The cell editor now moves
the DOM selection with Selection.modify and reads the result in the
same key event. This covers plain and Shift arrows and word moves.
Other shortcuts stay with the browser.

- Workbench.svelte no longer filters access events. Rust already drops
  them.
- Delete the unused TipTap Writer: WriterHost.svelte, writerMenu.ts,
  writerMarks.ts, headingMarks.ts, and their two unit tests.
- Remove the 8 TipTap packages that only the Writer used, and its entry
  in docs/menu-surfaces.json.

With a 4x CPU slowdown, 11 of 60 runs failed before the fix and 0 of
120 after it. pnpm check, pnpm test, and the smoke suite pass.
This commit is contained in:
Knotty Wyvern
2026-10-28 09:39:08 -07:00
parent 36a06a9c6b
commit 80f5d908e0
12 changed files with 30 additions and 926 deletions
+2 -2
View File
@@ -2,8 +2,8 @@
// Type checks for the unit tests. They run in Node with --experimental-strip-types.
"extends": "../tsconfig.json",
"compilerOptions": {
// The TipTap packages declare the editor commands that src/lib/editor/writerMenu.ts uses.
"types": ["node", "@tiptap/starter-kit", "@tiptap/extension-highlight", "@tiptap/extension-table", "@tiptap/extension-task-list"],
// The TipTap packages declare the editor commands that the table cell editor uses.
"types": ["node", "@tiptap/starter-kit", "@tiptap/extension-highlight"],
"checkJs": false
},
"include": ["unit/**/*.ts", "../src/**/*.d.ts"],
-15
View File
@@ -1,15 +0,0 @@
import assert from "node:assert/strict";
import test from "node:test";
import { fenceOpenText, headingMarkText } from "../../../src/lib/editor/headingMarks.ts";
test("heading marks match the heading level", () => {
assert.equal(headingMarkText(1), "#");
assert.equal(headingMarkText(3), "###");
assert.equal(headingMarkText(6), "######");
});
test("fence open marks include the language", () => {
assert.equal(fenceOpenText(""), "```");
assert.equal(fenceOpenText("js"), "```js");
assert.equal(fenceOpenText(" pp "), "```pp");
});
-244
View File
@@ -1,244 +0,0 @@
import assert from "node:assert/strict";
import test from "node:test";
import {
copySelection,
cutSelection,
pasteClipboard,
writerContextItems,
} from "../../../src/lib/editor/writerMenu.ts";
type Harness = {
selected: string;
inTable: boolean;
deleted: boolean;
focused: boolean;
inserted: string | null;
dispatched: boolean;
};
function fakeEditor(init?: { selected?: string; inTable?: boolean }) {
const harness: Harness = {
selected: init?.selected ?? "",
inTable: init?.inTable ?? false,
deleted: false,
focused: false,
inserted: null,
dispatched: false,
};
const tr = {
insertText(text: string) {
harness.inserted = text;
return tr;
},
};
const chainApi = {
focus() {
harness.focused = true;
return chainApi;
},
deleteSelection() {
harness.deleted = true;
return chainApi;
},
run() {
return true;
},
};
const editor = {
isActive(name: string) {
return name === "table" && harness.inTable;
},
chain() {
return chainApi;
},
state: {
selection: { from: 0, to: harness.selected.length },
doc: {
textBetween() {
return harness.selected;
},
},
tr,
},
view: {
dispatch() {
harness.dispatched = true;
},
},
};
return { editor, harness };
}
function withClipboard(
impl: { writeText?: (text: string) => Promise<void>; readText?: () => Promise<string> },
run: () => Promise<void>,
): Promise<void> {
const previous = Object.getOwnPropertyDescriptor(globalThis, "navigator");
Object.defineProperty(globalThis, "navigator", {
configurable: true,
value: { clipboard: impl },
});
return run().finally(() => {
if (previous) {
Object.defineProperty(globalThis, "navigator", previous);
} else {
Reflect.deleteProperty(globalThis, "navigator");
}
});
}
function actionLabels(items: ReturnType<typeof writerContextItems>): string[] {
const labels: string[] = [];
for (const item of items) {
if (item.kind === "action") {
labels.push(item.label);
} else if (item.kind === "group") {
for (const sub of item.items) {
if (sub.kind === "action") {
labels.push(sub.label);
}
}
}
}
return labels;
}
test("paste inserts clipboard text without parsing tags", async () => {
const { editor, harness } = fakeEditor();
await withClipboard(
{ readText: async () => "<b>bold</b>" },
async () => {
await pasteClipboard(editor as never);
},
);
assert.equal(harness.focused, true);
assert.equal(harness.dispatched, true);
assert.equal(harness.inserted, "<b>bold</b>");
});
test("paste swallows clipboard errors", async () => {
const { editor, harness } = fakeEditor();
await withClipboard(
{
readText: async () => {
throw new Error("denied");
},
},
async () => {
await pasteClipboard(editor as never);
},
);
assert.equal(harness.dispatched, false);
assert.equal(harness.inserted, null);
});
test("cut keeps the selection when clipboard write fails", async () => {
const { editor, harness } = fakeEditor({ selected: "gone" });
await withClipboard(
{
writeText: async () => {
throw new Error("denied");
},
},
async () => {
await cutSelection(editor as never);
},
);
assert.equal(harness.deleted, false);
});
test("copy swallows clipboard errors", async () => {
const { editor } = fakeEditor({ selected: "keep" });
let attempted = false;
await withClipboard(
{
writeText: async () => {
attempted = true;
throw new Error("denied");
},
},
async () => {
await copySelection(editor as never);
},
);
assert.equal(attempted, true);
});
test("cut writes selected text then deletes it", async () => {
const { editor, harness } = fakeEditor({ selected: "clip" });
let written = "";
await withClipboard(
{
writeText: async (text) => {
written = text;
},
},
async () => {
await cutSelection(editor as never);
},
);
assert.equal(written, "clip");
assert.equal(harness.deleted, true);
});
test("table row and column actions disable outside a table", () => {
const { editor } = fakeEditor({ inTable: false });
const items = writerContextItems(editor as never);
const table = items.find((item) => item.kind === "group" && item.label === "Table");
assert.equal(table?.kind, "group");
if (table?.kind !== "group") {
return;
}
const byLabel = new Map(
table.items.filter((item) => item.kind === "action").map((item) => [item.label, item]),
);
assert.equal(byLabel.get("Insert table")?.disabled, undefined);
assert.equal(byLabel.get("Add row above")?.disabled, true);
assert.equal(byLabel.get("Delete table")?.disabled, true);
});
test("table row and column actions enable inside a table", () => {
const { editor } = fakeEditor({ inTable: true });
const items = writerContextItems(editor as never);
const table = items.find((item) => item.kind === "group" && item.label === "Table");
assert.equal(table?.kind, "group");
if (table?.kind !== "group") {
return;
}
const byLabel = new Map(
table.items.filter((item) => item.kind === "action").map((item) => [item.label, item]),
);
assert.equal(byLabel.get("Insert table")?.disabled, undefined);
assert.equal(byLabel.get("Add row above")?.disabled, false);
assert.equal(byLabel.get("Delete table")?.disabled, false);
});
test("style and format actions cover the requested commands", () => {
const { editor } = fakeEditor();
const labels = actionLabels(writerContextItems(editor as never));
for (const label of [
"Cut",
"Copy",
"Paste",
"Heading 1",
"Heading 6",
"Body",
"Quote",
"Bullet list",
"Numbered list",
"Task list",
"Bold",
"Italic",
"Strikethrough",
"Highlight",
"Code",
"Math",
"Comment",
"Clear formatting",
"Insert table",
]) {
assert.equal(labels.includes(label), true, label);
}
assert.equal(labels.includes("Superscript"), false);
assert.equal(labels.includes("Subscript"), false);
});