Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jetpack AI Image: handle enter shortcut on user custom prompt field #38103

Merged
merged 10 commits into from
Jun 28, 2024
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Jetpack AI Image: trigger an image generation when user hits enter on the custom user prompt field.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
display: flex;
justify-content: center;

div {
width: 100%;
display: flex;
}

textarea {
width: 100%;
outline: none;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { Button, Tooltip } from '@wordpress/components';
import { Button, Tooltip, KeyboardShortcuts } from '@wordpress/components';
import { useCallback, useRef, useState, useEffect } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { Icon, external } from '@wordpress/icons';
Expand Down Expand Up @@ -74,7 +74,7 @@ export default function AiImageModal( {

const handleUserPromptChange = useCallback(
( e: React.ChangeEvent< HTMLTextAreaElement > ) => {
setUserPrompt( e.target.value );
setUserPrompt( e.target.value.trim() );
},
[ setUserPrompt ]
);
Expand All @@ -98,22 +98,22 @@ export default function AiImageModal( {
const costTooltipText = cost === 1 ? costTooltipTextSingular : costTooltipTextPlural;

// Controllers
const disableInstructions = notEnoughRequests || generating;
const showUpgrade = ( requireUpgrade || notEnoughRequests ) && ! generating;
const showCounter = Boolean( ! isUnlimited && cost && currentLimit );
const disableTryAgainButton = ! userPrompt && ! postContent;
const disableGenerateButton =
const instructionsDisabled = notEnoughRequests || generating;
const upgradePromptVisible = ( requireUpgrade || notEnoughRequests ) && ! generating;
const counterVisible = Boolean( ! isUnlimited && cost && currentLimit );
const tryAgainButtonDisabled = ! userPrompt && ! postContent;
const generateButtonDisabled =
notEnoughRequests || generating || ( ! userPrompt && ! postContent );

const tryAgainButton = (
<Button onClick={ handleTryAgain } variant="secondary" disabled={ disableTryAgainButton }>
<Button onClick={ handleTryAgain } variant="secondary" disabled={ tryAgainButtonDisabled }>
{ __( 'Try again', 'jetpack' ) }
</Button>
);

const generateButton = (
<Tooltip text={ costTooltipText } placement="bottom">
<Button onClick={ handleGenerate } variant="secondary" disabled={ disableGenerateButton }>
<Button onClick={ handleGenerate } variant="secondary" disabled={ generateButtonDisabled }>
{ generateButtonLabel }
</Button>
</Tooltip>
Expand All @@ -138,16 +138,27 @@ export default function AiImageModal( {
<div className="ai-image-modal__content">
<div className="ai-image-modal__user-prompt">
<div className="ai-image-modal__user-prompt-textarea">
<textarea
disabled={ disableInstructions }
maxLength={ 1000 }
rows={ 2 }
onChange={ handleUserPromptChange }
placeholder={ instructionsPlaceholder }
></textarea>
<KeyboardShortcuts
bindGlobal
shortcuts={ {
enter: () => {
if ( ! generateButtonDisabled ) {
handleGenerate();
}
},
} }
>
<textarea
disabled={ instructionsDisabled }
maxLength={ 1000 }
rows={ 2 }
onChange={ handleUserPromptChange }
placeholder={ instructionsPlaceholder }
></textarea>
</KeyboardShortcuts>
</div>
</div>
{ showUpgrade && (
{ upgradePromptVisible && (
<UpgradePrompt
description={ upgradeDescription }
placement={ FEATURED_IMAGE_UPGRADE_PROMPT_PLACEMENT }
Expand All @@ -156,7 +167,7 @@ export default function AiImageModal( {
) }
<div className="ai-image-modal__actions">
<div className="ai-image-modal__actions-left">
{ showCounter && (
{ counterVisible && (
<UsageCounter
cost={ cost }
currentLimit={ currentLimit }
Expand Down
Loading