-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathindex.js
More file actions
231 lines (210 loc) · 6.5 KB
/
index.js
File metadata and controls
231 lines (210 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Internal dependencies
*/
import SVGUploadIcon from './images/upload.svg'
import AdvancedControl, { extractControlProps } from '../base-control2'
import DynamicContentControl, { useDynamicContentControlProps } from '../dynamic-content-control'
import { ResetButton } from '../base-control2/reset-button'
import Button from '../button'
/**
* External dependencies
*/
import classnames from 'classnames'
import {
i18n, cimo, isPro,
} from 'stackable'
import {
useAttributeName, useBlockAttributesContext, useBlockSetAttributesContext,
} from '~stackable/hooks'
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n'
import {
Fragment, memo, useEffect, useState,
} from '@wordpress/element'
import { MediaUpload } from '@wordpress/block-editor'
import { currentUserHasCapability } from '~stackable/util'
const ImageControl = memo( props => {
const attrNameId = useAttributeName( `${ props.attribute }Id`, props.responsive, props.hover )
const attrNameUrl = useAttributeName( `${ props.attribute }Url`, props.responsive, props.hover )
const attrWidthAttribute = useAttributeName( `${ props.attribute }HeightAttribute`, props.responsive, props.hover )
const attrHeightAttribute = useAttributeName( `${ props.attribute }WidthAttribute`, props.responsive, props.hover )
const attrAlt = useAttributeName( `${ props.attribute }Alt`, props.responsive, props.hover )
const attributes = useBlockAttributesContext( attributes => {
return {
[ attrNameId ]: attributes[ attrNameId ],
[ attrNameUrl ]: attributes[ attrNameUrl ],
}
} )
const setAttributes = useBlockSetAttributesContext()
const _onChange = image => {
setAttributes( {
[ attrNameId ]: image.id,
[ attrNameUrl ]: image.url,
[ attrWidthAttribute ]: image.width || '',
[ attrHeightAttribute ]: image.height || '',
[ attrAlt ]: image.alt || '',
} )
}
const onChange = typeof props.onChange !== 'undefined' ? props.onChange : _onChange
const [ _propsToPass, controlProps ] = extractControlProps( props )
const onChangeReset = url => {
return onChange( {
url,
id: '',
width: '',
height: '',
alt: '',
} )
}
const dynamicContentProps = useDynamicContentControlProps( {
onChange: onChangeReset,
value: attributes[ attrNameUrl ],
} )
const imageId = typeof props.imageId !== 'undefined' ? props.imageId : attributes[ attrNameId ]
const imageUrl = typeof props.imageURL !== 'undefined' ? props.imageURL : dynamicContentProps.value || attributes[ attrNameUrl ]
const type = imageUrl && imageUrl.match( /(mp4|webm|ogg)$/i ) ? 'video' : 'image'
const onRemove = () => {
onChange( {
url: '',
id: '',
height: '',
width: '',
alt: '',
} )
}
const [ CimoDownloadNotice, setCimoDownloadNotice ] = useState( null )
useEffect( () => {
// Skip displaying the Cimo notice if the plugin is already activated or the user has chosen to hide the notice
if ( isPro || ! cimo || cimo.hideNotice || cimo.status === 'activated' ) {
return
}
const userCanInstall = currentUserHasCapability( 'install_plugins' )
const userCanActivate = currentUserHasCapability( 'activate_plugins' )
// Show the Cimo notice only if the user has permissions to install or activate plugins
if ( ( cimo.status === 'not_installed' && userCanInstall ) || ( cimo.status === 'installed' && userCanActivate ) ) {
const loadNotice = async () => {
try {
// Import the Cimo notice component with explicit chunk naming
const { default: CimoNoticeComponent } = await import(
/* webpackChunkName: "cimo-download-notice" */
/* webpackMode: "lazy" */
'../../lazy-components/cimo'
)
setCimoDownloadNotice( () => CimoNoticeComponent )
} catch ( err ) {
// eslint-disable-next-line no-console
console.error( 'Failed to load Cimo download notice component:', err )
}
}
loadNotice()
}
}, [] )
return ( <>
<AdvancedControl
{ ...controlProps }
valueCheckAttribute={ props.attribute + 'Url' }
className={ classnames( 'ugb-image-control', props.className ) }
>
{ imageUrl &&
<MediaUpload
onSelect={ onChange }
allowedTypes={ props.allowedTypes }
value={ imageId }
render={ obj => {
return (
<Fragment>
<div className="ugb-image-preview-wrapper">
{ type === 'video' && (
<video
className="ugb-image-preview"
autoPlay
muted
loop
src={ imageUrl }
onClick={ obj.open }
onKeyDown={ event => {
if ( event.keyCode === 13 ) {
obj.open()
}
} }
/>
) }
{ type === 'image' && (
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
<img
className="ugb-image-preview"
draggable="false"
src={ imageUrl }
onClick={ obj.open }
onKeyDown={ event => {
if ( event.keyCode === 13 ) {
obj.open()
}
} }
alt={ __( 'preview', i18n ) }
/>
) }
</div>
</Fragment>
)
} }
/>
}
<DynamicContentControl
enable={ props.isDynamic }
hasPanelModifiedIndicator={ props.hasPanelModifiedIndicator }
type="image-url"
{ ...dynamicContentProps }
>
<MediaUpload
onSelect={ onChange }
allowedTypes={ props.allowedTypes }
value={ imageId }
render={ obj => {
return (
<Fragment>
<Button
className="ugb-image-upload"
onClick={ obj.open }
icon={ <SVGUploadIcon viewBox="0 0 20 20" /> }
isSecondary
onKeyDown={ event => {
if ( event.keyCode === 13 ) {
obj.open()
}
} }
>
<span className="ugb-image-upload__label">{ ! imageUrl ? __( 'Upload', i18n ) : __( 'Replace', i18n ) } </span>
</Button>
</Fragment>
)
} }
/>
</DynamicContentControl>
<ResetButton
allowReset={ props.allowReset && ! props.dynamic }
value={ imageUrl }
default={ props.default }
onChange={ onRemove }
hasPanelModifiedIndicator={ props.hasPanelModifiedIndicator }
/>
</AdvancedControl>
{ CimoDownloadNotice && <CimoDownloadNotice onDismiss={ () => setCimoDownloadNotice( null ) } /> }
</>
)
} )
ImageControl.defaultProps = {
label: '',
attribute: '',
allowedTypes: [ 'image' ],
responsive: false,
hover: false,
isDynamic: true,
value: undefined,
onChange: undefined,
allowReset: true,
hasPanelModifiedIndicator: true,
}
export default ImageControl