|
|
import { makeSelectorFromBlockName, validatePublishedFormFields } from './shared'; |
|
|
import { BlockFlow, EditorContext, PublishedPostContext } from '.'; |
|
|
|
|
|
interface ConfigurationData { |
|
|
prompt: string; |
|
|
} |
|
|
|
|
|
interface ValidationData { |
|
|
sampleInputLabel: string; |
|
|
submitButtonText: string; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class FormAiFlow implements BlockFlow { |
|
|
private configurationData: ConfigurationData; |
|
|
private validationData: ValidationData | undefined; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor( configurationData: ConfigurationData ) { |
|
|
this.configurationData = configurationData; |
|
|
} |
|
|
|
|
|
blockSidebarName = 'Form'; |
|
|
blockTestName = 'Form (AI)'; |
|
|
blockEditorSelector = makeSelectorFromBlockName( 'Form' ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async configure( context: EditorContext ): Promise< void > { |
|
|
const aiInputParentLocator = await context.editorPage.getEditorCanvas(); |
|
|
|
|
|
const possiblePlaceholders = [ |
|
|
|
|
|
'Example: a contact form with name, email, and message fields', |
|
|
'Example: a pizza ordering form with name, address, phone number and toppings', |
|
|
'Example: a survey form with multiple choice questions', |
|
|
|
|
|
'Ask Jetpack AI to edit…', |
|
|
]; |
|
|
const aiInputReadyLocator = await Promise.any( |
|
|
possiblePlaceholders.map( async ( placeholder ) => { |
|
|
const locator = aiInputParentLocator.getByPlaceholder( placeholder ); |
|
|
await locator.waitFor(); |
|
|
return locator; |
|
|
} ) |
|
|
); |
|
|
|
|
|
const aiInputBusyLocator = aiInputParentLocator.getByRole( 'button', { |
|
|
name: 'Stop request', |
|
|
} ); |
|
|
const sendButtonLocator = aiInputParentLocator.getByRole( 'button', { |
|
|
name: 'Send request', |
|
|
} ); |
|
|
await aiInputReadyLocator.fill( this.configurationData.prompt ); |
|
|
await sendButtonLocator.click(); |
|
|
await aiInputBusyLocator.waitFor( { state: 'detached' } ); |
|
|
|
|
|
this.validationData = { |
|
|
sampleInputLabel: await this.getFirstTextFieldLabel( context ), |
|
|
submitButtonText: await this.getSubmitButtonText( context ), |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async getFirstTextFieldLabel( context: EditorContext ): Promise< string > { |
|
|
return await context.addedBlockLocator |
|
|
.getByRole( 'document', { |
|
|
|
|
|
|
|
|
name: /^Block: (Text input|Name|Email|Multi-line text) field$/, |
|
|
} ) |
|
|
.locator( 'label' ) |
|
|
.first() |
|
|
.innerText(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async getSubmitButtonText( context: EditorContext ): Promise< string > { |
|
|
return await context.addedBlockLocator |
|
|
.getByRole( 'document', { |
|
|
name: 'Block: Button', |
|
|
} ) |
|
|
.first() |
|
|
.innerText(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async validateAfterPublish( context: PublishedPostContext ): Promise< void > { |
|
|
if ( ! this.validationData ) { |
|
|
throw new Error( 'Unable to find fields in the editor from the AI form.' ); |
|
|
} |
|
|
|
|
|
await validatePublishedFormFields( context.page, [ |
|
|
{ type: 'textbox', accessibleName: this.validationData.sampleInputLabel }, |
|
|
{ type: 'button', accessibleName: this.validationData.submitButtonText }, |
|
|
] ); |
|
|
} |
|
|
} |
|
|
|