Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/lint-framework/src/lint/Box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 & {
Expand Down
6 changes: 4 additions & 2 deletions packages/lint-framework/src/lint/SuggestionBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions packages/lint-framework/src/lint/googleDocsAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
Loading