add central images
This commit is contained in:
parent
d5b97d6efa
commit
8725e3fe72
|
@ -18,13 +18,19 @@
|
|||
"count": {
|
||||
"type": "number"
|
||||
},
|
||||
"imgId": {
|
||||
"type": "number",
|
||||
"default": 0
|
||||
"imgUrl": {
|
||||
"type": "string",
|
||||
"source": "attribute",
|
||||
"selector": ".img-primary",
|
||||
"attribute": "src",
|
||||
"default": ""
|
||||
},
|
||||
"imgIdHover": {
|
||||
"type": "number",
|
||||
"default": 0
|
||||
"imgUrlHover": {
|
||||
"type": "string",
|
||||
"source": "attribute",
|
||||
"selector": ".img-secondary",
|
||||
"attribute": "src",
|
||||
"default": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
94
src/edit.js
94
src/edit.js
|
@ -1,5 +1,5 @@
|
|||
import { useSelect, dispatch } from "@wordpress/data";
|
||||
import { useState, createElement } from "@wordpress/element";
|
||||
import { useState } from "@wordpress/element";
|
||||
import { __ } from "@wordpress/i18n";
|
||||
import {
|
||||
useBlockProps,
|
||||
|
@ -7,7 +7,13 @@ import {
|
|||
InspectorControls,
|
||||
MediaPlaceholder,
|
||||
} 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 "./editor.scss";
|
||||
|
@ -19,18 +25,6 @@ export default function Edit({ attributes, setAttributes, 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))) {
|
||||
setAttributes({ count: innerBlocks.length });
|
||||
}
|
||||
|
@ -38,7 +32,7 @@ export default function Edit({ attributes, setAttributes, clientId }) {
|
|||
const [removedBlocks, setRemovedBlocks] = useState([]);
|
||||
const [isAnimating, setIsAnimating] = useState(false);
|
||||
|
||||
const animatingClass = ' clockwise pulse';
|
||||
const animatingClass = " clockwise pulse";
|
||||
|
||||
const addBlock = () => {
|
||||
const index = innerBlocks.length - 1;
|
||||
|
@ -107,10 +101,9 @@ export default function Edit({ attributes, setAttributes, clientId }) {
|
|||
<div id="red-orbital-controls">
|
||||
<Panel>
|
||||
<PanelBody title="Settings" initialOpen={true}>
|
||||
|
||||
<PanelRow>
|
||||
<legend className="blocks-base-control__label">
|
||||
{ __( 'Orbital count', 'red-orbital' ) }
|
||||
{__("Orbital count", "red-orbital")}
|
||||
</legend>
|
||||
<NumberControl
|
||||
value={innerBlocks.length}
|
||||
|
@ -122,38 +115,41 @@ export default function Edit({ attributes, setAttributes, clientId }) {
|
|||
|
||||
<PanelRow>
|
||||
<MediaPlaceholder
|
||||
onSelect = {
|
||||
( img ) => {
|
||||
setAttributes( { imgId: img.id } );
|
||||
}
|
||||
}
|
||||
allowedTypes = { [ 'image' ] }
|
||||
onSelect={(img) => {
|
||||
console.log("onSelect", img);
|
||||
setAttributes({ imgUrl: img.url });
|
||||
}}
|
||||
allowedTypes={["image"]}
|
||||
multiple={false}
|
||||
labels = { { title: 'Central Image' } }
|
||||
value = {attributes.imgId}
|
||||
mediaPreview = {imgThumbUrl ? (
|
||||
<img src={imgThumbUrl} alt="" />
|
||||
) : <p>No image selected.</p>}
|
||||
labels={{ title: "Central Image" }}
|
||||
mediaPreview={
|
||||
attributes.imgUrl !== "" ? (
|
||||
<img src={attributes.imgUrl} alt="" />
|
||||
) : (
|
||||
<p>No image selected.</p>
|
||||
)
|
||||
}
|
||||
/>
|
||||
</PanelRow>
|
||||
|
||||
<PanelRow>
|
||||
<MediaPlaceholder
|
||||
onSelect = {
|
||||
( img ) => {
|
||||
setAttributes( { imgIdHover: img.id } );
|
||||
}
|
||||
}
|
||||
allowedTypes = { [ 'image' ] }
|
||||
onSelect={(img) => {
|
||||
console.log("onSelect", img);
|
||||
setAttributes({ imgUrlHover: img.url });
|
||||
}}
|
||||
allowedTypes={["image"]}
|
||||
multiple={false}
|
||||
labels = { { title: 'Central Image On Hover' } }
|
||||
value = {attributes.imgIdHover}
|
||||
mediaPreview = {imgThumbHoverUrl ? (
|
||||
<img src={imgThumbHoverUrl} alt="" />
|
||||
) : <p>No image selected.</p>}
|
||||
labels={{ title: "Central Image On Hover" }}
|
||||
mediaPreview={
|
||||
attributes.imgUrlHover !== "" ? (
|
||||
<img src={attributes.imgUrlHover} alt="" />
|
||||
) : (
|
||||
<p>No image selected.</p>
|
||||
)
|
||||
}
|
||||
/>
|
||||
</PanelRow>
|
||||
|
||||
</PanelBody>
|
||||
</Panel>
|
||||
<Panel>
|
||||
|
@ -174,18 +170,18 @@ export default function Edit({ attributes, setAttributes, clientId }) {
|
|||
|
||||
<div {...useBlockProps()}>
|
||||
<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="image-hover">
|
||||
<img
|
||||
className="img"
|
||||
src="https://via.placeholder.com/256x256/999/333"
|
||||
/>
|
||||
<img
|
||||
className="img"
|
||||
src="https://via.placeholder.com/256x256/666/ccc"
|
||||
/>
|
||||
<img className="img" src={attributes.imgUrl} />
|
||||
<img className="img" src={attributes.imgUrlHover} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
10
src/save.js
10
src/save.js
|
@ -31,14 +31,8 @@ export default function save({ attributes }) {
|
|||
|
||||
<div className="inner-content">
|
||||
<div className="image-hover">
|
||||
<img
|
||||
className="img"
|
||||
src="https://via.placeholder.com/256x256/999/333"
|
||||
/>
|
||||
<img
|
||||
className="img"
|
||||
src="https://via.placeholder.com/256x256/666/ccc"
|
||||
/>
|
||||
<img className="img img-primary" src={attributes.imgUrl} />
|
||||
<img className="img img-secondary" src={attributes.imgUrlHover} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue