Keep the user preferences in settings.json in the app config folder. WebView storage can be lost, and the host cannot read it. The file has a schema number, a revision number, and one value for each section: general, appearance, customThemes, customThemesLegacy, and language. - settings_read gives all sections, the revision, and a one-time notice. - settings_write replaces one section, writes the file atomically, and sends settings-changed to all windows. The limit is 8 MiB. - settings_migrate writes the first file only when no file exists. - The host moves a damaged file to settings.corrupt-<seconds>.json, starts from the defaults, and reports this one time. Each window loads the sections before its first render. One store in src/lib/settings/store.ts replaces the storage listeners. When no file exists, the window normalizes the old localStorage values and migrates them. The old keys stay in place for one release. The window layout and the open tabs stay in localStorage on purpose. The browser preview keeps all preferences in localStorage. Keep tab transfer offers in host memory, not in localStorage, because an offer can hold unsaved text. A window takes an offer once. The host checks the workspace root and a 120 second expiry. When the receiver accepts the tab, the host sends tab-transfer-accepted to the source window only. A refused offer stays open, and the source tab stays open. A new window and a drop on the tab ribbon use one check. The smoke harness serves the new commands from one shared state for each browser context. Tests read saved values from the mock settings file. Rust, unit, and smoke tests cover the changes.
187 lines
16 KiB
JavaScript
187 lines
16 KiB
JavaScript
// Link context menus in the Writer, Reading, IDE, 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 { 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 system 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 system 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 page.getByRole('group', { name: 'Document view', exact: true }).hover(); await page.getByRole('button', { name:'Read', exact:true }).click();
|
|
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 page.getByRole('group', { name: 'Document view', exact: true }).hover();
|
|
await page.getByRole('button',{name:'Edit',exact:true}).click();
|
|
await page.getByRole('group', { name:'Editor layout',exact:true }).hover(); await page.getByRole('button',{name:'IDE',exact:true}).click();
|
|
await page.locator('.monaco-editor').waitFor();
|
|
await page.evaluate(async () => { const {monaco}=await import('/src/lib/editor/monacoEnv.ts'); const editor=monaco.editor.getEditors()[0]; const model=editor.getModel(); const pos=model.getPositionAt(model.getValue().indexOf('Web label')+2); editor.setPosition(pos); editor.focus(); });
|
|
await page.keyboard.press('Shift+F10'); await item('Edit link').click();
|
|
assert.equal(await page.evaluate(async () => { const {monaco}=await import('/src/lib/editor/monacoEnv.ts'); const editor=monaco.editor.getEditors()[0]; return editor.getModel().getValueInRange(editor.getSelection()); }), 'https://example.com/path');
|
|
await page.screenshot({ path: `${output}/ide-edit-url-${theme}.png` });
|
|
await page.getByRole('group', { name:'Editor layout',exact:true }).hover(); await page.getByRole('button',{name:'Writer',exact:true}).click();
|
|
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 IDE 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 page.getByRole('group', { name: 'Document view', exact: true }).hover(); await page.getByRole('button', { name:'Read', exact:true }).click();
|
|
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 page.getByRole('group', { name: 'Document view', exact: true }).hover(); await page.getByRole('button', { name:'Edit', exact:true }).click();
|
|
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 page.getByRole('group', { name: 'Document view', exact: true }).hover(); await page.getByRole('button', { name:'Read', exact:true }).click();
|
|
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 });
|