/**
* BLOCK: algori-360-image
*
* Registering a basic block with Gutenberg.
* Simple block, renders and saves the same content without any interactivity.
*/
/**
* WordPress dependencies
*/
const {
IconButton,
PanelBody,
TextControl,
Toolbar,
withNotices } = wp.components; // import { IconButton, PanelBody, RangeControl, ToggleControl, Toolbar, withNotices } from '@wordpress/components';
const { Fragment } = wp.element; // import { Fragment } from '@wordpress/element';
const { __ } = wp.i18n; // Import __() from wp.i18n
const { registerBlockType } = wp.blocks; // Import registerBlockType() from wp.blocks
const {
BlockControls,
InspectorControls,
BlockAlignmentToolbar,
MediaPlaceholder,
MediaUpload,
AlignmentToolbar,
RichText,
} = wp.editor; // Import * from @wordpress/editor
/**
* Internal dependencies
*
* Import CSS.
*/
import './style.scss';
import './editor.scss';
const blockAttributes = {
title: {
type: 'array',
source: 'children',
selector: 'p',
},
url: {
type: 'string',
},
align: {
type: 'string',
},
width: {
type: 'number',
},
height: {
type: 'number',
},
contentAlign: {
type: 'string',
default: 'center',
},
id: {
type: 'number',
},
};
/**
* Register: aa Gutenberg Block.
*
* Registers a new block provided a unique name and an object defining its
* behavior. Once registered, the block is made editor as an option to any
* editor interface where blocks are implemented.
*
* @link https://wordpress.org/gutenberg/handbook/block-api/
* @param {string} name Block name.
* @param {Object} settings Block settings.
* @return {?WPBlock} The block, if it has been successfully
* registered; otherwise `undefined`.
*/
registerBlockType( 'cgb/block-algori-360-image', {
// Block name. Block names must be string that contains a namespace prefix. Example: my-plugin/my-custom-block.
title: __( '360° Image' ), // Block title.
description: __( 'If an image is worth 1,000 words, imagine how many more words an interactive 360° image is worth! Insert a single 360° image.' ), // Block description that appears in the block inspector. Make it short preferably.
icon: 'format-image', // Block icon from Dashicons → https://developer.wordpress.org/resource/dashicons/.
category: 'common', // Block category — Group blocks together based on common traits E.g. common, formatting, layout widgets, embed.
keywords: [ // Block search keywords
__( 'algori panorama image - three sixty degree photo' ),
__( 'spherical photo - full-sphere 3D image' ),
__( 'equirectangular image - VR (Virtual Reality) photography' ),
],
attributes: blockAttributes, // Block attributes for editing in the block inspector.
getEditWrapperProps( attributes ) {
const { align, width } = attributes;
if ( 'left' === align || 'center' === align || 'right' === align || 'wide' === align || 'full' === align ) {
return { 'data-align': align, 'data-resized': !! width };
}
},
/**
* The edit function describes the structure of your block in the context of the editor.
* This represents what the editor will render when the block is used.
*
* The "edit" property must be a valid function.
*
* @link https://wordpress.org/gutenberg/handbook/block-api/block-edit-save/
*/
edit: withNotices( ( { attributes, setAttributes, isSelected, className, noticeOperations, noticeUI } ) => {
const { url, title, align, width, height, contentAlign, id } = attributes;
const updateWidth = ( width ) => setAttributes( { width: parseInt( width, 10 ) } );
const updateHeight = ( height ) => setAttributes( { height: parseInt( height, 10 ) } );
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
const onSelectImage = ( media ) => {
if ( ! media || ! media.url ) {
setAttributes( { url: undefined, id: undefined } );
return;
}
setAttributes( { url: media.url, id: media.id } );
};
const controls = ( // Set Block and Inspector Controls
{ __( 'Image Dimensions' ) }