UNPKG

@wordpress/block-library

Version:
195 lines (176 loc) 6.12 kB
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/esm/inherits"; import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized"; import { createElement } from "@wordpress/element"; /** * External dependencies */ import classnames from 'classnames'; /** * WordPress dependencies */ import { Component, Fragment } from '@wordpress/element'; import { IconButton, Spinner } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { BACKSPACE, DELETE } from '@wordpress/keycodes'; import { withSelect } from '@wordpress/data'; import { RichText } from '@wordpress/block-editor'; import { isBlobURL } from '@wordpress/blob'; var GalleryImage = /*#__PURE__*/ function (_Component) { _inherits(GalleryImage, _Component); function GalleryImage() { var _this; _classCallCheck(this, GalleryImage); _this = _possibleConstructorReturn(this, _getPrototypeOf(GalleryImage).apply(this, arguments)); _this.onSelectImage = _this.onSelectImage.bind(_assertThisInitialized(_assertThisInitialized(_this))); _this.onSelectCaption = _this.onSelectCaption.bind(_assertThisInitialized(_assertThisInitialized(_this))); _this.onRemoveImage = _this.onRemoveImage.bind(_assertThisInitialized(_assertThisInitialized(_this))); _this.bindContainer = _this.bindContainer.bind(_assertThisInitialized(_assertThisInitialized(_this))); _this.state = { captionSelected: false }; return _this; } _createClass(GalleryImage, [{ key: "bindContainer", value: function bindContainer(ref) { this.container = ref; } }, { key: "onSelectCaption", value: function onSelectCaption() { if (!this.state.captionSelected) { this.setState({ captionSelected: true }); } if (!this.props.isSelected) { this.props.onSelect(); } } }, { key: "onSelectImage", value: function onSelectImage() { if (!this.props.isSelected) { this.props.onSelect(); } if (this.state.captionSelected) { this.setState({ captionSelected: false }); } } }, { key: "onRemoveImage", value: function onRemoveImage(event) { if (this.container === document.activeElement && this.props.isSelected && [BACKSPACE, DELETE].indexOf(event.keyCode) !== -1) { event.stopPropagation(); event.preventDefault(); this.props.onRemove(); } } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var _this$props = this.props, isSelected = _this$props.isSelected, image = _this$props.image, url = _this$props.url; if (image && !url) { this.props.setAttributes({ url: image.source_url, alt: image.alt_text }); } // unselect the caption so when the user selects other image and comeback // the caption is not immediately selected if (this.state.captionSelected && !isSelected && prevProps.isSelected) { this.setState({ captionSelected: false }); } } }, { key: "render", value: function render() { var _this$props2 = this.props, url = _this$props2.url, alt = _this$props2.alt, id = _this$props2.id, linkTo = _this$props2.linkTo, link = _this$props2.link, isSelected = _this$props2.isSelected, caption = _this$props2.caption, onRemove = _this$props2.onRemove, setAttributes = _this$props2.setAttributes, ariaLabel = _this$props2['aria-label']; var href; switch (linkTo) { case 'media': href = url; break; case 'attachment': href = link; break; } var img = // Disable reason: Image itself is not meant to be interactive, but should // direct image selection and unfocus caption fields. /* eslint-disable jsx-a11y/no-noninteractive-element-interactions */ createElement(Fragment, null, createElement("img", { src: url, alt: alt, "data-id": id, onClick: this.onSelectImage, onFocus: this.onSelectImage, onKeyDown: this.onRemoveImage, tabIndex: "0", "aria-label": ariaLabel, ref: this.bindContainer }), isBlobURL(url) && createElement(Spinner, null)) /* eslint-enable jsx-a11y/no-noninteractive-element-interactions */ ; var className = classnames({ 'is-selected': isSelected, 'is-transient': isBlobURL(url) }); return createElement("figure", { className: className }, isSelected && createElement("div", { className: "block-library-gallery-item__inline-menu" }, createElement(IconButton, { icon: "no-alt", onClick: onRemove, className: "blocks-gallery-item__remove", label: __('Remove Image') })), href ? createElement("a", { href: href }, img) : img, !RichText.isEmpty(caption) || isSelected ? createElement(RichText, { tagName: "figcaption", placeholder: __('Write caption…'), value: caption, isSelected: this.state.captionSelected, onChange: function onChange(newCaption) { return setAttributes({ caption: newCaption }); }, unstableOnFocus: this.onSelectCaption, inlineToolbar: true }) : null); } }]); return GalleryImage; }(Component); export default withSelect(function (select, ownProps) { var _select = select('core'), getMedia = _select.getMedia; var id = ownProps.id; return { image: id ? getMedia(id) : null }; })(GalleryImage); //# sourceMappingURL=gallery-image.js.map