import "./editor.scss"; import "./focus-point"; import { __ } from "@wordpress/i18n"; import { useSelect, dispatch, select } from "@wordpress/data"; import { useEffect, useState } from "@wordpress/element"; import { createBlock } from "@wordpress/blocks"; import { useBlockProps, MediaPlaceholder, BlockControls, MediaReplaceFlow, InspectorControls, } from "@wordpress/block-editor"; import { ToolbarButton, Spinner, withNotices, Button, PanelBody, } from "@wordpress/components"; import { InnerBlocks } from "@wordpress/block-editor"; import { isBlobURL, revokeBlobURL } from "@wordpress/blob"; function Edit({ attributes, setAttributes, noticeOperations, noticeList, noticeUI, clientId, ...props }) { const { coverUrl, coverId, coverAlt, focusBullets } = attributes; const [blobUrl, setBlobUrl] = useState(); const currentBlockDatas = useSelect((select) => { return select("core/block-editor").getBlocksByClientId(clientId)[0]; }); let children = useSelect( (select) => select("core/block-editor").getBlocksByClientId(clientId)[0].innerBlocks ); function onUploadError(message) { noticeOperations.removeAllNotices(); // Remove all previous notices noticeOperations.createErrorNotice(message); } function removeCoverImg() { setAttributes({ coverUrl: undefined, coverId: undefined, coverAlt: "", }); } function updateImage(image) { if (!image || !image.url) { setAttributes({ coverUrl: undefined, coverId: undefined, coverAlt: "", }); return; } setAttributes({ coverUrl: image.url, coverId: image.id, coverAlt: image.alt, }); } function passCoverUrlToChildren() { if (children && coverUrl) { children.forEach(function (child) { dispatch("core/block-editor").updateBlockAttributes(child.clientId, { coverUrl: coverUrl, }); }); } } function passIndexToChildren() { if (children) { children.forEach((child, index) => { dispatch("core/block-editor").updateBlockAttributes(child.clientId, { focusIndex: index + 1, }); }); } } function updateFocusPointBullets() { if (children) { const focusBullets = children.map((child, index) => { return { title: child.attributes.focusTitle, x: child.attributes.focusPosition.x, y: child.attributes.focusPosition.y, }; }); setAttributes({ focusBullets }); } } function insertFocusPointBlock() { const index = children && children.length ? children.length : 0; const newBlock = createBlock("homegrade-content-blocks/focus-point", {}); dispatch("core/block-editor").insertBlocks(newBlock, index, clientId); } function handleBulletClick(index) { if (currentBlockDatas && currentBlockDatas.innerBlocks) { console.log(currentBlockDatas.innerBlocks[index].clientId); dispatch("core/block-editor").selectBlock( currentBlockDatas.innerBlocks[index].clientId ); } } const renderedFocusPointBullets = focusBullets.map((focusBullet, index) => { return (