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:
@@ -220,19 +220,40 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
|
||||
// Native cursor movement can precede ProseMirror's selectionchange
|
||||
// observer (especially during key repeat). Test the visible caret.
|
||||
const native = document.getSelection();
|
||||
if (native?.anchorNode && native.focusNode && _editorView.dom.contains(native.anchorNode) && _editorView.dom.contains(native.focusNode)) {
|
||||
const readNative = (scroll = false) => {
|
||||
if (!native?.anchorNode || !native.focusNode || !_editorView.dom.contains(native.anchorNode) || !_editorView.dom.contains(native.focusNode)) return;
|
||||
const anchor = _editorView.posAtDOM(native.anchorNode, native.anchorOffset);
|
||||
const head = _editorView.posAtDOM(native.focusNode, native.focusOffset);
|
||||
if (anchor !== _editorView.state.selection.anchor || head !== _editorView.state.selection.head) editor!.commands.setTextSelection({ from: anchor, to: head });
|
||||
}
|
||||
if (event.shiftKey || event.ctrlKey || event.metaKey || event.altKey || !_editorView.state.selection.empty) return false;
|
||||
if (anchor === _editorView.state.selection.anchor && head === _editorView.state.selection.head) return;
|
||||
const chain = editor!.chain().setTextSelection({ from: anchor, to: head });
|
||||
(scroll ? chain.scrollIntoView() : chain).run();
|
||||
};
|
||||
readNative();
|
||||
// ProseMirror reads a native move on the next selectionchange. Its
|
||||
// delayed focus check can run first and restore the old selection,
|
||||
// so the move is lost under load. Move the DOM selection here and
|
||||
// read the result at once. Other shortcuts stay with the browser.
|
||||
const mac = navigator.platform.includes("Mac");
|
||||
const vertical = event.key === "ArrowUp" || event.key === "ArrowDown";
|
||||
const word = mac ? event.altKey : event.ctrlKey;
|
||||
const moveNative = () => {
|
||||
if (!native || event.metaKey || (mac ? event.ctrlKey : event.altKey) || (vertical && word)) return false;
|
||||
native.modify(
|
||||
event.shiftKey ? "extend" : "move",
|
||||
vertical ? (event.key === "ArrowUp" ? "backward" : "forward") : event.key === "ArrowLeft" ? "left" : "right",
|
||||
vertical ? "line" : word ? "word" : "character",
|
||||
);
|
||||
readNative(true);
|
||||
return true;
|
||||
};
|
||||
if (event.shiftKey || event.ctrlKey || event.metaKey || event.altKey || !_editorView.state.selection.empty) return moveNative();
|
||||
const direction = event.key === "ArrowLeft" ? "left" : event.key === "ArrowRight" ? "right" : event.key === "ArrowUp" ? "up" : "down";
|
||||
const horizontal = direction === "left" || direction === "right";
|
||||
const backwards = direction === "left" || direction === "up";
|
||||
const { $head } = _editorView.state.selection;
|
||||
// Pasted rich text can have several paragraphs within one cell.
|
||||
if ($head.depth && (backwards ? $head.before(1) > 0 : $head.after(1) < _editorView.state.doc.content.size)) return false;
|
||||
if (!_editorView.endOfTextblock(direction)) return false;
|
||||
if ($head.depth && (backwards ? $head.before(1) > 0 : $head.after(1) < _editorView.state.doc.content.size)) return moveNative();
|
||||
if (!_editorView.endOfTextblock(direction)) return moveNative();
|
||||
event.preventDefault();
|
||||
const cells = [...table!.querySelectorAll<HTMLTableCellElement>("th, td")];
|
||||
const next = horizontal
|
||||
|
||||
Reference in New Issue
Block a user