diff --git a/includes/blocks/lcp-viewport/build/index.asset.php b/includes/blocks/lcp-viewport/build/index.asset.php index 2f92f05..759a053 100644 --- a/includes/blocks/lcp-viewport/build/index.asset.php +++ b/includes/blocks/lcp-viewport/build/index.asset.php @@ -1 +1 @@ - array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => 'b8ff5a7ac1d726017948'); + array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => '432795d467d88e7a501f'); diff --git a/includes/blocks/lcp-viewport/build/index.js b/includes/blocks/lcp-viewport/build/index.js index 78bde25..f1da27f 100644 --- a/includes/blocks/lcp-viewport/build/index.js +++ b/includes/blocks/lcp-viewport/build/index.js @@ -1 +1 @@ -(()=>{"use strict";var e,t={798:()=>{const e=window.wp.blocks,t=window.wp.i18n,o=window.wp.blockEditor,n=(window.wp.hooks,window.wp.components),c=window.wp.element,r=window.wp.data,i=window.ReactJSXRuntime,s=JSON.parse('{"UU":"lcp/viewport"}');(0,e.registerBlockType)(s.UU,{edit:function({attributes:s,setAttributes:l,clientId:a}){const{hasSidecontent:d,headerContainerStickyType:p}=s,[u,w]=(0,c.useState)(!1),[h,k]=(0,c.useState)(!1),[v,f]=(0,c.useState)(!1),[b,B]=(0,c.useState)(null),m=(0,o.useBlockProps)({className:d?"has-sidecontent":""}),x=[d?["lcp/sidecontent"]:null,["lcp/header-container"],["lcp/main-area"],["lcp/footer-container"]].filter(Boolean);(0,c.useEffect)((()=>{if(d){const t=(0,e.createBlock)("lcp/sidecontent",{});wp.data.dispatch("core/block-editor").insertBlocks(t,1,a)}}),[d,a]),(0,c.useEffect)((()=>{const e=(0,r.subscribe)((()=>{const e=wp.data.select("core/block-editor").getBlocks();if(h||v)return;const t=e.find((e=>e.clientId!==a&&!wp.data.select("core/block-editor").getBlockParents(e.clientId).includes(a)));t&&(B(t),w(!0),f(!0))}));return()=>{e()}}),[a,h,v]);const y=()=>{if(b){const e=wp.data.select("core/block-editor").getBlocks(a).find((e=>"lcp/main-area"===e.name));e&&(console.log(e),wp.data.dispatch("core/block-editor").insertBlocks(b,e.clientId,0))}w(!1)};(0,c.useEffect)((()=>{v&&k(!1)}),[v]);const j=(0,r.useSelect)((e=>e("core/block-editor").getBlocks(a)),[a]);return(0,c.useEffect)((()=>{const e=j.find((e=>"lcp/header-container"===e.name));j.find((e=>"lcp/header-sidecontent"===e.name)),e&&e.attributes&&void 0!==e.attributes.sticky&&l({headerContainerStickyType:e.attributes.sticky})}),[j,a,l]),(0,i.jsxs)("div",{...m,children:[(0,i.jsx)(o.InspectorControls,{children:(0,i.jsx)(n.ToggleControl,{label:(0,t.__)("Include Side Content","lcp-viewport"),checked:d,onChange:e=>l({hasSidecontent:e})})}),(0,i.jsx)("div",{id:"lcp-viewport-outer",children:(0,i.jsx)("div",{id:"lcp-viewport-inner",children:(0,i.jsx)(o.InnerBlocks,{renderAppender:()=>(0,i.jsx)(o.InnerBlocks.ButtonBlockAppender,{}),template:x})})}),u&&!h&&(0,i.jsxs)(n.Modal,{title:(0,t.__)("Block Outside Viewport","lcp-viewport"),onRequestClose:y,className:"block-outside-popup",children:[(0,i.jsx)("p",{children:(0,t.__)("You added a block outside of the lcp/viewport container. Please make sure the block is inside the appropriate area.","lcp-viewport")}),(0,i.jsx)(n.Button,{isPrimary:!0,onClick:y,children:(0,t.__)("Close","lcp-viewport")}),(0,i.jsx)(n.Button,{isSecondary:!0,onClick:()=>{k(!0),w(!1)},children:(0,t.__)("Ignore","lcp-viewport")})]})]})},save:function({attributes:e}){const{hasSidecontent:t,headerContainerStickyType:n}=e;o.useBlockProps.save();let c="";return t&&(c+="lcp-has-sidecontent "),"always"===n?c+="lcp-has-sticky-header ":"onScroll"===n&&(c+="lcp-has-sticky-on-scroll-header "),(0,i.jsx)("div",{className:c,id:"lcp-viewport-outer",children:(0,i.jsx)("div",{id:"lcp-viewport-inner",className:c,children:(0,i.jsx)(o.InnerBlocks.Content,{})})})}})}},o={};function n(e){var c=o[e];if(void 0!==c)return c.exports;var r=o[e]={exports:{}};return t[e](r,r.exports,n),r.exports}n.m=t,e=[],n.O=(t,o,c,r)=>{if(!o){var i=1/0;for(d=0;d=r)&&Object.keys(n.O).every((e=>n.O[e](o[l])))?o.splice(l--,1):(s=!1,r0&&e[d-1][2]>r;d--)e[d]=e[d-1];e[d]=[o,c,r]},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var e={57:0,350:0};n.O.j=t=>0===e[t];var t=(t,o)=>{var c,r,i=o[0],s=o[1],l=o[2],a=0;if(i.some((t=>0!==e[t]))){for(c in s)n.o(s,c)&&(n.m[c]=s[c]);if(l)var d=l(n)}for(t&&t(o);an(798)));c=n.O(c)})(); \ No newline at end of file +(()=>{"use strict";var e,t={798:()=>{const e=window.wp.blocks,t=window.wp.i18n,o=window.wp.blockEditor,n=(window.wp.hooks,window.wp.components),r=window.wp.element,i=window.wp.data,c=window.ReactJSXRuntime,s=JSON.parse('{"UU":"lcp/viewport"}');(0,e.registerBlockType)(s.UU,{edit:function({attributes:s,setAttributes:l,clientId:a}){const{hasSidecontent:p,headerContainerStickyType:d}=s,[u,h]=(0,r.useState)(!1),[w,v]=(0,r.useState)(!1),[k,f]=(0,r.useState)(!1),[b,x]=(0,r.useState)(null),y=(0,o.useBlockProps)({className:p?"has-sidecontent":""}),j=[p?["lcp/sidecontent"]:null,["lcp/header-container"],["lcp/main-area"],["lcp/footer-container"]].filter(Boolean);(0,r.useEffect)((()=>{if(p){const t=(0,e.createBlock)("lcp/sidecontent",{});wp.data.dispatch("core/block-editor").insertBlocks(t,1,a)}}),[p,a]);const m=(0,i.useSelect)((e=>e("core/block-editor").getBlocks(a)),[a]);return(0,r.useEffect)((()=>{const e=m.find((e=>"lcp/header-container"===e.name));m.find((e=>"lcp/header-sidecontent"===e.name)),e&&e.attributes&&void 0!==e.attributes.sticky&&l({headerContainerStickyType:e.attributes.sticky})}),[m,a,l]),(0,c.jsxs)("div",{...y,children:[(0,c.jsx)(o.InspectorControls,{children:(0,c.jsx)(n.ToggleControl,{label:(0,t.__)("Include Side Content","lcp-viewport"),checked:p,onChange:e=>l({hasSidecontent:e})})}),(0,c.jsx)("div",{id:"lcp-viewport-outer",children:(0,c.jsx)("div",{id:"lcp-viewport-inner",children:(0,c.jsx)(o.InnerBlocks,{renderAppender:()=>(0,c.jsx)(o.InnerBlocks.ButtonBlockAppender,{}),template:j})})}),u&&!w&&(0,c.jsxs)(n.Modal,{title:(0,t.__)("Block Outside Viewport","lcp-viewport"),onRequestClose:closePopup,className:"block-outside-popup",children:[(0,c.jsx)("p",{children:(0,t.__)("You added a block outside of the lcp/viewport container. Please make sure the block is inside the appropriate area.","lcp-viewport")}),(0,c.jsx)(n.Button,{isPrimary:!0,onClick:closePopup,children:(0,t.__)("Close","lcp-viewport")}),(0,c.jsx)(n.Button,{isSecondary:!0,onClick:ignorePopup,children:(0,t.__)("Ignore","lcp-viewport")})]})]})},save:function({attributes:e}){const{hasSidecontent:t,headerContainerStickyType:n}=e;o.useBlockProps.save();let r="";return t&&(r+="lcp-has-sidecontent "),"always"===n?r+="lcp-has-sticky-header ":"onScroll"===n&&(r+="lcp-has-sticky-on-scroll-header "),(0,c.jsx)("div",{className:r,id:"lcp-viewport-outer",children:(0,c.jsx)("div",{id:"lcp-viewport-inner",className:r,children:(0,c.jsx)(o.InnerBlocks.Content,{})})})}})}},o={};function n(e){var r=o[e];if(void 0!==r)return r.exports;var i=o[e]={exports:{}};return t[e](i,i.exports,n),i.exports}n.m=t,e=[],n.O=(t,o,r,i)=>{if(!o){var c=1/0;for(p=0;p=i)&&Object.keys(n.O).every((e=>n.O[e](o[l])))?o.splice(l--,1):(s=!1,i0&&e[p-1][2]>i;p--)e[p]=e[p-1];e[p]=[o,r,i]},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var e={57:0,350:0};n.O.j=t=>0===e[t];var t=(t,o)=>{var r,i,c=o[0],s=o[1],l=o[2],a=0;if(c.some((t=>0!==e[t]))){for(r in s)n.o(s,r)&&(n.m[r]=s[r]);if(l)var p=l(n)}for(t&&t(o);an(798)));r=n.O(r)})(); \ No newline at end of file diff --git a/includes/blocks/lcp-viewport/src/edit.js b/includes/blocks/lcp-viewport/src/edit.js index c8a5db2..986283d 100644 --- a/includes/blocks/lcp-viewport/src/edit.js +++ b/includes/blocks/lcp-viewport/src/edit.js @@ -44,80 +44,16 @@ export default function Edit({ attributes, setAttributes, clientId }) { } }, [hasSidecontent, clientId]); // Only runs when `hasSidecontent` or `clientId` changes - // Track block additions and show popup if block is outside the lcp/viewport - useEffect(() => { - const unsubscribe = subscribe(() => { - const blocks = wp.data.select('core/block-editor').getBlocks(); // Get all blocks - - // If the popup is ignored, don't check for outside blocks - if (hasIgnoredPopup || blockAdded) return; - - // Check if any block added is outside the lcp/viewport (excluding lcp/viewport itself) - const addedBlockOutside = blocks.find((block) => { - if (block.clientId === clientId) { - // Skip the current block (lcp/viewport) - return false; - } - const parent = wp.data.select('core/block-editor').getBlockParents(block.clientId); - // If the block is not inside lcp/viewport or its children, show popup - return !parent.includes(clientId); - }); - - if (addedBlockOutside) { - setAddedBlock(addedBlockOutside); // Store the added block - setIsPopupVisible(true); // Show the popup if block is outside - setBlockAdded(true); // Mark that a block has been added - } - }); - - // Cleanup the subscription on component unmount - return () => { - unsubscribe(); - }; - }, [clientId, hasIgnoredPopup, blockAdded]); // Reset check when block is added or state changes - - // Handle the popup close - const closePopup = () => { - // Check if an added block exists - if (addedBlock) { - // Retrieve the parent block (main area) - const mainAreaBlock = wp.data.select('core/block-editor').getBlocks(clientId) - .find(block => block.name === 'lcp/main-area'); // Find the 'lcp/main-area' block - - if (mainAreaBlock) { - console.log(mainAreaBlock) - // Insert the block into the 'lcp/main-area' - wp.data.dispatch('core/block-editor').insertBlocks( - addedBlock, // The block to insert - mainAreaBlock.clientId, // Specify the clientId of the main-area block - 0 // Insert at the start (index 0) of the main-area block's InnerBlocks - ); - } - } - // Close the popup after moving the block - setIsPopupVisible(false); - }; - - // Handle the "Ignore" button click to dismiss popup and not show again until next block addition - const ignorePopup = () => { - setHasIgnoredPopup(true); // Set the state to indicate the user has ignored the popup - setIsPopupVisible(false); // Close the popup - }; // Reset the "Ignore" state when a new block is added (for the next block addition) - useEffect(() => { - if (blockAdded) { - // Reset ignore state after a new block has been added - setHasIgnoredPopup(false); - } - }, [blockAdded]); // Trigger when blockAdded state changes + // Use useSelect to subscribe to the state of inner blocks const innerBlocks = useSelect((select) => { return select('core/block-editor').getBlocks(clientId); }, [clientId]); - // useEffect to log "Header present" when the lcp/header-container block is found + // useEffect to handle sticky header useEffect(() => { const headerBlock = innerBlocks.find((block) => block.name === 'lcp/header-container'); const sidecontentBlock = innerBlocks.find((block) => block.name === 'lcp/header-sidecontent');