From d9049e140263e97cef863ad4d8d9b6600a70e187 Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Thu, 10 Aug 2023 13:24:59 +0300 Subject: [PATCH] chore: content generation ai block & toolbar request optimization --- src/blocks/blocks/content-generator/edit.js | 150 ++++++++++++------- src/blocks/blocks/content-generator/index.js | 10 ++ src/blocks/components/prompt/index.tsx | 10 +- src/blocks/plugins/ai-content/editor.scss | 5 +- src/blocks/plugins/ai-content/index.tsx | 144 +++++++----------- 5 files changed, 174 insertions(+), 145 deletions(-) diff --git a/src/blocks/blocks/content-generator/edit.js b/src/blocks/blocks/content-generator/edit.js index 6bfc4d5ca..10e4db606 100644 --- a/src/blocks/blocks/content-generator/edit.js +++ b/src/blocks/blocks/content-generator/edit.js @@ -23,7 +23,7 @@ import { useState } from '@wordpress/element'; -import { createBlock, createBlocksFromInnerBlocksTemplate } from '@wordpress/blocks'; +import { createBlock, rawHandler } from '@wordpress/blocks'; /** * Internal dependencies @@ -68,7 +68,8 @@ const ContentGenerator = ({ selectBlock, moveBlockToPosition, insertBlocks, - replaceBlock + replaceBlock, + replaceBlocks } = useDispatch( 'core/block-editor' ); /** @@ -85,6 +86,14 @@ const ContentGenerator = ({ replaceInnerBlocks( clientId, [ form ]); } + + if ( 'textTransformation' === attributes.promptID ) { + const blocks = rawHandler({ + HTML: result + }); + + replaceInnerBlocks( clientId, blocks ); + } }; const { hasInnerBlocks, containerId, getBlocks, getBlock, getBlockOrder, getBlockRootClientId } = useSelect( @@ -106,21 +115,29 @@ const ContentGenerator = ({ [ clientId ] ); - const transformToContainerBlock = () => { - const container = getBlock( containerId ); + /** + * Replace the block with the blocks generated from the prompt response + */ + const selfReplaceWithContent = () => { + const blocks = getBlocks( clientId ); - replaceBlock( clientId, container ); + replaceBlocks( clientId, blocks ); }; - const insertNewBlockFromContainer = () => { - const containerBlock = getBlock( containerId ); - const copy = createBlock( - containerBlock.name, - containerBlock.attributes, - containerBlock.innerBlocks?.map( block => { - return createBlock( block.name, block.attributes, block.innerBlocks ); - }) - ); + /** + * Insert the blocks generated from the prompt response below the current block + */ + const insertContentIntoPage = () => { + const blocks = getBlocks( clientId ); + const copy = blocks.map( blockRoot => { + return createBlock( + blockRoot.name, + blockRoot.attributes, + blockRoot.innerBlocks?.map( block => { + return createBlock( block.name, block.attributes, block.innerBlocks ); + }) + ); + }); insertBlockBelow( clientId, copy ); }; @@ -144,21 +161,45 @@ const ContentGenerator = ({ const PRESETS = { form: { - title: __( 'AI Form generator (Beta)', 'otter-blocks' ), + title: __( 'AI Form generator', 'otter-blocks' ), placeholder: __( 'Start describing what form you need...', 'otter-blocks' ), actions: ( props ) => { return ( + + ); + } + }, + textTransformation: { + title: __( 'AI Content generator', 'otter-blocks' ), + placeholder: __( 'Start describing what content you need...', 'otter-blocks' ), + actions: ( props ) => { + return ( + + +