Phase 4b. DocumentEditor.svelte replaces LiveWriter.svelte and the Monaco EditorHost.svelte. Each open document has one CodeMirror state. Write and Source are two compartment configurations of it, so they share the undo history. editorStates.ts keeps the state while the tab stays open. The Monaco model, the diff bridge, its timers, and sessions.ts are gone. - Source: syntax colors from the theme tokens, lazy language packages, line numbers, folding, and the current line in IDE, wrapped lines in Writing, several carets, and bracket matching and closing. - Lists (card 088e778b): Tab nests a list item and Shift+Tab moves it up, in Write and Source. Both renumber the list. The source keeps "1.". Write and Read show numbers, letters, then Roman numerals. - Indent (card 696c30ce): a fixed 4 spaces in Write and Source. - The code menu uses CodeMirror commands. The command palette item and the Monaco styles, search, theme, and workers are removed. - @codemirror/legacy-modes is pinned to 6.5.4. Version 6.5.5 uses parser types that do not match @codemirror/language 6.12. - 26 smoke tests use CodeMirror instead of Monaco. New tests: editor/listNesting and the markdownLists unit tests. - Docs: development, UI design, plugin host, user guide, and UI-040. pnpm check, pnpm test (341 of 341), and pnpm build pass. The full smoke suite passes, except one template test that passed on retry.
131 lines
9.0 KiB
JavaScript
131 lines
9.0 KiB
JavaScript
// Writer interactions: code selection and copy, caret and scroll restore, Ctrl+click links, shared heading typography, and spelling targets.
|
|
import assert from "node:assert/strict";
|
|
import { mockTauri, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs";
|
|
|
|
await runSmoke("editor/interactions", async ({ page }) => {
|
|
await page.context().grantPermissions(["clipboard-read", "clipboard-write"]);
|
|
await mockTauri(page, {
|
|
commands: test => ({
|
|
...test.workspace({
|
|
root: "C:\\editor-interactions-smoke",
|
|
files: {
|
|
"note.md": "# Stack\n\nHardware can be reused.\nAssume speeds of 1 Gb/s.\n\n## Hardware\n\n### Router\n\n- [ ] First task\n- [ ] Second task\n\n[Local](other.md) and [Web](https://example.com) and [[other|Wiki]]\n\n```sh\npnpm install --frozen-lockfile\npnpm check\npnpm test\n```\n\n" + Array.from({ length: 80 }, (_, i) => `Paragraph ${i}.\n\n`).join("") + "```js\nconst preserve = 42;\nconsole.log(preserve);\n```\n",
|
|
"other.md": "# Other document\n\nKeep the original tab.\n",
|
|
},
|
|
}),
|
|
"plugin:opener|open_url": () => null,
|
|
spelling_menu_install: () => null,
|
|
}),
|
|
});
|
|
async function writer() {
|
|
await page.locator(".cm-content").waitFor();
|
|
await page.evaluate(async () => {
|
|
const { EditorView } = await import("/node_modules/@codemirror/view/dist/index.js");
|
|
window.writerView = EditorView.findFromDOM(document.querySelector(".cm-content"));
|
|
});
|
|
}
|
|
async function top() {
|
|
await writer();
|
|
await page.evaluate(() => { window.writerView.dispatch({ selection: { anchor: 0 } }); window.writerView.scrollDOM.scrollTop = 0; });
|
|
}
|
|
await page.goto(smokeUrl);
|
|
await page.locator('[data-tree-rel="note.md"]').dblclick();
|
|
await writer();
|
|
const code = page.locator(".writer-code-line").filter({ hasText: "pnpm install" });
|
|
await code.scrollIntoViewIfNeeded();
|
|
const points = await page.evaluate(() => {
|
|
const view = window.writerView, from = view.state.doc.toString().indexOf("pnpm install");
|
|
return { from: view.coordsAtPos(from), to: view.coordsAtPos(from + 12) };
|
|
});
|
|
await page.mouse.move(points.from.left + 1, (points.from.top + points.from.bottom) / 2);
|
|
await page.mouse.down();
|
|
await page.mouse.move(points.to.left, (points.to.top + points.to.bottom) / 2, { steps: 8 });
|
|
await page.mouse.up();
|
|
assert.match(await page.evaluate(() => { const view = window.writerView; return view.state.sliceDoc(view.state.selection.main.from, view.state.selection.main.to); }), /pnpm install/);
|
|
// A selection inside a code block shows its source and hides the copy button (src/lib/editor/liveMarkdown.ts:246-247).
|
|
// Move the caret out of the block, as a reader would before copying.
|
|
await page.evaluate(() => { window.writerView.dispatch({ selection: { anchor: 0 } }); });
|
|
await page.getByRole("button", { name: "Copy code", exact: true }).first().click();
|
|
assert.equal((await page.evaluate(() => navigator.clipboard.readText())).replace(/\r\n/g, "\n"), "pnpm install --frozen-lockfile\npnpm check\npnpm test\n");
|
|
console.log("PASS: first drag selects code and the copy button copies only the code body");
|
|
|
|
await page.evaluate(() => { const view = window.writerView; view.dispatch({ selection: { anchor: view.state.doc.toString().indexOf("preserve =") + 5 }, scrollIntoView: true }); view.focus(); });
|
|
await page.keyboard.type("X");
|
|
await page.waitForTimeout(250);
|
|
const saved = await page.evaluate(() => ({ head: window.writerView.state.selection.main.head, scroll: window.writerView.scrollDOM.scrollTop, doc: window.writerView.state.sliceDoc(), height: window.writerView.scrollDOM.scrollHeight, caret: window.writerView.coordsAtPos(window.writerView.state.selection.main.head) }));
|
|
assert.ok(saved.scroll > 1000);
|
|
await page.locator('[data-tree-rel="other.md"]').dblclick();
|
|
await writer();
|
|
await page.getByRole("tab", { name: "note.md", exact: true }).click();
|
|
await writer();
|
|
await page.waitForTimeout(250);
|
|
const restored = await page.evaluate(() => ({ head: window.writerView.state.selection.main.head, scroll: window.writerView.scrollDOM.scrollTop, doc: window.writerView.state.sliceDoc(), height: window.writerView.scrollDOM.scrollHeight, caret: window.writerView.coordsAtPos(window.writerView.state.selection.main.head) }));
|
|
assert.equal(restored.head, saved.head);
|
|
assert.equal(restored.doc, saved.doc);
|
|
assert.ok(Math.abs(restored.caret.top - saved.caret.top) < 3, JSON.stringify({ saved, restored }));
|
|
await page.evaluate(() => { window.dispatchEvent(new Event("blur")); window.dispatchEvent(new Event("focus")); });
|
|
await page.waitForTimeout(150);
|
|
assert.equal(await page.evaluate(() => window.writerView.state.selection.main.head), saved.head);
|
|
console.log("PASS: code edit, caret, and scroll survive tab and window focus changes");
|
|
|
|
await top();
|
|
await page.locator('.writer-link[data-href="other.md"]').click({ modifiers: ["Control"] });
|
|
await page.getByRole("tab", { name: "other.md", exact: true }).waitFor();
|
|
assert.equal(await page.getByRole("tab", { name: "note.md", exact: true }).count(), 1);
|
|
await page.getByRole("tab", { name: "note.md", exact: true }).click();
|
|
await top();
|
|
await page.locator('.writer-link[data-href="https://example.com"]').click({ modifiers: ["Control"] });
|
|
await page.waitForFunction(() => window.__TEST__.calls.some(call => call.command === "plugin:opener|open_url" && call.args.url === "https://example.com"));
|
|
await page.keyboard.press("Control+e");
|
|
await page.locator(".source-editor .cm-content").waitFor();
|
|
const linkPoint = 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"));
|
|
const position = view.state.doc.toString().indexOf("[Local]") + 3;
|
|
view.dispatch({ selection: { anchor: position }, scrollIntoView: true }); view.focus();
|
|
return position;
|
|
});
|
|
await page.waitForTimeout(300);
|
|
const point = await page.evaluate(async position => {
|
|
const { EditorView } = await import("/node_modules/@codemirror/view/dist/index.js");
|
|
const coords = EditorView.findFromDOM(document.querySelector(".source-editor .cm-content")).coordsAtPos(position);
|
|
return { x: coords.left + 2, y: (coords.top + coords.bottom) / 2 };
|
|
}, linkPoint);
|
|
await page.keyboard.down("Control"); await page.mouse.click(point.x, point.y); await page.keyboard.up("Control");
|
|
await page.waitForFunction(() => document.querySelector('[role="tab"][aria-selected="true"]')?.textContent.includes("other.md"));
|
|
console.log("PASS: Writer and IDE Ctrl+click open workspace links while retaining the source tab");
|
|
|
|
await page.getByRole("tab", { name: "note.md", exact: true }).click();
|
|
await page.keyboard.press("Control+e");
|
|
await top();
|
|
const output = await outputDir("editor/interactions");
|
|
for (const theme of ["dark", "light"]) {
|
|
await page.evaluate(async mode => {
|
|
const { applyAppearance, parseAppearance } = await import("/src/lib/theme/appearance.ts");
|
|
applyAppearance({ ...parseAppearance(null), mode });
|
|
}, theme);
|
|
const writing = await page.locator(".writer-h1").first().evaluate(node => { const style = getComputedStyle(node); return { size: style.fontSize, line: style.lineHeight, family: style.fontFamily, weight: style.fontWeight }; });
|
|
await page.screenshot({ path: `${output}/editor-typography-${theme}.png` });
|
|
await page.getByRole("group", { name: "Document view", exact: true }).hover();
|
|
await page.getByRole("button", { name: "Read", exact: true }).click();
|
|
const reading = await page.locator(".reading h1:not(.document-title)").first().evaluate(node => { const style = getComputedStyle(node); return { size: style.fontSize, line: style.lineHeight, family: style.fontFamily, weight: style.fontWeight }; });
|
|
assert.deepEqual(writing, reading);
|
|
await page.screenshot({ path: `${output}/reader-typography-${theme}.png` });
|
|
await page.getByRole("group", { name: "Document view", exact: true }).hover();
|
|
await page.getByRole("button", { name: "Edit", exact: true }).click();
|
|
await top();
|
|
}
|
|
await page.setViewportSize({ width: 640, height: 750 });
|
|
await page.getByRole("button", { name: "Hide outline", exact: true }).click();
|
|
await page.screenshot({ path: `${output}/editor-typography-narrow.png` });
|
|
assert.ok(await page.locator(".cm-content").evaluate(node => node.getBoundingClientRect().width <= node.closest(".cm-scroller").getBoundingClientRect().width + 1));
|
|
const spelling = await page.evaluate(async () => {
|
|
const { isSpellingTarget } = await import("/src/lib/editor/spellingMenu.ts");
|
|
const area = document.createElement("textarea"); area.spellcheck = true; document.body.append(area);
|
|
const active = isSpellingTarget(area); area.readOnly = true; const readOnly = isSpellingTarget(area); area.remove();
|
|
return { active, readOnly };
|
|
});
|
|
assert.deepEqual(spelling, { active: true, readOnly: false });
|
|
console.log("PASS: shared heading typography in both themes, narrow Writer, and textarea spelling target");
|
|
}, { viewport: { width: 1280, height: 900 }, timeout: 15000 });
|