Skip to content

Commit

Permalink
Merge pull request #74 from studiopress/add/file-field
Browse files Browse the repository at this point in the history
Add a File field, like for .pdf uploads
  • Loading branch information
kienstra authored Jul 20, 2021
2 parents 7f31adb + e798b9e commit a2aedc4
Show file tree
Hide file tree
Showing 16 changed files with 447 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Contributors: lukecarbis, ryankienstra, Stino11, rheinardkorf, studiopress, wpengine
Tags: gutenberg, blocks, block editor, fields, template
Requires at least: 5.4
Tested up to: 5.7
Tested up to: 5.8
Requires PHP: 5.6
Stable tag: 1.3.0
License: GPLv2 or later
Expand Down
4 changes: 4 additions & 0 deletions css/src/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ div[class^="wp-block-genesis-custom-blocks-"],
margin-top: 0;
}
}

pre {
margin-left: 0.5em;
}
}
}

Expand Down
129 changes: 129 additions & 0 deletions js/src/block-editor/controls/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* External dependencies
*/
import * as React from 'react';

/**
* WordPress dependencies
*/
import {
MediaUpload,
MediaUploadCheck,
} from '@wordpress/block-editor';
import {
BaseControl,
Button,
Placeholder,
DropZone,
DropZoneProvider,
FormFileUpload,
Spinner,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { useMedia } from '../hooks';

const defaultFileId = 0;

const GcbFileControl = ( props ) => {
const { field, getValue, instanceId, onChange } = props;
const fieldValue = getValue( props );
const {
mediaSrc,
isUploading,
onSelect,
removeMedia,
setIsUploading,
uploadFiles,
} = useMedia( fieldValue, onChange );
const fileRegex = /[^\/]+\.[^\/]+$/;

return (
<BaseControl className="genesis-custom-blocks-media-controls" label={ field.label } id={ `gcb-file-${ instanceId }` }>
{ !! field.help
? <p className="components-base-control__help">{ field.help }</p>
: null
}
{ !! mediaSrc
? (
<>
{ mediaSrc.match( fileRegex )
? <pre>{ mediaSrc.match( fileRegex )[ 0 ] }</pre>
: null
}
<Button
disabled={ !! isUploading }
className="gcb-image__remove"
onClick={ () => {
onChange( defaultFileId );
removeMedia();
} }
>
{ __( 'Remove', 'genesis-custom-blocks' ) }
</Button>
</>
) : (
<Placeholder
className="gcb-image__placeholder"
icon="media-default"
label={ __( 'File', 'genesis-custom-blocks' ) }
instructions={ __( 'Drag a file, upload a new one or select a file from your library.', 'genesis-custom-blocks' ) }
>
<DropZoneProvider>
<DropZone
onFilesDrop={ ( files ) => {
if ( files.length ) {
setIsUploading( true );
uploadFiles( files );
}
} }
/>
</DropZoneProvider>
{ isUploading
? <Spinner />
: (
<>
<FormFileUpload
disabled={ !! isUploading }
onChange={ ( event ) => {
setIsUploading( true );
uploadFiles( event.target.files );
} }
accept="*"
multiple={ false }
>
{ __( 'Upload', 'genesis-custom-blocks' ) }
</FormFileUpload>
<MediaUploadCheck>
<MediaUpload
gallery={ false }
multiple={ false }
onSelect={ onSelect }
value={ getValue( props ) }
render={ ( { open } ) => (
<div className="components-media-library-button">
<Button
disabled={ !! isUploading }
className="editor-media-placeholder__button"
onClick={ open }
>
{ __( 'Media Library', 'genesis-custom-blocks' ) }
</Button>
</div>
) }
/>
</MediaUploadCheck>
</>
)
}
</Placeholder>
)
}
</BaseControl>
);
};

export default GcbFileControl;
57 changes: 31 additions & 26 deletions js/src/block-editor/controls/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import * as React from 'react';
/**
* WordPress dependencies
*/
import { MediaUpload } from '@wordpress/block-editor';
import {
MediaUpload,
MediaUploadCheck,
} from '@wordpress/block-editor';
import {
BaseControl,
Button,
Expand All @@ -21,7 +24,7 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { useImage } from '../hooks';
import { useMedia } from '../hooks';

const allowedTypes = [ 'image' ];
const defaultImgId = 0;
Expand All @@ -30,25 +33,25 @@ const GcbImageControl = ( props ) => {
const { field, getValue, instanceId, onChange } = props;
const fieldValue = getValue( props );
const {
imageAlt,
imageSrc,
mediaAlt,
mediaSrc,
isUploading,
onSelect,
removeImage,
removeMedia: removeImage,
setIsUploading,
uploadFiles,
} = useImage( fieldValue, onChange, allowedTypes );
} = useMedia( fieldValue, onChange, allowedTypes );

return (
<BaseControl className="genesis-custom-blocks-media-controls" label={ field.label } id={ `gcb-image-${ instanceId }` }>
{ !! field.help
? <p className="components-base-control__help">{ field.help }</p>
: null
}
{ !! imageSrc
{ !! mediaSrc
? (
<>
<img className="gcb-image__img" src={ imageSrc } alt={ imageAlt } />
<img className="gcb-image__img" src={ mediaSrc } alt={ mediaAlt } />
<Button
disabled={ !! isUploading }
className="gcb-image__remove"
Expand Down Expand Up @@ -87,24 +90,26 @@ const GcbImageControl = ( props ) => {
>
{ __( 'Upload', 'genesis-custom-blocks' ) }
</FormFileUpload>
<MediaUpload
gallery={ false }
multiple={ false }
onSelect={ onSelect }
allowedTypes={ allowedTypes }
value={ getValue( props ) }
render={ ( { open } ) => (
<div className="components-media-library-button">
<Button
disabled={ !! isUploading }
className="editor-media-placeholder__button"
onClick={ open }
>
{ __( 'Media Library', 'genesis-custom-blocks' ) }
</Button>
</div>
) }
/>
<MediaUploadCheck>
<MediaUpload
gallery={ false }
multiple={ false }
onSelect={ onSelect }
allowedTypes={ allowedTypes }
value={ getValue( props ) }
render={ ( { open } ) => (
<div className="components-media-library-button">
<Button
disabled={ !! isUploading }
className="editor-media-placeholder__button"
onClick={ open }
>
{ __( 'Media Library', 'genesis-custom-blocks' ) }
</Button>
</div>
) }
/>
</MediaUploadCheck>
</>
)
}
Expand Down
2 changes: 2 additions & 0 deletions js/src/block-editor/controls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import GcbTextControl from './text';
import GcbTextareaControl from './textarea';
import GcbURLControl from './url';
import GcbEmailControl from './email';
import GcbFileControl from './file';
import GcbNumberControl from './number';
import GcbColorControl from './color';
import GcbImageControl from './image';
Expand All @@ -21,6 +22,7 @@ export default {
textarea: GcbTextareaControl,
url: GcbURLControl,
email: GcbEmailControl,
file: GcbFileControl,
number: GcbNumberControl,
color: GcbColorControl,
image: GcbImageControl,
Expand Down
45 changes: 45 additions & 0 deletions js/src/block-editor/controls/test/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* External dependencies
*/
import { render } from '@testing-library/react';

/**
* Internal dependencies
*/
import GcbFileControl from '../file';

jest.mock( '@wordpress/api-fetch', () => {
return jest.fn( () => {
return Promise.resolve( {
json: () => Promise.resolve( {} ),
} );
} );
} );

// @todo: remove this when the console warning no longer appears.
// Expected mock function not to be called but it was called with:
// ["wp.components.DropZoneProvider is deprecated. Note: wp.component.DropZone no longer needs a provider. wp.components.DropZoneProvider is safe to remove from your code."]
// Core still has an older components file, so removing the provider now crashes the editor.
console.warn = jest.fn(); /* eslint-disable-line no-console */

/**
* Gets the props for the tested component.
*
* @return {Object} The props to pass to the component.
*/
const getProps = () => ( {
field: {
label: 'Here is a label',
help: 'And here is some text',
},
getValue: jest.fn(),
onChange: jest.fn(),
} );

test( 'file control', () => {
const props = getProps();
const { getByText } = render( <GcbFileControl { ...props } /> );

expect( getByText( props.field.label ) ).toBeInTheDocument();
expect( getByText( props.field.help ) ).toBeInTheDocument();
} );
1 change: 1 addition & 0 deletions js/src/block-editor/helpers/test/addControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ test( 'addControls', () => {
checkbox: expect.anything(),
color: expect.anything(),
email: expect.anything(),
file: expect.anything(),
image: expect.anything(),
inner_blocks: expect.anything(),
multiselect: expect.anything(),
Expand Down
2 changes: 1 addition & 1 deletion js/src/block-editor/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default as useImage } from './useImage';
export { default as useMedia } from './useMedia';
Loading

0 comments on commit a2aedc4

Please sign in to comment.