Minor changes to filenames
This commit is contained in:
29
blocks/lcp-viewport/src/block.json
Normal file
29
blocks/lcp-viewport/src/block.json
Normal file
@ -0,0 +1,29 @@
|
||||
{
|
||||
"$schema": "https://schemas.wp.org/trunk/block.json",
|
||||
"apiVersion": 3,
|
||||
"name": "lcp/viewport",
|
||||
"version": "0.1.0",
|
||||
"title": "Viewport",
|
||||
"category": "widgets",
|
||||
"icon": "smiley",
|
||||
"description": "Viewport container which is designed to be the parent of all other blocks",
|
||||
"example": {},
|
||||
"supports": {
|
||||
"color": {
|
||||
"background": true,
|
||||
"text": false,
|
||||
"link": false
|
||||
}
|
||||
},
|
||||
"attributes" :{
|
||||
"hasSidecontent":{
|
||||
"type":"boolean",
|
||||
"default":true
|
||||
}
|
||||
},
|
||||
"textdomain": "lcp-viewport",
|
||||
"editorScript": "file:./index.js",
|
||||
"editorStyle": "file:./index.css",
|
||||
"style": "file:./style-index.css",
|
||||
"viewScript": "file:./view.js"
|
||||
}
|
||||
45
blocks/lcp-viewport/src/edit.js
Normal file
45
blocks/lcp-viewport/src/edit.js
Normal file
@ -0,0 +1,45 @@
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { useBlockProps, InnerBlocks, InspectorControls } from '@wordpress/block-editor';
|
||||
import { ToggleControl } from '@wordpress/components';
|
||||
import './editor.scss';
|
||||
|
||||
export default function Edit({ attributes, setAttributes }) {
|
||||
const { hasSidecontent } = attributes;
|
||||
|
||||
// Block props
|
||||
const blockProps = useBlockProps();
|
||||
|
||||
// Custom template logic based on hasSidecontent
|
||||
const template = [
|
||||
hasSidecontent && ['lcp/sidecontent'], // Only include lcp/sidecontent if the toggle is true
|
||||
['lcp/main-area'], // Always include lcp/main-area
|
||||
].filter(Boolean); // Filter out falsey values (e.g., undefined when hasSidecontent is false)
|
||||
|
||||
// Render appender logic to handle block insertion
|
||||
const renderAppender = () => {
|
||||
return <InnerBlocks.ButtonBlockAppender />;
|
||||
};
|
||||
|
||||
return (
|
||||
<div {...blockProps}>
|
||||
{/* Inspector Controls: Add a toggle for the `hasSidecontent` attribute */}
|
||||
<InspectorControls>
|
||||
<ToggleControl
|
||||
label={__('Include Side Content', 'lcp-viewport')}
|
||||
checked={hasSidecontent}
|
||||
onChange={(value) => setAttributes({ hasSidecontent: value })}
|
||||
/>
|
||||
</InspectorControls>
|
||||
|
||||
<p>
|
||||
{__('Lcp Viewport – hello from the editor!', 'lcp-viewport')}
|
||||
</p>
|
||||
|
||||
{/* Render the InnerBlocks with conditional template */}
|
||||
<InnerBlocks
|
||||
template={template} // Use the template logic here
|
||||
renderAppender={renderAppender} // Custom appender logic
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
9
blocks/lcp-viewport/src/editor.scss
Normal file
9
blocks/lcp-viewport/src/editor.scss
Normal file
@ -0,0 +1,9 @@
|
||||
/**
|
||||
* The following styles get applied inside the editor only.
|
||||
*
|
||||
* Replace them with your own styles or remove the file completely.
|
||||
*/
|
||||
|
||||
.wp-block-create-block-lcp-viewport {
|
||||
border: 1px dotted #f00;
|
||||
}
|
||||
39
blocks/lcp-viewport/src/index.js
Normal file
39
blocks/lcp-viewport/src/index.js
Normal file
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Registers a new block provided a unique name and an object defining its behavior.
|
||||
*
|
||||
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
|
||||
*/
|
||||
import { registerBlockType } from '@wordpress/blocks';
|
||||
|
||||
/**
|
||||
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
|
||||
* All files containing `style` keyword are bundled together. The code used
|
||||
* gets applied both to the front of your site and to the editor.
|
||||
*
|
||||
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
|
||||
*/
|
||||
import './style.scss';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import Edit from './edit';
|
||||
import save from './save';
|
||||
import metadata from './block.json';
|
||||
|
||||
/**
|
||||
* Every block starts by registering a new block type definition.
|
||||
*
|
||||
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
|
||||
*/
|
||||
registerBlockType( metadata.name, {
|
||||
/**
|
||||
* @see ./edit.js
|
||||
*/
|
||||
edit: Edit,
|
||||
|
||||
/**
|
||||
* @see ./save.js
|
||||
*/
|
||||
save,
|
||||
} );
|
||||
26
blocks/lcp-viewport/src/save.js
Normal file
26
blocks/lcp-viewport/src/save.js
Normal file
@ -0,0 +1,26 @@
|
||||
import { useBlockProps, InnerBlocks } from '@wordpress/block-editor';
|
||||
|
||||
/**
|
||||
* The save function defines the way in which the different attributes should
|
||||
* be combined into the final markup, which is then serialized by the block
|
||||
* editor into `post_content`.
|
||||
*
|
||||
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#save
|
||||
*
|
||||
* @return {Element} Element to render.
|
||||
*/
|
||||
export default function Save({ attributes }) {
|
||||
const { hasSidecontent } = attributes; // Retrieve the hasSidecontent attribute
|
||||
const blockProps = useBlockProps.save();
|
||||
|
||||
// Conditionally add the .lcp-has-sidecontent class if hasSidecontent is true
|
||||
const hasSidecontentClass = hasSidecontent ? 'class="lcp-has-sidecontent"' : null;
|
||||
|
||||
return (
|
||||
<div {...blockProps} id="lcp-viewport-outer" >
|
||||
<div hasSidecontentClass id="lcp-viewport-inner">
|
||||
<InnerBlocks.Content />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
7
blocks/lcp-viewport/src/style.scss
Normal file
7
blocks/lcp-viewport/src/style.scss
Normal file
@ -0,0 +1,7 @@
|
||||
/**
|
||||
* The following styles get applied both on the front of your site
|
||||
* and in the editor.
|
||||
*
|
||||
* Replace them with your own styles or remove the file completely.
|
||||
*/
|
||||
|
||||
25
blocks/lcp-viewport/src/view.js
Normal file
25
blocks/lcp-viewport/src/view.js
Normal file
@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Use this file for JavaScript code that you want to run in the front-end
|
||||
* on posts/pages that contain this block.
|
||||
*
|
||||
* When this file is defined as the value of the `viewScript` property
|
||||
* in `block.json` it will be enqueued on the front end of the site.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```js
|
||||
* {
|
||||
* "viewScript": "file:./view.js"
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* If you're not making any changes to this file because your project doesn't need any
|
||||
* JavaScript running in the front-end, then you should delete this file and remove
|
||||
* the `viewScript` property from `block.json`.
|
||||
*
|
||||
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#view-script
|
||||
*/
|
||||
|
||||
/* 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