diff --git a/web/e2e/wordcounter.spec.ts b/web/e2e/wordcounter.spec.ts index 6a735f4..5e3042f 100644 --- a/web/e2e/wordcounter.spec.ts +++ b/web/e2e/wordcounter.spec.ts @@ -60,13 +60,6 @@ async function abstractLimit(page: Page, max = '3') { .click({ position: { x: 10, y: 8 } }) } -// WebKit (like macOS) selects the word under a right-click, and the quick actions toolbar for that -// selection can cover the next line (#102). Collapse it with the keyboard, as a person would. -async function collapseRightClickSelection(page: Page) { - await expect(page.getByRole('dialog')).not.toBeVisible() - await page.keyboard.press('ArrowRight') -} - const shorter = (page: Page) => page.getByRole('button', { name: /^Suggest a shorter version:/ }) async function openWordCountSettings(page: Page) { @@ -434,7 +427,6 @@ Other prose here. await openLimit(page, heading) await page.getByLabel('Maximum words').fill('1') await page.getByRole('button', { name: 'Set limit', exact: true }).click() - await collapseRightClickSelection(page) } for (const [line, name] of [ ['\\section{Main}', 'Main'], @@ -477,7 +469,6 @@ Other prose here.` await openLimit(page, heading) await page.getByLabel('Maximum words').fill('1') await page.getByRole('button', { name: 'Set limit', exact: true }).click() - await collapseRightClickSelection(page) } await page .locator('.cm-line') @@ -607,3 +598,45 @@ Este texto supera su limite.` await openLimit(page, '\\subsection{Contexto}') await expect(page.getByLabel('Maximum words')).toHaveValue('1') }) + +test('a right-click selection shows no quick actions toolbar, a keyboard selection does', async ({ page, request }) => { + await openProject(page, request, true) + const toolbar = page.getByRole('toolbar', { name: 'Quick actions' }) + const line = page.locator('.cm-line').filter({ hasText: 'One two three' }) + await line.click({ button: 'right', position: { x: 10, y: 8 } }) + await expect(page.getByRole('menuitem', { name: 'Word limit…', exact: true })).toBeVisible() + await expect(toolbar).toHaveCount(0) + await page.keyboard.press('Escape') + await line.click({ position: { x: 10, y: 8 } }) + await page.keyboard.press('Shift+ArrowRight') + await page.keyboard.press('Shift+ArrowRight') + await expect(toolbar).toBeVisible() +}) + +test('Ask the assistant… from the context menu opens the instruction field for a right-click selection', async ({ + page, + request, +}) => { + await openProject(page, request, true) + const word = { position: { x: 10, y: 8 } } + const line = page.locator('.cm-line').filter({ hasText: 'One two three' }) + await line.dblclick(word) + await line.click({ button: 'right', ...word }) + await page.getByRole('menuitem', { name: 'Ask the assistant…', exact: true }).click() + await expect(page.getByRole('textbox', { name: 'Instruction' })).toBeVisible() +}) + +test('selecting the right-clicked word again shows the quick actions toolbar', async ({ page, request }) => { + await openProject(page, request, true) + const toolbar = page.getByRole('toolbar', { name: 'Quick actions' }) + const word = { position: { x: 10, y: 8 } } + const line = page.locator('.cm-line').filter({ hasText: 'One two three' }) + await line.dblclick(word) + await line.click({ button: 'right', ...word }) + await expect(page.getByRole('menuitem', { name: 'Word limit…', exact: true })).toBeVisible() + await page.keyboard.press('Escape') + await expect(toolbar).toHaveCount(0) + await page.keyboard.press('End') + await line.dblclick(word) + await expect(toolbar).toBeVisible() +}) diff --git a/web/src/Editor.tsx b/web/src/Editor.tsx index 83c1192..d65c635 100644 --- a/web/src/Editor.tsx +++ b/web/src/Editor.tsx @@ -106,6 +106,7 @@ export default function Editor({ const [mounted, setMounted] = useState(null) const [limitTarget, setLimitTarget] = useState(null) const contextPos = useRef(null) + const [menuSel, setMenuSel] = useState('') // selection a right-click made: the context menu is the intent, no toolbar const [dragging, setDragging] = useState(false) // no toolbar while a mouse selection is being made const [, bump] = useReducer((n: number) => n + 1, 0) // re-render the toolbar (it reads the view) const quickRef = useRef(null) @@ -267,6 +268,8 @@ export default function Editor({ bump() if (u.selectionSet || u.docChanged) { const { from, to, empty } = u.state.selection.main + // The right-click suppression covers only that selection: any other one shows the toolbar again. + setMenuSel((s) => (s && s !== `${from}-${to}` ? '' : s)) const a = u.state.doc.lineAt(from).number const b = u.state.doc.lineAt(to).number cb.current.onSelect?.(empty ? '' : a === b ? `${a}` : `${a}–${b}`) @@ -474,6 +477,13 @@ export default function Editor({ className="relative min-h-0 flex-1" onContextMenuCapture={(e) => { contextPos.current = view.current?.posAtCoords({ x: e.clientX, y: e.clientY }) ?? null + // WebKit selects the word under a right-click, maybe just after this event. + const mark = () => { + const m = view.current?.state.selection.main + setMenuSel(m && !m.empty ? `${m.from}-${m.to}` : '') + } + mark() + requestAnimationFrame(mark) }} > @@ -497,6 +507,7 @@ export default function Editor({ view={mounted} path={path} dragging={dragging} + hideSelection={menuSel} handle={quickRef} onCite={onCite ? () => cite(true) : undefined} instructions={instructions} diff --git a/web/src/QuickActions.tsx b/web/src/QuickActions.tsx index f4de972..cb5067b 100644 --- a/web/src/QuickActions.tsx +++ b/web/src/QuickActions.tsx @@ -58,6 +58,7 @@ type Props = { view: EditorView path: string dragging: boolean + hideSelection?: string // `from-to` of a selection made by a right-click: no toolbar unless asked for handle: Ref onCite?: () => void // open "Add a reference" for the selection instructions?: () => Promise // the project's REFRACT.md, as sent to the server @@ -65,7 +66,7 @@ type Props = { // Floating toolbar over the editor selection: one-shot AI edits (§4.7), shown // as an inline diff to accept (Tab) or reject (Esc). -export default function QuickActions({ view, path, dragging, handle, onCite, instructions }: Props) { +export default function QuickActions({ view, path, dragging, hideSelection, handle, onCite, instructions }: Props) { const [job, setJob] = useState(null) // running, or finished with an error/explanation const [running, setRunning] = useState(false) const [error, setError] = useState('') @@ -168,7 +169,10 @@ export default function QuickActions({ view, path, dragging, handle, onCite, ins setAsking(false) }, [sel.from, sel.to]) - const visible = !dragging && (view.hasFocus || within || !!job || !!pending) + const visible = + !dragging && + !(!pending && !job && !asking && hideSelection === `${sel.from}-${sel.to}`) && + (view.hasFocus || within || !!job || !!pending) const a = range && visible ? view.coordsAtPos(range.from) : null const box = view.dom.getBoundingClientRect() const above = a ? a.top - box.top - 44 : 0