edit.js
219 lines
| 1 | import { |
| 2 | userAccess, |
| 3 | valueFormats, |
| 4 | } from './options'; |
| 5 | import preview from './preview'; |
| 6 | |
| 7 | const { |
| 8 | ToolBarFields, |
| 9 | GeneralFields, |
| 10 | AdvancedFields, |
| 11 | FieldWrapper, |
| 12 | FieldSettingsWrapper, |
| 13 | ValidationBlockMessage, |
| 14 | ValidationToggleGroup, |
| 15 | AdvancedInspectorControl, |
| 16 | AttributeHelp, |
| 17 | } = JetFBComponents; |
| 18 | const { |
| 19 | useIsAdvancedValidation, |
| 20 | useUniqueNameOnDuplicate, |
| 21 | } = JetFBHooks; |
| 22 | const { |
| 23 | __, |
| 24 | } = wp.i18n; |
| 25 | |
| 26 | const { |
| 27 | useBlockProps, |
| 28 | InspectorControls, |
| 29 | } = wp.blockEditor; |
| 30 | |
| 31 | const { |
| 32 | SelectControl, |
| 33 | ToggleControl, |
| 34 | FormTokenField, |
| 35 | TextControl, |
| 36 | __experimentalNumberControl, |
| 37 | __experimentalInputControl, |
| 38 | PanelBody, |
| 39 | } = wp.components; |
| 40 | |
| 41 | let { NumberControl, InputControl } = wp.components; |
| 42 | |
| 43 | if ( typeof NumberControl === 'undefined' ) { |
| 44 | NumberControl = __experimentalNumberControl; |
| 45 | } |
| 46 | |
| 47 | if ( typeof InputControl === 'undefined' ) { |
| 48 | InputControl = __experimentalInputControl; |
| 49 | } |
| 50 | |
| 51 | const localizeData = window.jetFormMediaFieldData; |
| 52 | |
| 53 | export default function MediaEdit( props ) { |
| 54 | |
| 55 | const blockProps = useBlockProps(); |
| 56 | const isAdvancedValidation = useIsAdvancedValidation(); |
| 57 | useUniqueNameOnDuplicate(); |
| 58 | |
| 59 | const { |
| 60 | attributes, |
| 61 | setAttributes, |
| 62 | isSelected, |
| 63 | editProps: { uniqKey, attrHelp }, |
| 64 | } = props; |
| 65 | |
| 66 | if ( attributes.isPreview ) { |
| 67 | return <div style={ { |
| 68 | width: '100%', |
| 69 | display: 'flex', |
| 70 | justifyContent: 'center', |
| 71 | } }> |
| 72 | { preview } |
| 73 | </div>; |
| 74 | } |
| 75 | |
| 76 | return [ |
| 77 | <ToolBarFields |
| 78 | key={ uniqKey( 'ToolBarFields' ) } |
| 79 | { ...props } |
| 80 | />, |
| 81 | isSelected && ( |
| 82 | <InspectorControls |
| 83 | key={ uniqKey( 'InspectorControls' ) } |
| 84 | > |
| 85 | <GeneralFields/> |
| 86 | <FieldSettingsWrapper { ...props }> |
| 87 | <SelectControl |
| 88 | key="allowed_user_cap" |
| 89 | label={ __( 'User access' ) } |
| 90 | labelPosition="top" |
| 91 | value={ attributes.allowed_user_cap } |
| 92 | onChange={ ( newValue ) => { |
| 93 | setAttributes( { allowed_user_cap: newValue } ); |
| 94 | } } |
| 95 | options={ userAccess } |
| 96 | /> |
| 97 | { 'any_user' !== attributes.allowed_user_cap && <> |
| 98 | <ToggleControl |
| 99 | key="insert_attachment" |
| 100 | label={ __( 'Insert attachment' ) } |
| 101 | checked={ attributes.insert_attachment } |
| 102 | help={ attrHelp( 'insert_attachment' ) } |
| 103 | onChange={ ( newValue ) => { |
| 104 | setAttributes( { |
| 105 | insert_attachment: Boolean( newValue ), |
| 106 | } ); |
| 107 | } } |
| 108 | /> |
| 109 | { attributes.insert_attachment && <SelectControl |
| 110 | key="value_format" |
| 111 | label={ __( 'Field value' ) } |
| 112 | labelPosition="top" |
| 113 | value={ attributes.value_format } |
| 114 | onChange={ ( newValue ) => { |
| 115 | props.setAttributes( |
| 116 | { value_format: newValue } ); |
| 117 | } } |
| 118 | options={ valueFormats } |
| 119 | /> } |
| 120 | </> } |
| 121 | <AdvancedInspectorControl |
| 122 | value={ attributes.max_files } |
| 123 | label={ __( 'Maximum allowed files to upload', |
| 124 | 'jet-form-builder' ) } |
| 125 | onChangePreset={ max_files => setAttributes( |
| 126 | { max_files } ) } |
| 127 | > |
| 128 | { ( { instanceId } ) => <TextControl |
| 129 | id={ instanceId } |
| 130 | className="jet-fb m-unset" |
| 131 | value={ attributes.max_files } |
| 132 | onChange={ max_files => setAttributes( |
| 133 | { max_files } ) } |
| 134 | /> } |
| 135 | </AdvancedInspectorControl> |
| 136 | <AttributeHelp name="max_files"> |
| 137 | { __( |
| 138 | 'If not set allow to upload 1 file.', |
| 139 | 'jet-form-builder', |
| 140 | ) } |
| 141 | </AttributeHelp> |
| 142 | <AdvancedInspectorControl |
| 143 | value={ attributes.max_size } |
| 144 | label={ __( 'Maximum size in Mb', |
| 145 | 'jet-form-builder' ) } |
| 146 | onChangePreset={ max_size => setAttributes( |
| 147 | { max_size } ) } |
| 148 | > |
| 149 | { ( { instanceId } ) => <TextControl |
| 150 | id={ instanceId } |
| 151 | className="jet-fb m-unset" |
| 152 | value={ attributes.max_size } |
| 153 | onChange={ max_size => setAttributes( |
| 154 | { max_size } ) } |
| 155 | /> } |
| 156 | </AdvancedInspectorControl> |
| 157 | <AttributeHelp name="max_size"/> |
| 158 | <TextControl |
| 159 | label={ __( |
| 160 | 'Maximum file size message', |
| 161 | 'jet-form-builder', |
| 162 | ) } |
| 163 | value={ attributes?.validation?.messages?.max_size ?? |
| 164 | 'Maximum file size: %max_size%' } |
| 165 | onChange={ max_size => { |
| 166 | setAttributes( { |
| 167 | validation: { |
| 168 | messages: { max_size }, |
| 169 | }, |
| 170 | } ); |
| 171 | } } |
| 172 | help={ __( |
| 173 | `Use the %max_size% macro to display |
| 174 | the maximum allowed file size`, |
| 175 | 'jet-form-builder', |
| 176 | ) } |
| 177 | /> |
| 178 | <FormTokenField |
| 179 | key="allowed_mimes" |
| 180 | value={ attributes.allowed_mimes } |
| 181 | label={ __( 'Allow MIME types', 'jet-form-builder' ) } |
| 182 | suggestions={ localizeData.mime_types } |
| 183 | onChange={ allowed_mimes => setAttributes( |
| 184 | { allowed_mimes } ) } |
| 185 | tokenizeOnSpace |
| 186 | /> |
| 187 | </FieldSettingsWrapper> |
| 188 | <PanelBody |
| 189 | title={ __( 'Validation', 'jet-form-builder' ) } |
| 190 | > |
| 191 | <ValidationToggleGroup/> |
| 192 | { isAdvancedValidation && <> |
| 193 | <ValidationBlockMessage name="max_files"/> |
| 194 | <ValidationBlockMessage name="file_max_size"/> |
| 195 | { Boolean( attributes.allowed_mimes.length ) && ( |
| 196 | <ValidationBlockMessage name="file_ext"/> |
| 197 | ) } |
| 198 | </> } |
| 199 | </PanelBody> |
| 200 | <AdvancedFields |
| 201 | key={ uniqKey( 'AdvancedFields' ) } |
| 202 | { ...props } |
| 203 | /> |
| 204 | </InspectorControls> |
| 205 | ), |
| 206 | <div { ...blockProps } key={ uniqKey( 'viewBlock' ) }> |
| 207 | <FieldWrapper |
| 208 | key={ uniqKey( 'FieldWrapper' ) } |
| 209 | { ...props } |
| 210 | > |
| 211 | <InputControl |
| 212 | key={ uniqKey( 'place_holder_block_new' ) } |
| 213 | type={ 'file' } |
| 214 | disabled={ true } |
| 215 | /> |
| 216 | </FieldWrapper> |
| 217 | </div>, |
| 218 | ]; |
| 219 | } |