// Link context menus in the Writer, Reading, Source, and table cells: browser and workspace actions, Edit link with undo, keyboard use, narrow windows, bookmarks, rename, and move. import assert from "node:assert/strict"; import { chooseView, mockTauri, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs"; await runSmoke("links/linkMenus", async ({ page }) => { const output = await outputDir("links/linkMenus"); await mockTauri(page, { commands: test => { const note = name => `# ${name}\n\nOriginal text.\n\n\`\`\`js\nconst answer = 42;\n\`\`\`\n\n| Name | Value |\n| --- | --- |\n| Alpha | One |\n`; const files = Object.fromEntries(["b.md", "c.md", "d.md"].map(name => [name, note(name)])); // Enter in a table cell moves to the next row, and adds one after the last row (src/lib/editor/tableControls.ts:278-285). // The second row keeps each link edit a single undo step. files["a.md"] = "# Links\n\nSelected reading text.\n\n[Web label](https://example.com/path)\n\n[[b.md|Workspace note]]\n\n| Link | Note |\n| --- | --- |\n| [Cell web](https://example.com/cell) | [[b.md\\|Cell note]] |\n| Plain | Row |\n"; files["sample.txt"] = "const answer = 42;\n"; const workspace = test.workspace({ root: "C:\\tab-menu-workspace", folders: ["notes"], files }); Object.assign(test, { files: workspace.files, clipboard: null, opened: null, app: null, revealed: null, searched: null, windows: [] }); Object.defineProperty(navigator, "clipboard", { value: { writeText: async text => { test.clipboard = text; } } }); return { ...workspace, workspace_search: args => { test.searched = args.query; return []; }, workspace_rename: args => { workspace.files.set(args.to, workspace.files.get(args.from)); workspace.files.delete(args.from); }, workspace_reveal: args => { test.revealed = args.rel; }, workspace_open_default: args => { test.app = args.rel; }, workspace_open_window: args => { test.windows.push(args); }, "plugin:opener|open_url": args => { test.opened = args.url; }, spelling_menu_install: () => {}, }; }, }); const menu = name => page.getByRole('menu', { name, exact: true }); const item = name => page.getByRole('menuitem', { name, exact: true }); const dismiss = async () => { await page.keyboard.press('Escape'); await page.locator('.rs-menu').waitFor({ state: 'detached' }); }; const report = []; async function writerMenu(target='Web label') { await page.locator('.cm-content').getByText('Selected reading text.', { exact: true }).click(); await page.locator('.cm-content').getByText(target, { exact: true }).click({ button: 'right' }); await menu('Markdown text actions').waitFor(); } await page.goto(smokeUrl); await page.locator('[data-tree-rel="a.md"]').click(); await page.locator('.cm-content').waitFor(); await page.evaluate(async () => { const { pluginHost } = await import('/src/lib/plugins/registry.ts'); pluginHost.register('link-smoke', 'browser', { open: async target => { window.__TEST__.browser = target; return true; }, mountSettings: () => () => {}, mountBookmarks: () => () => {} }); }); for (const theme of ['dark', 'light']) { await page.evaluate(async mode => { const { saveAppearance } = await import('/src/lib/theme/appearance.ts'); saveAppearance({theme:'default', mode}); }, theme); await writerMenu(); for (const label of ['Open link in app browser', 'Open link in default browser', 'Copy URL', 'Edit link']) assert.equal(await item(label).count(), 1); await page.screenshot({ path: `${output}/writer-link-${theme}.png` }); await item('Copy URL').click(); await page.waitForFunction(() => window.__TEST__.clipboard === 'https://example.com/path'); await writerMenu(); await item('Open link in default browser').click(); await page.waitForFunction(() => window.__TEST__.opened === 'https://example.com/path'); await writerMenu(); await item('Open link in app browser').click(); await page.waitForFunction(() => window.__TEST__.browser === 'https://example.com/path'); await writerMenu(); await item('Edit link').click(); assert.equal(await page.evaluate(async () => { const { EditorView } = await import('/node_modules/@codemirror/view/dist/index.js'); const v = EditorView.findFromDOM(document.querySelector('.cm-content')); return v.state.sliceDoc(v.state.selection.main.from, v.state.selection.main.to); }), 'https://example.com/path'); await page.screenshot({ path: `${output}/edit-url-${theme}.png` }); await page.keyboard.type('https://changed.example'); await page.keyboard.press('Control+z'); await page.locator('.cm-content').getByText('Selected reading text.', {exact:true}).click(); await writerMenu('Workspace note'); for (const label of ['Open in new tab', 'Open in new window', 'Copy path', 'Bookmark', 'Show in Explorer', 'Reveal file in navigation', 'Edit link']) assert.equal(await item(label).count(), 1); for (const label of ['Rename…', 'Move file to…', 'Open in default app']) assert.equal(await item(label).count(), 0); await page.screenshot({ path: `${output}/workspace-link-${theme}.png` }); await dismiss(); await page.getByRole('button',{name:'Files',exact:true}).click(); await page.getByRole('button',{name:'Hide outline',exact:true}).click(); await page.setViewportSize({width:560,height:620}); await writerMenu('Workspace note'); await page.keyboard.press('End'); assert.match(await page.evaluate(() => document.activeElement.textContent), /Select all/); await page.screenshot({path:`${output}/workspace-narrow-${theme}.png`}); await dismiss(); await page.setViewportSize({width:1440,height:1000}); await page.getByRole('button',{name:'Files',exact:true}).click(); await page.getByRole('button',{name:'Show outline',exact:true}).click(); await writerMenu('Workspace note'); await item('Copy path').focus(); await page.keyboard.press('ArrowRight'); await item('Workspace path').click(); await page.waitForFunction(() => window.__TEST__.clipboard === 'b.md'); await writerMenu('Workspace note'); await item('Show in Explorer').click(); await page.waitForFunction(() => window.__TEST__.revealed === 'b.md'); await writerMenu('Workspace note'); await item('Reveal file in navigation').click(); await page.waitForFunction(() => document.activeElement?.getAttribute('data-tree-rel') === 'b.md'); await chooseView(page, 'Read'); const reading = page.locator('.reading'); await reading.getByText('Selected reading text.',{exact:true}).evaluate(node => { const range = document.createRange(); range.selectNodeContents(node); const s = getSelection(); s.removeAllRanges(); s.addRange(range); node.closest('.reading').focus(); }); await page.keyboard.press('Shift+F10'); await menu('Reading actions').waitFor(); await page.keyboard.press('End'); assert.match(await page.evaluate(() => document.activeElement.textContent), /^Search for/); await page.screenshot({ path: `${output}/reading-selection-${theme}.png` }); await page.keyboard.press('Home'); await page.keyboard.press('Enter'); await page.waitForFunction(() => window.__TEST__.clipboard === 'Selected reading text.'); await page.keyboard.press('Shift+F10'); await page.getByRole('menuitem', {name:/^Search for/}).click(); await page.waitForFunction(() => window.__TEST__.searched === 'Selected reading text.'); await page.evaluate(() => getSelection().removeAllRanges()); await reading.getByRole('link',{name:'Web label'}).click({button:'right'}); await item('Copy URL').click(); await page.waitForFunction(() => window.__TEST__.clipboard === 'https://example.com/path'); await page.getByRole('button', { name: 'Search', exact: true }).click(); await page.getByRole('button', { name: 'Hide outline', exact: true }).click(); await page.setViewportSize({ width: 560, height: 620 }); await reading.getByRole('link',{name:'Web label'}).click({button:'right'}); const rect=await menu('Reading actions').boundingBox(); assert.ok(rect.x>=0 && rect.x+rect.width<=560 && rect.y+rect.height<=620); await page.screenshot({ path: `${output}/reading-narrow-${theme}.png` }); await dismiss(); assert.equal(await reading.evaluate(node => node.contains(document.activeElement)), true); await page.setViewportSize({width:1440,height:1000}); await page.getByRole('button', { name: 'Files', exact: true }).click(); await page.getByRole('button', { name: 'Show outline', exact: true }).click(); await chooseView(page, 'Source'); await page.locator('.source-editor .cm-content').waitFor(); await page.evaluate(async () => { const {EditorView}=await import('/node_modules/@codemirror/view/dist/index.js'); const view=EditorView.findFromDOM(document.querySelector('.source-editor .cm-content')); view.dispatch({selection:{anchor:view.state.doc.toString().indexOf('Web label')+2}}); view.focus(); }); await page.keyboard.press('Shift+F10'); await item('Edit link').click(); assert.equal(await page.evaluate(async () => { const {EditorView}=await import('/node_modules/@codemirror/view/dist/index.js'); const {state}=EditorView.findFromDOM(document.querySelector('.source-editor .cm-content')); return state.sliceDoc(state.selection.main.from,state.selection.main.to); }), 'https://example.com/path'); await page.screenshot({ path: `${output}/ide-edit-url-${theme}.png` }); await chooseView(page, 'Write'); await page.locator('.cm-content').getByText('Selected reading text.', {exact:true}).click(); const cellLink = page.locator('.writer-table a').filter({hasText:'Cell web'}); await cellLink.click({button:'right'}); await item('Edit link').waitFor(); await page.screenshot({path:`${output}/table-link-${theme}.png`}); await item('Copy URL').click(); await page.waitForFunction(() => window.__TEST__.clipboard === 'https://example.com/cell'); await cellLink.click({button:'right'}); await item('Edit link').click(); const sourceCell = page.locator('.writer-table .is-source-editing .writer-cell-editor'); await sourceCell.waitFor(); assert.equal(await page.getByLabel('Edit link target').count(), 0); assert.equal(await page.evaluate(() => getSelection().toString()), 'https://example.com/cell'); assert.equal(await sourceCell.locator('.writer-link-target').innerText(), 'https://example.com/cell'); await page.screenshot({path:`${output}/table-edit-${theme}.png`}); await page.keyboard.type('https://changed.example/cell'); await page.keyboard.press('Enter'); await page.waitForFunction(() => document.querySelector('.writer-table a')?.getAttribute('href') === 'https://changed.example/cell'); await page.locator('.cm-content').getByText('Selected reading text.', {exact:true}).click(); await page.keyboard.press('Control+z'); await page.waitForFunction(() => document.querySelector('.writer-table a')?.getAttribute('href') === 'https://example.com/cell'); const cellNote = page.locator('.writer-table a').filter({hasText:'Cell note'}); await cellNote.click({button:'right'}); await item('Edit link').click(); assert.equal(await page.evaluate(() => getSelection().toString()), 'b.md'); await page.keyboard.type('c.md'); await page.keyboard.press('Enter'); await page.waitForFunction(() => [...document.querySelectorAll('.writer-table a')].some(node => node.textContent === 'Cell note' && node.getAttribute('href') === 'c.md')); await page.locator('.cm-content').getByText('Selected reading text.', {exact:true}).click(); await page.keyboard.press('Control+z'); await page.waitForFunction(() => [...document.querySelectorAll('.writer-table a')].some(node => node.textContent === 'Cell note' && node.getAttribute('href') === 'b.md')); await cellLink.click({button:'right'}); await item('Edit link').click(); await page.keyboard.type('https://escape.example'); await page.keyboard.press('Escape'); assert.equal(await sourceCell.count(), 0); assert.equal(await cellLink.getAttribute('href'), 'https://escape.example'); await page.locator('.cm-content').getByText('Selected reading text.', {exact:true}).click(); await page.keyboard.press('Control+z'); await page.waitForFunction(() => document.querySelector('.writer-table a')?.getAttribute('href') === 'https://example.com/cell'); await page.getByRole('button',{name:'Files',exact:true}).click(); await page.getByRole('button',{name:'Hide outline',exact:true}).click(); await page.setViewportSize({width:560,height:620}); await cellLink.click(); await page.keyboard.press('Shift+F10'); await item('Edit link').waitFor(); await page.screenshot({path:`${output}/table-narrow-${theme}.png`}); await dismiss(); await page.setViewportSize({width:1440,height:1000}); await page.getByRole('button',{name:'Files',exact:true}).click(); await page.getByRole('button',{name:'Show outline',exact:true}).click(); await page.locator('.cm-content').getByText('Selected reading text.', {exact:true}).click(); report.push({theme,passed:['Writer and Source edit URL selection','browser destinations','workspace file actions','Reader copy/search/link actions','inline table web/wiki link edits and undo','table source Escape and keyboard menu','keyboard focus return','narrow viewport'],nativeDesktop:'not tested'}); } await writerMenu('Workspace note'); await item('Open in new window').click(); await page.waitForFunction(() => window.__TEST__.windows.length === 1); await page.evaluate(() => { const {transfer}=window.__TEST__.windows[0]; window.__TEST__.emit('tab-transfer-accepted',{id:transfer}); }); assert.equal(await page.getByRole('tab',{name:/b.md/}).count(),1,'Opening a linked document in a new window keeps the source tab'); await page.locator('[data-tree-rel="a.md"]').click(); await writerMenu('Workspace note'); await item('Bookmark').click(); await page.waitForFunction(() => JSON.parse(window.__TEST__.files.get('.runestone/marks.json') || '{}').bookmarks?.includes('b.md')); await writerMenu('Workspace note'); await item('Copy path').focus(); await page.keyboard.press('ArrowRight'); await item('System path').click(); await page.waitForFunction(() => window.__TEST__.clipboard.endsWith('\\b.md')); await chooseView(page, 'Read'); const readerWorkspaceMenu = async () => { await page.locator('.reading').getByRole('link',{name:'Workspace note'}).click({button:'right'}); }; await readerWorkspaceMenu(); await item('Open in default app').click(); await page.waitForFunction(() => window.__TEST__.app === 'b.md'); await readerWorkspaceMenu(); await item('Rename…').click(); await page.getByLabel('New file name').fill('renamed.md'); await page.getByRole('button',{name:'Continue',exact:true}).click(); await page.waitForFunction(() => window.__TEST__.files.has('renamed.md') && !window.__TEST__.files.has('b.md')); assert.equal(await page.evaluate(() => JSON.parse(window.__TEST__.files.get('.runestone/marks.json')).bookmarks.includes('renamed.md')),true); await chooseView(page, 'Write'); await page.locator('.cm-content').evaluate(async node => { const {EditorView}=await import('/node_modules/@codemirror/view/dist/index.js'); const view=EditorView.findFromDOM(node); const from=view.state.doc.toString().indexOf('b.md'); view.dispatch({changes:{from,to:from+4,insert:'renamed.md'}}); }); await chooseView(page, 'Read'); await readerWorkspaceMenu(); await item('Move file to…').click(); await page.getByLabel('Move file to (workspace path, including filename)').fill('notes/moved.md'); await page.getByRole('button',{name:'Continue',exact:true}).click(); await page.waitForFunction(() => window.__TEST__.files.has('notes/moved.md') && !window.__TEST__.files.has('renamed.md')); assert.equal(await page.evaluate(() => JSON.parse(window.__TEST__.files.get('.runestone/marks.json')).bookmarks.includes('notes/moved.md')),true); console.log(JSON.stringify(report,null,2)); }, { timeout: 15000 });