Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix: issue with popups persisting too long
Elijah Potterb947ea62c543
diff --git a/.gitignore b/.gitignoreindex f66335eb0..ecc86dd4e 100644--- a/.gitignore+++ b/.gitignore@@ -9,7 +9,7 @@ yarn-error.log*coverage# Compiled binary addons (https://nodejs.org/api/addons.html)-build/Release+build# Dependency directoriesnode_modules/diff --git a/src/harper/Box.ts b/src/harper/Box.tsindex 886876547..6cd7dafa3 100644--- a/src/harper/Box.ts+++ b/src/harper/Box.ts@@ -11,4 +11,7 @@ export type Box = {height: number;};-export type LintBox = Box & { lint: Lint, applySuggestion: (sug: Suggestion) => void };+export type LintBox = Box & {+ lint: Lint;+ applySuggestion: ( sug: Suggestion ) => void;+};diff --git a/src/harper/Highlighter.tsx b/src/harper/Highlighter.tsxindex 8034afbd7..f6d15eccb 100644--- a/src/harper/Highlighter.tsx+++ b/src/harper/Highlighter.tsx@@ -1,4 +1,4 @@-import React from 'react';+import React, { useCallback, useRef } from 'react';import { useState, useEffect } from 'react';import { LocalLinter, Lint, WorkerLinter, Suggestion } from 'harper.js';import SuggestionControl from './SuggestionControl';@@ -47,10 +47,10 @@ async function getLintBoxesForNode(width: targetRect.width,height: targetRect.height,lint,- applySuggestion: (async (sug: Suggestion) => {- let fixed = await linter.applySuggestion(text, sug, span)+ applySuggestion: async ( sug: Suggestion ) => {+ let fixed = await linter.applySuggestion( text, sug, span );node.textContent = fixed;- })+ },} );}@@ -60,9 +60,13 @@ async function getLintBoxesForNode(export default function Highlighter( {container,target,+ requestClosePopups,+ registerCloseHandler,}: {container: Element;target: Node;+ requestClosePopups: () => void;+ registerCloseHandler: ( handler: () => void ) => void;} ) {const [ targetBoxes, setTargetBoxes ] = useState< LintBox[] >( [] );@@ -89,7 +93,11 @@ export default function Highlighter( {return (<>{ targetBoxes.map( ( b ) => (- <SuggestionControl lintBox={ b } />+ <SuggestionControl+ lintBox={ b }+ requestClosePopups={ requestClosePopups }+ registerCloseHandler={registerCloseHandler}+ />) ) }</>);diff --git a/src/harper/SidebarControl.tsx b/src/harper/SidebarControl.tsxindex 84b950186..cbb216ec5 100644--- a/src/harper/SidebarControl.tsx+++ b/src/harper/SidebarControl.tsx@@ -1,4 +1,4 @@-import { ReactPortal, useMemo } from 'react';+import { ReactPortal, useCallback, useMemo, useRef } from 'react';import useFrameCount from './useFrameCount';import { createPortal } from 'react-dom';import Highlighter from './Highlighter';@@ -52,6 +52,12 @@ export default function SidebarControl() {[ documentContainer, frameCount ]);+ const closeHandlers = useRef( new Set< () => void >() );++ let requestClosePopups = useCallback( () => {+ closeHandlers.current.forEach( ( h ) => h() );+ }, [] );+let highlights = targetNodes.flatMap( ( n ) => {if ( ! documentContainer ) return [];@@ -61,7 +67,11 @@ export default function SidebarControl() {return textChildren.map( ( n ) =>createPortal(- <Highlighter container={ documentContainer } target={ n } />,+ <Highlighter container={ documentContainer } target={ n } requestClosePopups={requestClosePopups}+ registerCloseHandler={ ( handler ) =>+ closeHandlers.current.add( handler )+ }+ />,documentContainer));diff --git a/src/harper/SuggestionControl.tsx b/src/harper/SuggestionControl.tsxindex 045b7d153..24124478d 100644--- a/src/harper/SuggestionControl.tsx+++ b/src/harper/SuggestionControl.tsx@@ -6,8 +6,7 @@ import React, {useState,} from 'react';import { LintBox } from './Box';-import { Suggestion, SuggestionKind } from 'harper.js';-import { MouseEventHandler } from 'react';+import { SuggestionKind } from 'harper.js';import useElementPosition from './useElementPosition';function suggestionText(@@ -25,7 +24,15 @@ function suggestionText(}/** A control for an individual suggestion shown on the screen. */-export default function SuggestionControl( { lintBox }: { lintBox: LintBox } ) {+export default function SuggestionControl( {+ lintBox,+ requestClosePopups,+ registerCloseHandler,+}: {+ lintBox: LintBox;+ requestClosePopups: () => void;+ registerCloseHandler: ( handler: () => void ) => void;+} ) {let { x, y, width, height, lint, applySuggestion } = lintBox;let underlineRef = useRef< HTMLElement | null >( null );@@ -34,6 +41,10 @@ export default function SuggestionControl( { lintBox }: { lintBox: LintBox } ) {let suggestions = useMemo( () => lint.suggestions(), [ lint ] );const [ showSuggestions, setShowSuggestions ] = useState( false );+ useEffect( () => {+ registerCloseHandler( () => setShowSuggestions( false ) );+ }, [] );+useEffect( () => {function mouseMove( e: MouseEvent ) {if (@@ -42,7 +53,8 @@ export default function SuggestionControl( { lintBox }: { lintBox: LintBox } ) {e.pageY > offsetY &&e.pageY < offsetY + height) {- setShowSuggestions( true );+ requestClosePopups();+ setShowSuggestions( () => true );}}@@ -54,6 +66,7 @@ export default function SuggestionControl( { lintBox }: { lintBox: LintBox } ) {e.pageY > offsetY + height) {setShowSuggestions( false );+ requestClosePopups();}}@@ -78,7 +91,9 @@ export default function SuggestionControl( { lintBox }: { lintBox: LintBox } ) {mouseUp);};- }, [ offsetX, offsetY ] );+ }, [ offsetX, offsetY, requestClosePopups ] );++return (<>@@ -107,7 +122,7 @@ export default function SuggestionControl( { lintBox }: { lintBox: LintBox } ) {{ lint.message() }{ suggestions.map( ( sug ) => (- <button onClick={() => applySuggestion(sug) }>+ <button onClick={ () => applySuggestion( sug ) }>{ suggestionText(sug.kind(),lint.get_problem_text(),