From 896d629ae30580f4f1f23b32cc5cedda7dee83cf Mon Sep 17 00:00:00 2001 From: Nikita Date: Wed, 2 Aug 2023 17:25:48 +0300 Subject: [PATCH] changed `replaceBlocks` store to `insertionPlace` --- src/extensions/block-toolbar/index.js | 23 +++++------------------ src/extensions/paragraph/index.js | 5 +++-- src/popup/index.js | 25 +++++++++++++++++-------- src/store/popup/actions.js | 6 +++--- src/store/popup/reducer.js | 10 +++++----- src/store/popup/selectors.js | 4 ++-- 6 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/extensions/block-toolbar/index.js b/src/extensions/block-toolbar/index.js index 79b900a..e7d0119 100644 --- a/src/extensions/block-toolbar/index.js +++ b/src/extensions/block-toolbar/index.js @@ -11,7 +11,7 @@ import { __, sprintf } from '@wordpress/i18n'; import { addFilter } from '@wordpress/hooks'; import { BlockControls } from '@wordpress/block-editor'; import { createHigherOrderComponent } from '@wordpress/compose'; -import { useSelect, useDispatch } from '@wordpress/data'; +import { useDispatch } from '@wordpress/data'; import { ToolbarGroup, DropdownMenu, @@ -74,28 +74,15 @@ function isToolbarAllowed(data) { } function Toolbar() { - const { selectedClientIds } = useSelect((select) => { - const { getSelectedBlockClientIds } = select('core/block-editor'); - - const ids = getSelectedBlockClientIds(); - - return { - selectedClientIds: ids, - }; - }, []); - - const { open, setInput, setContext, setReplaceBlocks, requestAI } = + const { open, setInput, setContext, setInsertionPlace, requestAI } = useDispatch('mind/popup'); function openModal(prompt) { open(); setInput(prompt); - - if (selectedClientIds && selectedClientIds.length) { - setContext('selected-blocks'); - setReplaceBlocks(selectedClientIds); - requestAI(); - } + setContext('selected-blocks'); + setInsertionPlace('selected-blocks'); + requestAI(); } return ( diff --git a/src/extensions/paragraph/index.js b/src/extensions/paragraph/index.js index fddb460..0a343b9 100644 --- a/src/extensions/paragraph/index.js +++ b/src/extensions/paragraph/index.js @@ -35,7 +35,7 @@ const withMindAI = createHigherOrderComponent((OriginalComponent) => { const { content } = attributes; const previousContent = usePrevious(content); - const { open } = useDispatch('mind/popup'); + const { open, setInsertionPlace } = useDispatch('mind/popup'); useEffect(() => { if ( @@ -44,8 +44,9 @@ const withMindAI = createHigherOrderComponent((OriginalComponent) => { content === ' ' ) { open(); + setInsertionPlace('selected-blocks'); } - }, [name, previousContent, content, open]); + }, [name, previousContent, content, open, setInsertionPlace]); return ; } diff --git a/src/popup/index.js b/src/popup/index.js index b0c336f..710ac6c 100644 --- a/src/popup/index.js +++ b/src/popup/index.js @@ -92,7 +92,7 @@ export default function Popup(props) { isOpen, input, context, - replaceBlocks, + insertionPlace, screen, loading, response, @@ -102,7 +102,7 @@ export default function Popup(props) { isOpen: checkIsOpen, getInput, getContext, - getReplaceBlocks, + getInsertionPlace, getScreen, getLoading, getResponse, @@ -113,7 +113,7 @@ export default function Popup(props) { isOpen: checkIsOpen(), input: getInput(), context: getContext(), - replaceBlocks: getReplaceBlocks(), + insertionPlace: getInsertionPlace(), screen: getScreen(), loading: getLoading(), response: getResponse(), @@ -121,6 +121,16 @@ export default function Popup(props) { }; }); + const { selectedClientIds } = useSelect((select) => { + const { getSelectedBlockClientIds } = select('core/block-editor'); + + const ids = getSelectedBlockClientIds(); + + return { + selectedClientIds: ids, + }; + }, []); + let contextLabel = context; switch (context) { @@ -133,8 +143,7 @@ export default function Popup(props) { // no default } - const { insertBlocks: wpInsertBlocks, replaceBlocks: wpReplaceBlocks } = - useDispatch('core/block-editor'); + const { insertBlocks, replaceBlocks } = useDispatch('core/block-editor'); function focusInput() { if (ref?.current) { @@ -156,10 +165,10 @@ export default function Popup(props) { const parsedBlocks = rawHandler({ HTML: response }); if (parsedBlocks.length) { - if (replaceBlocks && replaceBlocks.length) { - wpReplaceBlocks(replaceBlocks, parsedBlocks); + if (insertionPlace === 'selected-blocks') { + replaceBlocks(selectedClientIds, parsedBlocks); } else { - wpInsertBlocks(parsedBlocks); + insertBlocks(parsedBlocks); } setHighlightBlocks( diff --git a/src/store/popup/actions.js b/src/store/popup/actions.js index 1085509..262cbb6 100644 --- a/src/store/popup/actions.js +++ b/src/store/popup/actions.js @@ -41,10 +41,10 @@ export function setContext(context) { }; } -export function setReplaceBlocks(replaceBlocks) { +export function setInsertionPlace(insertionPlace) { return { - type: 'SET_REPLACE_BLOCKS', - replaceBlocks, + type: 'SET_INSERTION_PLACE', + insertionPlace, }; } diff --git a/src/store/popup/reducer.js b/src/store/popup/reducer.js index 0457969..d3215bf 100644 --- a/src/store/popup/reducer.js +++ b/src/store/popup/reducer.js @@ -4,8 +4,8 @@ function reducer( state = { isOpen: false, input: '', - replaceBlocks: [], context: '', + insertionPlace: '', screen: '', loading: false, response: false, @@ -51,11 +51,11 @@ function reducer( }; } break; - case 'SET_REPLACE_BLOCKS': - if (state.replaceBlocks !== action.replaceBlocks) { + case 'SET_INSERTION_PLACE': + if (state.insertionPlace !== action.insertionPlace) { return { ...state, - replaceBlocks: action.replaceBlocks, + insertionPlace: action.insertionPlace, }; } break; @@ -115,8 +115,8 @@ function reducer( return { ...state, input: '', - replaceBlocks: [], context: '', + insertionPlace: '', screen: '', response: false, error: false, diff --git a/src/store/popup/selectors.js b/src/store/popup/selectors.js index 1cd075f..934afee 100644 --- a/src/store/popup/selectors.js +++ b/src/store/popup/selectors.js @@ -10,8 +10,8 @@ export function getContext(state) { return state?.context || ''; } -export function getReplaceBlocks(state) { - return state?.replaceBlocks || []; +export function getInsertionPlace(state) { + return state?.insertionPlace || ''; } export function getScreen(state) {