Skip to content

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

fix(chrome-ext): shrink LintBoxes (#1654)

Elijah Potter412efad1a54d
diff --git a/packages/chrome-plugin/src/Box.ts b/packages/chrome-plugin/src/Box.ts
index 6ee413b2e..5dc704d21 100644
--- a/packages/chrome-plugin/src/Box.ts
+++ b/packages/chrome-plugin/src/Box.ts
@@ -100,3 +100,11 @@ export function closestBox(target: Box, boxes: Box[]): number {
}
return idx;
}
+
+export function shrinkBoxToFit(inner: Box, outer: Box): Box {
+ const nx = Math.max(inner.x, outer.x);
+ const ny = Math.max(inner.y, outer.y);
+ const rx = Math.min(inner.x + inner.width, outer.x + outer.width);
+ const by = Math.min(inner.y + inner.height, outer.y + outer.height);
+ return { x: nx, y: ny, width: Math.max(0, rx - nx), height: Math.max(0, by - ny) };
+}
diff --git a/packages/chrome-plugin/src/computeLintBoxes.ts b/packages/chrome-plugin/src/computeLintBoxes.ts
index aea01a499..8de174981 100644
--- a/packages/chrome-plugin/src/computeLintBoxes.ts
+++ b/packages/chrome-plugin/src/computeLintBoxes.ts
@@ -1,5 +1,11 @@
import type { Span } from 'harper.js';
-import { domRectToBox, type IgnorableLintBox, isBottomEdgeInBox, type LintBox } from './Box';
+import {
+ domRectToBox,
+ type IgnorableLintBox,
+ isBottomEdgeInBox,
+ type LintBox,
+ shrinkBoxToFit,
+} from './Box';
import { getRangeForTextSpan } from './domUtils';
import { getLexicalRoot, getSlateRoot } from './editorUtils';
import ProtocolClient from './ProtocolClient';
@@ -51,11 +57,13 @@ export default function computeLintBoxes(el: HTMLElement, lint: UnpackedLint): I
continue;
}
+ const shrunkBox = shrinkBoxToFit(targetRect, elBox);
+
boxes.push({
- x: targetRect.x,
- y: targetRect.y,
- width: targetRect.width,
- height: targetRect.height,
+ x: shrunkBox.x,
+ y: shrunkBox.y,
+ width: shrunkBox.width,
+ height: shrunkBox.height,
lint,
source,
applySuggestion: (sug: UnpackedSuggestion) => {