Skip to content

Commit

Permalink
AI Proofread: Only point to cursor if long sentence
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoagds committed Jul 15, 2024
1 parent 925485e commit e34a425
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import features from './index';
/**
* Types
*/
import type { BreveDispatch } from '../types';
import type { BreveDispatch, Anchor } from '../types';

let highlightTimeout: number;
let anchorTimeout: number;
Expand All @@ -21,14 +21,17 @@ function handleMouseEnter( e: React.MouseEvent ) {

anchorTimeout = setTimeout( () => {
const el = e.target as HTMLElement;
const rect = el.getBoundingClientRect();
const diff = e.clientY - Math.floor( rect.top );
const offset = diff === 0 ? 10 : 0;
let virtual = el;

( dispatch( 'jetpack/ai-breve' ) as BreveDispatch ).setHighlightHover( true );
( dispatch( 'jetpack/ai-breve' ) as BreveDispatch ).setPopoverAnchor( {
target: e.target as HTMLElement,
virtual: {
const words = el?.innerText?.match?.( /\b\w+\b/g );
const shouldPointToCursor = words?.length > 3;

if ( shouldPointToCursor ) {
const rect = el.getBoundingClientRect();
const diff = e.clientY - Math.floor( rect.top );
const offset = diff === 0 ? 10 : 0;

virtual = {
getBoundingClientRect() {
return {
top: e.clientY + offset,
Expand All @@ -42,8 +45,14 @@ function handleMouseEnter( e: React.MouseEvent ) {
} as DOMRect;
},
contextElement: e.target as HTMLElement,
},
} as unknown as HTMLElement );
} as unknown as HTMLElement;
}

( dispatch( 'jetpack/ai-breve' ) as BreveDispatch ).setHighlightHover( true );
( dispatch( 'jetpack/ai-breve' ) as BreveDispatch ).setPopoverAnchor( {
target: e.target as HTMLElement,
virtual: virtual,
} as Anchor );
}, 100 );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Types
*/
import type { BreveState } from '../types';
import type { Anchor, BreveState } from '../types';

// POPOVER

Expand All @@ -13,7 +13,7 @@ export function isPopoverHover( state: BreveState ) {
return state.popover?.isPopoverHover;
}

export function getPopoverAnchor( state: BreveState ): HTMLElement | EventTarget | null {
export function getPopoverAnchor( state: BreveState ): Anchor {
return state?.popover?.anchor ?? null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export type BreveControls = () => React.JSX.Element;

type Anchor = {
export type Anchor = {
target: HTMLElement;
virtual: {
getBoundingClientRect: () => DOMRect;
contextElement: HTMLElement;
contextElement?: HTMLElement;
};
};

Expand Down Expand Up @@ -34,7 +34,7 @@ export type BreveSelect = {
export type BreveDispatch = {
setHighlightHover: ( isHover: boolean ) => void;
setPopoverHover: ( isHover: boolean ) => void;
setPopoverAnchor: ( anchor: HTMLElement | EventTarget ) => void;
setPopoverAnchor: ( anchor: Anchor ) => void;
increasePopoverLevel: () => void;
decreasePopoverLevel: () => void;
toggleProofread: ( force?: boolean ) => void;
Expand Down

0 comments on commit e34a425

Please sign in to comment.