Skip to content

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

fix: vast majority of `eslint` and `tsc` errors

Elijah Potterda07f574443f
diff --git a/.eslintrc.js b/.eslintrc.js
new file mode 100644
index 000000000..5f6d1da82
--- /dev/null
+++ b/.eslintrc.js
@@ -0,0 +1,3 @@
+module.exports = {
+ extends: ['plugin:@wordpress/eslint-plugin/recommended'],
+};
diff --git a/package.json b/package.json
index 1a8e411bc..870d0aec3 100644
--- a/package.json
+++ b/package.json
@@ -12,13 +12,18 @@
"start": "wp-scripts start --webpack-copy-php"
},
"devDependencies": {
+ "@wordpress/eslint-plugin": "^22.2.0",
"@wordpress/scripts": "^30.8.1",
- "typescript": "^5.7.2"
+ "eslint": "^8.57.0",
+ "prettier": "^3.4.2",
+ "typescript": "^5.7.3"
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e",
"dependencies": {
"@wordpress/data": "^10.15.1",
"@wordpress/editor": "^14.15.1",
- "harper.js": "^0.16.0"
+ "harper.js": "^0.16.0",
+ "react": "^19.0.0",
+ "react-dom": "^19.0.0"
}
}
diff --git a/src/harper/Box.ts b/src/harper/Box.ts
index 64c586fab..a36175cb1 100644
--- a/src/harper/Box.ts
+++ b/src/harper/Box.ts
@@ -13,11 +13,11 @@ export type Box = {
export type LintBox = Box & {
lint: Lint;
- applySuggestion: ( sug: Suggestion ) => void;
+ applySuggestion: (sug: Suggestion) => void;
};
-export function isPointInBox( point: [ number, number ], box: Box ) {
- let [ x, y ] = point;
+export function isPointInBox(point: [number, number], box: Box) {
+ const [x, y] = point;
return (
x > box.x &&
diff --git a/src/harper/DataBlock.ts b/src/harper/DataBlock.ts
index bc2ba9bca..a93bafe69 100644
--- a/src/harper/DataBlock.ts
+++ b/src/harper/DataBlock.ts
@@ -2,60 +2,58 @@ import { getNodesFromQuerySelector, getRichTextContainers } from './domUtils';
import RichText from './RichText';
import { dispatch } from '@wordpress/data';
-/** Represents a Gutenberg block on-screen.
- * So named because all of these blocks have a `data-block` attribute. */
+/**
+ * Represents a Gutenberg block on-screen.
+ * So named because all of these blocks have a `data-block` attribute.
+ */
export default class DataBlock {
public readonly targetElement: Element;
- constructor( targetElement: Element ) {
+ constructor(targetElement: Element) {
this.targetElement = targetElement;
}
private getClientId(): string {
- return this.targetElement.getAttribute( 'data-block' )!;
+ return this.targetElement.getAttribute('data-block')!;
}
public getAllRichText(): RichText[] {
- let cont = getRichTextContainers( this.targetElement );
+ const containers = getRichTextContainers(this.targetElement);
- return cont.map(
- ( cont ) =>
- new RichText( cont, this, async ( newContent: string ) => {
+ return containers.map(
+ (cont) =>
+ new RichText(cont, this, async (newContent: string) => {
const { updateBlockAttributes } =
- dispatch( 'core/block-editor' );
+ dispatch('core/block-editor');
- console.log(
- `Updating ${ this.getClientId() } to "${ newContent }"`
- );
-
- let attributeName =
- cont.getAttribute( 'data-wp-block-attribute-key' ) ??
+ const attributeName =
+ cont.getAttribute('data-wp-block-attribute-key') ??
'content';
- await updateBlockAttributes( this.getClientId(), {
- [ attributeName ]: newContent,
- } );
- } )
+ await updateBlockAttributes(this.getClientId(), {
+ [attributeName]: newContent,
+ });
+ })
);
}
public static getAllDataBlocks(): DataBlock[] {
- let container = this.getContainer();
+ const container = this.getContainer();
- let targetNodes = [
- ...getNodesFromQuerySelector( container, '[data-block]' ),
+ const targetNodes = [
+ ...getNodesFromQuerySelector(container, '[data-block]'),
];
- return targetNodes.map( ( node ) => new DataBlock( node ) );
+ return targetNodes.map((node) => new DataBlock(node));
}
public static getContainer(): Element {
- const iframe = document.querySelector( 'iframe[name="editor-canvas"]' );
+ const iframe = document.querySelector('iframe[name="editor-canvas"]');
const iframeDocument =
iframe?.contentDocument || iframe?.contentWindow.document;
const container =
iframeDocument?.body ||
- document.querySelector( '.edit-post-visual-editor > div' );
+ document.querySelector('.edit-post-visual-editor > div');
return container;
}
}
diff --git a/src/harper/Highlighter.tsx b/src/harper/Highlighter.tsx
index 623910144..0ef87be1b 100644
--- a/src/harper/Highlighter.tsx
+++ b/src/harper/Highlighter.tsx
@@ -1,82 +1,82 @@
-import { setBlockContent } from './gutenbergUtils';
-import React, { useCallback } from 'react';
-import { useState, useEffect } from 'react';
-import { Lint, WorkerLinter, Suggestion, Span } from 'harper.js';
+import React, { useCallback, useState, useEffect } from 'react';
+import { Lint, WorkerLinter } from 'harper.js';
import SuggestionControl from './SuggestionControl';
import { LintBox } from './Box';
-import DataBlock from './DataBlock';
import RichText from './RichText';
-let linter = new WorkerLinter();
+const linter = new WorkerLinter();
-export default function Highlighter( { richText }: { richText: RichText } ) {
- const [ targetBoxes, setTargetBoxes ] = useState< LintBox[] >( [] );
- const [ lints, setLints ] = useState< Lint[] >( [] );
+export default function Highlighter({ richText }: { richText: RichText }) {
+ const [targetBoxes, setTargetBoxes] = useState<LintBox[]>([]);
+ const [lints, setLints] = useState<Lint[]>([]);
- let updateLints = useCallback( async () => {
+ const 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 ] );
+ const contents = richText.getTextContent();
+ const newLints = await linter.lint(contents);
+ setLints(newLints);
+ }, [richText]);
- useEffect( () => {
+ useEffect(() => {
updateLints();
- let observer = new MutationObserver( updateLints );
- observer.observe( richText.getTargetElement(), {
+ const observer = new MutationObserver(updateLints);
+ observer.observe(richText.getTargetElement(), {
childList: true,
characterData: true,
subtree: true,
- } );
+ });
return () => {
observer.disconnect();
};
- }, [ richText ] );
+ }, [richText, updateLints]);
// Update the lint boxes each frame.
// Probably overkill.
//
// TODO: revisit this to do more lazily.
// Maybe `onLayoutEffect`?
- useEffect( () => {
+ useEffect(() => {
let running = true;
- function onFrame( _timestep: DOMHighResTimeStamp ) {
- let lintBoxes = lints
- .map( ( lint ) => richText.computeLintBox( lint ) )
+ function onFrame() {
+ const lintBoxes = lints
+ .map((lint) => richText.computeLintBox(lint))
.flat();
- setTargetBoxes( lintBoxes );
+ setTargetBoxes(lintBoxes);
- if ( running ) {
- requestAnimationFrame( onFrame );
+ if (running) {
+ requestAnimationFrame(onFrame);
}
}
- requestAnimationFrame( onFrame );
+ requestAnimationFrame(onFrame);
return () => {
running = false;
};
- } );
+ });
// Disable browser spellchecking in favor of ours
- useEffect( () => {
+ useEffect(() => {
richText.getTargetElement().spellcheck = false;
return () => {
richText.getTargetElement().spellcheck = true;
};
- }, [ richText ] );
+ }, [richText]);
- let visible = richText.getTargetElement().checkVisibility();
+ const visible = richText.getTargetElement().checkVisibility();
return (
<>
- { visible &&
- targetBoxes.map( ( b ) => (
- <SuggestionControl lintBox={ b } />
- ) ) }
+ {visible &&
+ targetBoxes.map((b) => (
+ <SuggestionControl
+ lintBox={b}
+ key={richText.getTextContent()}
+ />
+ ))}
</>
);
}
diff --git a/src/harper/Logo.jsx b/src/harper/Logo.jsx
index 7fabfc569..6b13071b6 100644
--- a/src/harper/Logo.jsx
+++ b/src/harper/Logo.jsx
@@ -9,13 +9,13 @@ export default function Logo() {
xmlns="http://www.w3.org/2000/svg"
xmlnsXlink="http://www.w3.org/1999/xlink"
xmlSpace="preserve"
- style={ {
+ style={{
fillRule: 'evenodd',
clipRule: 'evenodd',
strokeLinecap: 'round',
strokeLinejoin: 'round',
strokeMiterlimit: 1.5,
- } }
+ }}
>
<g transform="matrix(1,0,0,1,-5,-1720)">
<g
@@ -26,96 +26,96 @@ export default function Logo() {
<g transform="matrix(1,0,0,1,-22.3927,1.08043)">
<path
d="M1930.93,1121.75C1930.93,1121.75 1974.66,1080.73 2041.34,1094.1C2086.61,1103.18 2122.83,1145.4 2122.83,1145.4"
- style={ {
+ style={{
fill: 'none',
stroke: 'currentColor',
strokeWidth: '22.92px',
- } }
+ }}
/>
</g>
<g transform="matrix(1,0,0,1,-47.3485,12.3935)">
<path
d="M2250.3,1107.1C2250.3,1107.1 2261.8,1065.58 2311.59,1047.05C2361.62,1028.44 2422.42,1051.13 2422.42,1051.13"
- style={ {
+ style={{
fill: 'none',
stroke: 'currentColor',
strokeWidth: '22.92px',
- } }
+ }}
/>
</g>
<g transform="matrix(1.10085,0,0,1.10085,-212.096,-122.054)">
<g transform="matrix(1,0,0,1,14.3186,-0.853887)">
<ellipse
- cx={ 1981.62 }
- cy={ 1247.49 }
- rx={ 87.401 }
- ry={ 87.881 }
- style={ {
+ cx={1981.62}
+ cy={1247.49}
+ rx={87.401}
+ ry={87.881}
+ style={{
fill: 'none',
stroke: 'currentColor',
strokeWidth: '20.82px',
- } }
+ }}
/>
</g>
<rect
- x={ 2083.34 }
- y={ 1231.11 }
- width={ 66.702 }
- height={ 15.521 }
- style={ {
+ x={2083.34}
+ y={1231.11}
+ width={66.702}
+ height={15.521}
+ style={{
fill: 'none',
stroke: 'currentColor',
strokeWidth: '20.82px',
- } }
+ }}
/>
<rect
- x={ 1892.23 }
- y={ 1208.69 }
- width={ 16.306 }
- height={ 30.182 }
- style={ {
+ x={1892.23}
+ y={1208.69}
+ width={16.306}
+ height={30.182}
+ style={{
fill: 'none',
stroke: 'currentColor',
strokeWidth: '20.82px',
- } }
+ }}
/>
<g transform="matrix(-1,0,0,1,4281.79,-0.853887)">
<ellipse
- cx={ 1981.62 }
- cy={ 1247.49 }
- rx={ 87.401 }
- ry={ 87.881 }
- style={ {
+ cx={1981.62}
+ cy={1247.49}
+ rx={87.401}
+ ry={87.881}
+ style={{
fill: 'none',
stroke: 'currentColor',
strokeWidth: '20.82px',
- } }
+ }}
/>
</g>
<g transform="matrix(-1,0,0,1,4296.11,0)">
<rect
- x={ 2083.34 }
- y={ 1231.11 }
- width={ 66.702 }
- height={ 15.521 }
- style={ {
+ x={2083.34}
+ y={1231.11}
+ width={66.702}
+ height={15.521}
+ style={{
fill: 'none',
stroke: 'currentColor',
strokeWidth: '20.82px',
- } }
+ }}
/>
</g>
<g transform="matrix(-1,0,0,1,4296.11,0)">
<rect
- x={ 1892.23 }
- y={ 1208.69 }
- width={ 16.306 }
- height={ 30.182 }
- style={ {
+ x={1892.23}
+ y={1208.69}
+ width={16.306}
+ height={30.182}
+ style={{
fill: 'none',
stroke: 'currentColor',
strokeWidth: '20.82px',
- } }
+ }}
/>
</g>
</g>
diff --git a/src/harper/RichText.ts b/src/harper/RichText.ts
index 7c4f21c9f..4efdb25f5 100644
--- a/src/harper/RichText.ts
+++ b/src/harper/RichText.ts
@@ -2,12 +2,13 @@ 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;
+export type EditContentCallback = (newContent: string) => void;
-/** Represents a rich text element on-screen.
- * It can either be a child element of a `DataBlock` or be the `DataBlock` itself. */
+/**
+ * Represents a rich text element on-screen.
+ * It can either be a child element of a `DataBlock` or be the `DataBlock` itself.
+ */
export default class RichText {
private targetElement: Element;
private parent: DataBlock;
@@ -31,36 +32,36 @@ export default class RichText {
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();
+ public computeLintBox(lint: Lint): LintBox[] {
+ const text = this.targetElement.textContent;
+ const span = lint.span();
+ const range = getRangeForTextSpan(this.targetElement, span);
+ const linter = new LocalLinter();
- if ( range == null || text == null ) {
- console.log( 'Could not locate range.' );
+ if (range === null || text === null) {
+ console.log('Could not locate range.');
return [];
}
- let targetRects = range.getClientRects();
- let contRect = container.getBoundingClientRect();
+ const targetRects = range.getClientRects();
+ const container = DataBlock.getContainer();
+ const contRect = container.getBoundingClientRect();
- let boxes: LintBox[] = [];
+ const boxes: LintBox[] = [];
- for ( let targetRect of targetRects ) {
- boxes.push( {
+ for (const 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 );
+ applySuggestion: async (sug: Suggestion) => {
+ const fixed = await linter.applySuggestion(text, sug, span);
- this.editContent( fixed );
+ this.editContent(fixed);
},
- } );
+ });
}
return boxes;
diff --git a/src/harper/SidebarControl.tsx b/src/harper/SidebarControl.tsx
index 37434924b..034d501ba 100644
--- a/src/harper/SidebarControl.tsx
+++ b/src/harper/SidebarControl.tsx
@@ -1,50 +1,49 @@
import DataBlock from './DataBlock';
-import { useCallback, useEffect, useMemo, useState } from 'react';
+import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { createPortal } from 'react-dom';
import Highlighter from './Highlighter';
-import React from 'react';
export default function SidebarControl() {
- let documentContainer = useMemo< Element >(
+ const documentContainer = useMemo<Element>(
() => DataBlock.getContainer(),
[]
);
- const [ blocks, setBlocks ] = useState< DataBlock[] >( [] );
+ const [blocks, setBlocks] = useState<DataBlock[]>([]);
const updateBlocks = useCallback(
- () => setBlocks( DataBlock.getAllDataBlocks() ),
+ () => setBlocks(DataBlock.getAllDataBlocks()),
[]
);
- useEffect( updateBlocks, [] );
+ useEffect(updateBlocks, [updateBlocks]);
- useEffect( () => {
- let observer = new MutationObserver( updateBlocks );
+ useEffect(() => {
+ const observer = new MutationObserver(updateBlocks);
- observer.observe( documentContainer, {
+ observer.observe(documentContainer, {
subtree: true,
childList: true,
- } );
+ });
return () => observer.disconnect();
- }, [ documentContainer, updateBlocks ] );
+ }, [documentContainer, updateBlocks]);
const richTexts = useMemo(
- () => blocks.flatMap( ( block ) => block.getAllRichText() ),
- [ blocks ]
+ () => blocks.flatMap((block) => block.getAllRichText()),
+ [blocks]
);
- let highlights =
+ const highlights =
documentContainer &&
- richTexts.map( ( richText ) =>
+ richTexts.map((richText) =>
createPortal(
<Highlighter
- richText={ richText }
- key={ richText.getTextContent() }
+ richText={richText}
+ key={richText.getTextContent()}
/>,
documentContainer
)
);
- return <>{ highlights }</>;
+ return <>{highlights}</>;
}
diff --git a/src/harper/SidebarLintCard.tsx b/src/harper/SidebarLintCard.tsx
index e2684fda7..f7dfdbb6e 100644
--- a/src/harper/SidebarLintCard.tsx
+++ b/src/harper/SidebarLintCard.tsx
@@ -1,6 +1,6 @@
import { Lint } from 'harper.js';
import React from 'react';
-export default function SidebarLintCard( { lint }: { lint: Lint } ) {
+export default function SidebarLintCard({ lint }: { lint: Lint }) {
return <></>;
}
diff --git a/src/harper/SuggestionControl.tsx b/src/harper/SuggestionControl.tsx
index 8fa899a86..0c21cd606 100644
--- a/src/harper/SuggestionControl.tsx
+++ b/src/harper/SuggestionControl.tsx
@@ -7,102 +7,107 @@ function suggestionText(
problemText: string,
replacementText: string
): string {
- if ( kind == SuggestionKind.Remove ) {
- return `Remove "${ problemText }"`;
- } else if ( kind == SuggestionKind.Replace ) {
- return `Replace with “${ replacementText }”`;
- } else {
- return `Insert "${ replacementText }"`;
+ if (kind === SuggestionKind.Remove) {
+ return `Remove "${problemText}"`;
+ } else if (kind === SuggestionKind.Replace) {
+ return `Replace with “${replacementText}”`;
}
+ return `Insert "${replacementText}"`;
}
-/** A control for an individual suggestion shown on the screen.
+/**
+ * A control for an individual suggestion shown on the screen.
* This includes both the underline to be shown, and the control that appears when you hover over it.
- * */
-export default function SuggestionControl( { lintBox }: { lintBox: LintBox } ) {
- let { x, y, width, height, lint, applySuggestion } = lintBox;
+ * @param root0
+ * @param root0.lintBox
+ */
+export default function SuggestionControl({ lintBox }: { lintBox: LintBox }) {
+ const { x, y, width, height, lint, applySuggestion } = lintBox;
- let underlineRef = useRef< HTMLElement | null >( null );
- let popoverRef = useRef< HTMLElement | null >( null );
+ const underlineRef = useRef<HTMLDivElement | null>(null);
+ const popoverRef = useRef<HTMLDivElement | null>(null);
- let suggestions = useMemo( () => lint.suggestions(), [ lint ] );
- const [ showPopover, setShowPopover ] = useState( false );
+ const suggestions = useMemo(() => lint.suggestions(), [lint]);
+ const [showPopover, setShowPopover] = useState(false);
- useEffect( () => {
- function mouseUp( e: MouseEvent ) {
- if ( underlineRef.current == null ) {
+ useEffect(() => {
+ const effectTarget = underlineRef.current;
+ const popover = popoverRef.current;
+
+ function mouseUp(e: MouseEvent) {
+ if (effectTarget === null) {
return;
}
- let underlineRect = underlineRef.current.getBoundingClientRect();
- let popoverRect = popoverRef.current?.getBoundingClientRect();
+ const underlineRect = effectTarget.getBoundingClientRect();
+ const popoverRect = popover?.getBoundingClientRect();
if (
- isPointInBox( [ e.clientX, e.clientY ], underlineRect ) ||
- ( popoverRect &&
- isPointInBox( [ e.clientX, e.clientY ], popoverRect ) )
+ isPointInBox([e.clientX, e.clientY], underlineRect) ||
+ (popoverRect &&
+ isPointInBox([e.clientX, e.clientY], popoverRect))
) {
- setShowPopover( () => true );
+ setShowPopover(() => true);
} else {
- setShowPopover( false );
+ setShowPopover(false);
}
}
- underlineRef.current?.parentElement?.addEventListener(
- 'mouseup',
- mouseUp
- );
+ effectTarget?.parentElement?.addEventListener('mouseup', mouseUp);
return () => {
- underlineRef.current?.parentElement?.removeEventListener(
+ effectTarget?.parentElement?.removeEventListener(
'mouseup',
mouseUp
);
};
- }, [ underlineRef.current, popoverRef.current ] );
+ }, []);
return (
<>
<div
- ref={ underlineRef }
- className={ `harper-underline-${ lint.lint_kind() }` }
- style={ {
+ ref={underlineRef}
+ className={`harper-underline-${lint.lint_kind()}`}
+ style={{
position: 'absolute',
- top: `${ y }px`,
- left: ` ${ x }px`,
- width: `${ width }px`,
- height: `${ height }px`,
+ top: `${y}px`,
+ left: ` ${x}px`,
+ width: `${width}px`,
+ height: `${height}px`,
zIndex: -100,
- } }
+ }}
></div>
- { showPopover && (
+ {showPopover && (
<div
- ref={ popoverRef }
+ ref={popoverRef}
className="harper-popover"
- style={ {
+ style={{
position: 'absolute',
- top: `${ y + height + 4 }px`,
- left: ` ${ x }px`,
+ top: `${y + height + 4}px`,
+ left: ` ${x}px`,
zIndex: 100,
- } }
+ }}
>
- <h1 className={ `harper-underline-${ lint.lint_kind() }` }>
- { lint.lint_kind() }
+ <h1 className={`harper-underline-${lint.lint_kind()}`}>
+ {lint.lint_kind()}
</h1>
- <p>{ lint.message() }</p>
+ <p>{lint.message()}</p>
- { suggestions.map( ( sug ) => (
- <button onClick={ () => applySuggestion( sug ) }>
- { suggestionText(
+ {suggestions.map((sug, index) => (
+ <button
+ key={index}
+ onClick={() => applySuggestion(sug)}
+ >
+ {suggestionText(
sug.kind(),
lint.get_problem_text(),
sug.get_replacement_text()
- ) }
+ )}
</button>
- ) ) }
+ ))}
</div>
- ) }
+ )}
</>
);
}
diff --git a/src/harper/domUtils.ts b/src/harper/domUtils.ts
index c8ee3408c..6770f7c99 100644
--- a/src/harper/domUtils.ts
+++ b/src/harper/domUtils.ts
@@ -1,77 +1,85 @@
import { Span } from 'harper.js';
-/** Turn a `NodeList` into a normal JavaScript array. */
+/**
+ * Turn a `NodeList` into a normal JavaScript array.
+ * @param collection
+ */
export function extractFromHTMLCollection(
collection: HTMLCollection
): Element[] {
- let elements: Element[] = [];
+ const elements: Element[] = [];
- for ( let el of collection ) {
- elements.push( el );
+ for (const el of collection) {
+ elements.push(el);
}
return elements;
}
-/** Turn a `NodeList` into a normal JavaScript array. */
-export function extractFromNodeList< T extends Node >(
- list: NodeListOf< T >
-): T[] {
- let elements: T[] = [];
+/**
+ * Turn a `NodeList` into a normal JavaScript array.
+ * @param list
+ */
+export function extractFromNodeList<T extends Node>(list: NodeListOf<T>): T[] {
+ const elements: T[] = [];
- for ( let i = 0; i < list.length; i++ ) {
- let item = list[ i ];
- elements.push( item );
+ for (let i = 0; i < list.length; i++) {
+ const item = list[i];
+ elements.push(item);
}
return elements;
}
-export function getNodesFromQuerySelector( element: Element, query: string ) {
- return extractFromNodeList( element.querySelectorAll( query ) );
+export function getNodesFromQuerySelector(element: Element, query: string) {
+ return extractFromNodeList(element.querySelectorAll(query));
}
-/** Flatten a provided node, and it's children into a single array. */
-export function leafNodes( node: Element ): Element[] {
- let out = [];
+/**
+ * Flatten a provided node, and it's children into a single array.
+ * @param node
+ */
+export function leafNodes(node: Node): Node[] {
+ const out: Node[] = [];
- let children = extractFromNodeList( node.childNodes );
+ const children = extractFromNodeList(node.childNodes);
- if ( children.length == 0 ) {
- return [ node ];
+ if (children.length === 0) {
+ return [node];
}
- for ( let child of children ) {
- let sub = leafNodes( child );
- sub.forEach( ( v ) => out.push( v ) );
+ for (const child of children) {
+ const sub = leafNodes(child);
+ sub.forEach((v) => out.push(v));
}
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: Element,
- span: Span
-): Range | null {
- let children = leafNodes( target );
+/**
+ * Given an element and a Span of text inside it, compute the Range that represents the region of the DOM represented.
+ * @param target
+ * @param span
+ */
+export function getRangeForTextSpan(target: Element, span: Span): Range | null {
+ const children = leafNodes(target);
- let range = document.createRange();
+ const 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;
+ for (let i = 0; i < children.length; i++) {
+ const child = children[i] as HTMLElement;
+ const childText = child.textContent ?? '';
- if ( traversed + childText.length > span.start && ! startFound ) {
- range.setStart( child, span.start - traversed );
+ 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 );
+ if (startFound && traversed + childText.length >= span.end) {
+ range.setEnd(child, span.end - traversed);
return range;
}
@@ -81,19 +89,20 @@ export function getRangeForTextSpan(
return null;
}
-/** Locate the rich text containers inside a given element.
- * Notice: this function may return the provided element. */
-export function getRichTextContainers( target: Element ): Element[] {
- let elms: Element[] = [];
+/**
+ * Locate the rich text containers inside a given element.
+ * Notice: this function may return the provided element.
+ * @param target
+ */
+export function getRichTextContainers(target: Element): Element[] {
+ const elms: Element[] = [];
- if ( target.classList.contains( 'rich-text' ) ) {
- elms.push( target );
+ if (target.classList.contains('rich-text')) {
+ elms.push(target);
}
elms.push(
- ...extractFromHTMLCollection(
- target.getElementsByClassName( 'rich-text' )
- )
+ ...extractFromHTMLCollection(target.getElementsByClassName('rich-text'))
);
return elms;
diff --git a/src/harper/gutenbergUtils.ts b/src/harper/gutenbergUtils.ts
deleted file mode 100644
index 0d2447cc4..000000000
--- a/src/harper/gutenbergUtils.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-import { dispatch } from '@wordpress/data';
-
-export async function setBlockContent( clientId: string, text: string ) {
- const { updateBlockAttributes } = dispatch( 'core/block-editor' );
-
- let k = await updateBlockAttributes( clientId, { content: text } );
-}
diff --git a/src/harper/index.tsx b/src/harper/index.tsx
index a7bf09892..202e74e70 100644
--- a/src/harper/index.tsx
+++ b/src/harper/index.tsx
@@ -9,10 +9,10 @@ import React from 'react';
function Sidebar() {
return (
<>
- <PluginSidebarMoreMenuItem target="harper-sidebar" icon={ Logo }>
+ <PluginSidebarMoreMenuItem target="harper-sidebar" icon={Logo}>
Harper
</PluginSidebarMoreMenuItem>
- <PluginSidebar name="harper-sidebar" title="Harper" icon={ Logo }>
+ <PluginSidebar name="harper-sidebar" title="Harper" icon={Logo}>
<SidebarControl />
</PluginSidebar>
</>
@@ -20,8 +20,8 @@ function Sidebar() {
}
// @ts-ignore
-if ( ! window.__harperSidebarRegistered ) {
- registerPlugin( 'harper-sidebar', { render: Sidebar } );
+if (!window.__harperSidebarRegistered) {
+ registerPlugin('harper-sidebar', { render: Sidebar });
// @ts-ignore
window.__harperSidebarRegistered = true;
}
diff --git a/src/harper/useFrameCount.ts b/src/harper/useFrameCount.ts
deleted file mode 100644
index e43042cdb..000000000
--- a/src/harper/useFrameCount.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-import { useState, useEffect } from 'react';
-
-/** A React hook that counts the number of frames since since a component was first created. Will update every frame. */
-export default function useFrameCount(): number {
- let [ count, setFrameCount ] = useState( 0 );
-
- useEffect( () => {
- let running = true;
-
- function incRep() {
- setFrameCount( ( v ) => v + 1 );
- requestAnimationFrame( incRep );
- }
-
- requestAnimationFrame( incRep );
-
- return () => {
- running = false;
- };
- } );
-
- return count;
-}
diff --git a/yarn.lock b/yarn.lock
index ce569a73a..9531b92a8 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -3098,6 +3098,23 @@
core-js "^3.31.0"
react "^18.3.0"
+"@wordpress/babel-preset-default@^8.16.0":
+ version "8.16.0"
+ resolved "https://registry.yarnpkg.com/@wordpress/babel-preset-default/-/babel-preset-default-8.16.0.tgz#963249404a2a49effa725767954e093a3face5af"
+ integrity sha512-CPAMxQ5eMmsRNJN0edUZvsJDnmALQFGbWyCxsJiJJ/0LFi1lYFC7r5YGcJXVVD+oX9L908NZpxwsQBHb6gkxig==
+ dependencies:
+ "@babel/core" "7.25.7"
+ "@babel/plugin-transform-react-jsx" "7.25.7"
+ "@babel/plugin-transform-runtime" "7.25.7"
+ "@babel/preset-env" "7.25.7"
+ "@babel/preset-typescript" "7.25.7"
+ "@babel/runtime" "7.25.7"
+ "@wordpress/browserslist-config" "^6.16.0"
+ "@wordpress/warning" "^3.16.0"
+ browserslist "^4.21.10"
+ core-js "^3.31.0"
+ react "^18.3.0"
+
"@wordpress/base-styles@^5.15.0":
version "5.15.0"
resolved "https://registry.yarnpkg.com/@wordpress/base-styles/-/base-styles-5.15.0.tgz#bf33c65bcf6ddc96e8ac1394a3fb39423034fa89"
@@ -3210,6 +3227,11 @@
resolved "https://registry.yarnpkg.com/@wordpress/browserslist-config/-/browserslist-config-6.15.0.tgz#ffd7728e2f70eb8407baf2668f69773d01a63359"
integrity sha512-JmpThXSvE/ZsihJ/GOBmZZUEgVN78xWIPDacRzEXCPp9FA4UuJpXgx1JY3nWe4L3FyB6XIz+Klk0TfEwfL8S2w==
+"@wordpress/browserslist-config@^6.16.0":
+ version "6.16.0"
+ resolved "https://registry.yarnpkg.com/@wordpress/browserslist-config/-/browserslist-config-6.16.0.tgz#a8887c7b6735e3951cb16eff3b38e0a6d8ad3309"
+ integrity sha512-ppjgUOjCFwLjH5XCDAfavRkIAj9DKEVGj12YMGL+dzSikbDU9L8VnMT1p08G1bAcBkTRLtBX8KkJJdyUI1JGsQ==
+
"@wordpress/commands@^1.15.1":
version "1.15.1"
resolved "https://registry.yarnpkg.com/@wordpress/commands/-/commands-1.15.1.tgz#8b1654c66905615ff9643ca4715168f263538546"
@@ -3513,6 +3535,29 @@
globals "^13.12.0"
requireindex "^1.2.0"
+"@wordpress/eslint-plugin@^22.2.0":
+ version "22.2.0"
+ resolved "https://registry.yarnpkg.com/@wordpress/eslint-plugin/-/eslint-plugin-22.2.0.tgz#f1c6df429176525c651d451e67ef77695bbb5af0"
+ integrity sha512-JKs2tEpkg6txe1emR+P7gw9OrSXvu89TMHuN1uk0wxdJeEqoF7HcxTdJBEaMHCz5HFhBHeuI5vShtX/WN6jf3g==
+ dependencies:
+ "@babel/eslint-parser" "7.25.7"
+ "@typescript-eslint/eslint-plugin" "^6.4.1"
+ "@typescript-eslint/parser" "^6.4.1"
+ "@wordpress/babel-preset-default" "^8.16.0"
+ "@wordpress/prettier-config" "^4.16.0"
+ cosmiconfig "^7.0.0"
+ eslint-config-prettier "^8.3.0"
+ eslint-plugin-import "^2.25.2"
+ eslint-plugin-jest "^27.4.3"
+ eslint-plugin-jsdoc "^46.4.6"
+ eslint-plugin-jsx-a11y "^6.5.1"
+ eslint-plugin-playwright "^0.15.3"
+ eslint-plugin-prettier "^5.0.0"
+ eslint-plugin-react "^7.27.0"
+ eslint-plugin-react-hooks "^4.3.0"
+ globals "^13.12.0"
+ requireindex "^1.2.0"
+
"@wordpress/fields@^0.7.1":
version "0.7.1"
resolved "https://registry.yarnpkg.com/@wordpress/fields/-/fields-0.7.1.tgz#af7404045d86d499aa6c725de8ecf204dad7e079"
@@ -3734,6 +3779,11 @@
resolved "https://registry.yarnpkg.com/@wordpress/prettier-config/-/prettier-config-4.15.0.tgz#15409909be8271da2cf87e33620d445751c083d1"
integrity sha512-e5GZCnbm65cXbndAX/lAEpeixTetlXofJ6xk93ppm9bt9Y6RkUVe3+LfNlc6AYpRAt97BGIBEG7CXtMPVEz1ew==
+"@wordpress/prettier-config@^4.16.0":
+ version "4.16.0"
+ resolved "https://registry.yarnpkg.com/@wordpress/prettier-config/-/prettier-config-4.16.0.tgz#7e5770c143a73a61589a131cc82f3e23734f3e84"
+ integrity sha512-raaEW2x+pPfIYXF7RUJGEH8zeYWd4esIE0bJbKTAJV0yM+RE/DOWsGLGPiVvLEAAPg2AkIZ7oX0UPOYsBrsBqw==
+
"@wordpress/primitives@^4.15.1":
version "4.15.1"
resolved "https://registry.yarnpkg.com/@wordpress/primitives/-/primitives-4.15.1.tgz#ff1c2408bccbe6958bdce4814492917472d99094"
@@ -3977,6 +4027,11 @@
resolved "https://registry.yarnpkg.com/@wordpress/warning/-/warning-3.15.0.tgz#1a86ab1a7aa6615e869d39024c928933cbe1ce91"
integrity sha512-qBE1kWiyey9rlLGQHf3ngdWgov58CV2a8tQR8UMy1EwKUDAS56UXJcAvfAi7b7aMN5gZ+hVa4LWK9WbVuKEdRg==
+"@wordpress/warning@^3.16.0":
+ version "3.16.0"
+ resolved "https://registry.yarnpkg.com/@wordpress/warning/-/warning-3.16.0.tgz#e3a4d4a1535c4fe0b07253a039a606458534c12b"
+ integrity sha512-XsgqRPvB+VSecXnD3VfvJJxhcdTTX4EkgdzvWspmQnw0rNCV636KByZVgolzYhvr3La9EgqO+MqXzwvPHg/xfQ==
+
"@wordpress/wordcount@^4.15.0":
version "4.15.0"
resolved "https://registry.yarnpkg.com/@wordpress/wordcount/-/wordcount-4.15.0.tgz#9b796ded432a95ed9b834fa89c04777a17651023"
@@ -6136,7 +6191,7 @@ eslint-visitor-keys@^4.2.0:
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-4.2.0.tgz#687bacb2af884fcdda8a6e7d65c606f46a14cd45"
integrity sha512-UyLnSehNt62FFhSwjZlHmeokpRK59rcz29j+F1/aDgbkbRTk7wIc9XzdoasMUbRNKDM0qQt/+BJ4BrpFeABemw==
-eslint@^8.3.0:
+eslint@^8.3.0, eslint@^8.57.0:
version "8.57.1"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-8.57.1.tgz#7df109654aba7e3bbe5c8eae533c5e461d3c6ca9"
integrity sha512-ypowyDxpVSYpkXr9WPv2PAZCtNip1Mv5KTW0SCurXv/9iOpcrH9PaqUElksqEB6pChqHGDRCFTyrZlGhnLNGiA==
@@ -9932,6 +9987,11 @@ prettier-linter-helpers@^1.0.0:
dependencies:
fast-diff "^1.1.2"
+prettier@^3.4.2:
+ version "3.4.2"
+ resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.4.2.tgz#a5ce1fb522a588bf2b78ca44c6e6fe5aa5a2b13f"
+ integrity sha512-e9MewbtFo+Fevyuxn/4rrcDAaq0IYxPGLvObpQjiZBMAzB9IGmzlnG9RZy3FFas+eBMu2vA0CszMeduow5dIuQ==
+
"prettier@npm:[email protected]":
version "3.0.3"
resolved "https://registry.yarnpkg.com/wp-prettier/-/wp-prettier-3.0.3.tgz#2b30647d044b83afd10dacfc2805d55fc180d852"
@@ -10113,6 +10173,13 @@ react-dom@^18.3.0:
loose-envify "^1.1.0"
scheduler "^0.23.2"
+react-dom@^19.0.0:
+ version "19.0.0"
+ resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-19.0.0.tgz#43446f1f01c65a4cd7f7588083e686a6726cfb57"
+ integrity sha512-4GV5sHFG0e/0AD4X+ySy6UJd3jVl1iNsNHdpad0qhABJ11twS3TTBnseqsKurKcsNqCEFeGL3uLpVChpIO3QfQ==
+ dependencies:
+ scheduler "^0.25.0"
+
react-easy-crop@^5.0.6:
version "5.2.0"
resolved "https://registry.yarnpkg.com/react-easy-crop/-/react-easy-crop-5.2.0.tgz#7b59525c558f568e8796a84c91d1e609e9775ee0"
@@ -10170,6 +10237,11 @@ react@^18.3.0:
dependencies:
loose-envify "^1.1.0"
+react@^19.0.0:
+ version "19.0.0"
+ resolved "https://registry.yarnpkg.com/react/-/react-19.0.0.tgz#6e1969251b9f108870aa4bff37a0ce9ddfaaabdd"
+ integrity sha512-V8AVnmPIICiWpGfm6GLzCR/W5FXLchHop40W4nXBmdlEceh16rCN8O8LNWm5bh5XUX91fh7KpA+W0TgMKmgTpQ==
+
read-pkg-up@^7.0.1:
version "7.0.1"
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-7.0.1.tgz#f3a6135758459733ae2b95638056e1854e7ef507"
@@ -10568,6 +10640,11 @@ scheduler@^0.23.2:
dependencies:
loose-envify "^1.1.0"
+scheduler@^0.25.0:
+ version "0.25.0"
+ resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.25.0.tgz#336cd9768e8cceebf52d3c80e3dcf5de23e7e015"
+ integrity sha512-xFVuu11jh+xcO7JOAGJNOXld8/TcEHK/4CituBUeUb5hqxJLj9YuemAEuvm9gQ/+pgXYfbQuqAkiYu+u7YEsNA==
+
schema-utils@^3.0.0, schema-utils@^3.2.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-3.3.0.tgz#f50a88877c3c01652a15b622ae9e9795df7a60fe"
@@ -11720,10 +11797,10 @@ typedarray-to-buffer@^3.1.5:
dependencies:
is-typedarray "^1.0.0"
-typescript@^5.7.2:
- version "5.7.2"
- resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.7.2.tgz#3169cf8c4c8a828cde53ba9ecb3d2b1d5dd67be6"
- integrity sha512-i5t66RHxDvVN40HfDd1PsEThGNnlMCMT3jMUuoh9/0TaqWevNontacunWyN02LA9/fIbEWlcHZcgTKb9QoaLfg==
+typescript@^5.7.3:
+ version "5.7.3"
+ resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.7.3.tgz#919b44a7dbb8583a9b856d162be24a54bf80073e"
+ integrity sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw==
uc.micro@^1.0.1, uc.micro@^1.0.5:
version "1.0.6"