Skip to content

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

fix: Google Docs bridge and replaceText issue for the popup suggestion (#3847)

Yuvraj P3bd6bf0f0695
diff --git a/packages/lint-framework/src/lint/Box.ts b/packages/lint-framework/src/lint/Box.ts
index 057f21ec3..abbfe3d5d 100644
--- a/packages/lint-framework/src/lint/Box.ts
+++ b/packages/lint-framework/src/lint/Box.ts
@@ -17,7 +17,7 @@ export type LintBox = Box & {
source: SourceElement;
/** Optionally provided to improve highlight rendering performance. */
range?: Range;
- applySuggestion: (sug: UnpackedSuggestion) => void;
+ applySuggestion: (sug: UnpackedSuggestion) => void | Promise<void>;
};
export type IgnorableLintBox = LintBox & {
diff --git a/packages/lint-framework/src/lint/SuggestionBox.ts b/packages/lint-framework/src/lint/SuggestionBox.ts
index 990cba36e..65768040e 100644
--- a/packages/lint-framework/src/lint/SuggestionBox.ts
+++ b/packages/lint-framework/src/lint/SuggestionBox.ts
@@ -193,6 +193,7 @@ function button(
className: 'harper-btn',
style: extraStyle,
onclick: onClick,
+ onmousedown: (e: Event) => e.preventDefault(),
title: desc,
type: 'button',
'aria-label': desc,
@@ -546,8 +547,9 @@ export default function SuggestionBox(
body(box.lint.message_html),
footer(
suggestions(box.lint.lint_kind, box.lint.suggestions, (v) => {
- box.applySuggestion(v);
- close();
+ Promise.resolve(box.applySuggestion(v)).finally(() => {
+ close();
+ });
}),
[
box.lint.lint_kind === 'Spelling' && actions.addToUserDictionary
diff --git a/packages/lint-framework/src/lint/computeLintBoxes/googleDocsUtilities.ts b/packages/lint-framework/src/lint/computeLintBoxes/googleDocsUtilities.ts
index 427b37d44..4269a6073 100644
--- a/packages/lint-framework/src/lint/computeLintBoxes/googleDocsUtilities.ts
+++ b/packages/lint-framework/src/lint/computeLintBoxes/googleDocsUtilities.ts
@@ -302,11 +302,11 @@ function getGoogleDocsPositionedLeafHost(child: Node, target: HTMLElement): HTML
* The payload includes nearby context so the bridge can recover when the original
* offsets have gone stale by the time the suggestion is applied.
*/
-export function replaceGoogleDocsValue(
+export async function replaceGoogleDocsValue(
span: { start: number; end: number },
replacementText: string,
source: string,
-) {
+): Promise<boolean> {
try {
const safeStart = Math.max(0, Math.min(span.start, source.length));
const safeEnd = Math.max(safeStart, Math.min(span.end, source.length));
@@ -323,23 +323,23 @@ export function replaceGoogleDocsValue(
beforeContext,
afterContext,
};
- // This looks awkward because lint-framework cannot import chrome-plugin code directly.
- // The content script puts the bridge client on window so this shared package can call it.
+
const bridgeClient = (window as WindowWithGoogleDocsBridgeClient)
.__harperGoogleDocsBridgeClient;
if (bridgeClient && typeof bridgeClient.replaceText === 'function') {
- void Promise.resolve(
- bridgeClient.replaceText(
- payload.start,
- payload.end,
- payload.replacementText,
- payload.expectedText,
- payload.beforeContext,
- payload.afterContext,
- ),
+ const applied = await bridgeClient.replaceText(
+ payload.start,
+ payload.end,
+ payload.replacementText,
+ payload.expectedText,
+ payload.beforeContext,
+ payload.afterContext,
);
+ return Boolean(applied);
}
- } catch {
- // Ignore bridge dispatch failures.
+
+ return false;
+ } catch (e) {
+ return false;
}
}
diff --git a/packages/lint-framework/src/lint/googleDocsAdapter.ts b/packages/lint-framework/src/lint/googleDocsAdapter.ts
index 4fc31c8dc..858943baa 100644
--- a/packages/lint-framework/src/lint/googleDocsAdapter.ts
+++ b/packages/lint-framework/src/lint/googleDocsAdapter.ts
@@ -58,9 +58,9 @@ export function maybeComputeGoogleDocsLintBoxes(
lint,
source: editor,
rule,
- applySuggestion: (suggestion: UnpackedSuggestion) => {
+ applySuggestion: async (suggestion: UnpackedSuggestion) => {
const replacementText = suggestionToReplacementText(suggestion, resolvedSpan, source);
- replaceGoogleDocsValue(resolvedSpan, replacementText, source);
+ await replaceGoogleDocsValue(resolvedSpan, replacementText, source);
},
ignoreLint: opts.ignoreLint ? () => opts.ignoreLint!(lint.context_hash) : undefined,
});