Changes to directory structure
This commit is contained in:
46
includes/blocks/lcp-button/src/block.json
Normal file
46
includes/blocks/lcp-button/src/block.json
Normal file
@ -0,0 +1,46 @@
|
||||
{
|
||||
"$schema": "https://schemas.wp.org/trunk/block.json",
|
||||
"apiVersion": 3,
|
||||
"name": "lcp/button",
|
||||
"version": "0.1.0",
|
||||
"title": "Button",
|
||||
"category": "widgets",
|
||||
"icon": "smiley",
|
||||
"description": "Example block scaffolded with Create Block tool.",
|
||||
"example": {},
|
||||
"supports": {
|
||||
"html": false
|
||||
},
|
||||
"attributes": {
|
||||
"buttonFunction": {
|
||||
"type": "string",
|
||||
"default": "customUrl"
|
||||
},
|
||||
"buttonText": {
|
||||
"type": "string",
|
||||
"default": "Button Text"
|
||||
},
|
||||
"displayIcon": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"iconSource": {
|
||||
"type": "string",
|
||||
"default": "svgPath"
|
||||
},
|
||||
"iconSvgPath": {
|
||||
"type": "string"
|
||||
},
|
||||
"iconSvgId": {
|
||||
"type": "string",
|
||||
"default": ""
|
||||
},
|
||||
"popUpId": {
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"textdomain": "button",
|
||||
"editorScript": "file:./index.js",
|
||||
"editorStyle": "file:./index.css",
|
||||
"style": "file:./style-index.css",
|
||||
"viewScript": "file:./view.js"
|
||||
}
|
||||
152
includes/blocks/lcp-button/src/edit.js
Normal file
152
includes/blocks/lcp-button/src/edit.js
Normal file
@ -0,0 +1,152 @@
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { useBlockProps, InspectorControls } from '@wordpress/block-editor';
|
||||
import { PanelBody, SelectControl, TextControl, TextareaControl, ToggleControl } from '@wordpress/components';
|
||||
import { useState, useEffect } from '@wordpress/element';
|
||||
import './editor.scss';
|
||||
import { useSelect } from '@wordpress/data';
|
||||
import { IconSelectControl } from '../components/IconSelectControl';
|
||||
|
||||
|
||||
// Sample JSON data for icons (with UUIDs)
|
||||
const iconData = [
|
||||
{
|
||||
"uuid": "c0a8012345678f3d5b847ad0f8a890f1",
|
||||
"iconId": "comment-dots",
|
||||
"family": "Font Awesome",
|
||||
"sub-family": "solid",
|
||||
"name": "Comment Dots",
|
||||
"path": "<path d='M168.2 384.9c-15-5.4-31.7-3.1-44.6 6.4c-8.2 6-22.3 14.8-39.4 22.7c5.6-14.7 9.9-31.3 11.3-49.4c1-12.9-3.3-25.7-11.8-35.5C60.4 302.8 48 272 48 240c0-79.5 83.3-160 208-160s208 80.5 208 160s-83.3 160-208 160c-31.6 0-61.3-5.5-87.8-15.1zM26.3 423.8c-1.6 2.7-3.3 5.4-5.1 8.1l-.3 .5c-1.6 2.3-3.2 4.6-4.8 6.9c-3.5 4.7-7.3 9.3-11.3 13.5c-4.6 4.6-5.9 11.4-3.4 17.4c2.5 6 8.3 9.9 14.8 9.9c5.1 0 10.2-.3 15.3-.8l.7-.1c4.4-.5 8.8-1.1 13.2-1.9c.8-.1 1.6-.3 2.4-.5c17.8-3.5 34.9-9.5 50.1-16.1c22.9-10 42.4-21.9 54.3-30.6c31.8 11.5 67 17.9 104.1 17.9c141.4 0 256-93.1 256-208S397.4 32 256 32S0 125.1 0 240c0 45.1 17.7 86.8 47.7 120.9c-1.9 24.5-11.4 46.3-21.4 62.9zM144 272a32 32 0 1 0 0-64 32 32 0 1 0 0 64zm144-32a32 32 0 1 0 -64 0 32 32 0 1 0 64 0zm80 32a32 32 0 1 0 0-64 32 32 0 1 0 0 64z'/>"
|
||||
},
|
||||
{
|
||||
"uuid": "a1b2c3d4e5f6789abcdef0123456789ab",
|
||||
"iconId": "newspaper",
|
||||
"family": "Font Awesome",
|
||||
"sub-family": "solid",
|
||||
"name": "Newspaper",
|
||||
"path": "<path d='M168 80c-13.3 0-24 10.7-24 24l0 304c0 8.4-1.4 16.5-4.1 24L440 432c13.3 0 24-10.7 24-24l0-304c0-13.3-10.7-24-24-24L168 80zM72 480c-39.8 0-72-32.2-72-72L0 112C0 98.7 10.7 88 24 88s24 10.7 24 24l0 296c0 13.3 10.7 24 24 24s24-10.7 24-24l0-304c0-39.8 32.2-72 72-72l272 0c39.8 0 72 32.2 72 72l0 304c0 39.8-32.2 72-72 72L72 480zM176 136c0-13.3 10.7-24 24-24l96 0c13.3 0 24 10.7 24 24l0 80c0 13.3-10.7 24-24 24l-96 0c-13.3 0-24-10.7-24-24l0-80zm200-24l32 0c13.3 0 24 10.7 24 24s-10.7 24-24 24l-32 0c-13.3 0-24-10.7-24-24s10.7-24 24-24zm0 80l32 0c13.3 0 24 10.7 24 24s-10.7 24-24 24l-32 0c-13.3 0-24-10.7-24-24s10.7-24 24-24zM200 272l208 0c13.3 0 24 10.7 24 24s-10.7 24-24 24l-208 0c-13.3 0-24-10.7-24-24s10.7-24 24-24zm0 80l208 0c13.3 0 24 10.7 24 24s-10.7 24-24 24l-208 0c-13.3 0-24-10.7-24-24s10.7-24 24-24z'/>"
|
||||
}
|
||||
];
|
||||
|
||||
export default function Edit(props) {
|
||||
const { attributes, setAttributes } = props;
|
||||
const { buttonFunction, popUpId, buttonText, iconSvgId, iconSvgPath, displayIcon, iconSource} = attributes; // iconSvgId holds the UUID and iconSvgPath holds the SVG path
|
||||
const [popups, setPopups] = useState([]);
|
||||
|
||||
// Fetch published popups (lcp-popup CPT)
|
||||
useEffect(() => {
|
||||
const fetchPopups = async () => {
|
||||
const response = await fetch('/wp-json/wp/v2/lcp-popup');
|
||||
const data = await response.json();
|
||||
setPopups(data);
|
||||
};
|
||||
fetchPopups();
|
||||
}, []);
|
||||
|
||||
// Handle icon selection from dropdown
|
||||
const handleIconChange = (selectedIconId) => {
|
||||
console.log("an icon is selected");
|
||||
// Find the selected icon from the iconData array using the UUID
|
||||
const selectedIcon = iconData.find(icon => icon.uuid === selectedIconId);
|
||||
|
||||
if (selectedIcon) {
|
||||
// Update both iconSvgId (UUID) and iconSvgPath (SVG path)
|
||||
setAttributes({
|
||||
iconSvgId: selectedIconId, // UUID of the selected icon
|
||||
iconSvgPath: selectedIcon.path // SVG path of the selected icon
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const handleToggleChange = (value) => {
|
||||
setAttributes({ displayIcon: value });
|
||||
};
|
||||
|
||||
const iconSourceOptions = [
|
||||
{ value: 'manualSvgPath', label: 'SVG Path' },
|
||||
{ value: 'iconSelector', label: 'Select Icon' },
|
||||
];
|
||||
|
||||
const buttonFunctionOptions = [
|
||||
{ value: 'customUrl', label: 'Custom URL' },
|
||||
{ value: 'openPopup', label: 'Open Popup' },
|
||||
{ value: 'showLoginForm', label: 'Show Login Form' },
|
||||
{ value: 'logOut', label: 'Open Popup' },
|
||||
{ value: 'shareCurrentPost', label: 'Share Current Post' }
|
||||
];
|
||||
|
||||
|
||||
return (
|
||||
<>
|
||||
<InspectorControls>
|
||||
|
||||
<PanelBody title={__("Button Settings")}>
|
||||
<SelectControl
|
||||
label={__("Popup")}
|
||||
value={popUpId}
|
||||
options={iconSourceOptions}
|
||||
onChange={(value) => setAttributes({ iconSource })}
|
||||
/>
|
||||
{buttonFunction === 'openPopup' && (
|
||||
<SelectControl
|
||||
label={__("Popup")}
|
||||
value={popUpId}
|
||||
options={iconSourceOptions}
|
||||
onChange={(value) => setAttributes({ iconSource })}
|
||||
/>
|
||||
)}
|
||||
<TextControl
|
||||
label={__("Button Text")}
|
||||
value={buttonText}
|
||||
onChange={(value) => setAttributes({ buttonText: value })}
|
||||
/>
|
||||
<ToggleControl
|
||||
label="Display Icon"
|
||||
checked={displayIcon}
|
||||
onChange={handleToggleChange}
|
||||
/>
|
||||
{displayIcon && (
|
||||
<>
|
||||
<SelectControl
|
||||
label={__("Icon Source")}
|
||||
value={iconSource}
|
||||
options={iconSourceOptions}
|
||||
onChange={(value) => setAttributes({ iconSource: value })}
|
||||
/>
|
||||
{iconSource === 'manualSvgPath' && (
|
||||
<TextareaControl
|
||||
label="Icon SVG Path"
|
||||
value={iconSvgPath}
|
||||
onChange={(value) => props.setAttributes({ iconSvgPath: value })}
|
||||
/>
|
||||
)}
|
||||
{iconSource === 'iconSelector' && (
|
||||
<>
|
||||
<SelectControl
|
||||
label={__("Icon")}
|
||||
value={iconSvgId}
|
||||
options={iconData.map((icon) => ({
|
||||
label: icon.name,
|
||||
value: icon.uuid, // Store the UUID as the value
|
||||
}))}
|
||||
onChange={handleIconChange}
|
||||
/>
|
||||
<IconSelectControl
|
||||
value={props.attributes.icon}
|
||||
onChange={handleIconChange}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
|
||||
|
||||
</PanelBody>
|
||||
</InspectorControls>
|
||||
<div {...useBlockProps()}>
|
||||
<div className="button-container">
|
||||
<span>{buttonText}</span>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
9
includes/blocks/lcp-button/src/editor.scss
Normal file
9
includes/blocks/lcp-button/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-button {
|
||||
border: 1px dotted #f00;
|
||||
}
|
||||
39
includes/blocks/lcp-button/src/index.js
Normal file
39
includes/blocks/lcp-button/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,
|
||||
} );
|
||||
25
includes/blocks/lcp-button/src/save.js
Normal file
25
includes/blocks/lcp-button/src/save.js
Normal file
@ -0,0 +1,25 @@
|
||||
import { useBlockProps } from '@wordpress/block-editor';
|
||||
|
||||
export default function save(props) {
|
||||
const { attributes } = props;
|
||||
const { type, popUpId, buttonText, iconSvgPath } = attributes; // Destructure buttonText and iconSvgPath from attributes
|
||||
|
||||
// Get the block props for the button
|
||||
const blockProps = useBlockProps.save();
|
||||
|
||||
// Conditionally add data-open-popup if type is 'openPopUp' and popUpId is not empty
|
||||
if (type === 'openPopUp' && popUpId) {
|
||||
blockProps['data-open-popup'] = popUpId; // Add the data attribute dynamically
|
||||
}
|
||||
|
||||
return (
|
||||
<button {...blockProps}>
|
||||
{/* Conditionally render the icon if iconSvgPath is set */}
|
||||
{iconSvgPath && (
|
||||
<svg className="icon" viewBox="0 0 24 24" width="100" height="100" dangerouslySetInnerHTML={{ __html: iconSvgPath }} />
|
||||
)}
|
||||
{/* Render the button text */}
|
||||
{buttonText || 'Button'} {/* Use buttonText or fallback */}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
12
includes/blocks/lcp-button/src/style.scss
Normal file
12
includes/blocks/lcp-button/src/style.scss
Normal file
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
.wp-block-create-block-button {
|
||||
background-color: #21759b;
|
||||
color: #fff;
|
||||
padding: 2px;
|
||||
}
|
||||
25
includes/blocks/lcp-button/src/view.js
Normal file
25
includes/blocks/lcp-button/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-button block)' );
|
||||
/* eslint-enable no-console */
|
||||
Reference in New Issue
Block a user