base-js-sw 1.0.7 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,7 +9,7 @@ import { __ } from '@wordpress/i18n';
9
9
  import { MediaUpload, MediaUploadCheck } from '@wordpress/blockEditor';
10
10
  import { PanelBody, Button } from '@wordpress/components';
11
11
  import { __experimentalAlignmentMatrixControl as AlignmentMatrixControl } from '@wordpress/components';
12
- const { siteDomain } = themeVars; // vars passed from enqueue_backend.php
12
+ const { siteDomain, isBedrock = true } = themeVars;
13
13
  import './ImagePicker.scss';
14
14
 
15
15
  export const ImagePicker = ({
@@ -40,7 +40,12 @@ export const ImagePicker = ({
40
40
  if (imageObject.url.endsWith('.svg')) {
41
41
  resizedImageUrl = imageObject.url;
42
42
  } else {
43
- let imageUrlMinusBase = imageObject.url.replace(siteDomain + '/app/uploads', ''); // remove base and directory
43
+ let pathToReplace = siteDomain + '/app/uploads';
44
+ if (!isBedrock) {
45
+ pathToReplace = siteDomain + '/wp-content/uploads';
46
+ }
47
+
48
+ let imageUrlMinusBase = imageObject.url.replace(pathToReplace, ''); // remove base and directory
44
49
  resizedImageUrl = siteDomain + '/' + imageOptionsString + imageUrlMinusBase;
45
50
  }
46
51
  }
@@ -86,6 +91,7 @@ export const ImagePicker = ({
86
91
  setAttributes={setAttributes}
87
92
  imageRef={imageRef}
88
93
  buttonText={buttonText}
94
+ crop={showCrop ? undefined : false}
89
95
  />
90
96
  )}
91
97
  </div>
@@ -124,7 +130,7 @@ export const ImagePickerPreview = ({
124
130
  height = 500,
125
131
  crop = true,
126
132
  imageRef = 'imageObject',
127
- buttonText = 'Select image'
133
+ buttonText = 'Select image',
128
134
  }) => {
129
135
 
130
136
  let componentClass = 'component-image-picker-preview';
@@ -139,9 +145,13 @@ export const ImagePickerPreview = ({
139
145
  imageElementWrapperClass = blockClass + '__image-wrapper';
140
146
  }
141
147
 
142
- let imagecrop = crop ? '1' : '0';
143
- if (imageObject.crop) {
144
- imagecrop = imageObject.crop.replace(/\s+/g, '-');
148
+ let imagecrop;
149
+ if (crop === false) {
150
+ imagecrop = '0';
151
+ } else if (typeof imageObject.crop === 'string' && imageObject.crop !== '') {
152
+ imagecrop = imageObject.crop === '0' ? '0' : imageObject.crop.replace(/\s+/g, '-');
153
+ } else {
154
+ imagecrop = '1';
145
155
  }
146
156
 
147
157
  let imageOptionsString = `images/width=${width},height=${height},crop=${imagecrop}`;
@@ -156,7 +166,8 @@ export const ImagePickerPreview = ({
156
166
  // Check if the image domain matches the current site's domain
157
167
  if (imageURL.origin === siteURL.origin) {
158
168
  // Same domain: remove base directory
159
- let imageUrlMinusBase = imageObject.url.replace(siteDomain + '/app/uploads', '');
169
+ const baseDir = isBedrock ? '/app/uploads' : '/wp-content/uploads';
170
+ let imageUrlMinusBase = imageObject.url.replace(siteDomain + baseDir, '');
160
171
 
161
172
  if (imageObject.url.endsWith('.svg')) {
162
173
  resizedImageUrl = imageObject.url;
@@ -192,6 +203,7 @@ export const ImagePickerPreview = ({
192
203
  replaceText=''
193
204
  removeText=''
194
205
  imageId={imageObject.id}
206
+ crop={crop}
195
207
  />
196
208
  </MediaUploadCheck>
197
209
  ) : ( // No image
@@ -200,6 +212,7 @@ export const ImagePickerPreview = ({
200
212
  setAttributes={setAttributes}
201
213
  imageRef={imageRef}
202
214
  buttonText={buttonText}
215
+ crop={crop}
203
216
  />
204
217
  )}
205
218
  </div>
@@ -207,11 +220,11 @@ export const ImagePickerPreview = ({
207
220
  };
208
221
 
209
222
  // ============= Image Controls (appear over image within editor)
210
- export const EditImageButtons = ({ componentClass, setAttributes, replaceText, removeText, imageRef, imageId }) => {
223
+ export const EditImageButtons = ({ componentClass, setAttributes, replaceText, removeText, imageRef, imageId, crop }) => {
211
224
  return (
212
225
  <MediaUpload
213
226
  title={'Icon'}
214
- onSelect={(media) => updateImageAttr(setAttributes, media, imageRef)}
227
+ onSelect={(media) => updateImageAttr(setAttributes, media, imageRef, crop)}
215
228
  allowedTypes={['image']}
216
229
  value={imageId}
217
230
  render={({ open }) => (
@@ -230,7 +243,7 @@ export const EditImageButtons = ({ componentClass, setAttributes, replaceText, r
230
243
  className={componentClass + "__edit-button"}
231
244
  isDestructive
232
245
  onClick={(media) => {
233
- updateImageAttr(setAttributes, false, imageRef)
246
+ updateImageAttr(setAttributes, false, imageRef, crop)
234
247
  }}
235
248
  icon="remove"
236
249
  >
@@ -242,10 +255,7 @@ export const EditImageButtons = ({ componentClass, setAttributes, replaceText, r
242
255
  )
243
256
  }
244
257
 
245
- export const UploadImageButton = ({ componentClass, setAttributes, imageRef, buttonText }) => {
246
-
247
-
248
-
258
+ export const UploadImageButton = ({ componentClass, setAttributes, imageRef, buttonText, crop }) => {
249
259
  return (
250
260
  <MediaUploadCheck>
251
261
  <MediaUpload
@@ -281,20 +291,25 @@ const updateImageAttr = (
281
291
  setAttributes,
282
292
  media = false,
283
293
  imageRef,
294
+ crop = 'center center'
284
295
  ) => {
285
-
286
296
  imageRef ??= 'imageObject';
287
297
 
288
- let newImageUrl = media ? media.sizes.full.url : ''
298
+ let newImageUrl = media ? (media.sizes?.full?.url || media.url) : '';
299
+
300
+ let newImage = {
301
+ id: media ? media.id : 0,
302
+ url: newImageUrl,
303
+ alt: media ? media.alt : '',
304
+ };
289
305
 
306
+ if (crop === false) {
307
+ newImage.crop = '0';
308
+ } else {
309
+ newImage.crop = typeof crop === 'string' ? crop : 'center center';
310
+ }
290
311
 
291
312
  setAttributes({
292
- [imageRef]: {
293
- id: media.id,
294
- url: newImageUrl,
295
- alt: media.alt,
296
- crop: 'center center'
297
- },
313
+ [imageRef]: newImage,
298
314
  });
299
315
  };
300
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "base-js-sw",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Reusable Gutenberg block components for WordPress projects",
5
5
  "main": "index.js",
6
6
  "author": "Shape Works",