/** * 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', default: 600, }, height: { type: 'number', default: 300, }, 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. /** * 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 onSelectImage = ( media ) => { if ( ! media || ! media.url ) { setAttributes( { url: undefined, id: undefined } ); return; } setAttributes( { url: media.url, id: media.id } ); }; const onSelectURL = ( newURL ) => { if ( newURL !== url ) { setAttributes( { url: newURL, id: undefined } ); } } const controls = ( // Set Block and Inspector Controls ( ) } /> { !! url && (

{ __( 'Image Dimensions' ) }

) }
); if ( ! url ) { // Upload image if it doesn't exist return ( { controls } ); } return ( // Return 360 image with element settings (css classes) and block controls. Get image using either { url } or { id } { controls }
); } ), /** * The save function defines the way in which the different attributes should be combined * into the final markup, which is then serialized by Gutenberg into post_content. * * The "save" property must be specified and must be a valid function. * * @link https://wordpress.org/gutenberg/handbook/block-api/block-edit-save/ */ save: ( { attributes, className } ) => { const { url, title, align, width, height, contentAlign, id } = attributes; return (
); }, /** * Array of deprecated forms of this block. * * @link https://wordpress.org/gutenberg/handbook/block-api/deprecated-blocks/ */ deprecated: [ { attributes: { ...blockAttributes, }, save: ( { attributes, className } ) => { const { url, title, align, width, height, contentAlign, id } = attributes; return (
); }, } ], } );