Skip to content

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

refactor: DOM-related functions into their own module

Elijah Potter5791d09bf395
diff --git a/src/harper/Highlighter.tsx b/src/harper/Highlighter.tsx
index faec369ee..96102b45b 100644
--- a/src/harper/Highlighter.tsx
+++ b/src/harper/Highlighter.tsx
@@ -1,43 +1,14 @@
-import { select, dispatch } from '@wordpress/data';
-import React, { useCallback, useLayoutEffect, useRef } from 'react';
+import { setBlockContent } from './gutenbergUtils';
+import React, { useCallback } from 'react';
import { useState, useEffect } from 'react';
-import { LocalLinter, Lint, WorkerLinter, Suggestion, Span } from 'harper.js';
+import { Lint, WorkerLinter, Suggestion, Span } from 'harper.js';
import SuggestionControl from './SuggestionControl';
import { LintBox } from './Box';
-import { leafNodes } from './domUtils';
+import { getRangeForTextSpan } from './domUtils';
let linter = new WorkerLinter();
-function getRangeForTextSpan( target: HTMLElement, span: Span ): Range | null {
- let children = leafNodes( target );
-
- let range = document.createRange();
- let traversed = 0;
-
- let startFound = false;
-
- for ( let i = 0; i < children.length; i++ ) {
- let child = children[ i ] as HTMLElement;
- let childText = child.textContent;
-
- if ( traversed + childText.length > span.start && ! startFound ) {
- range.setStart( child, span.start - traversed );
- startFound = true;
- }
-
- if ( startFound && traversed + childText.length >= span.end ) {
- range.setEnd( child, span.end - traversed );
- return range;
- }
-
- traversed += childText?.length ?? 0;
- }
-
- return null;
-}
-
-/** Get target boxes for a text node.
- * Each box represents a Harper lint in the Node. */
+/** Given an element and the lint results for it, create bounding boxes that represent those errors on-screen. */
async function computeLintBoxes(
target: HTMLElement,
container: Element,
@@ -80,14 +51,6 @@ async function computeLintBoxes(
return boxes;
}
-function setBlockContent( clientId: string, text: string ) {
- const { selectBlock, updateBlockAttributes } =
- dispatch( 'core/block-editor' );
-
- selectBlock( clientId );
- updateBlockAttributes( clientId, { content: text } );
-}
-
export default function Highlighter( {
container,
target,
diff --git a/src/harper/domUtils.ts b/src/harper/domUtils.ts
index b25f4e561..563bfbdfc 100644
--- a/src/harper/domUtils.ts
+++ b/src/harper/domUtils.ts
@@ -1,3 +1,5 @@
+import { Span } from 'harper.js';
+
/** Turn a `NodeList` into a normal JavaScript array. */
export function extractFromHTMLCollection(
collection: HTMLCollection
@@ -46,3 +48,35 @@ export function leafNodes( node: Element ): Element[] {
return out;
}
+
+/** Given an element and a Span of text inside it, compute the Range that represents the region of the DOM represented. */
+export function getRangeForTextSpan(
+ target: HTMLElement,
+ span: Span
+): Range | null {
+ let children = leafNodes( target );
+
+ let range = document.createRange();
+ let traversed = 0;
+
+ let startFound = false;
+
+ for ( let i = 0; i < children.length; i++ ) {
+ let child = children[ i ] as HTMLElement;
+ let childText = child.textContent;
+
+ if ( traversed + childText.length > span.start && ! startFound ) {
+ range.setStart( child, span.start - traversed );
+ startFound = true;
+ }
+
+ if ( startFound && traversed + childText.length >= span.end ) {
+ range.setEnd( child, span.end - traversed );
+ return range;
+ }
+
+ traversed += childText?.length ?? 0;
+ }
+
+ return null;
+}
diff --git a/src/harper/gutenbergUtils.ts b/src/harper/gutenbergUtils.ts
new file mode 100644
index 000000000..04aaab913
--- /dev/null
+++ b/src/harper/gutenbergUtils.ts
@@ -0,0 +1,9 @@
+import { dispatch } from '@wordpress/data';
+
+export function setBlockContent( clientId: string, text: string ) {
+ const { selectBlock, updateBlockAttributes } =
+ dispatch( 'core/block-editor' );
+
+ selectBlock( clientId );
+ updateBlockAttributes( clientId, { content: text } );
+}