/** * BLOCK: algori-pdf-viewer * * Algori PDF Viewer is a Gutenberg Block Plugin that enables you to easily display PDF documents directly on your website. */ /** * WordPress dependencies */ const { withSelect } = wp.data; // import { withSelect } from '@wordpress/data' const { IconButton, PanelBody, TextControl, SelectControl, ToggleControl, 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, ContrastChecker, PanelColorSettings, BlockAlignmentToolbar, MediaPlaceholder, MediaUpload, MediaUploadCheck, AlignmentToolbar, RichText, BlockIcon, } = wp.editor; // Import * from @wordpress/editor /** * Internal dependencies * * Import CSS. */ import './style.scss'; import './editor.scss'; /** * Module Constants */ const ALLOWED_MEDIA_TYPES = [ 'application' ]; 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-pdf-viewer', { // Block name. Block names must be string that contains a namespace prefix. Example: my-plugin/my-custom-block. title: __( 'PDF Viewer' ), // Block title. description: __( 'Let visitors view your PDF documents directly on your site! Insert a PDF file.' ), // Block description that appears in the block inspector. Make it short preferably. icon: , // Block icon from Material Design Icons → https://material.io/tools/icons/ category: 'common', // Block category — Group blocks together based on common traits E.g. common, formatting, layout widgets, embed. keywords: [ // Block search keywords __( 'portable document format' ), __( 'file' ), __( 'algori' ), ], 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 onSelectPDF = ( 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 && (
) }
); if ( ! url ) { // Upload PDF if it doesn't exist return ( { controls } } className={ className } labels={ { title: __( 'PDF Viewer' ), instructions: __( 'Drag a PDF, upload a new one, insert from URL or select a file from your library.' ), } } onSelect={ onSelectPDF } onSelectURL={ onSelectURL } accept="application/pdf" allowedTypes={ ALLOWED_MEDIA_TYPES} notices={ noticeUI } onError={ noticeOperations.createErrorNotice } /> ); } return ( // Return PDF viewer with element settings (css classes) and block controls. Get PDF using either { url } or { id } { controls }
); }, /** * 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 (