Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions web/e2e/wordcounter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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()
})
11 changes: 11 additions & 0 deletions web/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export default function Editor({
const [mounted, setMounted] = useState<EditorView | null>(null)
const [limitTarget, setLimitTarget] = useState<LimitTarget | null>(null)
const contextPos = useRef<number | null>(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<QuickHandle>(null)
Expand Down Expand Up @@ -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}`)
Expand Down Expand Up @@ -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}` : '')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double-click a word, right-click it, dismiss the context menu, then clear the selection and double-click the same word again. The toolbar stays hidden because the new selection has the same offsets as the one saved here. Could we clear the suppression when the selection changes so it applies only to that right-click selection?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: the suppression is cleared as soon as the selection changes, so it only applies to the selection that right-click made. Reselecting the same word shows the toolbar again. Covered by the new test 'selecting the right-clicked word again shows the quick actions toolbar' (it fails without the fix in Chrome; the spec passes in Chrome and WebKit).

}
mark()
requestAnimationFrame(mark)
}}
>
<WithContextMenu items={menu} className="h-full select-text">
Expand All @@ -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}
Expand Down
8 changes: 6 additions & 2 deletions web/src/QuickActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ 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<QuickHandle>
onCite?: () => void // open "Add a reference" for the selection
instructions?: () => Promise<string | undefined> // the project's REFRACT.md, as sent to the server
}

// 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<Job | null>(null) // running, or finished with an error/explanation
const [running, setRunning] = useState(false)
const [error, setError] = useState('')
Expand Down Expand Up @@ -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
Expand Down
Loading