// Links in table cells: clicks keep the caret in place, keyboard entry, inline Edit link, wiki aliases, repeated targets, save, and undo, in both themes and a narrow window. import assert from "node:assert/strict"; import path from "node:path"; import { mockTauri, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs"; await runSmoke("tables/linkCaret", async ({ page }) => { const output = await outputDir("tables/linkCaret"); await mockTauri(page, { commands: test => { localStorage.clear(); const workspace = test.workspace({ root: "C:\\tab-menu-workspace", folders: ["notes"], files: { "a.md": '# Links\n\nOutside table.\n\n| Name | Value |\n| --- | --- |\n| One | Text |\n| Two | Text |\n| Three | Text |\n| Four | Text |\n| Five | [Roadmp](docs/roadmap.md) |\n| Keyboard | Before [Guide](docs/guide.md) after |\n| Wiki | [[guide\\|Guide]] |\n| Repeated | **Prefix** [One](same.md) + [Sec**ond**](same.md "Title") |\n' }, }); test.files = workspace.files; test.prompts = []; return { ...workspace, spelling_menu_install: () => null, "plugin:dialog|message": ({ message }) => { test.prompts.push(message); return "Cancel"; }, }; }, }); page.setDefaultTimeout(15000); const sourceCell = () => page.locator('.is-source-editing .writer-cell-editor'); async function clickTextAt(locator, offset, button = 'left') { const point = await locator.evaluate((element, offset) => { const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT); let node; while ((node = walker.nextNode())) { if (offset > node.textContent.length) { offset -= node.textContent.length; continue; } const range = document.createRange(); range.setStart(node, offset); range.collapse(true); const rect = range.getBoundingClientRect(); return { x: rect.x, y: rect.y + rect.height / 2 }; } throw new Error('No text position'); }, offset); await page.mouse.click(point.x, point.y, { button }); } const selected = () => page.evaluate(() => getSelection().toString()); const caretPrefix = () => 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(); }); const documentText = () => page.locator('.cm-content').evaluate(async node => { const { EditorView } = await import('/node_modules/@codemirror/view/dist/index.js'); return EditorView.findFromDOM(node).state.doc.toString(); }); const shot = name => page.screenshot({ path: path.join(output, `${name}.png`) }); for (const theme of ['dark', 'light']) for (const width of [1440, 620]) { await page.setViewportSize({ width, height: 1000 }); await page.goto(smokeUrl); await page.locator('[data-tree-rel="a.md"]').click(); await page.locator('.writer-table').waitFor(); await page.evaluate(async mode => (await import('/src/lib/theme/appearance.ts')).saveAppearance({ theme:'default', mode }), theme); if (width < 1000) { await page.getByRole('button', { name:'Files', exact:true }).click(); await page.getByRole('button', { name:'Hide outline', exact:true }).click(); } const cell = page.getByRole('cell', { name:'B6 · Row 6, column 2', exact:true }); await clickTextAt(cell.locator('a'), 5); await sourceCell().waitFor(); assert.equal(await cell.locator('.writer-link-target').innerText(), 'docs/roadmap.md', 'Ordinary click reveals the URL'); assert.equal(await caretPrefix(), '[Roadm', 'The caret stays at the clicked label character'); await page.keyboard.type('a'); assert.equal(await sourceCell().innerText(), '[Roadmap](docs/roadmap.md)'); await shot(`click-label-${theme}-${width}`); await page.keyboard.press('Escape'); assert.equal(await cell.locator('a').innerText(), 'Roadmap'); await clickTextAt(cell.locator('a'), 3); await page.keyboard.press('Shift+F10'); await page.getByRole('menuitem', { name:'Edit link', exact:true }).click(); assert.equal(await selected(), 'docs/roadmap.md'); assert.equal(await page.getByRole('dialog').count(), 0); await shot(`edit-target-${theme}-${width}`); await page.keyboard.type('docs/changed.md'); await page.keyboard.press('Enter'); assert.equal(await cell.locator('a').getAttribute('href'), 'docs/changed.md'); await page.locator('.cm-line').filter({ hasText:'Outside table.' }).click(); await page.keyboard.press('Control+z'); assert.equal(await cell.locator('a').getAttribute('href'), 'docs/roadmap.md'); await page.keyboard.press('Control+y'); assert.equal(await cell.locator('a').getAttribute('href'), 'docs/changed.md'); await page.keyboard.press('Control+s'); assert.match(await page.evaluate(() => window.__TEST__.files.get('a.md')), /\[Roadmap\]\(docs\/changed.md\)/); const keyboard = page.getByRole('cell', { name:'B7 · Row 7, column 2', exact:true }); await clickTextAt(keyboard, 7); assert.equal(await sourceCell().count(), 0, 'Plain cell text remains rendered'); await page.keyboard.press('ArrowRight'); await sourceCell().waitFor(); assert.equal(await keyboard.locator('.writer-link-target').innerText(), 'docs/guide.md'); assert.equal(await caretPrefix(), 'Before [G'); await shot(`keyboard-${theme}-${width}`); await page.keyboard.press('Escape'); const wiki = page.getByRole('cell', { name:'B8 · Row 8, column 2', exact:true }); await clickTextAt(wiki.locator('a'), 3); await sourceCell().waitFor(); assert.equal(await caretPrefix(), '[[guide\\|Gui'); await page.keyboard.press('Shift+F10'); await page.getByRole('menuitem', { name:'Edit link', exact:true }).click(); assert.equal(await selected(), 'guide'); await page.keyboard.type('new-guide'); await page.keyboard.press('Enter'); assert.equal(await wiki.locator('a').getAttribute('href'), 'new-guide'); const repeated = page.getByRole('cell', { name:'B9 · Row 9, column 2', exact:true }); await clickTextAt(repeated.locator('a').nth(1), 4); await sourceCell().waitFor(); assert.equal(await caretPrefix(), '**Prefix** [One](same.md) + [Sec**o'); await page.keyboard.press('Shift+F10'); await page.getByRole('menuitem', { name:'Edit link', exact:true }).click(); assert.equal(await selected(), 'same.md'); await page.keyboard.type('second.md'); await page.keyboard.press('Enter'); assert.equal(await repeated.locator('a').first().getAttribute('href'), 'same.md'); assert.equal(await repeated.locator('a').nth(1).getAttribute('href'), 'second.md'); assert.match(await documentText(), /\[Sec\*\*ond\*\*\]\(second.md "Title"\)/); assert.deepEqual(await page.evaluate(() => window.__TEST__.prompts), []); } });