frontblocks-download-button-option.js
2 months ago
frontblocks-download-button-option.jsx
2 months ago
frontblocks-download-button-option.jsx
169 lines
| 1 | const { createHigherOrderComponent } = wp.compose; |
| 2 | const { Fragment } = wp.element; |
| 3 | const { InspectorControls, MediaUpload, MediaUploadCheck } = wp.blockEditor; |
| 4 | const { PanelBody, ToggleControl, Button } = wp.components; |
| 5 | const { __ } = wp.i18n; |
| 6 | |
| 7 | const BUTTON_BLOCK = 'core/button'; |
| 8 | |
| 9 | // Register download attributes for the native button block. |
| 10 | wp.hooks.addFilter( |
| 11 | 'blocks.registerBlockType', |
| 12 | 'frontblocks/add-download-attributes', |
| 13 | ( settings, name ) => { |
| 14 | if ( BUTTON_BLOCK === name ) { |
| 15 | settings.attributes = Object.assign( settings.attributes || {}, { |
| 16 | frblDownloadEnabled: { |
| 17 | type: 'boolean', |
| 18 | default: false, |
| 19 | }, |
| 20 | frblDownloadFileId: { |
| 21 | type: 'number', |
| 22 | default: 0, |
| 23 | }, |
| 24 | frblDownloadFileUrl: { |
| 25 | type: 'string', |
| 26 | default: '', |
| 27 | }, |
| 28 | frblDownloadFileName: { |
| 29 | type: 'string', |
| 30 | default: '', |
| 31 | }, |
| 32 | } ); |
| 33 | } |
| 34 | return settings; |
| 35 | } |
| 36 | ); |
| 37 | |
| 38 | const withDownloadControl = createHigherOrderComponent( ( BlockEdit ) => { |
| 39 | return ( props ) => { |
| 40 | if ( BUTTON_BLOCK !== props.name ) { |
| 41 | return <BlockEdit { ...props } />; |
| 42 | } |
| 43 | |
| 44 | const { attributes, setAttributes } = props; |
| 45 | const isDownloadEnabled = !! attributes.frblDownloadEnabled; |
| 46 | const fileId = attributes.frblDownloadFileId || 0; |
| 47 | const fileUrl = attributes.frblDownloadFileUrl || ''; |
| 48 | const fileName = attributes.frblDownloadFileName || ''; |
| 49 | |
| 50 | const setDownloadEnabled = ( enabled ) => { |
| 51 | if ( enabled ) { |
| 52 | setAttributes( { frblDownloadEnabled: true } ); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | const newAttributes = { |
| 57 | frblDownloadEnabled: false, |
| 58 | frblDownloadFileId: 0, |
| 59 | frblDownloadFileUrl: '', |
| 60 | frblDownloadFileName: '', |
| 61 | }; |
| 62 | |
| 63 | // Clear the button URL only when it still points to the download file. |
| 64 | if ( fileUrl && attributes.url === fileUrl ) { |
| 65 | newAttributes.url = undefined; |
| 66 | } |
| 67 | |
| 68 | setAttributes( newAttributes ); |
| 69 | }; |
| 70 | |
| 71 | const onSelectFile = ( media ) => { |
| 72 | if ( ! media || ! media.url ) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | const mediaFileName = media.filename || media.url.split( '/' ).pop(); |
| 77 | |
| 78 | setAttributes( { |
| 79 | frblDownloadFileId: media.id || 0, |
| 80 | frblDownloadFileUrl: media.url, |
| 81 | frblDownloadFileName: mediaFileName, |
| 82 | url: media.url, |
| 83 | } ); |
| 84 | }; |
| 85 | |
| 86 | const onRemoveFile = () => { |
| 87 | const newAttributes = { |
| 88 | frblDownloadFileId: 0, |
| 89 | frblDownloadFileUrl: '', |
| 90 | frblDownloadFileName: '', |
| 91 | }; |
| 92 | |
| 93 | if ( fileUrl && attributes.url === fileUrl ) { |
| 94 | newAttributes.url = undefined; |
| 95 | } |
| 96 | |
| 97 | setAttributes( newAttributes ); |
| 98 | }; |
| 99 | |
| 100 | return ( |
| 101 | <Fragment> |
| 102 | <BlockEdit { ...props } /> |
| 103 | |
| 104 | <InspectorControls> |
| 105 | <PanelBody |
| 106 | title={ __( 'FrontBlocks - Download', 'frontblocks' ) } |
| 107 | initialOpen={ false } |
| 108 | > |
| 109 | <ToggleControl |
| 110 | label={ __( 'Download file on click', 'frontblocks' ) } |
| 111 | checked={ isDownloadEnabled } |
| 112 | onChange={ setDownloadEnabled } |
| 113 | help={ |
| 114 | isDownloadEnabled ? |
| 115 | __( 'The button will download the selected file instead of opening a URL.', 'frontblocks' ) : |
| 116 | __( 'Enable to make this button download a file.', 'frontblocks' ) |
| 117 | } |
| 118 | /> |
| 119 | |
| 120 | { isDownloadEnabled && ( |
| 121 | <MediaUploadCheck> |
| 122 | <MediaUpload |
| 123 | onSelect={ onSelectFile } |
| 124 | value={ fileId } |
| 125 | render={ ( { open } ) => ( |
| 126 | <div> |
| 127 | { fileName && ( |
| 128 | <p> |
| 129 | <strong>{ __( 'Selected file:', 'frontblocks' ) }</strong> |
| 130 | <br /> |
| 131 | { fileName } |
| 132 | </p> |
| 133 | ) } |
| 134 | <Button |
| 135 | variant="primary" |
| 136 | onClick={ open } |
| 137 | > |
| 138 | { fileUrl ? |
| 139 | __( 'Replace file', 'frontblocks' ) : |
| 140 | __( 'Select or upload file', 'frontblocks' ) |
| 141 | } |
| 142 | </Button> |
| 143 | { fileUrl && ( |
| 144 | <Button |
| 145 | variant="secondary" |
| 146 | onClick={ onRemoveFile } |
| 147 | style={ { marginLeft: '8px' } } |
| 148 | > |
| 149 | { __( 'Remove file', 'frontblocks' ) } |
| 150 | </Button> |
| 151 | ) } |
| 152 | </div> |
| 153 | ) } |
| 154 | /> |
| 155 | </MediaUploadCheck> |
| 156 | ) } |
| 157 | </PanelBody> |
| 158 | </InspectorControls> |
| 159 | </Fragment> |
| 160 | ); |
| 161 | }; |
| 162 | }, 'withDownloadControl' ); |
| 163 | |
| 164 | wp.hooks.addFilter( |
| 165 | 'editor.BlockEdit', |
| 166 | 'frontblocks/download-button-control', |
| 167 | withDownloadControl |
| 168 | ); |
| 169 |