mirror of
https://github.com/WenPai-org/wpmind.git
synced 2025-08-07 14:35:18 +08:00
changed replaceBlocks
store to insertionPlace
This commit is contained in:
parent
b08b333ab4
commit
896d629ae3
6 changed files with 35 additions and 38 deletions
|
@ -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 (
|
||||
|
|
|
@ -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 <OriginalComponent {...props} />;
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue