Skip to content

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

feat(chrome-ext): 'block rule' button (#2145)

Elijah Potteracf9a80880bd
diff --git a/packages/chrome-plugin/src/ProtocolClient.ts b/packages/chrome-plugin/src/ProtocolClient.ts
index 9027a2449..ae54b4646 100644
--- a/packages/chrome-plugin/src/ProtocolClient.ts
+++ b/packages/chrome-plugin/src/ProtocolClient.ts
@@ -34,6 +34,12 @@ export default class ProtocolClient {
await chrome.runtime.sendMessage({ kind: 'setConfig', config: lintConfig });
}
+ public static async setRuleEnabled(ruleId: string, enabled: boolean): Promise<void> {
+ const config = await this.getLintConfig();
+ const nextConfig: LintConfig = { ...config, [ruleId]: enabled };
+ await this.setLintConfig(nextConfig);
+ }
+
public static async getLintDescriptions(): Promise<Record<string, string>> {
return (await chrome.runtime.sendMessage({ kind: 'getLintDescriptions' })).descriptions;
}
diff --git a/packages/chrome-plugin/src/contentScript/index.ts b/packages/chrome-plugin/src/contentScript/index.ts
index cfc5b2d6c..370540566 100644
--- a/packages/chrome-plugin/src/contentScript/index.ts
+++ b/packages/chrome-plugin/src/contentScript/index.ts
@@ -18,6 +18,10 @@ const fw = new LintFramework((text, domain) => ProtocolClient.lint(text, domain)
ruleId,
'',
),
+ setRuleEnabled: async (ruleId, enabled) => {
+ await ProtocolClient.setRuleEnabled(ruleId, enabled);
+ fw.update();
+ },
});
function padWithContext(source: string, start: number, end: number, contextLength: number): string {
diff --git a/packages/chrome-plugin/tests/github.spec.ts b/packages/chrome-plugin/tests/github.spec.ts
index fa2f22a1b..c43642f8e 100644
--- a/packages/chrome-plugin/tests/github.spec.ts
+++ b/packages/chrome-plugin/tests/github.spec.ts
@@ -4,6 +4,7 @@ import {
getTextarea,
replaceEditorContent,
testBasicSuggestionTextarea,
+ testCanBlockRuleTextareaSuggestion,
testCanIgnoreTextareaSuggestion,
} from './testUtils';
@@ -11,6 +12,7 @@ const TEST_PAGE_URL = 'http://localhost:8081/github_textarea.html';
testBasicSuggestionTextarea(TEST_PAGE_URL);
testCanIgnoreTextareaSuggestion(TEST_PAGE_URL);
+testCanBlockRuleTextareaSuggestion(TEST_PAGE_URL);
test('Wraps correctly', async ({ page }) => {
await page.goto(TEST_PAGE_URL);
diff --git a/packages/chrome-plugin/tests/hn.spec.ts b/packages/chrome-plugin/tests/hn.spec.ts
index 3a9393dee..231e94315 100644
--- a/packages/chrome-plugin/tests/hn.spec.ts
+++ b/packages/chrome-plugin/tests/hn.spec.ts
@@ -4,6 +4,7 @@ import {
getTextarea,
replaceEditorContent,
testBasicSuggestionTextarea,
+ testCanBlockRuleTextareaSuggestion,
testCanIgnoreTextareaSuggestion,
} from './testUtils';
@@ -11,6 +12,7 @@ const TEST_PAGE_URL = 'https://news.ycombinator.com/item?id=45798898';
testBasicSuggestionTextarea(TEST_PAGE_URL);
testCanIgnoreTextareaSuggestion(TEST_PAGE_URL);
+testCanBlockRuleTextareaSuggestion(TEST_PAGE_URL);
test('Hacker News wraps correctly', async ({ page }) => {
await page.goto(TEST_PAGE_URL);
diff --git a/packages/chrome-plugin/tests/simple_textarea.spec.ts b/packages/chrome-plugin/tests/simple_textarea.spec.ts
index 36f5c934b..b9b83a26e 100644
--- a/packages/chrome-plugin/tests/simple_textarea.spec.ts
+++ b/packages/chrome-plugin/tests/simple_textarea.spec.ts
@@ -5,6 +5,7 @@ import {
getTextarea,
replaceEditorContent,
testBasicSuggestionTextarea,
+ testCanBlockRuleTextareaSuggestion,
testCanIgnoreTextareaSuggestion,
} from './testUtils';
@@ -12,6 +13,7 @@ const TEST_PAGE_URL = 'http://localhost:8081/simple_textarea.html';
testBasicSuggestionTextarea(TEST_PAGE_URL);
testCanIgnoreTextareaSuggestion(TEST_PAGE_URL);
+testCanBlockRuleTextareaSuggestion(TEST_PAGE_URL);
test('Wraps correctly', async ({ page }, testInfo) => {
await page.goto(TEST_PAGE_URL);
diff --git a/packages/chrome-plugin/tests/testUtils.ts b/packages/chrome-plugin/tests/testUtils.ts
index 8c9f4d81a..c6c90a435 100644
--- a/packages/chrome-plugin/tests/testUtils.ts
+++ b/packages/chrome-plugin/tests/testUtils.ts
@@ -116,6 +116,25 @@ export async function testCanIgnoreTextareaSuggestion(testPageUrl: string) {
});
}
+export async function testCanBlockRuleTextareaSuggestion(testPageUrl: string) {
+ test('Can hide with rule block button', async ({ page }) => {
+ await page.goto(testPageUrl);
+
+ const editor = getTextarea(page);
+ await replaceEditorContent(editor, 'This is an test.');
+
+ await page.waitForTimeout(6000);
+
+ await clickHarperHighlight(page);
+
+ await page.getByTitle('Disable the AnA rule').click();
+
+ await page.waitForTimeout(500);
+
+ await assertHarperHighlightBoxes(page, []);
+ });
+}
+
export async function assertHarperHighlightBoxes(page: Page, boxes: Box[]): Promise<void> {
const highlights = getHarperHighlights(page);
expect(await highlights.count()).toBe(boxes.length);
diff --git a/packages/lint-framework/package.json b/packages/lint-framework/package.json
index ef30d067a..64f72de15 100644
--- a/packages/lint-framework/package.json
+++ b/packages/lint-framework/package.json
@@ -21,6 +21,8 @@
"test": "echo 'no tests'"
},
"dependencies": {
+ "@fortawesome/fontawesome-svg-core": "^7.1.0",
+ "@fortawesome/free-solid-svg-icons": "^7.1.0",
"bowser": "^2.12.1",
"virtual-dom": "^2.1.1"
},
diff --git a/packages/lint-framework/src/lint/LintFramework.ts b/packages/lint-framework/src/lint/LintFramework.ts
index 4f36e9ffa..34fdf1134 100644
--- a/packages/lint-framework/src/lint/LintFramework.ts
+++ b/packages/lint-framework/src/lint/LintFramework.ts
@@ -32,7 +32,8 @@ export default class LintFramework {
getActivationKey?: () => Promise<ActivationKey>;
openOptions?: () => Promise<void>;
addToUserDictionary?: (words: string[]) => Promise<void>;
- reportError?: (lint: UnpackedLint) => Promise<void>;
+ reportError?: (lint: UnpackedLint, ruleId: string) => Promise<void>;
+ setRuleEnabled?: (ruleId: string, enabled: boolean) => Promise<void> | void;
};
constructor(
@@ -42,7 +43,8 @@ export default class LintFramework {
getActivationKey?: () => Promise<ActivationKey>;
openOptions?: () => Promise<void>;
addToUserDictionary?: (words: string[]) => Promise<void>;
- reportError?: () => Promise<void>;
+ reportError?: (lint: UnpackedLint, ruleId: string) => Promise<void>;
+ setRuleEnabled?: (ruleId: string, enabled: boolean) => Promise<void> | void;
},
) {
this.lintProvider = lintProvider;
@@ -53,6 +55,7 @@ export default class LintFramework {
openOptions: actions.openOptions,
addToUserDictionary: actions.addToUserDictionary,
reportError: actions.reportError,
+ setRuleEnabled: actions.setRuleEnabled,
});
this.targets = new Set();
this.scrollableAncestors = new Set();
diff --git a/packages/lint-framework/src/lint/PopupHandler.ts b/packages/lint-framework/src/lint/PopupHandler.ts
index 67f10547d..677f9b4a0 100644
--- a/packages/lint-framework/src/lint/PopupHandler.ts
+++ b/packages/lint-framework/src/lint/PopupHandler.ts
@@ -1,6 +1,7 @@
import h from 'virtual-dom/h';
import { closestBox, type IgnorableLintBox, isPointInBox } from './Box';
import { getCaretPosition } from './editorUtils';
+import type { UnpackedLint } from './unpackLint';
type ActivationKey = 'off' | 'shift' | 'control';
@@ -39,14 +40,16 @@ export default class PopupHandler {
getActivationKey?: () => Promise<ActivationKey>;
openOptions?: () => Promise<void>;
addToUserDictionary?: (words: string[]) => Promise<void>;
- reportError?: () => Promise<void>;
+ reportError?: (lint: UnpackedLint, ruleId: string) => Promise<void>;
+ setRuleEnabled?: (ruleId: string, enabled: boolean) => Promise<void> | void;
};
constructor(actions: {
getActivationKey?: () => Promise<ActivationKey>;
openOptions?: () => Promise<void>;
addToUserDictionary?: (words: string[]) => Promise<void>;
- reportError?: () => Promise<void>;
+ reportError?: (lint: UnpackedLint, ruleId: string) => Promise<void>;
+ setRuleEnabled?: (ruleId: string, enabled: boolean) => Promise<void> | void;
}) {
this.actions = actions;
this.currentLintBoxes = [];
diff --git a/packages/lint-framework/src/lint/SuggestionBox.ts b/packages/lint-framework/src/lint/SuggestionBox.ts
index 77808a02c..adf73ed7d 100644
--- a/packages/lint-framework/src/lint/SuggestionBox.ts
+++ b/packages/lint-framework/src/lint/SuggestionBox.ts
@@ -1,4 +1,7 @@
/** biome-ignore-all lint/complexity/useArrowFunction: It cannot be an arrow function for the logic to work. */
+import { type IconDefinition, icon } from '@fortawesome/fontawesome-svg-core';
+import { faBan, faGear } from '@fortawesome/free-solid-svg-icons';
+import type { VNode } from 'virtual-dom';
import h from 'virtual-dom/h';
import bookDownSvg from '../assets/bookDownSvg';
import type { IgnorableLintBox, LintBox } from './Box';
@@ -6,6 +9,13 @@ import lintKindColor from './lintKindColor';
// Decoupled: actions passed in by framework consumer
import type { UnpackedLint, UnpackedSuggestion } from './unpackLint';
+function iconSvg(definition: IconDefinition): string {
+ return icon(definition).html.join('');
+}
+
+const settingsIconSvg = iconSvg(faGear);
+const disableIconSvg = iconSvg(faBan);
+
var FocusHook: any = function () {};
FocusHook.prototype.hook = function (node: any, _propertyName: any, _previousValue: any) {
if ((node as any).__harperAutofocused) {
@@ -50,6 +60,7 @@ function header(
onClose: () => void,
openOptions?: () => Promise<void>,
rule?: string,
+ setRuleEnabled?: (ruleId: string, enabled: boolean) => Promise<void> | void,
): any {
const closeButton = h(
'button',
@@ -72,30 +83,36 @@ function header(
},
title: 'Settings',
'aria-label': 'Settings',
+ innerHTML: settingsIconSvg,
},
- '⚙',
+ [],
)
: undefined;
- const controlsChildren = settingsButton ? [settingsButton, closeButton] : [closeButton];
+ const disableRuleButton =
+ setRuleEnabled && rule
+ ? h(
+ 'button',
+ {
+ className: 'harper-disable-btn',
+ onclick: () => {
+ Promise.resolve(setRuleEnabled(rule, false)).finally(() => {
+ onClose();
+ });
+ },
+ title: `Disable the ${rule} rule`,
+ 'aria-label': 'Disable this lint rule',
+ innerHTML: disableIconSvg,
+ },
+ [],
+ )
+ : undefined;
+
+ const controlsChildren = [disableRuleButton, settingsButton, closeButton].filter(
+ (node): node is VNode => node != null,
+ );
const controls = h('div', { className: 'harper-controls' }, controlsChildren);
- const trimmedRule = rule?.trim();
- const titleChildren = [title] as any[];
- if (trimmedRule) {
- titleChildren.push(
- h(
- 'span',
- {
- className: 'harper-info-icon',
- title: trimmedRule,
- 'aria-label': `Grammar rule: ${trimmedRule}`,
- role: 'img',
- },
- 'i',
- ),
- );
- }
- const titleEl = h('span', { className: 'harper-title' }, titleChildren);
+ const titleEl = h('span', { className: 'harper-title' }, [title]);
return h(
'div',
@@ -243,19 +260,6 @@ function styleTag() {
align-items:center;
gap:6px;
}
- .harper-info-icon{
- display:inline-flex;
- align-items:center;
- justify-content:center;
- width:16px;
- height:16px;
- border-radius:50%;
- background:#eaeef2;
- color:#1f2328;
- font-size:11px;
- font-weight:700;
- cursor:default;
- }
.harper-body{
font-size:14px;
line-height:20px;
@@ -280,9 +284,28 @@ function styleTag() {
.harper-btn:active{transform:scale(0.97)}
.harper-close-btn{background:transparent;border:none;cursor:pointer;font-size:20px;line-height:1;color:#57606a;padding:0 4px;}
.harper-close-btn:hover{color:#1f2328;}
- .harper-gear-btn{background:transparent;border:none;cursor:pointer;font-size:22px;line-height:1;color:#57606a;padding:0 4px;}
+ .harper-disable-btn,
+ .harper-gear-btn{
+ background:transparent;
+ border:none;
+ cursor:pointer;
+ font-size:18px;
+ line-height:1;
+ color:#57606a;
+ padding:0 4px;
+ display:inline-flex;
+ align-items:center;
+ justify-content:center;
+ }
+ .harper-disable-btn:hover,
.harper-gear-btn:hover{color:#1f2328;}
- .harper-controls{display:flex;align-items:center;gap:6px;}
+ .harper-disable-btn svg,
+ .harper-gear-btn svg{
+ width:18px;
+ height:18px;
+ display:block;
+ }
+ .harper-controls{display:flex;align-items:center;gap:3px;}
.harper-child-cont{
display:flex;
flex-wrap:wrap;
@@ -341,7 +364,6 @@ function styleTag() {
box-shadow:0 4px 12px rgba(1,4,9,0.85)
}
.harper-header{color:#e6edf3}
- .harper-info-icon{background:#30363d;color:#c9d1d9}
.harper-body{color:#8b949e}
.harper-btn{
background:#21262d;
@@ -350,7 +372,9 @@ function styleTag() {
.harper-btn:hover{filter:brightness(1.15)}
.harper-close-btn{color:#8b949e;}
.harper-close-btn:hover{color:#e6edf3;}
+ .harper-disable-btn,
.harper-gear-btn{color:#8b949e;}
+ .harper-disable-btn:hover,
.harper-gear-btn:hover{color:#e6edf3;}
.harper-btn[style*="background: #2DA44E"]{background:#238636}
.harper-btn[style*="background: #e5e5e5"]{
@@ -382,7 +406,7 @@ function styleTag() {
function ignoreLint(onIgnore: () => void | Promise<void>): any {
return button(
- 'Ignore',
+ 'Dismiss',
{ background: '#e5e5e5', color: '#000000', fontWeight: 'lighter' },
onIgnore,
'Ignore this lint',
@@ -395,6 +419,7 @@ export default function SuggestionBox(
openOptions?: () => Promise<void>;
addToUserDictionary?: (words: string[]) => Promise<void>;
reportError?: (lint: UnpackedLint, ruleId: string) => Promise<void>;
+ setRuleEnabled?: (ruleId: string, enabled: boolean) => Promise<void> | void;
},
hint: string | null,
close: () => void,
@@ -429,6 +454,7 @@ export default function SuggestionBox(
close,
actions.openOptions,
box.rule,
+ actions.setRuleEnabled,
),
body(box.lint.message_html),
footer(
@@ -445,12 +471,7 @@ export default function SuggestionBox(
),
hintDrawer(hint),
actions.reportError
- ? reportProblemButton(() => {
- if (actions.reportError) {
- return actions.reportError(box.lint, box.rule);
- }
- return Promise.resolve();
- })
+ ? reportProblemButton(() => actions.reportError!(box.lint, box.rule))
: undefined,
],
);
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index fc1061c60..f8c3a8f46 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -182,6 +182,12 @@ importers:
packages/lint-framework:
dependencies:
+ '@fortawesome/fontawesome-svg-core':
+ specifier: ^7.1.0
+ version: 7.1.0
+ '@fortawesome/free-solid-svg-icons':
+ specifier: ^7.1.0
+ version: 7.1.0
bowser:
specifier: ^2.12.1
version: 2.12.1
@@ -2274,6 +2280,10 @@ packages:
resolution: {integrity: sha512-l/BQM7fYntsCI//du+6sEnHOP6a74UixFyOYUyz2DLMXKx+6DEhfR3F2NYGE45XH1JJuIamacb4IZs9S0ZOWLA==}
engines: {node: '>=6'}
+ '@fortawesome/[email protected]':
+ resolution: {integrity: sha512-fNxRUk1KhjSbnbuBxlWSnBLKLBNun52ZBTcs22H/xEEzM6Ap81ZFTQ4bZBxVQGQgVY0xugKGoRcCbaKjLQ3XZA==}
+ engines: {node: '>=6'}
+
'@fortawesome/[email protected]':
resolution: {integrity: sha512-Udu3K7SzAo9N013qt7qmm22/wo2hADdheXtBfxFTecp+ogsc0caQNRKEb7pkvvagUGOpG9wJC1ViH6WXs8oXIA==}
engines: {node: '>=6'}
@@ -14330,6 +14340,10 @@ snapshots:
'@fortawesome/[email protected]': {}
+ '@fortawesome/[email protected]':
+ dependencies:
+ '@fortawesome/fontawesome-common-types': 7.1.0
+
'@fortawesome/[email protected]':
dependencies:
'@fortawesome/fontawesome-common-types': 7.1.0