Added theme archvier and changes to blocks
This commit is contained in:
@ -19,6 +19,10 @@
|
||||
"hasSidecontent": {
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
},
|
||||
"hasStickyHeader": {
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
}
|
||||
},
|
||||
"textdomain": "lcp-viewport",
|
||||
|
||||
@ -1 +1 @@
|
||||
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n'), 'version' => '6fbadff15d92199f61a3');
|
||||
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => 'a54c989d69efb5b39b31');
|
||||
|
||||
@ -1 +1 @@
|
||||
(()=>{"use strict";var e,t={771:()=>{const e=window.wp.blocks,t=window.wp.i18n,n=window.wp.blockEditor,o=window.wp.components,r=window.wp.element,c=window.ReactJSXRuntime,i=JSON.parse('{"UU":"lcp/viewport"}');(0,e.registerBlockType)(i.UU,{edit:function({attributes:i,setAttributes:s,clientId:l}){const{hasSidecontent:p}=i,d=(0,n.useBlockProps)({className:p?"has-sidecontent":""}),a=[p?["lcp/sidecontent"]:null,["lcp/header-container"],,["lcp/main-area"],["lcp/footer-container"]].filter(Boolean);return(0,r.useEffect)((()=>{if(p){const t=(0,e.createBlock)("lcp/sidecontent",{});wp.data.select("core/block-editor").getBlocks(l),wp.data.dispatch("core/block-editor").insertBlocks(t,1,l)}}),[p,l]),(0,c.jsxs)("div",{...d,children:[(0,c.jsx)(n.InspectorControls,{children:(0,c.jsx)(o.ToggleControl,{label:(0,t.__)("Include Side Content","lcp-viewport"),checked:p,onChange:e=>s({hasSidecontent:e})})}),(0,c.jsx)("div",{id:"lcp-viewport-outer",children:(0,c.jsx)("div",{id:"lcp-viewport-inner",children:(0,c.jsx)(n.InnerBlocks,{renderAppender:()=>(0,c.jsx)(n.InnerBlocks.ButtonBlockAppender,{}),template:a})})})]})},save:function({attributes:e}){const{hasSidecontent:t}=e,o=(n.useBlockProps.save(),t?"lcp-has-sidecontent":"");return(0,c.jsx)("div",{class:o,id:"lcp-viewport-outer",children:(0,c.jsx)("div",{id:"lcp-viewport-inner",class:o,children:(0,c.jsx)(n.InnerBlocks.Content,{})})})}})}},n={};function o(e){var r=n[e];if(void 0!==r)return r.exports;var c=n[e]={exports:{}};return t[e](c,c.exports,o),c.exports}o.m=t,e=[],o.O=(t,n,r,c)=>{if(!n){var i=1/0;for(d=0;d<e.length;d++){n=e[d][0],r=e[d][1],c=e[d][2];for(var s=!0,l=0;l<n.length;l++)(!1&c||i>=c)&&Object.keys(o.O).every((e=>o.O[e](n[l])))?n.splice(l--,1):(s=!1,c<i&&(i=c));if(s){e.splice(d--,1);var p=r();void 0!==p&&(t=p)}}return t}c=c||0;for(var d=e.length;d>0&&e[d-1][2]>c;d--)e[d]=e[d-1];e[d]=[n,r,c]},o.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var e={57:0,350:0};o.O.j=t=>0===e[t];var t=(t,n)=>{var r,c,i=n[0],s=n[1],l=n[2],p=0;if(i.some((t=>0!==e[t]))){for(r in s)o.o(s,r)&&(o.m[r]=s[r]);if(l)var d=l(o)}for(t&&t(n);p<i.length;p++)c=i[p],o.o(e,c)&&e[c]&&e[c][0](),e[c]=0;return o.O(d)},n=self.webpackChunklcp_viewport=self.webpackChunklcp_viewport||[];n.forEach(t.bind(null,0)),n.push=t.bind(null,n.push.bind(n))})();var r=o.O(void 0,[350],(()=>o(771)));r=o.O(r)})();
|
||||
(()=>{"use strict";var e,t={199:()=>{const e=window.wp.blocks,t=window.wp.i18n,o=window.wp.blockEditor,c=window.wp.components,n=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,hasStickyHeader:p}=s,[u,w]=(0,n.useState)(!1),[k,h]=(0,n.useState)(!1),[v,f]=(0,n.useState)(!1),[b,B]=(0,n.useState)(null),x=(0,o.useBlockProps)({className:d?"has-sidecontent":""}),j=[d?["lcp/sidecontent"]:null,["lcp/header-container"],["lcp/main-area"],["lcp/footer-container"]].filter(Boolean);(0,n.useEffect)((()=>{if(d){const t=(0,e.createBlock)("lcp/sidecontent",{});wp.data.dispatch("core/block-editor").insertBlocks(t,1,a)}}),[d,a]),(0,n.useEffect)((()=>{const e=(0,r.subscribe)((()=>{const e=wp.data.select("core/block-editor").getBlocks();if(k||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,k,v]);const m=()=>{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)};return(0,n.useEffect)((()=>{v&&h(!1)}),[v]),(0,r.useSelect)((e=>e("core/block-editor").getBlocks(x.clientId)),[x.clientId]),(0,i.jsxs)("div",{...x,children:[(0,i.jsx)(o.InspectorControls,{children:(0,i.jsx)(c.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:j})})}),u&&!k&&(0,i.jsxs)(c.Modal,{title:(0,t.__)("Block Outside Viewport","lcp-viewport"),onRequestClose:m,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)(c.Button,{isPrimary:!0,onClick:m,children:(0,t.__)("Close","lcp-viewport")}),(0,i.jsx)(c.Button,{isSecondary:!0,onClick:()=>{h(!0),w(!1)},children:(0,t.__)("Ignore","lcp-viewport")})]})]})},save:function({attributes:e}){const{hasSidecontent:t,hasStickyHeader:c}=e;o.useBlockProps.save();let n="";return t&&(n+="lcp-has-sidecontent "),c&&(n+="lcp-has-sticky-header"),(0,i.jsx)("div",{className:n,id:"lcp-viewport-outer",children:(0,i.jsx)("div",{id:"lcp-viewport-inner",className:n,children:(0,i.jsx)(o.InnerBlocks.Content,{})})})}})}},o={};function c(e){var n=o[e];if(void 0!==n)return n.exports;var r=o[e]={exports:{}};return t[e](r,r.exports,c),r.exports}c.m=t,e=[],c.O=(t,o,n,r)=>{if(!o){var i=1/0;for(d=0;d<e.length;d++){o=e[d][0],n=e[d][1],r=e[d][2];for(var s=!0,l=0;l<o.length;l++)(!1&r||i>=r)&&Object.keys(c.O).every((e=>c.O[e](o[l])))?o.splice(l--,1):(s=!1,r<i&&(i=r));if(s){e.splice(d--,1);var a=n();void 0!==a&&(t=a)}}return t}r=r||0;for(var d=e.length;d>0&&e[d-1][2]>r;d--)e[d]=e[d-1];e[d]=[o,n,r]},c.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var e={57:0,350:0};c.O.j=t=>0===e[t];var t=(t,o)=>{var n,r,i=o[0],s=o[1],l=o[2],a=0;if(i.some((t=>0!==e[t]))){for(n in s)c.o(s,n)&&(c.m[n]=s[n]);if(l)var d=l(c)}for(t&&t(o);a<i.length;a++)r=i[a],c.o(e,r)&&e[r]&&e[r][0](),e[r]=0;return c.O(d)},o=self.webpackChunklcp_viewport=self.webpackChunklcp_viewport||[];o.forEach(t.bind(null,0)),o.push=t.bind(null,o.push.bind(o))})();var n=c.O(void 0,[350],(()=>c(199)));n=c.O(n)})();
|
||||
@ -1 +1 @@
|
||||
<?php return array('dependencies' => array(), 'version' => 'd4e4a494008d04e1eb42');
|
||||
<?php return array('dependencies' => array(), 'version' => '31d6cfe0d16ae931b73c');
|
||||
|
||||
@ -1 +0,0 @@
|
||||
console.log("Hello World! (from create-block-lcp-viewport block)");
|
||||
@ -19,6 +19,10 @@
|
||||
"hasSidecontent":{
|
||||
"type":"boolean",
|
||||
"default":false
|
||||
},
|
||||
"hasStickyHeader":{
|
||||
"type":"boolean",
|
||||
"default":false
|
||||
}
|
||||
},
|
||||
"textdomain": "lcp-viewport",
|
||||
|
||||
@ -1,13 +1,20 @@
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { useBlockProps, InnerBlocks, InspectorControls } from '@wordpress/block-editor';
|
||||
import { ToggleControl } from '@wordpress/components';
|
||||
import { useEffect } from '@wordpress/element';
|
||||
import { useEffect, useState } from '@wordpress/element';
|
||||
import { createBlock } from '@wordpress/blocks'; // Used for creating new blocks
|
||||
|
||||
import { useSelect, subscribe } from '@wordpress/data'; // Import subscribe
|
||||
import { Modal, Button } from '@wordpress/components'; // Import Modal for popup
|
||||
import './editor.scss';
|
||||
|
||||
export default function Edit({ attributes, setAttributes, clientId }) {
|
||||
const { hasSidecontent } = attributes;
|
||||
const { hasSidecontent, hasStickyHeader } = attributes;
|
||||
|
||||
// Popup visibility state
|
||||
const [isPopupVisible, setIsPopupVisible] = useState(false);
|
||||
const [hasIgnoredPopup, setHasIgnoredPopup] = useState(false); // Track if user has ignored the popup
|
||||
const [blockAdded, setBlockAdded] = useState(false); // Track whether a block has been added
|
||||
const [addedBlock, setAddedBlock] = useState(null); // Track the block that was added outside of viewport
|
||||
|
||||
// Block props with custom className management
|
||||
const blockProps = useBlockProps({
|
||||
@ -18,7 +25,6 @@ export default function Edit({ attributes, setAttributes, clientId }) {
|
||||
const template = [
|
||||
hasSidecontent ? ['lcp/sidecontent'] : null, // Only include 'lcp/sidecontent' if `hasSidecontent` is true
|
||||
['lcp/header-container'], // Add initial content to header-container
|
||||
, // Always include 'lcp/header-container' and add a paragraph block inside it
|
||||
['lcp/main-area'], // Always include 'lcp/main-area'
|
||||
['lcp/footer-container'] // Always include 'lcp/footer-container'
|
||||
].filter(Boolean); // Remove any null or undefined values from the array
|
||||
@ -32,18 +38,84 @@ export default function Edit({ attributes, setAttributes, clientId }) {
|
||||
useEffect(() => {
|
||||
if (hasSidecontent) {
|
||||
// Create a new block and insert it in the block's inner blocks
|
||||
const blockToAdd = createBlock('lcp/sidecontent', {
|
||||
|
||||
});
|
||||
|
||||
// Insert the block programmatically into the block
|
||||
const innerBlocks = wp.data.select('core/block-editor').getBlocks(clientId);
|
||||
|
||||
wp.data.dispatch('core/block-editor').insertBlocks(blockToAdd, 1, clientId); // Insert at position 0
|
||||
|
||||
const blockToAdd = createBlock('lcp/sidecontent', {});
|
||||
wp.data.dispatch('core/block-editor').insertBlocks(blockToAdd, 1, clientId); // Insert at position 0
|
||||
}
|
||||
}, [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(blockProps.clientId);
|
||||
}, [blockProps.clientId]);
|
||||
|
||||
return (
|
||||
<div {...blockProps}>
|
||||
{/* Inspector Controls: Add a toggle for the `hasSidecontent` attribute */}
|
||||
@ -64,6 +136,19 @@ export default function Edit({ attributes, setAttributes, clientId }) {
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Modal for Popup */}
|
||||
{isPopupVisible && !hasIgnoredPopup && (
|
||||
<Modal
|
||||
title={__('Block Outside Viewport', 'lcp-viewport')}
|
||||
onRequestClose={closePopup} // Close on clicking outside
|
||||
className="block-outside-popup"
|
||||
>
|
||||
<p>{__('You added a block outside of the lcp/viewport container. Please make sure the block is inside the appropriate area.', 'lcp-viewport')}</p>
|
||||
<Button isPrimary onClick={closePopup}>{__('Close', 'lcp-viewport')}</Button>
|
||||
<Button isSecondary onClick={ignorePopup}>{__('Ignore', 'lcp-viewport')}</Button>
|
||||
</Modal>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@ -10,15 +10,21 @@ import { useBlockProps, InnerBlocks } from '@wordpress/block-editor';
|
||||
* @return {Element} Element to render.
|
||||
*/
|
||||
export default function Save({ attributes }) {
|
||||
const { hasSidecontent } = attributes; // Retrieve the hasSidecontent attribute
|
||||
const { hasSidecontent, hasStickyHeader } = attributes; // Retrieve the hasSidecontent and hasStickyHeader attributes
|
||||
const blockProps = useBlockProps.save();
|
||||
|
||||
// Conditionally add the 'has-sidecontent' class if hasSidecontent is true
|
||||
const className = hasSidecontent ? 'lcp-has-sidecontent' : '';
|
||||
// Conditionally add the 'has-sidecontent' and 'lcp-has-sticky-header' classes
|
||||
let classNames = '';
|
||||
if (hasSidecontent) {
|
||||
classNames += 'lcp-has-sidecontent ';
|
||||
}
|
||||
if (hasStickyHeader) {
|
||||
classNames += 'lcp-has-sticky-header';
|
||||
}
|
||||
|
||||
return (
|
||||
<div class={className} id="lcp-viewport-outer">
|
||||
<div id="lcp-viewport-inner" class={className}>
|
||||
<div className={classNames} id="lcp-viewport-outer">
|
||||
<div id="lcp-viewport-inner" className={classNames}>
|
||||
<InnerBlocks.Content />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -21,5 +21,4 @@
|
||||
*/
|
||||
|
||||
/* eslint-disable no-console */
|
||||
console.log( 'Hello World! (from create-block-lcp-viewport block)' );
|
||||
/* eslint-enable no-console */
|
||||
|
||||
Reference in New Issue
Block a user