Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: can now fix issues in basic blocks
Elijah Potter16f894e7733f
diff --git a/src/harper/Box.ts b/src/harper/Box.tsnew file mode 100644index 000000000..886876547--- /dev/null+++ b/src/harper/Box.ts@@ -0,0 +1,14 @@+import type { Lint, Suggestion } from 'harper.js';++export type Box = {+ /** Horizontal position in pixels */+ x: number;+ /** Vertical position in pixels */+ y: number;+ /** Width in pixels */+ width: number;+ /** Height in pixels */+ height: number;+};++export type LintBox = Box & { lint: Lint, applySuggestion: (sug: Suggestion) => void };diff --git a/src/harper/Highlight.tsx b/src/harper/Highlighter.tsxsimilarity index 50%rename from src/harper/Highlight.tsxrename to src/harper/Highlighter.tsxindex a57439d13..8034afbd7 100644--- a/src/harper/Highlight.tsx+++ b/src/harper/Highlighter.tsx@@ -1,41 +1,33 @@import React from 'react';import { useState, useEffect } from 'react';-import { LocalLinter, Lint, WorkerLinter } from 'harper.js';--type Box = {- /** Horizontal position in pixels */- x: number;- /** Vertical position in pixels */- y: number;- /** Width in pixels */- width: number;- /** Height in pixels */- height: number;-};--type LintBox = Box & { lint: Lint }+import { LocalLinter, Lint, WorkerLinter, Suggestion } from 'harper.js';+import SuggestionControl from './SuggestionControl';+import { LintBox } from './Box';let linter = new WorkerLinter();-let lints = new Map<string, Lint[]>();+let lints = new Map< string, Lint[] >();-async function memoizedLint(text: string): Promise<Lint[]>{- if (lints.has(text)){- return Promise.resolve(lints.get(text)!);+async function memoizedLint( text: string ): Promise< Lint[] > {+ if ( lints.has( text ) ) {+ return Promise.resolve( lints.get( text )! );}- let newLints = await linter.lint(text);- lints.set(text, newLints);+ let newLints = await linter.lint( text );+ lints.set( text, newLints );return newLints;}/** Get target boxes for a text node.* Each box represents a Harper lint in the Node. */-async function getLintBoxesForNode( node: Node, container: Element ): Promise<LintBox[]> {+async function getLintBoxesForNode(+ node: Node,+ container: Element+): Promise< LintBox[] > {let text = node.textContent ?? '';- let lints = await memoizedLint(text);+ let lints = await memoizedLint( text );let boxes: LintBox[] = [];@@ -54,21 +46,25 @@ async function getLintBoxesForNode( node: Node, container: Element ): Promise<Liy: targetRect.y - contRect.y,width: targetRect.width,height: targetRect.height,- lint+ lint,+ applySuggestion: (async (sug: Suggestion) => {+ let fixed = await linter.applySuggestion(text, sug, span)+ node.textContent = fixed;+ })} );}return boxes;}-export default function Highlight( {+export default function Highlighter( {container,target,}: {container: Element;target: Node;} ) {- const [ targetBoxes, setTargetBoxes ] = useState< Box[] >( [] );+ const [ targetBoxes, setTargetBoxes ] = useState< LintBox[] >( [] );useEffect( () => {let running = true;@@ -76,7 +72,7 @@ export default function Highlight( {function onFrame( _timestep: DOMHighResTimeStamp ) {//let contRect = container.getBoundingClientRect();- getLintBoxesForNode( target, container ).then(setTargetBoxes)+ getLintBoxesForNode( target, container ).then( setTargetBoxes );if ( running ) {requestAnimationFrame( onFrame );@@ -92,18 +88,8 @@ export default function Highlight( {return (<>- { targetBoxes.map( ( { x, y, width, height } ) => (- <div- style={ {- position: 'absolute',- top: `${ y }px`,- left: ` ${ x }px`,- width: `${ width }px`,- height: `${ height }px`,- zIndex: -100,- borderBottom: '3px solid red',- } }- ></div>+ { targetBoxes.map( ( b ) => (+ <SuggestionControl lintBox={ b } />) ) }</>);diff --git a/src/harper/SidebarControl.tsx b/src/harper/SidebarControl.tsxindex 217e799e9..84b950186 100644--- a/src/harper/SidebarControl.tsx+++ b/src/harper/SidebarControl.tsx@@ -1,7 +1,7 @@import { ReactPortal, useMemo } from 'react';import useFrameCount from './useFrameCount';import { createPortal } from 'react-dom';-import Highlight from './Highlight';+import Highlighter from './Highlighter';import React from 'react';function getDocumentContainer(): Element | null {@@ -61,7 +61,7 @@ export default function SidebarControl() {return textChildren.map( ( n ) =>createPortal(- <Highlight container={ documentContainer } target={ n } />,+ <Highlighter container={ documentContainer } target={ n } />,documentContainer));diff --git a/src/harper/SuggestionControl.tsx b/src/harper/SuggestionControl.tsxnew file mode 100644index 000000000..045b7d153--- /dev/null+++ b/src/harper/SuggestionControl.tsx@@ -0,0 +1,122 @@+import React, {+ useCallback,+ useEffect,+ useMemo,+ useRef,+ useState,+} from 'react';+import { LintBox } from './Box';+import { Suggestion, SuggestionKind } from 'harper.js';+import { MouseEventHandler } from 'react';+import useElementPosition from './useElementPosition';++function suggestionText(+ kind: SuggestionKind,+ problemText: string,+ replacementText: string+): string {+ if ( kind == SuggestionKind.Remove ) {+ return `Remove "${ problemText }"`;+ } else if ( kind == SuggestionKind.Replace ) {+ return `Replace "${ problemText }" with "${ replacementText }"`;+ } else {+ return `Insert "${ replacementText }" after ${ problemText }`;+ }+}++/** A control for an individual suggestion shown on the screen. */+export default function SuggestionControl( { lintBox }: { lintBox: LintBox } ) {+ let { x, y, width, height, lint, applySuggestion } = lintBox;++ let underlineRef = useRef< HTMLElement | null >( null );+ let [ offsetX, offsetY ] = useElementPosition( underlineRef );++ let suggestions = useMemo( () => lint.suggestions(), [ lint ] );+ const [ showSuggestions, setShowSuggestions ] = useState( false );++ useEffect( () => {+ function mouseMove( e: MouseEvent ) {+ if (+ e.pageX > offsetX &&+ e.pageX < offsetX + width &&+ e.pageY > offsetY &&+ e.pageY < offsetY + height+ ) {+ setShowSuggestions( true );+ }+ }++ function mouseUp( e: MouseEvent ) {+ if (+ e.pageX < offsetX &&+ e.pageX > offsetX + width &&+ e.pageY < offsetY &&+ e.pageY > offsetY + height+ ) {+ setShowSuggestions( false );+ }+ }++ underlineRef.current?.parentElement?.addEventListener(+ 'mousemove',+ mouseMove+ );++ underlineRef.current?.parentElement?.addEventListener(+ 'mouseup',+ mouseUp+ );++ return () => {+ underlineRef.current?.parentElement?.removeEventListener(+ 'mousemove',+ mouseMove+ );++ underlineRef.current?.parentElement?.removeEventListener(+ 'mouseup',+ mouseUp+ );+ };+ }, [ offsetX, offsetY ] );++ return (+ <>+ <div+ ref={ underlineRef }+ style={ {+ position: 'absolute',+ top: `${ y }px`,+ left: ` ${ x }px`,+ width: `${ width }px`,+ height: `${ height }px`,+ zIndex: -100,+ borderBottom: '3px solid red',+ } }+ ></div>+ { showSuggestions && (+ <div+ className="harper-popover"+ style={ {+ position: 'absolute',+ top: `${ y + height + 4 }px`,+ left: ` ${ x }px`,+ zIndex: 100,+ } }+ >+ { lint.message() }++ { suggestions.map( ( sug ) => (+ <button onClick={() => applySuggestion(sug) }>+ { suggestionText(+ sug.kind(),+ lint.get_problem_text(),+ sug.get_replacement_text()+ ) }+ </button>+ ) ) }+ </div>+ ) }+ </>+ );+}diff --git a/src/harper/index.css b/src/harper/index.cssindex e69de29bb..ce273b1c6 100644--- a/src/harper/index.css+++ b/src/harper/index.css@@ -0,0 +1,26 @@+.harper-popover {+ border-radius: 4px;+ border: 1px solid black;+ padding: 4px;+ background-color: white;+ font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;+ font-size: 16px;+ font-weight: 750;+ filter: drop-shadow(5px 5px 5px #CCCCCC);+ width: 200px;++ display: flex;+ flex-direction: column;+ align-items: center;+ text-align: left;+}++.harper-popover > button {+ width: 95%;+ padding: 4px;+ margin-top: 4px;+ border-radius: 4px;+ border: 0px;+ background-color: black;+ color: white;+}diff --git a/src/harper/index.tsx b/src/harper/index.tsxindex 8f10a4235..3eb2a033a 100644--- a/src/harper/index.tsx+++ b/src/harper/index.tsx@@ -2,6 +2,9 @@ import { registerPlugin } from '@wordpress/plugins';import { PluginSidebar, PluginSidebarMoreMenuItem } from '@wordpress/editor';import SidebarControl from './SidebarControl';+import './index.css';+import React from 'react';+function Sidebar() {return (<>diff --git a/src/harper/useElementPosition.ts b/src/harper/useElementPosition.tsnew file mode 100644index 000000000..7bc53f97e--- /dev/null+++ b/src/harper/useElementPosition.ts@@ -0,0 +1,40 @@+import {+ MutableRefObject,+ useCallback,+ useEffect,+ useLayoutEffect,+ useState,+} from 'react';++/** Get the pixel coordinates in page space, of an element's top left corner. */+export default function useElementPosition(+ ref: MutableRefObject< HTMLElement | null >+): [ number, number ] {+ let [ position, setPosition ] = useState< [ number, number ] >( [ 0, 0 ] );++ let handleUpdate = useCallback( () => {+ if ( ref.current != null ) {+ let rect = ref.current.getBoundingClientRect();++ setPosition( [+ rect.left + window.screenX,+ rect.top + window.screenY,+ ] );+ }+ }, [ ref.current ] );++ useEffect( () => {+ handleUpdate();+ }, [] );++ useLayoutEffect( () => {+ handleUpdate();+ window.addEventListener( 'resize', handleUpdate );++ return () => {+ window.removeEventListener( 'resize', handleUpdate );+ };+ }, [ ref.current, handleUpdate ] );++ return position;+}