UNPKG

@atlaskit/renderer

Version:
194 lines • 8.22 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } import React, { PureComponent } from 'react'; import { defaultImageCardDimensions } from '@atlaskit/media-card'; import { FilmstripView } from '@atlaskit/media-filmstrip'; import { fg } from '@atlaskit/platform-feature-flags'; import { VcMediaWrapperProps } from '@atlaskit/react-ufo/vc-media'; // Ignored via go/ees005 // eslint-disable-next-line @repo/internal/react/no-class-components var MediaGroup = /*#__PURE__*/function (_PureComponent) { function MediaGroup() { var _this; _classCallCheck(this, MediaGroup); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, MediaGroup, [].concat(args)); _defineProperty(_this, "state", { animate: false, offset: 0 }); _defineProperty(_this, "handleSize", function (_ref) { var offset = _ref.offset; return _this.setState({ offset: offset }); }); _defineProperty(_this, "handleScroll", function (_ref2) { var animate = _ref2.animate, offset = _ref2.offset; return _this.setState({ animate: animate, offset: offset }); }); _defineProperty(_this, "onMediaClick", function (cardClickHandler, child, surroundingItems) { return ( // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any function (event, analyticsEvent) { var surroundings = { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion collectionName: child.props.collection, list: surroundingItems }; cardClickHandler(event, surroundings, analyticsEvent); } ); }); return _this; } _inherits(MediaGroup, _PureComponent); return _createClass(MediaGroup, [{ key: "render", value: function render() { var numChildren = React.Children.count(this.props.children); var content; if (numChildren === 1) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any var card = React.Children.toArray(this.props.children)[0]; switch (card.props.type) { case 'file': content = this.renderSingleFile(card); break; case 'link': content = null; break; default: content = this.renderSingleLink(card); } } else { content = this.renderStrip(); } return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", _extends({ className: "MediaGroup" }, VcMediaWrapperProps), content)); } }, { key: "renderSingleFile", value: function renderSingleFile(child) { return /*#__PURE__*/React.cloneElement(child, { // the media group component renders in crop mode in editor thus this enables consistency // also crop is much easier to make consistent across SSR and hydration resizeMode: fg('media-perf-uplift-mutation-fix') ? 'crop' : 'stretchy-fit', cardDimensions: defaultImageCardDimensions, useInlinePlayer: false, featureFlags: this.props.featureFlags, enableDownloadButton: this.props.enableDownloadButton }); } }, { key: "renderSingleLink", value: function renderSingleLink(child) { return /*#__PURE__*/React.cloneElement(child, { appearance: 'auto', featureFlags: this.props.featureFlags }); } }, { key: "cloneFileCard", value: function cloneFileCard(child, surroundingItems) { var cardClickHandler = this.props && this.props.eventHandlers && this.props.eventHandlers.media && this.props.eventHandlers.media.onClick; var onClick = cardClickHandler ? this.onMediaClick(cardClickHandler, child, surroundingItems) : undefined; return /*#__PURE__*/React.cloneElement(child, { useInlinePlayer: false, eventHandlers: _objectSpread(_objectSpread({}, child.props.eventHandlers), {}, { media: { onClick: onClick } }), featureFlags: this.props.featureFlags, enableDownloadButton: this.props.enableDownloadButton }); } }, { key: "renderStrip", value: function renderStrip() { var _this2 = this; var children = this.props.children; var _this$state = this.state, animate = _this$state.animate, offset = _this$state.offset; var childIdentifiers = React.Children.map(children, function (child) { if ( /*#__PURE__*/React.isValidElement(child)) { return _this2.mapMediaPropsToIdentifier(child.props); } }); var surroundingItems = (childIdentifiers || []).filter(function (identifier) { return !!identifier; }); return /*#__PURE__*/React.createElement(FilmstripView, { animate: animate, offset: offset, onSize: this.handleSize, onScroll: this.handleScroll }, React.Children.map(children, function (rawChild) { var child = rawChild; switch (child.props.type) { case 'file': return _this2.cloneFileCard(child, surroundingItems); case 'link': return null; default: return /*#__PURE__*/React.cloneElement(child, { featureFlags: _this2.props.featureFlags, enableDownloadButton: _this2.props.enableDownloadButton }); } })); } }, { key: "mapMediaPropsToIdentifier", value: function mapMediaPropsToIdentifier(_ref3) { var id = _ref3.id, type = _ref3.type, occurrenceKey = _ref3.occurrenceKey, collection = _ref3.collection; switch (type) { case 'file': return { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion id: id, mediaItemType: type, occurrenceKey: occurrenceKey, collectionName: collection }; case 'link': return undefined; case 'external': return { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion id: id, mediaItemType: 'file', occurrenceKey: occurrenceKey, collectionName: collection }; } } }]); }(PureComponent); export { MediaGroup as default };