select central images

This commit is contained in:
Ray Elliott 2022-07-23 15:43:54 +01:00
parent 3626a1083a
commit d5b97d6efa
2 changed files with 58 additions and 1 deletions

View File

@ -17,6 +17,14 @@
"attributes": { "attributes": {
"count": { "count": {
"type": "number" "type": "number"
},
"imgId": {
"type": "number",
"default": 0
},
"imgIdHover": {
"type": "number",
"default": 0
} }
} }
} }

View File

@ -5,8 +5,9 @@ import {
useBlockProps, useBlockProps,
InnerBlocks, InnerBlocks,
InspectorControls, InspectorControls,
MediaPlaceholder,
} from "@wordpress/block-editor"; } from "@wordpress/block-editor";
import { __experimentalNumberControl as NumberControl, ToggleControl, Panel, PanelBody, PanelRow } from "@wordpress/components"; import { __experimentalNumberControl as NumberControl, Button, ToggleControl, Panel, PanelBody, PanelRow } from "@wordpress/components";
import { getSaveElement, createBlock } from "@wordpress/blocks"; import { getSaveElement, createBlock } from "@wordpress/blocks";
import "./editor.scss"; import "./editor.scss";
@ -18,6 +19,18 @@ export default function Edit({ attributes, setAttributes, clientId }) {
select("core/block-editor").getBlock(clientId) select("core/block-editor").getBlock(clientId)
); );
const centralImg = useSelect((select) =>
select( "core" ).getEntityRecord( "postType", "attachment", attributes.imgId),
);
console.log(centralImg);
const imgThumbUrl = centralImg ? centralImg?.media_details?.sizes?.thumbnail?.source_url : '';
const centralImgHover = useSelect((select) =>
select( "core" ).getEntityRecord( "postType", "attachment", attributes.imgIdHover),
);
console.log(centralImgHover);
const imgThumbHoverUrl = centralImg ? centralImgHover?.media_details?.sizes?.thumbnail?.source_url : '';
if (isNaN(parseInt(attributes.count, 10))) { if (isNaN(parseInt(attributes.count, 10))) {
setAttributes({count: innerBlocks.length}); setAttributes({count: innerBlocks.length});
} }
@ -94,6 +107,7 @@ export default function Edit({ attributes, setAttributes, clientId }) {
<div id="red-orbital-controls"> <div id="red-orbital-controls">
<Panel> <Panel>
<PanelBody title="Settings" initialOpen={ true }> <PanelBody title="Settings" initialOpen={ true }>
<PanelRow> <PanelRow>
<legend className="blocks-base-control__label"> <legend className="blocks-base-control__label">
{ __( 'Orbital count', 'red-orbital' ) } { __( 'Orbital count', 'red-orbital' ) }
@ -105,6 +119,41 @@ export default function Edit({ attributes, setAttributes, clientId }) {
onChange={onCountChange} onChange={onCountChange}
/> />
</PanelRow> </PanelRow>
<PanelRow>
<MediaPlaceholder
onSelect = {
( img ) => {
setAttributes( { imgId: img.id } );
}
}
allowedTypes = { [ 'image' ] }
multiple = { false }
labels = { { title: 'Central Image' } }
value = {attributes.imgId}
mediaPreview = {imgThumbUrl ? (
<img src={imgThumbUrl} alt="" />
) : <p>No image selected.</p>}
/>
</PanelRow>
<PanelRow>
<MediaPlaceholder
onSelect = {
( img ) => {
setAttributes( { imgIdHover: img.id } );
}
}
allowedTypes = { [ 'image' ] }
multiple = { false }
labels = { { title: 'Central Image On Hover' } }
value = {attributes.imgIdHover}
mediaPreview = {imgThumbHoverUrl ? (
<img src={imgThumbHoverUrl} alt="" />
) : <p>No image selected.</p>}
/>
</PanelRow>
</PanelBody> </PanelBody>
</Panel> </Panel>
<Panel> <Panel>