Skip to content

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

fix(chrome-ext): scrolling on Notion + other tweaks (#1317)

Elijah Potteraef7207d1373
diff --git a/harper-core/src/linting/phrase_corrections.rs b/harper-core/src/linting/phrase_corrections.rs
index 669eb9623..d561da4fb 100644
--- a/harper-core/src/linting/phrase_corrections.rs
+++ b/harper-core/src/linting/phrase_corrections.rs
@@ -1249,6 +1249,12 @@ pub fn lint_group() -> LintGroup {
"The standard form is `as of late`.",
"Corrects `as of lately` to `as of late`."
),
+ "RapidFire" => (
+ ["rapid fire"],
+ ["rapid-fire"],
+ "It is more idiomatic to hypenate `rapid-fire`.",
+ "Checks to ensure writers hyphenate `rapid-fire`."
+ ),
"PointsOfView" => (
["point of views"],
["points of view"],
diff --git a/packages/chrome-plugin/src/Highlights.ts b/packages/chrome-plugin/src/Highlights.ts
index 70a128507..696a5643e 100644
--- a/packages/chrome-plugin/src/Highlights.ts
+++ b/packages/chrome-plugin/src/Highlights.ts
@@ -9,6 +9,7 @@ import {
getLexicalRoot,
getMediumRoot,
getNotionRoot,
+ getP2Root,
getQuillJsRoot,
getShredditComposerRoot,
getSlateRoot,
@@ -132,6 +133,7 @@ export default class Highlights {
getShredditComposerRoot,
getQuillJsRoot,
getLexicalRoot,
+ getP2Root,
getTrixRoot,
];
diff --git a/packages/chrome-plugin/src/LintFramework.ts b/packages/chrome-plugin/src/LintFramework.ts
index 2e55b40fe..f08459470 100644
--- a/packages/chrome-plugin/src/LintFramework.ts
+++ b/packages/chrome-plugin/src/LintFramework.ts
@@ -1,4 +1,3 @@
-import { memoize } from 'lodash-es';
import Highlights from './Highlights';
import PopupHandler from './PopupHandler';
import ProtocolClient from './ProtocolClient';
@@ -93,7 +92,7 @@ export default class LintFramework {
}
const observer = new MutationObserver(this.updateEventCallback);
- const config = { attributes: true, childList: true, subtree: true, characterData: true };
+ const config = { subtree: true, characterData: true };
if (target.tagName == undefined) {
observer.observe(target.parentElement!, config);
@@ -144,10 +143,12 @@ function getScrollableAncestors(element: Element): Element[] {
const { overflowY, overflowX } = style;
// Vertical scroll check: overflow-y is scrollable and content overflows
const canScrollY =
- (overflowY === 'auto' || overflowY === 'scroll') && parent.scrollHeight > parent.clientHeight;
+ (overflowY.includes('auto') || overflowY.includes('scroll')) &&
+ parent.scrollHeight > parent.clientHeight;
// Horizontal scroll check: overflow-x is scrollable and content overflows
const canScrollX =
- (overflowX === 'auto' || overflowX === 'scroll') && parent.scrollWidth > parent.clientWidth;
+ (overflowX.includes('auto') || overflowX.includes('scroll')) &&
+ parent.scrollWidth > parent.clientWidth;
if (canScrollY || canScrollX) {
scrollables.push(parent);
}
diff --git a/packages/chrome-plugin/src/background/index.ts b/packages/chrome-plugin/src/background/index.ts
index 8430f3b84..41768ff29 100644
--- a/packages/chrome-plugin/src/background/index.ts
+++ b/packages/chrome-plugin/src/background/index.ts
@@ -55,7 +55,7 @@ async function enableDefaultDomains() {
'playground.lexical.dev',
'discord.com',
'youtube.com',
- 'instagram.com',
+ 'www.instagram.com',
'web.whatsapp.com',
'outlook.live.com',
'www.reddit.com',
diff --git a/packages/chrome-plugin/src/computeLintBoxes.ts b/packages/chrome-plugin/src/computeLintBoxes.ts
index e256a41ff..6cba6b7bd 100644
--- a/packages/chrome-plugin/src/computeLintBoxes.ts
+++ b/packages/chrome-plugin/src/computeLintBoxes.ts
@@ -2,7 +2,7 @@ import { type IgnorableLintBox, type LintBox, domRectToBox, isBottomEdgeInBox }
import ProtocolClient from './ProtocolClient';
import TextFieldRange from './TextFieldRange';
import { getRangeForTextSpan } from './domUtils';
-import { getSlateRoot } from './editorUtils';
+import { getLexicalRoot, getSlateRoot } from './editorUtils';
import { type UnpackedLint, type UnpackedSuggestion, applySuggestion } from './unpackLint';
function isFormEl(el: HTMLElement): el is HTMLTextAreaElement | HTMLInputElement {
@@ -68,11 +68,12 @@ export default function computeLintBoxes(el: HTMLElement, lint: UnpackedLint): I
function replaceValue(el: HTMLElement, value: string) {
const slateRoot = getSlateRoot(el);
+ const lexicalRoot = getLexicalRoot(el);
if (isFormEl(el)) {
el.value = value;
- } else if (slateRoot != null) {
- replaceValueSlate(el, value);
+ } else if (slateRoot != null || lexicalRoot != null) {
+ replaceValueSpecial(el, value);
} else {
el.textContent = value;
@@ -83,13 +84,13 @@ function replaceValue(el: HTMLElement, value: string) {
el.dispatchEvent(new Event('change', { bubbles: true }));
}
-/** Replace the content of a Slate editor node. */
-function replaceValueSlate(el: HTMLElement, value: string) {
- slateSelectAllText(el);
- slateInsertText(el, value);
+/** Replace the content of a special editor node. */
+function replaceValueSpecial(el: HTMLElement, value: string) {
+ specialSelectAllText(el);
+ specialInsertText(el, value);
}
-function slateSelectAllText(target: Node): Range {
+function specialSelectAllText(target: Node): Range {
const range = target.ownerDocument!.createRange();
if (target.nodeType === Node.TEXT_NODE) {
const len = (target as Text).data.length;
@@ -104,7 +105,7 @@ function slateSelectAllText(target: Node): Range {
return range;
}
-function slateInsertText(el: HTMLElement, raw: string): void {
+function specialInsertText(el: HTMLElement, raw: string): void {
const inputType = 'insertText';
const evInit: InputEventInit = {
diff --git a/packages/chrome-plugin/src/editorUtils.ts b/packages/chrome-plugin/src/editorUtils.ts
index 1485a7c10..00534779a 100644
--- a/packages/chrome-plugin/src/editorUtils.ts
+++ b/packages/chrome-plugin/src/editorUtils.ts
@@ -15,6 +15,12 @@ export function findAncestor(
return null;
}
+/** Determines if a given node is a child of a P2 editor instance.
+ * If so, returns the root node of that instance. */
+export function getP2Root(el: HTMLElement): HTMLElement | null {
+ return findAncestor(el, (node: HTMLElement) => node.classList.contains('p2-editor'));
+}
+
/** Determines if a given node is a child of a Lexical editor instance.
* If so, returns the root node of that instance. */
export function getLexicalRoot(el: HTMLElement): HTMLElement | null {
diff --git a/packages/chrome-plugin/tests/lexical.spec.ts b/packages/chrome-plugin/tests/lexical.spec.ts
new file mode 100644
index 000000000..86e676d58
--- /dev/null
+++ b/packages/chrome-plugin/tests/lexical.spec.ts
@@ -0,0 +1,42 @@
+import { expect, test } from './fixtures';
+import { clickHarperHighlight, getLexicalEditor, replaceEditorContent } from './testUtils';
+
+const TEST_PAGE_URL = 'https://playground.lexical.dev/';
+
+test('Can apply basic suggestion.', async ({ page }) => {
+ await page.goto(TEST_PAGE_URL);
+
+ const lexical = getLexicalEditor(page);
+ await replaceEditorContent(lexical, 'This is an test');
+
+ await page.waitForTimeout(3000);
+
+ await clickHarperHighlight(page);
+ await page.getByTitle('Replace with "a"').click();
+
+ await page.waitForTimeout(3000);
+
+ await expect(lexical).toContainText('This is a test');
+ await lexical.press('Control+ArrowDown');
+
+ await lexical.pressSequentially(" of Harper's grammar checking.");
+ await expect(lexical).toContainText("This is a test of Harper's grammar checking.");
+});
+
+test('Can ignore suggestion.', async ({ page }) => {
+ await page.goto(TEST_PAGE_URL);
+ const lexical = getLexicalEditor(page);
+
+ await replaceEditorContent(lexical, 'This is an test.');
+
+ await page.waitForTimeout(3000);
+
+ await clickHarperHighlight(page);
+ await page.getByTitle('Ignore this lint').click();
+
+ await page.waitForTimeout(3000);
+
+ // Nothing should change.
+ expect(lexical).toContainText('This is an test');
+ expect(await clickHarperHighlight(page)).toBe(false);
+});
diff --git a/packages/chrome-plugin/tests/slate.spec.ts b/packages/chrome-plugin/tests/slate.spec.ts
index db1bf2f0a..97eeae9ed 100644
--- a/packages/chrome-plugin/tests/slate.spec.ts
+++ b/packages/chrome-plugin/tests/slate.spec.ts
@@ -1,8 +1,10 @@
import { expect, test } from './fixtures';
import { clickHarperHighlight, getSlateEditor, replaceEditorContent } from './testUtils';
+const TEST_PAGE_URL = 'https://slatejs.org';
+
test('Can apply basic suggestion.', async ({ page }) => {
- await page.goto('https://slatejs.org');
+ await page.goto(TEST_PAGE_URL);
const slate = getSlateEditor(page);
await replaceEditorContent(slate, 'This is an test');
@@ -22,7 +24,7 @@ test('Can apply basic suggestion.', async ({ page }) => {
});
test('Can ignore suggestion.', async ({ page }) => {
- await page.goto('https://slatejs.org');
+ await page.goto(TEST_PAGE_URL);
const slate = getSlateEditor(page);
await replaceEditorContent(slate, 'This is an test.');
diff --git a/packages/chrome-plugin/tests/testUtils.ts b/packages/chrome-plugin/tests/testUtils.ts
index b2b4c42c0..0c6c33674 100644
--- a/packages/chrome-plugin/tests/testUtils.ts
+++ b/packages/chrome-plugin/tests/testUtils.ts
@@ -5,6 +5,11 @@ export function getSlateEditor(page: Page): Locator {
return page.locator('[data-slate-editor="true"]');
}
+/** Locate the [`Lexical`](https://lexical.dev/) editor on the page. */
+export function getLexicalEditor(page: Page): Locator {
+ return page.locator('[data-lexical-editor="true"]');
+}
+
/** Replace the content of a text editor. */
export async function replaceEditorContent(editorEl: Locator, text: string) {
await editorEl.selectText();