Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

fix: keep the editor typable after clicking a highlighted lint (#3834)

Matt Van Hornf7683239f605
diff --git a/packages/chrome-plugin/tests/simple_textarea.spec.ts b/packages/chrome-plugin/tests/simple_textarea.spec.ts
index 5de681031..88b825ef4 100644
--- a/packages/chrome-plugin/tests/simple_textarea.spec.ts
+++ b/packages/chrome-plugin/tests/simple_textarea.spec.ts
@@ -20,6 +20,76 @@ testCanIgnoreSuggestion(TEST_PAGE_URL, getTextarea);
testCanBlockRuleSuggestion(TEST_PAGE_URL, getTextarea);
testMultipleSuggestionsAndUndo(TEST_PAGE_URL, getTextarea);
+test('Keeps the textarea typable after opening a suggestion with a click', async ({ page }) => {
+ await page.goto(TEST_PAGE_URL);
+
+ const editor = getTextarea(page);
+ await replaceEditorContent(editor, 'This word is misspellled.');
+
+ expect(await clickHarperHighlight(page)).toBe(true);
+ await page.locator('.harper-container').waitFor({ state: 'visible' });
+ await assertLocatorIsFocused(page, editor);
+
+ const valueBeforeDelete = await editor.inputValue();
+ await page.keyboard.press('Delete');
+ expect(await editor.inputValue()).not.toBe(valueBeforeDelete);
+});
+
+test('Focuses suggestions when opening the popup with the activation key', async ({
+ browserName,
+ context,
+ page,
+}) => {
+ test.skip(
+ browserName === 'firefox',
+ 'Firefox MV3 background context is not exposed reliably in playwright-webextext.',
+ );
+ const background = await getBackground(context);
+ await background.evaluate(() => chrome.storage.local.set({ activationKey: 'shift' }));
+ await page.goto(TEST_PAGE_URL);
+
+ const editor = getTextarea(page);
+ await replaceEditorContent(editor, 'This is an test.');
+ await page.locator('#harper-highlight').waitFor({ state: 'visible' });
+
+ await page.keyboard.press('Shift');
+ await page.waitForTimeout(50);
+ await page.keyboard.press('Shift');
+
+ const firstSuggestion = page.getByTitle('Click to replace "an" with "a"');
+ await firstSuggestion.waitFor({ state: 'visible' });
+ // The row lives in the popup's shadow root, so `:focus` also matches its host.
+ await expect(firstSuggestion).toBeFocused();
+
+ await page.keyboard.press('Escape');
+ await page.locator('.harper-container').waitFor({ state: 'hidden' });
+ await assertLocatorIsFocused(page, editor);
+
+ await page.keyboard.press('Shift');
+ await page.waitForTimeout(50);
+ await page.keyboard.press('Shift');
+ await firstSuggestion.waitFor({ state: 'visible' });
+ await expect(firstSuggestion).toBeFocused();
+
+ await page.keyboard.press('Enter');
+ await expect(editor).toHaveValue('This is a test.');
+});
+
+test('Dismisses a click-opened popup when clicking outside its highlight', async ({ page }) => {
+ await page.goto(TEST_PAGE_URL);
+
+ const editor = getTextarea(page);
+ await replaceEditorContent(editor, 'This word is misspellled.');
+
+ expect(await clickHarperHighlight(page)).toBe(true);
+ await page.locator('.harper-container').waitFor({ state: 'visible' });
+
+ await editor.click({ position: { x: 2, y: 2 } });
+
+ await page.locator('.harper-container').waitFor({ state: 'hidden' });
+ await assertLocatorIsFocused(page, editor);
+});
+
test('Wraps correctly', async ({ page }, testInfo) => {
await page.goto(TEST_PAGE_URL);
diff --git a/packages/lint-framework/src/lint/PopupHandler.ts b/packages/lint-framework/src/lint/PopupHandler.ts
index e25a33d22..b9f183af2 100644
--- a/packages/lint-framework/src/lint/PopupHandler.ts
+++ b/packages/lint-framework/src/lint/PopupHandler.ts
@@ -33,6 +33,8 @@ export default class PopupHandler {
private currentLintBoxes: IgnorableLintBox[];
private popupLint: number | undefined;
private selectedSuggestion = 0;
+ /** Only a keyboard activation moves focus into the popup; a click leaves it in the editor. */
+ private autofocusSuggestion = false;
private currentHint: string | null | undefined;
private currentHintFor: number | undefined;
private renderBox: RenderBox;
@@ -98,6 +100,7 @@ export default class PopupHandler {
this.popupLint = closestIdx;
this.selectedSuggestion = 0;
+ this.autofocusSuggestion = true;
this.render();
if (popupFocused) this.focusSelectedSuggestion();
}
@@ -119,6 +122,7 @@ export default class PopupHandler {
if (this.popupLint != null) this.close();
this.popupLint = i;
this.selectedSuggestion = 0;
+ this.autofocusSuggestion = false;
this.render();
return;
}
@@ -130,6 +134,7 @@ export default class PopupHandler {
private close = () => {
this.popupLint = undefined;
this.selectedSuggestion = 0;
+ this.autofocusSuggestion = false;
this.render();
};
@@ -153,6 +158,7 @@ export default class PopupHandler {
tree = SuggestionBox({
box,
selectedIndex: this.selectedSuggestion,
+ autofocus: this.autofocusSuggestion,
onSelect: (index) => {
if (index === this.selectedSuggestion) return;
this.selectedSuggestion = index;
diff --git a/packages/lint-framework/src/lint/SuggestionBox.ts b/packages/lint-framework/src/lint/SuggestionBox.ts
index a5c6a3834..f442bad86 100644
--- a/packages/lint-framework/src/lint/SuggestionBox.ts
+++ b/packages/lint-framework/src/lint/SuggestionBox.ts
@@ -152,7 +152,7 @@ function popupHook(props: Parameters<typeof SuggestionBox>[0], refocusClose: ()
const frame = requestAnimationFrame(() => {
if (!node.isConnected) return;
measure();
- if (!node.dataset.autofocused) {
+ if (props.autofocus && !node.dataset.autofocused) {
saveCursorState();
node.dataset.autofocused = 'true';
focusRow(node, props.selectedIndex);
@@ -502,6 +502,8 @@ function styleTag(kind: LintKind) {
export default function SuggestionBox(props: {
box: IgnorableLintBox;
selectedIndex: number;
+ /** Move focus to the selected row when the popup opens. */
+ autofocus: boolean;
onSelect: (index: number) => void;
/** Optional consumer capabilities; the popup never persists configuration itself. */
actions: {