Keep a link click in a table cell that just got focus
A click on a link in a focused table cell could be lost on a busy computer. ProseMirror reads a click on the next selectionchange. Its delayed focus check could run first and restore the old selection, and the link source did not open. - The mousedown handler sets the clicked position at once. The linkClickAfterFocus smoke test forces this order of events.
This commit is contained in:
@@ -451,6 +451,13 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
|
|||||||
event.preventDefault(); activate(cell);
|
event.preventDefault(); activate(cell);
|
||||||
const hit = editor?.view.posAtCoords({ left: event.clientX, top: event.clientY });
|
const hit = editor?.view.posAtCoords({ left: event.clientX, top: event.clientY });
|
||||||
if (hit) { editor?.commands.setTextSelection(hit.pos); editor?.view.focus(); }
|
if (hit) { editor?.commands.setTextSelection(hit.pos); editor?.view.focus(); }
|
||||||
|
} else if (event.detail === 1 && editor && !editor.view.composing && editor.view.dom.contains(event.target)) {
|
||||||
|
// ProseMirror reads a native click on the next selectionchange. Its
|
||||||
|
// delayed focus check can run first and restore the old selection, so
|
||||||
|
// the click is lost under load. Set the clicked position here. The
|
||||||
|
// browser then places the same caret, and a drag starts from it.
|
||||||
|
const hit = editor.view.posAtCoords({ left: event.clientX, top: event.clientY });
|
||||||
|
if (hit) editor.commands.setTextSelection(hit.pos);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
refreshFormulas();
|
refreshFormulas();
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
// A click on a link in a table cell that took focus a moment earlier. The ProseMirror focus check runs before selectionchange, and the link source still opens at the clicked character.
|
||||||
|
import assert from "node:assert/strict";
|
||||||
|
import { mockTauri, runSmoke, smokeUrl } from "../_harness/index.mjs";
|
||||||
|
|
||||||
|
await runSmoke("tables/linkClickAfterFocus", async ({ page }) => {
|
||||||
|
await mockTauri(page, {
|
||||||
|
commands: test => ({
|
||||||
|
...test.workspace({
|
||||||
|
root: "C:\\tab-menu-workspace",
|
||||||
|
files: { "a.md": "| Name | Value |\n| --- | --- |\n| One | Text |\n| Two | Before [Guide](docs/guide.md) after |\n" },
|
||||||
|
}),
|
||||||
|
spelling_menu_install: () => null,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
await page.goto(smokeUrl);
|
||||||
|
await page.locator('[data-tree-rel="a.md"]').click();
|
||||||
|
await page.locator('.writer-table').waitFor();
|
||||||
|
await page.getByRole('cell', { name:'B2 · Row 2, column 2', exact:true }).click();
|
||||||
|
await page.locator('.writer-cell-editor').waitFor();
|
||||||
|
|
||||||
|
// Force the order that load causes: the click moves the native selection, the focus check runs, and only then selectionchange reaches ProseMirror.
|
||||||
|
// The focus check is the 20 ms timer in the focus handler of prosemirror-view. Its source is the only timer source that reads currentSelection.
|
||||||
|
await page.evaluate(() => {
|
||||||
|
const setTimeout = window.setTimeout, checks = [];
|
||||||
|
const hold = event => event.stopImmediatePropagation();
|
||||||
|
window.setTimeout = (run, delay, ...rest) => {
|
||||||
|
if (delay !== 20 || !String(run).includes('currentSelection')) return setTimeout(run, delay, ...rest);
|
||||||
|
checks.push(run);
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
document.addEventListener('selectionchange', hold, true);
|
||||||
|
window.__runFocusCheck = () => {
|
||||||
|
window.setTimeout = setTimeout;
|
||||||
|
for (const check of checks) check();
|
||||||
|
document.removeEventListener('selectionchange', hold, true);
|
||||||
|
return checks.length;
|
||||||
|
};
|
||||||
|
});
|
||||||
|
await page.keyboard.press('Enter');
|
||||||
|
const linkCell = page.getByRole('cell', { name:'B3 · Row 3, column 2', exact:true });
|
||||||
|
await linkCell.locator('.writer-cell-editor').waitFor();
|
||||||
|
// Let the other timers of the new editor run. A flush after the click reads the native selection and hides the race.
|
||||||
|
await page.waitForTimeout(100);
|
||||||
|
const point = await linkCell.locator('a').evaluate(link => {
|
||||||
|
const range = document.createRange();
|
||||||
|
range.setStart(link.firstChild, 1); range.collapse(true);
|
||||||
|
const rect = range.getBoundingClientRect();
|
||||||
|
return { x: rect.x, y: rect.y + rect.height / 2 };
|
||||||
|
});
|
||||||
|
await page.mouse.click(point.x, point.y);
|
||||||
|
assert.ok(await page.evaluate(() => window.__runFocusCheck()) > 0, 'The focus check ran after the click');
|
||||||
|
|
||||||
|
await page.locator('.is-source-editing .writer-cell-editor').waitFor();
|
||||||
|
assert.equal(await linkCell.locator('.writer-link-target').innerText(), 'docs/guide.md', 'The click opens the link source');
|
||||||
|
const caretPrefix = await page.evaluate(() => {
|
||||||
|
const selection = getSelection(), range = document.createRange();
|
||||||
|
range.selectNodeContents(document.querySelector('.is-source-editing .writer-cell-editor'));
|
||||||
|
range.setEnd(selection.anchorNode, selection.anchorOffset);
|
||||||
|
return range.toString();
|
||||||
|
});
|
||||||
|
assert.equal(caretPrefix, 'Before [G', 'The caret stays at the clicked label character');
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user