Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
refactor: move logic further into `RichText` class
Elijah Potterdd511e446236
diff --git a/src/harper/DataBlock.ts b/src/harper/DataBlock.tsindex 6761ef9b7..a0ab77808 100644--- a/src/harper/DataBlock.ts+++ b/src/harper/DataBlock.ts@@ -4,18 +4,44 @@ import {getRangeForTextSpan,getRichTextContainers,} from './domUtils';+import RichText from './RichText';import { LintBox } from './Box';import { setBlockContent } from './gutenbergUtils';+import { dispatch } from '@wordpress/data';/** Represents a Gutenberg block on-screen.* So named because all of these blocks have a `data-block` attribute. */export default class DataBlock {- public targetElement: Element;+ public readonly targetElement: Element;constructor( targetElement: Element ) {this.targetElement = targetElement;}+ private getClientId(): string {+ return this.targetElement.getAttribute( 'data-block' )!;+ }++ public getAllRichText(): RichText[] {+ let cont = getRichTextContainers( this.targetElement );++ return cont.map(+ ( cont, index ) =>+ new RichText( cont, this, async ( newContent: string ) => {+ const { updateBlockAttributes } =+ dispatch( 'core/block-editor' );++ if ( cont == this.targetElement ) {+ await updateBlockAttributes( this.getClientId(), {+ content: newContent,+ } );+ } else {+ console.log( 'UNIMPLEMENTED' );+ }+ } )+ );+ }+public static getAllDataBlocks(): DataBlock[] {let container = this.getContainer();@@ -30,52 +56,7 @@ export default class DataBlock {return targetNodes.map( ( node ) => new DataBlock( node ) );}- public getTextContent(): string {- return this.targetElement.textContent!;- }-- public computeLintBox( lint: Lint ): LintBox[] {- let container = DataBlock.getContainer();- let text = this.targetElement.textContent;- let span = lint.span();- let range = getRangeForTextSpan( this.targetElement, span );- let linter = new LocalLinter();-- if ( range == null || text == null ) {- console.log( 'Could not locate range.' );- return [];- }-- let targetRects = range.getClientRects();- let contRect = container.getBoundingClientRect();-- let boxes: LintBox[] = [];-- for ( let targetRect of targetRects ) {- boxes.push( {- x: targetRect.x - contRect.x,- y: targetRect.y - contRect.y,- width: targetRect.width,- height: targetRect.height,- lint,- applySuggestion: async ( sug: Suggestion ) => {- let fixed = await linter.applySuggestion( text, sug, span );-- console.log( 'Applying suggestion' );-- setBlockContent( this.getClientId(), fixed );- },- } );- }-- return boxes;- }-- private getClientId(): string {- return this.targetElement.getAttribute( 'data-block' )!;- }-- private static getContainer(): Element {+ public static getContainer(): Element {const iframe = document.querySelector( 'iframe[name="editor-canvas"]' );const iframeDocument =iframe?.contentDocument || iframe?.contentWindow.document;diff --git a/src/harper/Highlighter.tsx b/src/harper/Highlighter.tsxindex 3a7adfff8..623910144 100644--- a/src/harper/Highlighter.tsx+++ b/src/harper/Highlighter.tsx@@ -4,23 +4,26 @@ import { useState, useEffect } from 'react';import { Lint, WorkerLinter, Suggestion, Span } from 'harper.js';import SuggestionControl from './SuggestionControl';import { LintBox } from './Box';-import { getRangeForTextSpan } from './domUtils';import DataBlock from './DataBlock';+import RichText from './RichText';let linter = new WorkerLinter();-export default function Highlighter( { block }: { block: DataBlock } ) {+export default function Highlighter( { richText }: { richText: RichText } ) {const [ targetBoxes, setTargetBoxes ] = useState< LintBox[] >( [] );const [ lints, setLints ] = useState< Lint[] >( [] );- let updateLints = useCallback( () => {- linter.lint( block.getTextContent() ).then( setLints );- }, [ block ] );+ let updateLints = useCallback( async () => {+ // We assume that a given index always refers to the same rich text field.+ let contents = richText.getTextContent();+ let lints = await linter.lint( contents );+ setLints( lints );+ }, [ richText ] );useEffect( () => {updateLints();let observer = new MutationObserver( updateLints );- observer.observe( block.targetElement, {+ observer.observe( richText.getTargetElement(), {childList: true,characterData: true,subtree: true,@@ -29,7 +32,7 @@ export default function Highlighter( { block }: { block: DataBlock } ) {return () => {observer.disconnect();};- }, [ block ] );+ }, [ richText ] );// Update the lint boxes each frame.// Probably overkill.@@ -41,7 +44,7 @@ export default function Highlighter( { block }: { block: DataBlock } ) {function onFrame( _timestep: DOMHighResTimeStamp ) {let lintBoxes = lints- .map( ( lint ) => block.computeLintBox( lint ) )+ .map( ( lint ) => richText.computeLintBox( lint ) ).flat();setTargetBoxes( lintBoxes );@@ -59,14 +62,14 @@ export default function Highlighter( { block }: { block: DataBlock } ) {// Disable browser spellchecking in favor of oursuseEffect( () => {- block.targetElement.spellcheck = false;+ richText.getTargetElement().spellcheck = false;return () => {- block.targetElement.spellcheck = true;+ richText.getTargetElement().spellcheck = true;};- }, [ block ] );+ }, [ richText ] );- let visible = block.targetElement.checkVisibility();+ let visible = richText.getTargetElement().checkVisibility();return (<>diff --git a/src/harper/RichText.ts b/src/harper/RichText.tsnew file mode 100644index 000000000..7f8cc9e37--- /dev/null+++ b/src/harper/RichText.ts@@ -0,0 +1,67 @@+import { Lint, LocalLinter, Suggestion } from 'harper.js';+import { LintBox } from './Box';+import DataBlock from './DataBlock';+import { getRangeForTextSpan } from './domUtils';+import { setBlockContent } from './gutenbergUtils';++export type EditContentCallback = ( newContent: string ) => void;++/** Represents a rich text element on-screen. */+export default class RichText {+ private targetElement: Element;+ private parent: DataBlock;+ private editContent: EditContentCallback;++ constructor(+ targetElement: Element,+ parent: DataBlock,+ editContent: EditContentCallback+ ) {+ this.targetElement = targetElement;+ this.parent = parent;+ this.editContent = editContent;+ }++ public getTargetElement(): Element {+ return this.targetElement;+ }++ public getTextContent(): string {+ return this.targetElement.textContent ?? '';+ }++ public computeLintBox( lint: Lint ): LintBox[] {+ let container = DataBlock.getContainer();+ let text = this.targetElement.textContent;+ let span = lint.span();+ let range = getRangeForTextSpan( this.targetElement, span );+ let linter = new LocalLinter();++ if ( range == null || text == null ) {+ console.log( 'Could not locate range.' );+ return [];+ }++ let targetRects = range.getClientRects();+ let contRect = container.getBoundingClientRect();++ let boxes: LintBox[] = [];++ for ( let targetRect of targetRects ) {+ boxes.push( {+ x: targetRect.x - contRect.x,+ y: targetRect.y - contRect.y,+ width: targetRect.width,+ height: targetRect.height,+ lint,+ applySuggestion: async ( sug: Suggestion ) => {+ let fixed = await linter.applySuggestion( text, sug, span );++ this.editContent( fixed );+ },+ } );+ }++ return boxes;+ }+}diff --git a/src/harper/SidebarControl.tsx b/src/harper/SidebarControl.tsxindex 7d9dd3ff7..ca3ca94a5 100644--- a/src/harper/SidebarControl.tsx+++ b/src/harper/SidebarControl.tsx@@ -17,16 +17,18 @@ function getDocumentContainer(): Element | null {}export default function SidebarControl() {- let frameCount = useFrameCount();-let documentContainer = useMemo( getDocumentContainer, [] );let blocks = DataBlock.getAllDataBlocks();+ let richTexts = blocks.flatMap( ( block ) => block.getAllRichText() );let highlights =documentContainer &&- blocks.map( ( block ) =>- createPortal( <Highlighter block={ block } />, documentContainer )+ richTexts.map( ( richText ) =>+ createPortal(+ <Highlighter richText={ richText } />,+ documentContainer+ ));return (diff --git a/src/harper/gutenbergUtils.ts b/src/harper/gutenbergUtils.tsindex f4e52a9ac..0d2447cc4 100644--- a/src/harper/gutenbergUtils.ts+++ b/src/harper/gutenbergUtils.ts@@ -1,8 +1,7 @@import { dispatch } from '@wordpress/data';export async function setBlockContent( clientId: string, text: string ) {- const { selectBlock, updateBlockAttributes } =- dispatch( 'core/block-editor' );+ const { updateBlockAttributes } = dispatch( 'core/block-editor' );let k = await updateBlockAttributes( clientId, { content: text } );}