deprecated
2 months ago
block.json
1 month ago
edit.js
2 months ago
icon.svg
2 months ago
index.js
2 months ago
index.php
2 months ago
mediaUpload.js
2 months ago
save.js
2 weeks ago
style.scss
2 months ago
utils.js
2 months ago
mediaUpload.js
147 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { Button } from '@wordpress/components'; |
| 3 | import { fixBrokenUnicode } from '@vkblocks/utils/fixBrokenUnicode'; |
| 4 | import { MediaUpload } from '@wordpress/block-editor'; |
| 5 | |
| 6 | // 過去に保存された画像か確認 |
| 7 | export const isDeprecatedImage = (image) => { |
| 8 | return image && image.indexOf('{') === -1; |
| 9 | }; |
| 10 | |
| 11 | // 画像の枠線設定を取得 |
| 12 | export const getImageBorder = (ImageBorderColor) => { |
| 13 | let imageBorderSettings = 'none'; |
| 14 | //borderColorが指定されなかった場合はボーダーを非表示に |
| 15 | if (ImageBorderColor) { |
| 16 | imageBorderSettings = `1px solid ${ImageBorderColor}`; |
| 17 | } |
| 18 | return imageBorderSettings; |
| 19 | }; |
| 20 | |
| 21 | export const PrContentMediaUploadEdit = ({ |
| 22 | ImageBorderColor, |
| 23 | setAttributes, |
| 24 | Image, |
| 25 | }) => { |
| 26 | /* eslint no-unused-vars: 0 */ |
| 27 | const imageBorderSettings = getImageBorder(ImageBorderColor); |
| 28 | const prContentDatas = {}; |
| 29 | |
| 30 | //画像のURL保存 |
| 31 | const setImageURL = (value) => |
| 32 | setAttributes({ Image: value.sizes.full.url }); |
| 33 | |
| 34 | //画像のJSONオブジェクト保存 |
| 35 | const setImageJSON = (value) => { |
| 36 | if (value) { |
| 37 | // JSONデータから抜き出し |
| 38 | const imageData = { |
| 39 | alt: value.alt, |
| 40 | sizes: { |
| 41 | full: { |
| 42 | url: value.sizes.full.url, |
| 43 | }, |
| 44 | }, |
| 45 | }; |
| 46 | |
| 47 | setAttributes({ Image: JSON.stringify(imageData) }); |
| 48 | } |
| 49 | }; |
| 50 | const getImagePlaceHolderDeprecated = (image, borderSettings) => { |
| 51 | if (!image) { |
| 52 | return __('Select image', 'vk-blocks'); |
| 53 | } |
| 54 | return ( |
| 55 | <img |
| 56 | className={'vk_prContent_colImg_image'} |
| 57 | src={image} |
| 58 | alt={__('Upload image', 'vk-blocks')} |
| 59 | style={{ border: borderSettings }} |
| 60 | /> |
| 61 | ); |
| 62 | }; |
| 63 | |
| 64 | const getImagePlaceHolder = (image, borderSettings) => { |
| 65 | image = JSON.parse(fixBrokenUnicode(image)); |
| 66 | |
| 67 | if (image === null || typeof image.sizes === 'undefined') { |
| 68 | return __('Select image', 'vk-blocks'); |
| 69 | } |
| 70 | return ( |
| 71 | <img |
| 72 | className={'vk_prContent_colImg_image'} |
| 73 | src={image.sizes.full.url} |
| 74 | alt={image.alt} |
| 75 | style={{ border: borderSettings }} |
| 76 | /> |
| 77 | ); |
| 78 | }; |
| 79 | |
| 80 | //バージョンによって画像の保存方式変更 |
| 81 | if (isDeprecatedImage(Image)) { |
| 82 | prContentDatas.setImage = setImageURL; |
| 83 | prContentDatas.value = Image; |
| 84 | prContentDatas.alt = __('Upload image', 'vk-blocks'); |
| 85 | prContentDatas.getImagePlaceHolder = getImagePlaceHolderDeprecated; |
| 86 | } else { |
| 87 | prContentDatas.setImage = setImageJSON; |
| 88 | prContentDatas.value = JSON.parse(fixBrokenUnicode(Image)); |
| 89 | prContentDatas.alt = prContentDatas.value.alt; |
| 90 | prContentDatas.getImagePlaceHolder = getImagePlaceHolder; |
| 91 | } |
| 92 | |
| 93 | const isImageUpload = |
| 94 | Image !== null && Image !== undefined && Image !== '' && Image !== '{}'; |
| 95 | |
| 96 | return ( |
| 97 | <MediaUpload |
| 98 | onSelect={prContentDatas.setImage} |
| 99 | type="image" |
| 100 | value={prContentDatas.value} |
| 101 | render={({ open }) => ( |
| 102 | <Button |
| 103 | onClick={open} |
| 104 | className={ |
| 105 | isImageUpload ? 'image-button' : 'button button-large' |
| 106 | } |
| 107 | > |
| 108 | {prContentDatas.getImagePlaceHolder( |
| 109 | Image, |
| 110 | imageBorderSettings |
| 111 | )} |
| 112 | </Button> |
| 113 | )} |
| 114 | /> |
| 115 | ); |
| 116 | }; |
| 117 | |
| 118 | export const PrContentMediaUpload = ({ Image, ImageBorderColor }) => { |
| 119 | if (!Image) { |
| 120 | return __('Select Image', 'vk-blocks'); |
| 121 | } |
| 122 | |
| 123 | const imageBorderSettings = getImageBorder(ImageBorderColor); |
| 124 | |
| 125 | if (Image && Image.indexOf('{') === -1) { |
| 126 | return ( |
| 127 | <img |
| 128 | className={'vk_prContent_colImg_image'} |
| 129 | src={Image} |
| 130 | alt={__('Upload image', 'vk-blocks')} |
| 131 | style={{ border: imageBorderSettings }} |
| 132 | /> |
| 133 | ); |
| 134 | } |
| 135 | const imageParse = JSON.parse(fixBrokenUnicode(Image)); |
| 136 | if (imageParse && typeof imageParse.sizes !== 'undefined') { |
| 137 | return ( |
| 138 | <img |
| 139 | className={'vk_prContent_colImg_image'} |
| 140 | src={imageParse.sizes.full.url} |
| 141 | alt={imageParse.alt} |
| 142 | style={{ border: imageBorderSettings }} |
| 143 | /> |
| 144 | ); |
| 145 | } |
| 146 | }; |
| 147 |