1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.backgroundImageStyles = backgroundImageStyles;
|
9 | exports.dimRatioToClass = dimRatioToClass;
|
10 | exports.default = exports.VIDEO_BACKGROUND_TYPE = exports.IMAGE_BACKGROUND_TYPE = void 0;
|
11 |
|
12 | var _element = require("@wordpress/element");
|
13 |
|
14 | var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));
|
15 |
|
16 | var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
17 |
|
18 | var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
19 |
|
20 | var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
|
21 |
|
22 | var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
|
23 |
|
24 | var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
25 |
|
26 | var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
|
27 |
|
28 | var _classnames = _interopRequireDefault(require("classnames"));
|
29 |
|
30 | var _fastAverageColor = _interopRequireDefault(require("fast-average-color"));
|
31 |
|
32 | var _tinycolor = _interopRequireDefault(require("tinycolor2"));
|
33 |
|
34 | var _components = require("@wordpress/components");
|
35 |
|
36 | var _compose = require("@wordpress/compose");
|
37 |
|
38 | var _editor = require("@wordpress/editor");
|
39 |
|
40 | var _i18n = require("@wordpress/i18n");
|
41 |
|
42 | var _icon = _interopRequireDefault(require("./icon"));
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | var IMAGE_BACKGROUND_TYPE = 'image';
|
60 | exports.IMAGE_BACKGROUND_TYPE = IMAGE_BACKGROUND_TYPE;
|
61 | var VIDEO_BACKGROUND_TYPE = 'video';
|
62 | exports.VIDEO_BACKGROUND_TYPE = VIDEO_BACKGROUND_TYPE;
|
63 | var ALLOWED_MEDIA_TYPES = ['image', 'video'];
|
64 | var INNER_BLOCKS_TEMPLATE = [['core/paragraph', {
|
65 | align: 'center',
|
66 | fontSize: 'large',
|
67 | placeholder: (0, _i18n.__)('Write title…')
|
68 | }]];
|
69 | var INNER_BLOCKS_ALLOWED_BLOCKS = ['core/button', 'core/heading', 'core/paragraph'];
|
70 |
|
71 | function retrieveFastAverageColor() {
|
72 | if (!retrieveFastAverageColor.fastAverageColor) {
|
73 | retrieveFastAverageColor.fastAverageColor = new _fastAverageColor.default();
|
74 | }
|
75 |
|
76 | return retrieveFastAverageColor.fastAverageColor;
|
77 | }
|
78 |
|
79 | function backgroundImageStyles(url) {
|
80 | return url ? {
|
81 | backgroundImage: "url(".concat(url, ")")
|
82 | } : {};
|
83 | }
|
84 |
|
85 | function dimRatioToClass(ratio) {
|
86 | return ratio === 0 || ratio === 50 ? null : 'has-background-dim-' + 10 * Math.round(ratio / 10);
|
87 | }
|
88 |
|
89 | var CoverEdit =
|
90 |
|
91 | function (_Component) {
|
92 | (0, _inherits2.default)(CoverEdit, _Component);
|
93 |
|
94 | function CoverEdit() {
|
95 | var _this;
|
96 |
|
97 | (0, _classCallCheck2.default)(this, CoverEdit);
|
98 | _this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(CoverEdit).apply(this, arguments));
|
99 | _this.state = {
|
100 | isDark: false
|
101 | };
|
102 | _this.imageRef = (0, _element.createRef)();
|
103 | _this.videoRef = (0, _element.createRef)();
|
104 | _this.changeIsDarkIfRequired = _this.changeIsDarkIfRequired.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
|
105 | return _this;
|
106 | }
|
107 |
|
108 | (0, _createClass2.default)(CoverEdit, [{
|
109 | key: "componentDidMount",
|
110 | value: function componentDidMount() {
|
111 | this.handleBackgroundMode();
|
112 | }
|
113 | }, {
|
114 | key: "componentDidUpdate",
|
115 | value: function componentDidUpdate(prevProps) {
|
116 | this.handleBackgroundMode(prevProps);
|
117 | }
|
118 | }, {
|
119 | key: "render",
|
120 | value: function render() {
|
121 | var _this$props = this.props,
|
122 | attributes = _this$props.attributes,
|
123 | setAttributes = _this$props.setAttributes,
|
124 | className = _this$props.className,
|
125 | noticeOperations = _this$props.noticeOperations,
|
126 | noticeUI = _this$props.noticeUI,
|
127 | overlayColor = _this$props.overlayColor,
|
128 | setOverlayColor = _this$props.setOverlayColor;
|
129 | var backgroundType = attributes.backgroundType,
|
130 | dimRatio = attributes.dimRatio,
|
131 | focalPoint = attributes.focalPoint,
|
132 | hasParallax = attributes.hasParallax,
|
133 | id = attributes.id,
|
134 | url = attributes.url;
|
135 |
|
136 | var onSelectMedia = function onSelectMedia(media) {
|
137 | if (!media || !media.url) {
|
138 | setAttributes({
|
139 | url: undefined,
|
140 | id: undefined
|
141 | });
|
142 | return;
|
143 | }
|
144 |
|
145 | var mediaType;
|
146 |
|
147 | if (media.media_type) {
|
148 | if (media.media_type === IMAGE_BACKGROUND_TYPE) {
|
149 | mediaType = IMAGE_BACKGROUND_TYPE;
|
150 | } else {
|
151 |
|
152 |
|
153 | mediaType = VIDEO_BACKGROUND_TYPE;
|
154 | }
|
155 | } else {
|
156 |
|
157 | if (media.type !== IMAGE_BACKGROUND_TYPE && media.type !== VIDEO_BACKGROUND_TYPE) {
|
158 | return;
|
159 | }
|
160 |
|
161 | mediaType = media.type;
|
162 | }
|
163 |
|
164 | setAttributes({
|
165 | url: media.url,
|
166 | id: media.id,
|
167 | backgroundType: mediaType
|
168 | });
|
169 | };
|
170 |
|
171 | var toggleParallax = function toggleParallax() {
|
172 | return setAttributes({
|
173 | hasParallax: !hasParallax
|
174 | });
|
175 | };
|
176 |
|
177 | var setDimRatio = function setDimRatio(ratio) {
|
178 | return setAttributes({
|
179 | dimRatio: ratio
|
180 | });
|
181 | };
|
182 |
|
183 | var style = (0, _objectSpread2.default)({}, backgroundType === IMAGE_BACKGROUND_TYPE ? backgroundImageStyles(url) : {}, {
|
184 | backgroundColor: overlayColor.color
|
185 | });
|
186 |
|
187 | if (focalPoint) {
|
188 | style.backgroundPosition = "".concat(focalPoint.x * 100, "% ").concat(focalPoint.y * 100, "%");
|
189 | }
|
190 |
|
191 | var controls = (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)(_editor.BlockControls, null, !!url && (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)(_editor.MediaUploadCheck, null, (0, _element.createElement)(_components.Toolbar, null, (0, _element.createElement)(_editor.MediaUpload, {
|
192 | onSelect: onSelectMedia,
|
193 | allowedTypes: ALLOWED_MEDIA_TYPES,
|
194 | value: id,
|
195 | render: function render(_ref) {
|
196 | var open = _ref.open;
|
197 | return (0, _element.createElement)(_components.IconButton, {
|
198 | className: "components-toolbar__control",
|
199 | label: (0, _i18n.__)('Edit media'),
|
200 | icon: "edit",
|
201 | onClick: open
|
202 | });
|
203 | }
|
204 | }))))), !!url && (0, _element.createElement)(_editor.InspectorControls, null, (0, _element.createElement)(_components.PanelBody, {
|
205 | title: (0, _i18n.__)('Cover Settings')
|
206 | }, IMAGE_BACKGROUND_TYPE === backgroundType && (0, _element.createElement)(_components.ToggleControl, {
|
207 | label: (0, _i18n.__)('Fixed Background'),
|
208 | checked: hasParallax,
|
209 | onChange: toggleParallax
|
210 | }), IMAGE_BACKGROUND_TYPE === backgroundType && !hasParallax && (0, _element.createElement)(_components.FocalPointPicker, {
|
211 | label: (0, _i18n.__)('Focal Point Picker'),
|
212 | url: url,
|
213 | value: focalPoint,
|
214 | onChange: function onChange(value) {
|
215 | return setAttributes({
|
216 | focalPoint: value
|
217 | });
|
218 | }
|
219 | }), (0, _element.createElement)(_editor.PanelColorSettings, {
|
220 | title: (0, _i18n.__)('Overlay'),
|
221 | initialOpen: true,
|
222 | colorSettings: [{
|
223 | value: overlayColor.color,
|
224 | onChange: setOverlayColor,
|
225 | label: (0, _i18n.__)('Overlay Color')
|
226 | }]
|
227 | }, (0, _element.createElement)(_components.RangeControl, {
|
228 | label: (0, _i18n.__)('Background Opacity'),
|
229 | value: dimRatio,
|
230 | onChange: setDimRatio,
|
231 | min: 0,
|
232 | max: 100,
|
233 | step: 10,
|
234 | required: true
|
235 | })))));
|
236 |
|
237 | if (!url) {
|
238 | var placeholderIcon = (0, _element.createElement)(_editor.BlockIcon, {
|
239 | icon: _icon.default
|
240 | });
|
241 | var label = (0, _i18n.__)('Cover');
|
242 | return (0, _element.createElement)(_element.Fragment, null, controls, (0, _element.createElement)(_editor.MediaPlaceholder, {
|
243 | icon: placeholderIcon,
|
244 | className: className,
|
245 | labels: {
|
246 | title: label,
|
247 | instructions: (0, _i18n.__)('Drag an image or a video, upload a new one or select a file from your library.')
|
248 | },
|
249 | onSelect: onSelectMedia,
|
250 | accept: "image/*,video/*",
|
251 | allowedTypes: ALLOWED_MEDIA_TYPES,
|
252 | notices: noticeUI,
|
253 | onError: noticeOperations.createErrorNotice
|
254 | }));
|
255 | }
|
256 |
|
257 | var classes = (0, _classnames.default)(className, dimRatioToClass(dimRatio), {
|
258 | 'is-dark-theme': this.state.isDark,
|
259 | 'has-background-dim': dimRatio !== 0,
|
260 | 'has-parallax': hasParallax
|
261 | });
|
262 | return (0, _element.createElement)(_element.Fragment, null, controls, (0, _element.createElement)("div", {
|
263 | "data-url": url,
|
264 | style: style,
|
265 | className: classes
|
266 | }, IMAGE_BACKGROUND_TYPE === backgroundType &&
|
267 | (0, _element.createElement)("img", {
|
268 | ref: this.imageRef,
|
269 | "aria-hidden": true,
|
270 | alt: "",
|
271 | style: {
|
272 | display: 'none'
|
273 | },
|
274 | src: url
|
275 | }), VIDEO_BACKGROUND_TYPE === backgroundType && (0, _element.createElement)("video", {
|
276 | ref: this.videoRef,
|
277 | className: "wp-block-cover__video-background",
|
278 | autoPlay: true,
|
279 | muted: true,
|
280 | loop: true,
|
281 | src: url
|
282 | }), (0, _element.createElement)("div", {
|
283 | className: "wp-block-cover__inner-container"
|
284 | }, (0, _element.createElement)(_editor.InnerBlocks, {
|
285 | template: INNER_BLOCKS_TEMPLATE,
|
286 | allowedBlocks: INNER_BLOCKS_ALLOWED_BLOCKS
|
287 | }))));
|
288 | }
|
289 | }, {
|
290 | key: "handleBackgroundMode",
|
291 | value: function handleBackgroundMode(prevProps) {
|
292 | var _this2 = this;
|
293 |
|
294 | var _this$props2 = this.props,
|
295 | attributes = _this$props2.attributes,
|
296 | overlayColor = _this$props2.overlayColor;
|
297 | var dimRatio = attributes.dimRatio,
|
298 | url = attributes.url;
|
299 |
|
300 |
|
301 | if (dimRatio > 50) {
|
302 | if (prevProps && prevProps.attributes.dimRatio > 50 && prevProps.overlayColor.color === overlayColor.color) {
|
303 |
|
304 | return;
|
305 | }
|
306 |
|
307 | if (!overlayColor.color) {
|
308 |
|
309 | this.changeIsDarkIfRequired(true);
|
310 | return;
|
311 | }
|
312 |
|
313 | this.changeIsDarkIfRequired((0, _tinycolor.default)(overlayColor.color).isDark());
|
314 | return;
|
315 | }
|
316 |
|
317 |
|
318 |
|
319 | if (prevProps && prevProps.attributes.dimRatio <= 50 && prevProps.attributes.url === url) {
|
320 |
|
321 | return;
|
322 | }
|
323 |
|
324 | var backgroundType = attributes.backgroundType;
|
325 | var element;
|
326 |
|
327 | switch (backgroundType) {
|
328 | case IMAGE_BACKGROUND_TYPE:
|
329 | element = this.imageRef.current;
|
330 | break;
|
331 |
|
332 | case VIDEO_BACKGROUND_TYPE:
|
333 | element = this.videoRef.current;
|
334 | break;
|
335 | }
|
336 |
|
337 | if (!element) {
|
338 | return;
|
339 | }
|
340 |
|
341 | retrieveFastAverageColor().getColorAsync(element, function (color) {
|
342 | _this2.changeIsDarkIfRequired(color.isDark);
|
343 | });
|
344 | }
|
345 | }, {
|
346 | key: "changeIsDarkIfRequired",
|
347 | value: function changeIsDarkIfRequired(newIsDark) {
|
348 | if (this.state.isDark !== newIsDark) {
|
349 | this.setState({
|
350 | isDark: newIsDark
|
351 | });
|
352 | }
|
353 | }
|
354 | }]);
|
355 | return CoverEdit;
|
356 | }(_element.Component);
|
357 |
|
358 | var _default = (0, _compose.compose)([(0, _editor.withColors)({
|
359 | overlayColor: 'background-color'
|
360 | }), _components.withNotices])(CoverEdit);
|
361 |
|
362 | exports.default = _default;
|
363 |
|
\ | No newline at end of file |