mirror of
https://github.com/WenPai-org/wpmind.git
synced 2025-08-08 06:56:05 +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 { addFilter } from '@wordpress/hooks';
|
||||||
import { BlockControls } from '@wordpress/block-editor';
|
import { BlockControls } from '@wordpress/block-editor';
|
||||||
import { createHigherOrderComponent } from '@wordpress/compose';
|
import { createHigherOrderComponent } from '@wordpress/compose';
|
||||||
import { useSelect, useDispatch } from '@wordpress/data';
|
import { useDispatch } from '@wordpress/data';
|
||||||
import {
|
import {
|
||||||
ToolbarGroup,
|
ToolbarGroup,
|
||||||
DropdownMenu,
|
DropdownMenu,
|
||||||
|
@ -74,29 +74,16 @@ function isToolbarAllowed(data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function Toolbar() {
|
function Toolbar() {
|
||||||
const { selectedClientIds } = useSelect((select) => {
|
const { open, setInput, setContext, setInsertionPlace, requestAI } =
|
||||||
const { getSelectedBlockClientIds } = select('core/block-editor');
|
|
||||||
|
|
||||||
const ids = getSelectedBlockClientIds();
|
|
||||||
|
|
||||||
return {
|
|
||||||
selectedClientIds: ids,
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const { open, setInput, setContext, setReplaceBlocks, requestAI } =
|
|
||||||
useDispatch('mind/popup');
|
useDispatch('mind/popup');
|
||||||
|
|
||||||
function openModal(prompt) {
|
function openModal(prompt) {
|
||||||
open();
|
open();
|
||||||
setInput(prompt);
|
setInput(prompt);
|
||||||
|
|
||||||
if (selectedClientIds && selectedClientIds.length) {
|
|
||||||
setContext('selected-blocks');
|
setContext('selected-blocks');
|
||||||
setReplaceBlocks(selectedClientIds);
|
setInsertionPlace('selected-blocks');
|
||||||
requestAI();
|
requestAI();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ToolbarGroup>
|
<ToolbarGroup>
|
||||||
|
|
|
@ -35,7 +35,7 @@ const withMindAI = createHigherOrderComponent((OriginalComponent) => {
|
||||||
const { content } = attributes;
|
const { content } = attributes;
|
||||||
|
|
||||||
const previousContent = usePrevious(content);
|
const previousContent = usePrevious(content);
|
||||||
const { open } = useDispatch('mind/popup');
|
const { open, setInsertionPlace } = useDispatch('mind/popup');
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (
|
if (
|
||||||
|
@ -44,8 +44,9 @@ const withMindAI = createHigherOrderComponent((OriginalComponent) => {
|
||||||
content === ' '
|
content === ' '
|
||||||
) {
|
) {
|
||||||
open();
|
open();
|
||||||
|
setInsertionPlace('selected-blocks');
|
||||||
}
|
}
|
||||||
}, [name, previousContent, content, open]);
|
}, [name, previousContent, content, open, setInsertionPlace]);
|
||||||
|
|
||||||
return <OriginalComponent {...props} />;
|
return <OriginalComponent {...props} />;
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,7 +92,7 @@ export default function Popup(props) {
|
||||||
isOpen,
|
isOpen,
|
||||||
input,
|
input,
|
||||||
context,
|
context,
|
||||||
replaceBlocks,
|
insertionPlace,
|
||||||
screen,
|
screen,
|
||||||
loading,
|
loading,
|
||||||
response,
|
response,
|
||||||
|
@ -102,7 +102,7 @@ export default function Popup(props) {
|
||||||
isOpen: checkIsOpen,
|
isOpen: checkIsOpen,
|
||||||
getInput,
|
getInput,
|
||||||
getContext,
|
getContext,
|
||||||
getReplaceBlocks,
|
getInsertionPlace,
|
||||||
getScreen,
|
getScreen,
|
||||||
getLoading,
|
getLoading,
|
||||||
getResponse,
|
getResponse,
|
||||||
|
@ -113,7 +113,7 @@ export default function Popup(props) {
|
||||||
isOpen: checkIsOpen(),
|
isOpen: checkIsOpen(),
|
||||||
input: getInput(),
|
input: getInput(),
|
||||||
context: getContext(),
|
context: getContext(),
|
||||||
replaceBlocks: getReplaceBlocks(),
|
insertionPlace: getInsertionPlace(),
|
||||||
screen: getScreen(),
|
screen: getScreen(),
|
||||||
loading: getLoading(),
|
loading: getLoading(),
|
||||||
response: getResponse(),
|
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;
|
let contextLabel = context;
|
||||||
|
|
||||||
switch (context) {
|
switch (context) {
|
||||||
|
@ -133,8 +143,7 @@ export default function Popup(props) {
|
||||||
// no default
|
// no default
|
||||||
}
|
}
|
||||||
|
|
||||||
const { insertBlocks: wpInsertBlocks, replaceBlocks: wpReplaceBlocks } =
|
const { insertBlocks, replaceBlocks } = useDispatch('core/block-editor');
|
||||||
useDispatch('core/block-editor');
|
|
||||||
|
|
||||||
function focusInput() {
|
function focusInput() {
|
||||||
if (ref?.current) {
|
if (ref?.current) {
|
||||||
|
@ -156,10 +165,10 @@ export default function Popup(props) {
|
||||||
const parsedBlocks = rawHandler({ HTML: response });
|
const parsedBlocks = rawHandler({ HTML: response });
|
||||||
|
|
||||||
if (parsedBlocks.length) {
|
if (parsedBlocks.length) {
|
||||||
if (replaceBlocks && replaceBlocks.length) {
|
if (insertionPlace === 'selected-blocks') {
|
||||||
wpReplaceBlocks(replaceBlocks, parsedBlocks);
|
replaceBlocks(selectedClientIds, parsedBlocks);
|
||||||
} else {
|
} else {
|
||||||
wpInsertBlocks(parsedBlocks);
|
insertBlocks(parsedBlocks);
|
||||||
}
|
}
|
||||||
|
|
||||||
setHighlightBlocks(
|
setHighlightBlocks(
|
||||||
|
|
|
@ -41,10 +41,10 @@ export function setContext(context) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setReplaceBlocks(replaceBlocks) {
|
export function setInsertionPlace(insertionPlace) {
|
||||||
return {
|
return {
|
||||||
type: 'SET_REPLACE_BLOCKS',
|
type: 'SET_INSERTION_PLACE',
|
||||||
replaceBlocks,
|
insertionPlace,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,8 @@ function reducer(
|
||||||
state = {
|
state = {
|
||||||
isOpen: false,
|
isOpen: false,
|
||||||
input: '',
|
input: '',
|
||||||
replaceBlocks: [],
|
|
||||||
context: '',
|
context: '',
|
||||||
|
insertionPlace: '',
|
||||||
screen: '',
|
screen: '',
|
||||||
loading: false,
|
loading: false,
|
||||||
response: false,
|
response: false,
|
||||||
|
@ -51,11 +51,11 @@ function reducer(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'SET_REPLACE_BLOCKS':
|
case 'SET_INSERTION_PLACE':
|
||||||
if (state.replaceBlocks !== action.replaceBlocks) {
|
if (state.insertionPlace !== action.insertionPlace) {
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
replaceBlocks: action.replaceBlocks,
|
insertionPlace: action.insertionPlace,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -115,8 +115,8 @@ function reducer(
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
input: '',
|
input: '',
|
||||||
replaceBlocks: [],
|
|
||||||
context: '',
|
context: '',
|
||||||
|
insertionPlace: '',
|
||||||
screen: '',
|
screen: '',
|
||||||
response: false,
|
response: false,
|
||||||
error: false,
|
error: false,
|
||||||
|
|
|
@ -10,8 +10,8 @@ export function getContext(state) {
|
||||||
return state?.context || '';
|
return state?.context || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getReplaceBlocks(state) {
|
export function getInsertionPlace(state) {
|
||||||
return state?.replaceBlocks || [];
|
return state?.insertionPlace || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getScreen(state) {
|
export function getScreen(state) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue