diff --git a/e2e/cursor-reset.test.ts b/e2e/cursor-reset.test.ts new file mode 100644 index 00000000..74133248 --- /dev/null +++ b/e2e/cursor-reset.test.ts @@ -0,0 +1,47 @@ +// Regression guard: the loading→ready widget transition fires +// `rebuildEditorState` *after* the editor is interactive, and used to +// reset the selection to position 0. + +import { expect, gotoSilverBulletPage, test } from "./fixtures.ts"; + +test.use({ + spaceFiles: { + "CursorTest.md": `# Title + +Line 2 +Line 3 +Line 4 +Line 5 +Line 6 +Line 7 +Line 8 +Line 9 +Line 10 + +\${"a lua directive"} + +Line 13 +Line 14 +`, + }, +}); + +test("cursor placed mid-document stays put across widget rebuild", async ({ sbServer, page }) => { + await gotoSilverBulletPage(page, sbServer, "CursorTest"); + + await page.evaluate(() => { + const view = (globalThis as any).client.editorView; + view.dispatch({ selection: { anchor: view.state.doc.line(8).from } }); + view.focus(); + }); + + // Sample a few times across the window where the rebuild could fire. + for (let i = 0; i < 10; i++) { + await page.waitForTimeout(150); + const line = await page.evaluate(() => { + const v = (globalThis as any).client.editorView; + return v.state.doc.lineAt(v.state.selection.main.head).number; + }); + expect(line).toBe(8); + } +}); diff --git a/e2e/inline-lua-directive.test.ts b/e2e/inline-lua-directive.test.ts new file mode 100644 index 00000000..d2086472 --- /dev/null +++ b/e2e/inline-lua-directive.test.ts @@ -0,0 +1,44 @@ +// Regression guard: inline `${…}` lua directives must render inline, +// on the same visual line as the surrounding text. They were briefly +// broken onto their own line when a `width: 100%` rule on +// `.sb-lua-wrapper` (added to keep block directives editor-width) +// applied to inline wrappers too. + +import { expect, gotoSilverBulletPage, test } from "./fixtures.ts"; + +test.use({ + spaceFiles: { + "Inline.md": `Add two numbers \${10 + 2} and continue.\n`, + }, +}); + +test("inline lua directive stays on the same line as surrounding text", async ({ sbServer, page }) => { + await gotoSilverBulletPage(page, sbServer, "Inline"); + + await page.waitForFunction(() => { + return !!document.querySelector( + "#sb-editor .sb-lua-wrapper .sb-lua-directive-inline", + ); + }, undefined, { timeout: 10_000 }); + + const layout = await page.evaluate(() => { + const line = document.querySelector( + "#sb-editor .cm-content .cm-line", + ) as HTMLElement; + const wrapper = line.querySelector(".sb-lua-wrapper") as HTMLElement; + const cs = getComputedStyle(line); + return { + lineHeight: line.getBoundingClientRect().height, + defaultLineHeight: parseFloat(cs.lineHeight), + lineWidth: line.getBoundingClientRect().width, + wrapperWidth: wrapper.getBoundingClientRect().width, + }; + }); + + // The cm-line must remain a single visual line. If the inline + // directive broke onto its own line, cm-line would be ≥2× the + // editor's line-height (it would contain two stacked line boxes). + expect(layout.lineHeight).toBeLessThan(layout.defaultLineHeight * 1.5); + // And the wrapper must be shrink-to-fit, not the full content width. + expect(layout.wrapperWidth).toBeLessThan(layout.lineWidth * 0.5); +}); diff --git a/e2e/widget-offscreen-arrow-skip.test.ts b/e2e/widget-offscreen-arrow-skip.test.ts new file mode 100644 index 00000000..d2890440 --- /dev/null +++ b/e2e/widget-offscreen-arrow-skip.test.ts @@ -0,0 +1,80 @@ +// Regression guard for #1985: arrow keys / clicks below a tall block +// widget (e.g. a `${query[[…]]}` rendering as a table) used to drift by +// a line or two when content was scrolled past the widget. + +import { expect, gotoSilverBulletPage, test } from "./fixtures.ts"; + +function manyPageSeed(n: number): Record { + const out: Record = {}; + for (let i = 1; i <= n; i++) { + out[`Page${i}.md`] = `#page\n\nThis is page ${i}\n`; + } + return out; +} + +test.use({ + spaceFiles: { + ...manyPageSeed(20), + "CursorBug.md": `Hello there [[aspiring page]] + +* Hello +* There +* This +* is a list + +\${query[[from o = index.tag "page" limit 20]]} +Sup yo + +* Another list +* This is it +* Sup +`, + }, + viewport: { width: 900, height: 500 }, +}); + +test("arrow keys advance one line at a time below a tall widget", async ({ sbServer, page }) => { + await gotoSilverBulletPage(page, sbServer, "CursorBug"); + + // Wait until the query widget has rendered tall (height map > 100px + // means it picked up the widget's actual size, not the one-line + // fallback). + await page.waitForFunction(() => { + const v = (globalThis as any).client?.editorView; + return v && v.lineBlockAt(v.state.doc.line(8).from).height > 100; + }, undefined, { timeout: 10_000 }); + + const result = await page.evaluate(() => { + const v = (globalThis as any).client.editorView; + v.scrollDOM.scrollTop = v.scrollDOM.scrollHeight; + const line13 = v.state.doc.line(13); + v.dispatch({ selection: { anchor: line13.from + line13.text.length } }); + + // Sweep up from the bottom list through the widget. + const visited = [v.state.doc.lineAt(v.state.selection.main.head).number]; + let range = v.state.selection.main; + for (let i = 0; i < 5; i++) { + range = v.moveVertically(range, false); + visited.push(v.state.doc.lineAt(range.head).number); + } + + // Click resolution: posAtCoords at each rendered line's vertical + // center must resolve to that same line. + const clicks: Array<{ line: number; resolvedLine: number | null }> = []; + for (let i = 9; i <= 13; i++) { + const ln = v.state.doc.line(i); + const c = v.coordsAtPos(ln.from); + const p = v.posAtCoords({ x: c.left + 20, y: (c.top + c.bottom) / 2 }); + clicks.push({ + line: i, + resolvedLine: p != null ? v.state.doc.lineAt(p).number : null, + }); + } + return { visited, clicks }; + }); + + expect(result.visited).toEqual([13, 12, 11, 10, 9, 8]); + for (const { line, resolvedLine } of result.clicks) { + expect(resolvedLine).toBe(line); + } +});