Skip to content

Commit

Permalink
AI Assistant: Hide input when user types on extended block (#37801)
Browse files Browse the repository at this point in the history
* add hook to hide the input when user types

* changelog

* add hideOnBlockFocus to the block handlers

* change hiding behavior to focus instead of keydown

* rename event listener function and fix autohide on stream
  • Loading branch information
dhasilva authored Jun 13, 2024
1 parent 9742c21 commit c4efc08
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: enhancement

AI Assistant: Hide input when user types on extended block
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class BlockHandler {
public feature: string = 'ai-assistant';
public adjustPosition: boolean = true;
public startOpen: boolean = false;
public hideOnBlockFocus: boolean = true;

constructor( clientId: string, renderRules: RenderHTMLRules = [] ) {
this.clientId = clientId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ export function getBlockHandler(
feature: handler.feature,
adjustPosition: handler.adjustPosition,
startOpen: handler.startOpen,
hideOnBlockFocus: handler.hideOnBlockFocus,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class JetpackChildrenFormHandler extends BlockHandler {
super( clientId, [] );
this.behavior = this.handleBehavior;
this.isChildBlock = true;
this.hideOnBlockFocus = false;
}

handleBehavior = ( { context } ) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class JetpackFormHandler extends BlockHandler {
this.feature = 'jetpack-form-ai-extension';
this.adjustPosition = false;
this.startOpen = true;
this.hideOnBlockFocus = false;
}

private setContent( newContent: string, isRequestDone = false ): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface IBlockHandler {
feature: string;
adjustPosition?: boolean;
startOpen?: boolean;
hideOnBlockFocus?: boolean;
}

export type BlockEditorSelect = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ import type {
} from '../components/ai-assistant-toolbar-dropdown/dropdown-content';
import type { ExtendedInlineBlockProp } from '../extensions/ai-assistant';
import type { PromptTypeProp } from '../lib/prompt';
import type { PromptMessagesProp, PromptItemProps } from '@automattic/jetpack-ai-client';
import type {
PromptMessagesProp,
PromptItemProps,
RequestingStateProp,
} from '@automattic/jetpack-ai-client';

const debug = debugFactory( 'jetpack-ai-assistant:extensions:with-ai-extension' );

Expand Down Expand Up @@ -82,6 +86,8 @@ const blockEditWithAiComponents = createHigherOrderComponent( BlockEdit => {
const [ action, setAction ] = useState< string >( '' );
// The last request made by the user, to be used when the user clicks the "Try Again" button.
const lastRequest = useRef< RequestOptions | null >( null );
// Ref to the requesting state to use it in the hideOnBlockFocus effect.
const requestingStateRef = useRef< RequestingStateProp | null >( null );
// Data and functions from the editor.
const { undo } = useDispatch( 'core/editor' ) as CoreEditorDispatch;
const { postId } = useSelect( select => {
Expand Down Expand Up @@ -121,6 +127,7 @@ const blockEditWithAiComponents = createHigherOrderComponent( BlockEdit => {
feature,
adjustPosition,
startOpen,
hideOnBlockFocus,
} = useMemo( () => getBlockHandler( blockName, clientId ), [ blockName, clientId ] );

// State to display the AI Control or not.
Expand Down Expand Up @@ -287,6 +294,11 @@ const blockEditWithAiComponents = createHigherOrderComponent( BlockEdit => {
},
} );

// Save the requesting state to use it in the hideOnBlockFocus effect.
useEffect( () => {
requestingStateRef.current = requestingState;
}, [ requestingState ] );

// Called when a suggestion from the toolbar is requested, like "Change tone".
const handleRequestSuggestion = useCallback< OnRequestSuggestion >(
( promptType, options, humanText ) => {
Expand Down Expand Up @@ -450,6 +462,33 @@ const blockEditWithAiComponents = createHigherOrderComponent( BlockEdit => {
};
}, [ adjustBlockPadding, adjustPosition, clientId, controlObserver, id, showAiControl ] );

// Hide the AI Control when the block is focused.
useEffect( () => {
if ( ! hideOnBlockFocus ) {
return;
}

if ( showAiControl ) {
const element = ownerDocument.current.getElementById( id );

const handleFocusInBlock = () => {
// If the AI Control is requesting or suggesting, don't hide it, as the block focus is programmatic.
if ( [ 'requesting', 'suggesting' ].includes( requestingStateRef.current as string ) ) {
return;
}

setShowAiControl( false );
element?.removeEventListener( 'focusin', handleFocusInBlock );
};

element?.addEventListener( 'focusin', handleFocusInBlock );

return () => {
element?.removeEventListener( 'focusin', handleFocusInBlock );
};
}
}, [ hideOnBlockFocus, showAiControl, id ] );

const aiInlineExtensionContent = (
<>
<BlockEdit { ...props } />
Expand Down

0 comments on commit c4efc08

Please sign in to comment.