-
Notifications
You must be signed in to change notification settings - Fork 20
/
GalleryEditor.js
207 lines (177 loc) · 6.75 KB
/
GalleryEditor.js
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
import { Fragment } from "@wordpress/element";
import { FocalPointPicker, PanelBody } from '@wordpress/components';
import { getGalleryLayout, GALLERY_BLOCK_CLASSES } from './getGalleryLayout';
import { useSelect } from '@wordpress/data';
import { GalleryCarousel } from './GalleryCarousel';
import { GalleryThreeColumns } from './GalleryThreeColumns';
import { GalleryGrid } from './GalleryGrid';
import { useGalleryImages } from './useGalleryImages';
import { InspectorControls } from '@wordpress/block-editor';
import { MediaPlaceholder, MediaUploadCheck } from "@wordpress/blockEditor";
const { RichText } = wp.blockEditor;
const { __ } = wp.i18n;
const renderEdit = (attributes, setAttributes) => {
const { image_data, className, gallery_block_style } = attributes;
const layout = getGalleryLayout(className, gallery_block_style);
const dimensions = { width: 400, height: 100 };
const hasImages = !!image_data.length;
const onSelectImage = value => {
let image_ids = [];
let image_data = [];
for (const key in value) {
image_ids.push(value[key].id);
let img_id = value[key].id;
image_data.push({ url: value[key].url, focalPoint: { x: 0.5, y: 0.5 }, id: img_id });
}
setAttributes({ multiple_image: image_ids.join(',') });
setAttributes({ image_data });
}
const onFocalPointChange = (image_id, value) => {
let updated_image_data = [];
let gallery_block_focus_points = {};
image_data.map(function (object) {
if (object.id === image_id) {
let x = parseFloat(value.x).toFixed(2);
let y = parseFloat(value.y).toFixed(2);
updated_image_data.push({ url: object.url, focalPoint: { x, y }, id: image_id });
gallery_block_focus_points[image_id] = `${x * 100}% ${y * 100}%`;
} else {
updated_image_data.push(object);
let img_id = object.id;
gallery_block_focus_points[img_id] = `${parseInt(object.focalPoint.x * 100)}% ${parseInt(object.focalPoint.y * 100)}%`;
}
});
setAttributes({ gallery_block_focus_points: JSON.stringify(gallery_block_focus_points) });
setAttributes({ image_data: updated_image_data });
}
const focalPointImages = layout === 'three-columns' ? image_data.slice(0, 3) : image_data;
return (
<Fragment>
<MediaUploadCheck>
<MediaPlaceholder
addToGallery={hasImages}
labels={{
title: __('Select Gallery Images', 'planet4-blocks-backend'),
instructions: __('Upload an image or select from the media library.', 'planet4-blocks-backend'),
}}
onSelect={onSelectImage}
accept="image/*"
allowedTypes={["image"]}
multiple
value={hasImages ? image_data : undefined}
/>
</MediaUploadCheck>
{hasImages && (
<InspectorControls>
<PanelBody title={__('Settings', 'planet4-blocks-backend')}>
<div className="wp-block-master-theme-gallery__FocalPointPicker">
<p>{__('Select gallery image focal point', 'planet4-blocks-backend')}</p>
<ul>
{focalPointImages.map((item, index) => (
<li key={index}>
<FocalPointPicker
url={item.url}
dimensions={dimensions}
value={item.focalPoint}
onChange={onFocalPointChange.bind(this, item.id)}
key={item.id}
/>
</li>
))}
</ul>
</div>
</PanelBody>
</InspectorControls>
)}
</Fragment >
);
}
const renderView = (attributes, setAttributes) => {
const {
gallery_block_title,
gallery_block_description,
gallery_block_style,
className,
multiple_image,
gallery_block_focus_points
} = attributes;
const layout = getGalleryLayout(className, gallery_block_style);
const toAttribute = attributeName => value => setAttributes({ [attributeName]: value });
const { postType } = useSelect(select => ({
postType: select('core/editor').getCurrentPostType()
}), []);
const { images } = useGalleryImages({ multiple_image, gallery_block_focus_points }, layout);
return (
<section className={`block ${GALLERY_BLOCK_CLASSES[layout]} ${className ?? ''}`}>
<header className="articles-title-container">
<RichText
tagName="h2"
className="page-section-header"
placeholder={__('Enter title', 'planet4-blocks-backend')}
value={gallery_block_title}
onChange={toAttribute('gallery_block_title')}
withoutInteractiveFormatting
multiline="false"
allowedFormats={[]}
/>
</header>
<RichText
tagName="p"
className="page-section-description"
placeholder={__('Enter description', 'planet4-blocks-backend')}
value={gallery_block_description}
onChange={toAttribute('gallery_block_description')}
withoutInteractiveFormatting
allowedFormats={['core/bold', 'core/italic']}
/>
{layout === 'slider' && <GalleryCarousel images={images || []} isEditing />}
{layout === 'three-columns' && <GalleryThreeColumns images={images || []} postType={postType} />}
{layout === 'grid' && <GalleryGrid images={images || []} />}
</section>
);
}
export const GalleryEditor = ({ isSelected, attributes, setAttributes }) => {
const { multiple_image } = attributes;
const { image_urls_array } = useSelect(select => {
let image_urls_array = [];
if (multiple_image) {
let image_id_array = multiple_image.split(',');
image_id_array.forEach(img_id => {
let img_details = select('core').getMedia(img_id);
if (img_details) {
image_urls_array[img_id] = img_details.source_url;
}
});
}
return { image_urls_array };
}, []);
let { image_data, gallery_block_focus_points } = attributes;
// Prepare image_data array on edit gallery block.
if (0 === image_data.length && 0 < image_urls_array.length) {
let new_image_data = [];
let focal_points_json = gallery_block_focus_points ? JSON.parse(gallery_block_focus_points) : {};
image_urls_array.forEach(img_id => {
let x, y;
if (!focal_points_json[img_id]) {
[x, y] = [50, 50];
} else {
[x, y] = focal_points_json[img_id].replace(/\%/g, '').split(' ');
}
new_image_data.push({
url: image_urls_array[img_id],
focalPoint: {
x: parseInt(x) / 100,
y: parseInt(y) / 100
},
id: img_id
});
});
setAttributes({ image_data: new_image_data });
}
return (
<Fragment>
{isSelected && renderEdit(attributes, setAttributes)}
{renderView(attributes, setAttributes)}
</Fragment>
);
}