Skip to content

Commit

Permalink
AI Inline Extensions: Support custom behavior (#37634)
Browse files Browse the repository at this point in the history
* AI Inline Extensions: Support Custom Behavior

* AI Inline Extensions: Expose actions through Context

* changelog

* AI Inline Extensions: Mark list-item as children

* AI Inline Extensions: Update child block name and context render
  • Loading branch information
renatoagds authored May 31, 2024
1 parent 3beba4f commit 4659a6f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Add support for custom behavior on AI Inline Extensions
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ export class BlockHandler {
public renderRules: RenderHTMLRules = [];
public firstUpdate: boolean = true;
public behavior: BlockBehavior = 'dropdown' as const;
public isChildBlock: boolean = false;

constructor(
clientId: string,
renderRules: RenderHTMLRules = [],
behavior: BlockBehavior = 'dropdown'
behavior: BlockBehavior = 'dropdown',
isChildBlock: boolean = false
) {
this.clientId = clientId;
this.renderRules = renderRules;
this.behavior = behavior;
this.isChildBlock = isChildBlock;
}

public getBlock(): Block {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import { aiAssistantIcon } from '@automattic/jetpack-ai-client';
import { useAnalytics } from '@automattic/jetpack-shared-extension-utils';
import { ToolbarButton, Dropdown } from '@wordpress/components';
import React, { useCallback, useContext } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import React, { useCallback } from 'react';
/*
* Internal dependencies
*/
import AiAssistantToolbarDropdownContent from '../../../components/ai-assistant-toolbar-dropdown/dropdown-content';
import useTransformToAssistant from '../../../hooks/use-transform-to-assistant';
import { InlineExtensionsContext } from '../../get-block-handler';
/*
* Types
*/
Expand Down Expand Up @@ -114,6 +115,7 @@ export default function AiAssistantExtensionToolbarDropdown( {
onRequestSuggestion,
}: AiAssistantExtensionToolbarDropdownProps ): ReactElement {
const { tracks } = useAnalytics();
const inlineExtensionsContext = useContext( InlineExtensionsContext );

const toggleHandler = useCallback(
( isOpen: boolean ) => {
Expand Down Expand Up @@ -153,6 +155,11 @@ export default function AiAssistantExtensionToolbarDropdown( {
} }
renderToggle={ ( { isOpen, onToggle } ) => {
const handleClick = () => {
if ( typeof behavior === 'function' ) {
behavior( { onToggle, onAskAiAssistant, context: inlineExtensionsContext } );
return;
}

switch ( behavior ) {
case 'action':
handleAskAiAssistant();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import { createContext } from '@wordpress/element';
import debugFactory from 'debug';
/**
* Internal dependencies
Expand All @@ -25,6 +26,8 @@ const handlers = {
'core/list': ListHandler,
};

export const InlineExtensionsContext = createContext( {} );

/**
* Gets the block handler based on the block type.
* The block handler is used to handle the request suggestions.
Expand All @@ -50,5 +53,6 @@ export function getBlockHandler(
onDone: handler.onDone.bind( handler ),
getContent: handler.getContent.bind( handler ),
behavior: handler.behavior,
isChildBlock: handler.isChildBlock,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import { BlockHandler } from '../block-handler';
export class ListItemHandler extends BlockHandler {
constructor( clientId: string ) {
super( clientId, [ 'listItem' ] );
this.isChildBlock = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import type { Block } from '@automattic/jetpack-ai-client';

export type OnSuggestion = ( suggestion: string ) => void;

export type BlockBehavior = 'dropdown' | 'action';
type CustomBlockBehavior = ( { onToggle, onAskAiAssistant, context } ) => void;
export type BlockBehavior = 'dropdown' | 'action' | CustomBlockBehavior;

export interface IBlockHandler {
onSuggestion: OnSuggestion;
onDone: ( suggestion: string ) => void;
getContent: () => string;
behavior: BlockBehavior;
isChildBlock?: boolean;
}

export type BlockEditorSelect = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import useAutoScroll from '../hooks/use-auto-scroll';
import { mapInternalPromptTypeToBackendPromptType } from '../lib/prompt/backend-prompt';
import AiAssistantInput from './components/ai-assistant-input';
import AiAssistantExtensionToolbarDropdown from './components/ai-assistant-toolbar-dropdown';
import { getBlockHandler } from './get-block-handler';
import { getBlockHandler, InlineExtensionsContext } from './get-block-handler';
import { isPossibleToExtendBlock } from './lib/is-possible-to-extend-block';
/*
* Types
Expand Down Expand Up @@ -114,6 +114,7 @@ const blockEditWithAiComponents = createHigherOrderComponent( BlockEdit => {
onDone: onBlockDone,
getContent,
behavior,
isChildBlock,
} = useMemo( () => getBlockHandler( blockName, clientId ), [ blockName, clientId ] );

// Called when the user clicks the "Ask AI Assistant" button.
Expand Down Expand Up @@ -431,7 +432,7 @@ const blockEditWithAiComponents = createHigherOrderComponent( BlockEdit => {
};
}, [ adjustBlockPadding, clientId, controlObserver, id, showAiControl ] );

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

Expand Down Expand Up @@ -462,6 +463,20 @@ const blockEditWithAiComponents = createHigherOrderComponent( BlockEdit => {
</BlockControls>
</>
);

if ( isChildBlock ) {
return aiInlineExtensionContent;
}

const ProviderProps = {
value: { [ blockName ]: { handleAskAiAssistant, handleRequestSuggestion } },
};

return (
<InlineExtensionsContext.Provider { ...ProviderProps }>
{ aiInlineExtensionContent }
</InlineExtensionsContext.Provider>
);
};
}, 'blockEditWithAiComponents' );

Expand Down

0 comments on commit 4659a6f

Please sign in to comment.