diff --git a/src/block.json b/src/block.json index 00927a1..45021a8 100644 --- a/src/block.json +++ b/src/block.json @@ -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": "" } } } diff --git a/src/edit.js b/src/edit.js index c6ba9d4..62bba54 100644 --- a/src/edit.js +++ b/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,26 +25,14 @@ 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}); + setAttributes({ count: innerBlocks.length }); } const [removedBlocks, setRemovedBlocks] = useState([]); const [isAnimating, setIsAnimating] = useState(false); - const animatingClass = ' clockwise pulse'; + const animatingClass = " clockwise pulse"; const addBlock = () => { const index = innerBlocks.length - 1; @@ -52,7 +46,7 @@ export default function Edit({ attributes, setAttributes, clientId }) { } dispatch("core/block-editor").insertBlock(block, index, clientId, false); - setAttributes({count: parseInt(attributes.count, 10) + 1}); + setAttributes({ count: parseInt(attributes.count, 10) + 1 }); }; const removeBlock = () => { @@ -65,7 +59,7 @@ export default function Edit({ attributes, setAttributes, clientId }) { removedBlocksUpdate.push(block); setRemovedBlocks(removedBlocksUpdate); - setAttributes({count: parseInt(attributes.count, 10) - 1}); + setAttributes({ count: parseInt(attributes.count, 10) - 1 }); }; const onCountChange = (value) => { @@ -106,65 +100,67 @@ export default function Edit({ attributes, setAttributes, clientId }) {
- - + - - { __( 'Orbital count', 'red-orbital' ) } - - - - - - { - setAttributes( { imgId: img.id } ); - } - } - allowedTypes = { [ 'image' ] } - multiple = { false } - labels = { { title: 'Central Image' } } - value = {attributes.imgId} - mediaPreview = {imgThumbUrl ? ( - - ) :

No image selected.

} + + {__("Orbital count", "red-orbital")} + +
{ - setAttributes( { imgIdHover: img.id } ); - } + onSelect={(img) => { + console.log("onSelect", img); + setAttributes({ imgUrl: img.url }); + }} + allowedTypes={["image"]} + multiple={false} + labels={{ title: "Central Image" }} + mediaPreview={ + attributes.imgUrl !== "" ? ( + + ) : ( +

No image selected.

+ ) } - allowedTypes = { [ 'image' ] } - multiple = { false } - labels = { { title: 'Central Image On Hover' } } - value = {attributes.imgIdHover} - mediaPreview = {imgThumbHoverUrl ? ( - - ) :

No image selected.

} />
+ + { + console.log("onSelect", img); + setAttributes({ imgUrlHover: img.url }); + }} + allowedTypes={["image"]} + multiple={false} + labels={{ title: "Central Image On Hover" }} + mediaPreview={ + attributes.imgUrlHover !== "" ? ( + + ) : ( +

No image selected.

+ ) + } + /> +
- + { - setIsAnimating( ( state ) => ! state ); - } } + checked={isAnimating} + onChange={() => { + setIsAnimating((state) => !state); + }} /> @@ -174,18 +170,18 @@ export default function Edit({ attributes, setAttributes, clientId }) {
-
    {viewList}
+
    + {viewList} +
- - + +
diff --git a/src/save.js b/src/save.js index 2936b77..7dc1b11 100644 --- a/src/save.js +++ b/src/save.js @@ -31,14 +31,8 @@ export default function save({ attributes }) {
- - + +