Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(chrome-ext): close the SuggestionBox with the Escape key (#1705)
Elijah Potterfd65edc5de1b
diff --git a/packages/chrome-plugin/src/SuggestionBox.ts b/packages/chrome-plugin/src/SuggestionBox.tsindex 9e4913f02..f397fe7f7 100644--- a/packages/chrome-plugin/src/SuggestionBox.ts+++ b/packages/chrome-plugin/src/SuggestionBox.ts@@ -22,6 +22,29 @@ FocusHook.prototype.hook = function (node, _propertyName, _previousValue) {});};+/** biome-ignore-all lint/complexity/useArrowFunction: It cannot be an arrow function for the logic to work. */+var CloseOnEscapeHook = function (onClose: () => void) {+ this.onClose = onClose;+};++CloseOnEscapeHook.prototype.hook = function (this: { onClose: () => void }, node: HTMLElement) {+ const handler = (e: KeyboardEvent) => {+ if (e.key === 'Escape') {+ this.onClose();+ }+ };+ window.addEventListener('keydown', handler);+ (node as any).__harperCloseOnEscapeHandler = handler;+};++CloseOnEscapeHook.prototype.unhook = function (this: any, node: HTMLElement) {+ const handler = (node as any).__harperCloseOnEscapeHandler;+ if (handler) {+ window.removeEventListener('keydown', handler);+ delete (node as any).__harperCloseOnEscapeHandler;+ }+};+function header(title: string, color: string, onClose: () => void): any {const closeButton = h('button',@@ -237,19 +260,27 @@ export default function SuggestionBox(box: IgnorableLintBox, close: () => void)left: `${left}px`,};- return h('div', { className: 'harper-container fade-in', style: positionStyle }, [- styleTag(),- header(box.lint.lint_kind_pretty, lintKindColor(box.lint.lint_kind), close),- body(box.lint.message_html),- footer(- suggestions(box.lint.suggestions, (v) => {- box.applySuggestion(v);- close();- }),- [- box.lint.lint_kind === 'Spelling' ? addToDictionary(box) : undefined,- ignoreLint(box.ignoreLint),- ],- ),- ]);+ return h(+ 'div',+ {+ className: 'harper-container fade-in',+ style: positionStyle,+ 'harper-close-on-escape': new CloseOnEscapeHook(close),+ },+ [+ styleTag(),+ header(box.lint.lint_kind_pretty, lintKindColor(box.lint.lint_kind), close),+ body(box.lint.message_html),+ footer(+ suggestions(box.lint.suggestions, (v) => {+ box.applySuggestion(v);+ close();+ }),+ [+ box.lint.lint_kind === 'Spelling' ? addToDictionary(box) : undefined,+ ignoreLint(box.ignoreLint),+ ],+ ),+ ],+ );}diff --git a/packages/chrome-plugin/tests/simple_textarea.spec.ts b/packages/chrome-plugin/tests/simple_textarea.spec.tsindex 514454adf..4a00eac9e 100644--- a/packages/chrome-plugin/tests/simple_textarea.spec.ts+++ b/packages/chrome-plugin/tests/simple_textarea.spec.ts@@ -1,6 +1,7 @@import { test } from './fixtures';import {assertHarperHighlightBoxes,+ clickHarperHighlight,getTextarea,replaceEditorContent,testBasicSuggestionTextarea,@@ -51,3 +52,23 @@ test('Scrolls correctly', async ({ page }) => {await assertHarperHighlightBoxes(page, [{ height: 19, width: 56, x: 97.953125, y: 63 }]);});++test('Can dismiss with escape key', async ({ page }) => {+ await page.goto(TEST_PAGE_URL);++ const editor = getTextarea(page);+ await replaceEditorContent(+ editor,+ 'This is a test of the Harper grammar checker, specifically if it is wrapped around a line weirdl y',+ );++ await page.waitForTimeout(6000);++ await clickHarperHighlight(page);++ await page.locator('.harper-container').waitFor({ state: 'visible' });++ await page.keyboard.press('Escape');++ await page.locator('.harper-container').waitFor({ state: 'hidden' });+});