add central images

This commit is contained in:
Ray Elliott 2022-07-25 13:46:41 +01:00
parent d5b97d6efa
commit 8725e3fe72
3 changed files with 80 additions and 84 deletions

View File

@ -18,13 +18,19 @@
"count": { "count": {
"type": "number" "type": "number"
}, },
"imgId": { "imgUrl": {
"type": "number", "type": "string",
"default": 0 "source": "attribute",
"selector": ".img-primary",
"attribute": "src",
"default": ""
}, },
"imgIdHover": { "imgUrlHover": {
"type": "number", "type": "string",
"default": 0 "source": "attribute",
"selector": ".img-secondary",
"attribute": "src",
"default": ""
} }
} }
} }

View File

@ -1,5 +1,5 @@
import { useSelect, dispatch } from "@wordpress/data"; import { useSelect, dispatch } from "@wordpress/data";
import { useState, createElement } from "@wordpress/element"; import { useState } from "@wordpress/element";
import { __ } from "@wordpress/i18n"; import { __ } from "@wordpress/i18n";
import { import {
useBlockProps, useBlockProps,
@ -7,7 +7,13 @@ import {
InspectorControls, InspectorControls,
MediaPlaceholder, MediaPlaceholder,
} from "@wordpress/block-editor"; } from "@wordpress/block-editor";
import { __experimentalNumberControl as NumberControl, Button, ToggleControl, Panel, PanelBody, PanelRow } from "@wordpress/components"; import {
__experimentalNumberControl as NumberControl,
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";
@ -19,18 +25,6 @@ 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 });
} }
@ -38,7 +32,7 @@ export default function Edit({ attributes, setAttributes, clientId }) {
const [removedBlocks, setRemovedBlocks] = useState([]); const [removedBlocks, setRemovedBlocks] = useState([]);
const [isAnimating, setIsAnimating] = useState(false); const [isAnimating, setIsAnimating] = useState(false);
const animatingClass = ' clockwise pulse'; const animatingClass = " clockwise pulse";
const addBlock = () => { const addBlock = () => {
const index = innerBlocks.length - 1; const index = innerBlocks.length - 1;
@ -107,10 +101,9 @@ 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")}
</legend> </legend>
<NumberControl <NumberControl
value={innerBlocks.length} value={innerBlocks.length}
@ -122,38 +115,41 @@ export default function Edit({ attributes, setAttributes, clientId }) {
<PanelRow> <PanelRow>
<MediaPlaceholder <MediaPlaceholder
onSelect = { onSelect={(img) => {
( img ) => { console.log("onSelect", img);
setAttributes( { imgId: img.id } ); setAttributes({ imgUrl: img.url });
} }}
} allowedTypes={["image"]}
allowedTypes = { [ 'image' ] }
multiple={false} multiple={false}
labels = { { title: 'Central Image' } } labels={{ title: "Central Image" }}
value = {attributes.imgId} mediaPreview={
mediaPreview = {imgThumbUrl ? ( attributes.imgUrl !== "" ? (
<img src={imgThumbUrl} alt="" /> <img src={attributes.imgUrl} alt="" />
) : <p>No image selected.</p>} ) : (
<p>No image selected.</p>
)
}
/> />
</PanelRow> </PanelRow>
<PanelRow> <PanelRow>
<MediaPlaceholder <MediaPlaceholder
onSelect = { onSelect={(img) => {
( img ) => { console.log("onSelect", img);
setAttributes( { imgIdHover: img.id } ); setAttributes({ imgUrlHover: img.url });
} }}
} allowedTypes={["image"]}
allowedTypes = { [ 'image' ] }
multiple={false} multiple={false}
labels = { { title: 'Central Image On Hover' } } labels={{ title: "Central Image On Hover" }}
value = {attributes.imgIdHover} mediaPreview={
mediaPreview = {imgThumbHoverUrl ? ( attributes.imgUrlHover !== "" ? (
<img src={imgThumbHoverUrl} alt="" /> <img src={attributes.imgUrlHover} alt="" />
) : <p>No image selected.</p>} ) : (
<p>No image selected.</p>
)
}
/> />
</PanelRow> </PanelRow>
</PanelBody> </PanelBody>
</Panel> </Panel>
<Panel> <Panel>
@ -174,18 +170,18 @@ export default function Edit({ attributes, setAttributes, clientId }) {
<div {...useBlockProps()}> <div {...useBlockProps()}>
<div className="orbital-view"> <div className="orbital-view">
<ul className={`orbiters count-${innerBlocks.length}${isAnimating ? animatingClass : ''}`}>{viewList}</ul> <ul
className={`orbiters count-${innerBlocks.length}${
isAnimating ? animatingClass : ""
}`}
>
{viewList}
</ul>
<div className="inner-content"> <div className="inner-content">
<div className="image-hover"> <div className="image-hover">
<img <img className="img" src={attributes.imgUrl} />
className="img" <img className="img" src={attributes.imgUrlHover} />
src="https://via.placeholder.com/256x256/999/333"
/>
<img
className="img"
src="https://via.placeholder.com/256x256/666/ccc"
/>
</div> </div>
</div> </div>
</div> </div>

View File

@ -31,14 +31,8 @@ export default function save({ attributes }) {
<div className="inner-content"> <div className="inner-content">
<div className="image-hover"> <div className="image-hover">
<img <img className="img img-primary" src={attributes.imgUrl} />
className="img" <img className="img img-secondary" src={attributes.imgUrlHover} />
src="https://via.placeholder.com/256x256/999/333"
/>
<img
className="img"
src="https://via.placeholder.com/256x256/666/ccc"
/>
</div> </div>
</div> </div>
</div> </div>