@atlaskit/media-card 70.10.0 → 72.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/CHANGELOG.md +79 -0
  2. package/dist/cjs/errors.js +76 -5
  3. package/dist/cjs/files/cardImageView/index.js +58 -89
  4. package/dist/cjs/files/index.js +0 -6
  5. package/dist/cjs/index.js +16 -6
  6. package/dist/cjs/root/card/cardAnalytics.js +33 -19
  7. package/dist/cjs/root/card/cardConstants.js +8 -0
  8. package/dist/cjs/root/card/cardLoader.js +66 -124
  9. package/dist/cjs/root/card/cardState.js +50 -0
  10. package/dist/cjs/root/card/getCardPreview/cache.js +5 -0
  11. package/dist/cjs/root/card/getCardPreview/filePreviewStatus.js +50 -0
  12. package/dist/cjs/root/card/getCardPreview/helpers.js +13 -21
  13. package/dist/cjs/root/card/getCardPreview/index.js +171 -100
  14. package/dist/cjs/root/card/getCardStatus.js +7 -1
  15. package/dist/cjs/root/card/index.js +407 -322
  16. package/dist/cjs/root/cardView.js +115 -76
  17. package/dist/cjs/root/index.js +9 -1
  18. package/dist/cjs/root/inline/loader.js +22 -21
  19. package/dist/cjs/root/inline/{inlineMediaCard.js → mediaInlineCard.js} +72 -25
  20. package/dist/cjs/root/inlinePlayer.js +5 -15
  21. package/dist/cjs/root/ui/iconMessage/index.js +17 -9
  22. package/dist/cjs/root/ui/imageRenderer/imageRenderer.js +36 -115
  23. package/dist/cjs/root/ui/styled.js +1 -1
  24. package/dist/cjs/root/ui/titleBox/failedTitleBox.js +7 -3
  25. package/dist/cjs/utils/analytics.js +26 -43
  26. package/dist/cjs/utils/cardActions/cardActionsDropdownMenu.js +16 -9
  27. package/dist/cjs/utils/dimensionComparer.js +1 -1
  28. package/dist/cjs/utils/document.js +12 -0
  29. package/dist/cjs/utils/getDataURIDimension.js +13 -2
  30. package/dist/cjs/utils/metadata.js +11 -3
  31. package/dist/cjs/utils/objectURLCache.js +6 -0
  32. package/dist/cjs/utils/resizeModeToMediaImageProps.js +13 -0
  33. package/dist/cjs/utils/shouldDisplayImageThumbnail.js +1 -1
  34. package/dist/cjs/version.json +1 -1
  35. package/dist/es2019/errors.js +42 -2
  36. package/dist/es2019/files/cardImageView/index.js +12 -46
  37. package/dist/es2019/files/index.js +1 -1
  38. package/dist/es2019/index.js +3 -3
  39. package/dist/es2019/root/card/cardAnalytics.js +23 -17
  40. package/dist/es2019/root/card/cardConstants.js +1 -0
  41. package/dist/es2019/root/card/cardLoader.js +47 -53
  42. package/dist/es2019/root/card/cardState.js +26 -0
  43. package/dist/es2019/root/card/getCardPreview/cache.js +5 -0
  44. package/dist/es2019/root/card/getCardPreview/filePreviewStatus.js +35 -0
  45. package/dist/es2019/root/card/getCardPreview/helpers.js +2 -12
  46. package/dist/es2019/root/card/getCardPreview/index.js +112 -79
  47. package/dist/es2019/root/card/getCardStatus.js +1 -0
  48. package/dist/es2019/root/card/index.js +356 -254
  49. package/dist/es2019/root/cardView.js +98 -58
  50. package/dist/es2019/root/index.js +2 -1
  51. package/dist/es2019/root/inline/loader.js +16 -15
  52. package/dist/es2019/root/inline/mediaInlineCard.js +132 -0
  53. package/dist/es2019/root/inlinePlayer.js +5 -13
  54. package/dist/es2019/root/ui/iconMessage/index.js +10 -6
  55. package/dist/es2019/root/ui/imageRenderer/imageRenderer.js +26 -74
  56. package/dist/es2019/root/ui/styled.js +1 -0
  57. package/dist/es2019/root/ui/titleBox/failedTitleBox.js +5 -3
  58. package/dist/es2019/utils/analytics.js +29 -40
  59. package/dist/es2019/utils/cardActions/cardActionsDropdownMenu.js +8 -4
  60. package/dist/es2019/utils/dimensionComparer.js +1 -1
  61. package/dist/es2019/utils/document.js +1 -0
  62. package/dist/es2019/utils/getDataURIDimension.js +8 -0
  63. package/dist/es2019/utils/metadata.js +12 -4
  64. package/dist/es2019/utils/objectURLCache.js +5 -0
  65. package/dist/es2019/utils/resizeModeToMediaImageProps.js +6 -0
  66. package/dist/es2019/utils/shouldDisplayImageThumbnail.js +1 -1
  67. package/dist/es2019/version.json +1 -1
  68. package/dist/esm/errors.js +60 -1
  69. package/dist/esm/files/cardImageView/index.js +55 -87
  70. package/dist/esm/files/index.js +1 -1
  71. package/dist/esm/index.js +3 -3
  72. package/dist/esm/root/card/cardAnalytics.js +23 -18
  73. package/dist/esm/root/card/cardConstants.js +1 -0
  74. package/dist/esm/root/card/cardLoader.js +66 -126
  75. package/dist/esm/root/card/cardState.js +32 -0
  76. package/dist/esm/root/card/getCardPreview/cache.js +6 -0
  77. package/dist/esm/root/card/getCardPreview/filePreviewStatus.js +35 -0
  78. package/dist/esm/root/card/getCardPreview/helpers.js +13 -21
  79. package/dist/esm/root/card/getCardPreview/index.js +142 -95
  80. package/dist/esm/root/card/getCardStatus.js +3 -0
  81. package/dist/esm/root/card/index.js +416 -325
  82. package/dist/esm/root/cardView.js +114 -73
  83. package/dist/esm/root/index.js +2 -1
  84. package/dist/esm/root/inline/loader.js +23 -22
  85. package/dist/esm/root/inline/mediaInlineCard.js +145 -0
  86. package/dist/esm/root/inlinePlayer.js +5 -13
  87. package/dist/esm/root/ui/iconMessage/index.js +12 -7
  88. package/dist/esm/root/ui/imageRenderer/imageRenderer.js +28 -106
  89. package/dist/esm/root/ui/styled.js +1 -1
  90. package/dist/esm/root/ui/titleBox/failedTitleBox.js +6 -3
  91. package/dist/esm/utils/analytics.js +22 -35
  92. package/dist/esm/utils/cardActions/cardActionsDropdownMenu.js +16 -9
  93. package/dist/esm/utils/dimensionComparer.js +1 -1
  94. package/dist/esm/utils/document.js +3 -0
  95. package/dist/esm/utils/getDataURIDimension.js +8 -0
  96. package/dist/esm/utils/metadata.js +12 -4
  97. package/dist/esm/utils/objectURLCache.js +6 -0
  98. package/dist/esm/utils/resizeModeToMediaImageProps.js +6 -0
  99. package/dist/esm/utils/shouldDisplayImageThumbnail.js +1 -1
  100. package/dist/esm/version.json +1 -1
  101. package/dist/types/errors.d.ts +15 -1
  102. package/dist/types/files/cardImageView/index.d.ts +5 -12
  103. package/dist/types/files/cardImageView/styled.d.ts +1 -1
  104. package/dist/types/files/index.d.ts +1 -1
  105. package/dist/types/index.d.ts +11 -13
  106. package/dist/types/root/card/cardAnalytics.d.ts +5 -7
  107. package/dist/types/root/card/cardConstants.d.ts +1 -0
  108. package/dist/types/root/card/cardLoader.d.ts +4 -18
  109. package/dist/types/root/card/cardState.d.ts +5 -0
  110. package/dist/types/root/card/getCardPreview/cache.d.ts +4 -2
  111. package/dist/types/root/card/getCardPreview/filePreviewStatus.d.ts +5 -0
  112. package/dist/types/root/card/getCardPreview/helpers.d.ts +4 -5
  113. package/dist/types/root/card/getCardPreview/index.d.ts +25 -14
  114. package/dist/types/root/card/getCardStatus.d.ts +1 -0
  115. package/dist/types/root/card/index.d.ts +18 -15
  116. package/dist/types/root/cardView.d.ts +14 -8
  117. package/dist/types/root/index.d.ts +1 -0
  118. package/dist/types/root/inline/loader.d.ts +8 -8
  119. package/dist/types/root/inline/{inlineMediaCard.d.ts → mediaInlineCard.d.ts} +4 -4
  120. package/dist/types/root/inlinePlayer.d.ts +1 -1
  121. package/dist/types/root/ui/iconMessage/index.d.ts +7 -2
  122. package/dist/types/root/ui/imageRenderer/imageRenderer.d.ts +5 -18
  123. package/dist/types/root/ui/titleBox/failedTitleBox.d.ts +2 -0
  124. package/dist/types/types.d.ts +9 -1
  125. package/dist/types/utils/analytics.d.ts +20 -21
  126. package/dist/types/utils/cardDimensions.d.ts +5 -1
  127. package/dist/types/utils/dimensionComparer.d.ts +1 -1
  128. package/dist/types/utils/document.d.ts +2 -0
  129. package/dist/types/utils/getDataURIDimension.d.ts +3 -1
  130. package/dist/types/utils/index.d.ts +1 -0
  131. package/dist/types/utils/lazyContent/styled.d.ts +1 -1
  132. package/dist/types/utils/lightCards/types.d.ts +1 -1
  133. package/dist/types/utils/metadata.d.ts +2 -2
  134. package/dist/types/utils/objectURLCache.d.ts +2 -1
  135. package/dist/types/utils/resizeModeToMediaImageProps.d.ts +5 -0
  136. package/dist/types/utils/shouldDisplayImageThumbnail.d.ts +1 -1
  137. package/example-helpers/developmentUseMessage.tsx +14 -0
  138. package/example-helpers/index.tsx +55 -4
  139. package/example-helpers/selectableCard.tsx +2 -1
  140. package/package.json +18 -14
  141. package/dist/cjs/root/card/getCardPreview/types.js +0 -5
  142. package/dist/cjs/utils/fileAttributesContext.js +0 -40
  143. package/dist/es2019/root/card/getCardPreview/types.js +0 -1
  144. package/dist/es2019/root/inline/inlineMediaCard.js +0 -92
  145. package/dist/es2019/utils/fileAttributesContext.js +0 -19
  146. package/dist/esm/root/card/getCardPreview/types.js +0 -1
  147. package/dist/esm/root/inline/inlineMediaCard.js +0 -100
  148. package/dist/esm/utils/fileAttributesContext.js +0 -18
  149. package/dist/types/root/card/getCardPreview/types.d.ts +0 -5
  150. package/dist/types/utils/fileAttributesContext.d.ts +0 -10
@@ -57,20 +57,18 @@ var _getCardPreview = require("./getCardPreview");
57
57
 
58
58
  var _metadata = require("../../utils/metadata");
59
59
 
60
- var _dimensionComparer = require("../../utils/dimensionComparer");
61
-
62
- var _getCardStatus = require("./getCardStatus");
63
-
64
60
  var _inlinePlayer = require("../inlinePlayer");
65
61
 
66
62
  var _analytics = require("../../utils/analytics");
67
63
 
68
- var _fileAttributesContext = require("../../utils/fileAttributesContext");
69
-
70
64
  var _errors = require("../../errors");
71
65
 
72
66
  var _cardAnalytics = require("./cardAnalytics");
73
67
 
68
+ var _document = _interopRequireDefault(require("../../utils/document"));
69
+
70
+ var _cardState = require("./cardState");
71
+
74
72
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
75
73
 
76
74
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -88,92 +86,212 @@ var CardBase = /*#__PURE__*/function (_Component) {
88
86
 
89
87
  var _super = _createSuper(CardBase);
90
88
 
89
+ // We initialise timeElapsedTillCommenced
90
+ // to avoid extra branching on a possibly undefined value.
91
91
  function CardBase(props) {
92
92
  var _this;
93
93
 
94
94
  (0, _classCallCheck2.default)(this, CardBase);
95
95
  _this = _super.call(this, props);
96
96
  (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "hasBeenMounted", false);
97
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onCopyListener", function () {
98
- if (typeof window.getSelection === 'function') {
99
- var selection = window.getSelection();
100
- var cardRef = _this.state.cardRef;
101
-
102
- if (cardRef && selection && selection.containsNode && selection.containsNode(cardRef, true)) {
103
- _this.fireFileCopiedEvent();
104
- }
105
- }
106
- });
107
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "shouldRefetchImage", function (current, next) {
108
- if (!current || !next) {
109
- return false;
110
- }
111
-
112
- return (0, _dimensionComparer.isBigger)(current, next);
113
- });
114
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "isLatestCardStatusUpdate", function (cardStatusUpdateTimestamp) {
115
- return !_this.lastCardStatusUpdateTimestamp || _this.lastCardStatusUpdateTimestamp <= cardStatusUpdateTimestamp;
116
- });
117
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onLocalPreviewError", function (error) {// TODO: track local preview success rate
118
- // https://product-fabric.atlassian.net/browse/MEX-774
97
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "timeElapsedTillCommenced", performance.now());
98
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getImageURLParams", function (_ref) {
99
+ var collection = _ref.collectionName;
100
+ return _objectSpread(_objectSpread({
101
+ collection: collection,
102
+ mode: (0, _mediaClient.imageResizeModeToFileImageMode)(_this.props.resizeMode)
103
+ }, _this.requestedDimensions), {}, {
104
+ allowAnimated: true
105
+ });
119
106
  });
120
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "createAddContextToDataURI", function (fileId, fileState, _ref, collectionName) {
121
- var width = _ref.width,
122
- height = _ref.height;
123
- return function (dataURI) {
124
- var _this$props = _this.props,
125
- contextId = _this$props.contextId,
126
- alt = _this$props.alt;
127
-
128
- if (!contextId) {
129
- return dataURI;
130
- }
131
-
132
- var metadata = (0, _metadata.getFileDetails)(fileState);
133
- return (0, _mediaClient.addFileAttrsToUrl)(dataURI, {
134
- id: fileId,
135
- collection: collectionName,
136
- contextId: contextId,
137
- mimeType: metadata.mimeType,
138
- name: metadata.name,
139
- size: metadata.size,
140
- width: width,
141
- height: height,
142
- alt: alt
143
- });
144
- };
107
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getMediaBlobUrlAttrs", function (identifier, fileState) {
108
+ var id = identifier.id,
109
+ collection = identifier.collectionName;
110
+ var _this$props = _this.props,
111
+ originalDimensions = _this$props.originalDimensions,
112
+ contextId = _this$props.contextId,
113
+ alt = _this$props.alt;
114
+
115
+ var _getFileDetails = (0, _metadata.getFileDetails)(identifier, fileState),
116
+ mimeType = _getFileDetails.mimeType,
117
+ name = _getFileDetails.name,
118
+ size = _getFileDetails.size;
119
+
120
+ return contextId ? _objectSpread(_objectSpread({
121
+ id: id,
122
+ collection: collection,
123
+ contextId: contextId,
124
+ mimeType: mimeType,
125
+ name: name,
126
+ size: size
127
+ }, originalDimensions || _this.requestedDimensions), {}, {
128
+ alt: alt
129
+ }) : undefined;
145
130
  });
146
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getCardPreviewParams", function (id, collectionName, fileState) {
147
- var _assertThisInitialize = (0, _assertThisInitialized2.default)(_this),
148
- requestedDimensions = _assertThisInitialize.requestedDimensions,
149
- onLocalPreviewError = _assertThisInitialize.onLocalPreviewError;
150
-
131
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getCardPreviewParams", function (identifier, fileState) {
132
+ var isBannedLocalPreview = _this.state.isBannedLocalPreview;
133
+ var id = identifier.id;
151
134
  var _this$props2 = _this.props,
152
135
  _this$props2$dimensio = _this$props2.dimensions,
153
136
  dimensions = _this$props2$dimensio === void 0 ? {} : _this$props2$dimensio,
154
- originalDimensions = _this$props2.originalDimensions,
155
- resizeMode = _this$props2.resizeMode,
156
137
  mediaClient = _this$props2.mediaClient;
157
138
  return {
158
139
  mediaClient: mediaClient,
159
140
  id: id,
160
- collectionName: collectionName,
161
141
  dimensions: dimensions,
162
- resizeMode: resizeMode,
163
- requestedDimensions: requestedDimensions,
164
- onLocalPreviewError: onLocalPreviewError,
165
- filePreview: (0, _getCardPreview.getFilePreviewFromFileState)(fileState),
142
+ onLocalPreviewError: _this.fireLocalPreviewErrorEvent,
143
+ filePreview: isBannedLocalPreview ? undefined : (0, _getCardPreview.getFilePreviewFromFileState)(fileState),
166
144
  isRemotePreviewReady: (0, _mediaClient.isImageRepresentationReady)(fileState),
167
- addContextToDataURI: _this.createAddContextToDataURI(id, fileState, originalDimensions || _this.requestedDimensions, collectionName)
145
+ imageUrlParams: _this.getImageURLParams(identifier),
146
+ mediaBlobUrlAttrs: _this.getMediaBlobUrlAttrs(identifier, fileState)
147
+ };
148
+ });
149
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "setCacheSSRPreview", function (identifier) {
150
+ var _this$props3 = _this.props,
151
+ mediaClient = _this$props3.mediaClient,
152
+ _this$props3$dimensio = _this$props3.dimensions,
153
+ dimensions = _this$props3$dimensio === void 0 ? {} : _this$props3$dimensio;
154
+ (0, _getCardPreview.fetchAndCacheRemotePreview)(mediaClient, identifier.id, dimensions, _this.getImageURLParams(identifier), _this.getMediaBlobUrlAttrs(identifier)).catch(function () {// No need to log this error.
155
+ // If preview fails, it will be refetched later
156
+ //TODO: test this catch
157
+ // https://product-fabric.atlassian.net/browse/MEX-1071
158
+ });
159
+ });
160
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "resolveSSRPreview", function (identifier, ssr) {
161
+ var mediaClient = _this.props.mediaClient;
162
+
163
+ try {
164
+ return (0, _getCardPreview.getSSRCardPreview)(ssr, mediaClient, identifier.id, _this.getImageURLParams(identifier), _this.getMediaBlobUrlAttrs(identifier));
165
+ } catch (e) {// TODO: log SSR reliability 'failed'
166
+ // https://product-fabric.atlassian.net/browse/MEX-770
167
+ }
168
+ });
169
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "resolvePreview", /*#__PURE__*/function () {
170
+ var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(identifier, fileState) {
171
+ var params, cardPreview, wrappedError;
172
+ return _regenerator.default.wrap(function _callee$(_context) {
173
+ while (1) {
174
+ switch (_context.prev = _context.next) {
175
+ case 0:
176
+ _context.prev = 0;
177
+ params = _this.getCardPreviewParams(identifier, fileState);
178
+ _context.next = 4;
179
+ return (0, _getCardPreview.getCardPreview)(params);
180
+
181
+ case 4:
182
+ cardPreview = _context.sent;
183
+
184
+ _this.safeSetState({
185
+ cardPreview: cardPreview
186
+ });
187
+
188
+ _context.next = 12;
189
+ break;
190
+
191
+ case 8:
192
+ _context.prev = 8;
193
+ _context.t0 = _context["catch"](0);
194
+ wrappedError = (0, _errors.ensureMediaCardError)('preview-fetch', _context.t0); // If remote preview fails, we set status 'error'
195
+ // If local preview fails (i.e, no remote preview available),
196
+ // we can stay in the same status until there is a remote preview available
197
+ // If it's any other error we set status 'error'
198
+
199
+ if ((0, _errors.isLocalPreviewError)(wrappedError)) {
200
+ _this.safeSetState({
201
+ isBannedLocalPreview: true
202
+ });
203
+ } else {
204
+ _this.safeSetState({
205
+ status: 'error',
206
+ error: wrappedError
207
+ });
208
+ }
209
+
210
+ case 12:
211
+ case "end":
212
+ return _context.stop();
213
+ }
214
+ }
215
+ }, _callee, null, [[0, 8]]);
216
+ }));
217
+
218
+ return function (_x, _x2) {
219
+ return _ref2.apply(this, arguments);
220
+ };
221
+ }());
222
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getPerformanceAttributes", function () {
223
+ var _assertThisInitialize = (0, _assertThisInitialized2.default)(_this),
224
+ timeElapsedTillCommenced = _assertThisInitialize.timeElapsedTillCommenced;
225
+
226
+ var timeElapsedTillEvent = performance.now();
227
+ var durationSinceCommenced = timeElapsedTillCommenced && timeElapsedTillEvent - timeElapsedTillCommenced;
228
+ return {
229
+ overall: {
230
+ durationSincePageStart: timeElapsedTillEvent,
231
+ durationSinceCommenced: durationSinceCommenced
232
+ }
168
233
  };
169
234
  });
170
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "fireFileCopiedEvent", function () {
235
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onImageError", function () {
236
+ var cardPreview = _this.state.cardPreview;
237
+ var error = new _errors.ImageLoadError(cardPreview === null || cardPreview === void 0 ? void 0 : cardPreview.source);
238
+ var isLocal = cardPreview && (0, _getCardPreview.isLocalPreview)(cardPreview);
239
+ var isSSR = cardPreview && (0, _getCardPreview.isSSRClientPreview)(cardPreview);
240
+
241
+ if (isLocal || isSSR) {
242
+ var updateState = {
243
+ cardPreview: undefined
244
+ };
245
+
246
+ if (isLocal) {
247
+ updateState.isBannedLocalPreview = true;
248
+
249
+ _this.fireLocalPreviewErrorEvent(error);
250
+ }
251
+
252
+ if (isSSR) {// TODO: set SSR-client reliability 'failed'.
253
+ // https://product-fabric.atlassian.net/browse/MEX-770
254
+ }
255
+
256
+ var _this$props4 = _this.props,
257
+ identifier = _this$props4.identifier,
258
+ _this$props4$dimensio = _this$props4.dimensions,
259
+ dimensions = _this$props4$dimensio === void 0 ? {} : _this$props4$dimensio;
260
+ (0, _mediaClient.isFileIdentifier)(identifier) && (0, _getCardPreview.removeCardPreviewFromCache)(identifier.id, dimensions);
261
+
262
+ _this.safeSetState(updateState);
263
+ } else {
264
+ _this.safeSetState({
265
+ status: 'error',
266
+ error: error
267
+ });
268
+ }
269
+ });
270
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onImageLoad", function () {
271
+ _this.safeSetState({
272
+ previewDidRender: true
273
+ });
274
+ });
275
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "fireCopiedEvent", function () {
171
276
  var createAnalyticsEvent = _this.props.createAnalyticsEvent;
172
- (0, _analytics.fireMediaCardEvent)((0, _analytics.getCopiedFilePayload)(_this.metadata.id), createAnalyticsEvent);
277
+ var cardRef = _this.state.cardRef;
278
+ cardRef && createAnalyticsEvent && (0, _cardAnalytics.fireCopiedEvent)(createAnalyticsEvent, _this.metadata.id, cardRef);
279
+ });
280
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "fireScreenEvent", function () {
281
+ var createAnalyticsEvent = _this.props.createAnalyticsEvent;
282
+ createAnalyticsEvent && (0, _cardAnalytics.fireScreenEvent)(createAnalyticsEvent, _this.fileAttributes);
283
+ });
284
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "fireLocalPreviewErrorEvent", function (error) {// TODO: track local preview success rate
285
+ // https://product-fabric.atlassian.net/browse/MEX-774
173
286
  });
174
287
  (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "safeSetState", function (state) {
175
288
  if (_this.hasBeenMounted) {
176
- _this.setState(state);
289
+ // If it's setting the status, we need to use a state updater function,
290
+ // which ensures that no non-final status overrides a final status.
291
+ // If no status is set, we don't need a sate updater
292
+ var updater = !!state.status ? (0, _cardState.createStateUpdater)(state) : state;
293
+
294
+ _this.setState(updater);
177
295
  }
178
296
  });
179
297
  (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "unsubscribe", function () {
@@ -182,16 +300,17 @@ var CardBase = /*#__PURE__*/function (_Component) {
182
300
  }
183
301
 
184
302
  if (_this.hasBeenMounted) {
303
+ // TODO MEX-788: add test for "do not remove the card preview when unsubscribing".
185
304
  _this.setState({
186
- cardPreview: undefined
305
+ isBannedLocalPreview: false
187
306
  });
188
307
  }
189
308
  });
190
309
  (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onCardViewClick", function (event, analyticsEvent) {
191
- var _this$props3 = _this.props,
192
- identifier = _this$props3.identifier,
193
- useInlinePlayer = _this$props3.useInlinePlayer,
194
- shouldOpenMediaViewer = _this$props3.shouldOpenMediaViewer;
310
+ var _this$props5 = _this.props,
311
+ identifier = _this$props5.identifier,
312
+ useInlinePlayer = _this$props5.useInlinePlayer,
313
+ shouldOpenMediaViewer = _this$props5.shouldOpenMediaViewer;
195
314
  var cardPreview = _this.state.cardPreview;
196
315
 
197
316
  var _assertThisInitialize2 = (0, _assertThisInitialized2.default)(_this),
@@ -243,13 +362,13 @@ var CardBase = /*#__PURE__*/function (_Component) {
243
362
  });
244
363
  });
245
364
  (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "renderInlinePlayer", function () {
246
- var _this$props4 = _this.props,
247
- identifier = _this$props4.identifier,
248
- mediaClient = _this$props4.mediaClient,
249
- dimensions = _this$props4.dimensions,
250
- selected = _this$props4.selected,
251
- testId = _this$props4.testId,
252
- originalDimensions = _this$props4.originalDimensions;
365
+ var _this$props6 = _this.props,
366
+ identifier = _this$props6.identifier,
367
+ mediaClient = _this$props6.mediaClient,
368
+ dimensions = _this$props6.dimensions,
369
+ selected = _this$props6.selected,
370
+ testId = _this$props6.testId,
371
+ originalDimensions = _this$props6.originalDimensions;
253
372
  return /*#__PURE__*/_react.default.createElement(_inlinePlayer.InlinePlayer, {
254
373
  mediaClient: mediaClient,
255
374
  dimensions: dimensions,
@@ -289,12 +408,12 @@ var CardBase = /*#__PURE__*/function (_Component) {
289
408
  });
290
409
  (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "renderMediaViewer", function () {
291
410
  var mediaViewerSelectedItem = _this.state.mediaViewerSelectedItem;
292
- var _this$props5 = _this.props,
293
- mediaClient = _this$props5.mediaClient,
294
- identifier = _this$props5.identifier,
295
- mediaViewerDataSource = _this$props5.mediaViewerDataSource,
296
- contextId = _this$props5.contextId,
297
- featureFlags = _this$props5.featureFlags;
411
+ var _this$props7 = _this.props,
412
+ mediaClient = _this$props7.mediaClient,
413
+ identifier = _this$props7.identifier,
414
+ mediaViewerDataSource = _this$props7.mediaViewerDataSource,
415
+ contextId = _this$props7.contextId,
416
+ featureFlags = _this$props7.featureFlags;
298
417
 
299
418
  if (!mediaViewerSelectedItem) {
300
419
  return;
@@ -315,20 +434,21 @@ var CardBase = /*#__PURE__*/function (_Component) {
315
434
  }), document.body);
316
435
  });
317
436
  (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "renderCard", function () {
318
- var _this$props6 = _this.props,
319
- identifier = _this$props6.identifier,
320
- isLazy = _this$props6.isLazy,
321
- appearance = _this$props6.appearance,
322
- resizeMode = _this$props6.resizeMode,
323
- dimensions = _this$props6.dimensions,
324
- selectable = _this$props6.selectable,
325
- selected = _this$props6.selected,
326
- disableOverlay = _this$props6.disableOverlay,
327
- alt = _this$props6.alt,
328
- testId = _this$props6.testId,
329
- featureFlags = _this$props6.featureFlags,
330
- titleBoxBgColor = _this$props6.titleBoxBgColor,
331
- titleBoxIcon = _this$props6.titleBoxIcon;
437
+ var _this$props8 = _this.props,
438
+ identifier = _this$props8.identifier,
439
+ isLazy = _this$props8.isLazy,
440
+ appearance = _this$props8.appearance,
441
+ resizeMode = _this$props8.resizeMode,
442
+ dimensions = _this$props8.dimensions,
443
+ selectable = _this$props8.selectable,
444
+ selected = _this$props8.selected,
445
+ disableOverlay = _this$props8.disableOverlay,
446
+ alt = _this$props8.alt,
447
+ testId = _this$props8.testId,
448
+ featureFlags = _this$props8.featureFlags,
449
+ titleBoxBgColor = _this$props8.titleBoxBgColor,
450
+ titleBoxIcon = _this$props8.titleBoxIcon,
451
+ ssr = _this$props8.ssr;
332
452
  var mediaItemType = identifier.mediaItemType;
333
453
  var _this$state = _this.state,
334
454
  status = _this$state.status,
@@ -341,7 +461,8 @@ var CardBase = /*#__PURE__*/function (_Component) {
341
461
  var dataURI = _this$state$cardPrevi.dataURI,
342
462
  orientation = _this$state$cardPrevi.orientation,
343
463
  error = _this$state.error,
344
- cardRef = _this$state.cardRef;
464
+ cardRef = _this$state.cardRef,
465
+ isCardVisible = _this$state.isCardVisible;
345
466
 
346
467
  var _assertThisInitialize3 = (0, _assertThisInitialized2.default)(_this),
347
468
  metadata = _assertThisInitialize3.metadata;
@@ -350,11 +471,17 @@ var CardBase = /*#__PURE__*/function (_Component) {
350
471
  onCardViewClick = _assertThisInitialize4.onCardViewClick,
351
472
  onDisplayImage = _assertThisInitialize4.onDisplayImage,
352
473
  actions = _assertThisInitialize4.actions,
353
- onMouseEnter = _assertThisInitialize4.onMouseEnter;
474
+ onMouseEnter = _assertThisInitialize4.onMouseEnter; // Card can be artificially turned visible before entering the viewport
475
+ // For example, when we have the image in cache
476
+
477
+
478
+ var nativeLazyLoad = isLazy && !isCardVisible; // Force Media Image to always display img for SSR
479
+
480
+ var forceSyncDisplay = !!ssr;
354
481
 
355
482
  var card = /*#__PURE__*/_react.default.createElement(_cardView.CardView, {
356
483
  status: status,
357
- error: error && error.secondaryError,
484
+ error: error,
358
485
  mediaItemType: mediaItemType,
359
486
  metadata: metadata,
360
487
  dataURI: dataURI,
@@ -375,7 +502,11 @@ var CardBase = /*#__PURE__*/function (_Component) {
375
502
  testId: testId,
376
503
  featureFlags: featureFlags,
377
504
  titleBoxBgColor: titleBoxBgColor,
378
- titleBoxIcon: titleBoxIcon
505
+ titleBoxIcon: titleBoxIcon,
506
+ onImageError: _this.onImageError,
507
+ onImageLoad: _this.onImageLoad,
508
+ nativeLazyLoad: nativeLazyLoad,
509
+ forceSyncDisplay: forceSyncDisplay
379
510
  });
380
511
 
381
512
  return isLazy ? /*#__PURE__*/_react.default.createElement(_viewportDetector.ViewportDetector, {
@@ -416,45 +547,81 @@ var CardBase = /*#__PURE__*/function (_Component) {
416
547
  onMouseEnter(cardEvent);
417
548
  }
418
549
  });
550
+ var _status = 'loading';
419
551
 
420
552
  var _cardPreview;
421
553
 
422
- var _this$props7 = _this.props,
423
- _identifier = _this$props7.identifier,
424
- _this$props7$dimensio = _this$props7.dimensions,
425
- _dimensions = _this$props7$dimensio === void 0 ? {} : _this$props7$dimensio;
554
+ var _this$props9 = _this.props,
555
+ _identifier = _this$props9.identifier,
556
+ _this$props9$dimensio = _this$props9.dimensions,
557
+ _dimensions = _this$props9$dimensio === void 0 ? {} : _this$props9$dimensio,
558
+ _ssr = _this$props9.ssr;
426
559
 
427
560
  if ((0, _mediaClient.isFileIdentifier)(_identifier)) {
428
561
  var id = _identifier.id;
429
562
  _cardPreview = (0, _getCardPreview.getCardPreviewFromCache)(id, _dimensions);
430
- }
431
- /**
432
- * If cardPreview is available from local cache, `isCardVisible`
433
- * should be true to avoid flickers during re-mount of the component
434
- */
563
+
564
+ if (!_cardPreview && _ssr) {
565
+ _this.fireCommencedEvent();
566
+
567
+ _cardPreview = _this.resolveSSRPreview(_identifier, _ssr);
568
+ }
569
+ } else if ((0, _mediaClient.isExternalImageIdentifier)(_identifier)) {
570
+ _this.fireCommencedEvent();
571
+
572
+ _status = 'loading-preview';
573
+ var dataURI = _identifier.dataURI;
574
+ _cardPreview = {
575
+ dataURI: dataURI,
576
+ orientation: 1,
577
+ source: 'external'
578
+ };
579
+ } // If cardPreview is available from local cache or external, `isCardVisible`
580
+ // should be true to avoid flickers during re-mount of the component
581
+ // should not be visible for SSR preview, otherwise we'll fire the metadata fetch from
582
+ // outside the viewport
435
583
 
436
584
 
585
+ var _isCardVisible = !_this.props.isLazy || !!_cardPreview && !(0, _getCardPreview.isSSRPreview)(_cardPreview);
586
+
437
587
  _this.state = {
438
- status: 'loading',
439
- isCardVisible: _cardPreview ? true : !_this.props.isLazy,
588
+ status: _status,
589
+ isCardVisible: _isCardVisible,
440
590
  isPlayingFile: false,
441
591
  cardPreview: _cardPreview,
442
- cardRef: null
592
+ cardRef: null,
593
+ isBannedLocalPreview: false,
594
+ previewDidRender: false
443
595
  };
444
596
  return _this;
445
- } // we add a listener for each of the cards on the page
446
- // and then check if the triggered listener is from the card
447
- // that contains a div in current window.getSelection()
448
- // won't work in IE11
449
-
597
+ }
450
598
 
451
599
  (0, _createClass2.default)(CardBase, [{
452
600
  key: "componentDidMount",
453
601
  value: function componentDidMount() {
454
602
  this.hasBeenMounted = true;
455
- this.fireCommencedEvent();
456
- this.updateStateForIdentifier();
457
- document.addEventListener('copy', this.onCopyListener);
603
+ var _this$state2 = this.state,
604
+ isCardVisible = _this$state2.isCardVisible,
605
+ cardPreview = _this$state2.cardPreview;
606
+ var _this$props10 = this.props,
607
+ identifier = _this$props10.identifier,
608
+ ssr = _this$props10.ssr;
609
+
610
+ if (isCardVisible && (0, _mediaClient.isFileIdentifier)(identifier)) {
611
+ this.updateStateForIdentifier(identifier);
612
+ }
613
+
614
+ if (isCardVisible && !!ssr && !!cardPreview && (0, _getCardPreview.isSSRClientPreview)(cardPreview) && (0, _mediaClient.isFileIdentifier)(identifier)) {
615
+ // Since the SSR preview brings the token in the query params,
616
+ // We need to fetch the remote preview to be able to cache it,
617
+ this.setCacheSSRPreview(identifier);
618
+ } // we add a listener for each of the cards on the page
619
+ // and then check if the triggered listener is from the card
620
+ // that contains a div in current window.getSelection()
621
+ // won't work in IE11
622
+
623
+
624
+ (0, _document.default)().addEventListener('copy', this.fireCopiedEvent);
458
625
  }
459
626
  }, {
460
627
  key: "componentDidUpdate",
@@ -463,28 +630,72 @@ var CardBase = /*#__PURE__*/function (_Component) {
463
630
  prevIdentifier = prevProps.identifier,
464
631
  prevDimensions = prevProps.dimensions;
465
632
  var prevIsCardVisible = prevState.isCardVisible;
466
- var _this$props8 = this.props,
467
- mediaClient = _this$props8.mediaClient,
468
- identifier = _this$props8.identifier,
469
- dimensions = _this$props8.dimensions;
470
- var isCardVisible = this.state.isCardVisible;
633
+ var _this$props11 = this.props,
634
+ mediaClient = _this$props11.mediaClient,
635
+ identifier = _this$props11.identifier,
636
+ dimensions = _this$props11.dimensions,
637
+ featureFlags = _this$props11.featureFlags;
638
+ var _this$state3 = this.state,
639
+ isCardVisible = _this$state3.isCardVisible,
640
+ cardPreview = _this$state3.cardPreview,
641
+ status = _this$state3.status,
642
+ fileState = _this$state3.fileState,
643
+ isBannedLocalPreview = _this$state3.isBannedLocalPreview,
644
+ previewDidRender = _this$state3.previewDidRender;
471
645
  var isDifferent = (0, _mediaClient.isDifferentIdentifier)(prevIdentifier, identifier);
646
+ var turnedVisible = !prevIsCardVisible && isCardVisible;
647
+ var isNewMediaClient = prevMediaClient !== mediaClient;
472
648
 
473
- if (prevIsCardVisible !== isCardVisible && isCardVisible || prevMediaClient !== mediaClient || isDifferent || this.shouldRefetchImage(prevDimensions, dimensions)) {
649
+ if ((0, _mediaClient.isExternalImageIdentifier)(identifier) && isDifferent) {
474
650
  this.fireCommencedEvent();
475
- this.updateStateForIdentifier();
651
+ var dataURI = identifier.dataURI;
652
+ this.setState({
653
+ status: 'loading-preview',
654
+ cardPreview: {
655
+ dataURI: dataURI,
656
+ orientation: 1,
657
+ source: 'external'
658
+ },
659
+ isCardVisible: true
660
+ });
661
+ }
662
+
663
+ if ((0, _mediaClient.isFileIdentifier)(identifier) && (turnedVisible || !!this.subscription && (isNewMediaClient || isDifferent))) {
664
+ this.updateStateForIdentifier(identifier);
476
665
  }
477
666
 
478
667
  if (this.state.status !== prevState.status) {
479
- var _this$state2 = this.state,
480
- status = _this$state2.status,
481
- cardPreview = _this$state2.cardPreview,
482
- error = _this$state2.error;
483
- var createAnalyticsEvent = this.props.createAnalyticsEvent;
484
- createAnalyticsEvent && (0, _cardAnalytics.fireOperationalEvent)(createAnalyticsEvent, status, this.fileAttributes, {
485
- cardPreview: cardPreview,
486
- error: error
668
+ this.fireOperationalEvent();
669
+
670
+ if (this.state.status === 'complete' || this.fileAttributes.fileMediatype === 'video' && !!cardPreview && this.state.status === 'processing') {
671
+ this.fireScreenEvent();
672
+ }
673
+ }
674
+
675
+ if ((0, _mediaClient.isFileIdentifier)(identifier) && fileState && (0, _getCardPreview.shouldResolvePreview)({
676
+ status: status,
677
+ fileState: fileState,
678
+ dimensions: dimensions,
679
+ prevDimensions: prevDimensions,
680
+ featureFlags: featureFlags,
681
+ hasCardPreview: !!cardPreview,
682
+ isBannedLocalPreview: isBannedLocalPreview
683
+ })) {
684
+ this.resolvePreview(identifier, fileState);
685
+ }
686
+
687
+ if (turnedVisible && this.props.ssr && !!cardPreview && (0, _getCardPreview.isSSRClientPreview)(cardPreview) && (0, _mediaClient.isFileIdentifier)(identifier)) {
688
+ // Since the SSR preview brings the token in the query params,
689
+ // We need to fetch the remote preview to be able to cache it,
690
+ this.setCacheSSRPreview(identifier);
691
+ }
692
+
693
+ if (previewDidRender && // We should't complete if status is uploading
694
+ ['loading-preview', 'processing'].includes(status)) {
695
+ this.safeSetState({
696
+ status: 'complete'
487
697
  });
698
+ this.unsubscribe();
488
699
  }
489
700
  }
490
701
  }, {
@@ -492,207 +703,103 @@ var CardBase = /*#__PURE__*/function (_Component) {
492
703
  value: function componentWillUnmount() {
493
704
  this.hasBeenMounted = false;
494
705
  this.unsubscribe();
495
- document.removeEventListener('copy', this.onCopyListener);
706
+ (0, _document.default)().removeEventListener('copy', this.fireCopiedEvent);
496
707
  }
497
708
  }, {
498
709
  key: "updateStateForIdentifier",
499
- value: function updateStateForIdentifier() {
500
- var _this$props9 = this.props,
501
- mediaClient = _this$props9.mediaClient,
502
- identifier = _this$props9.identifier;
503
- var isCardVisible = this.state.isCardVisible;
504
-
505
- if (!isCardVisible) {
506
- return;
507
- }
508
-
509
- if (identifier.mediaItemType === 'file') {
510
- this.unsubscribe();
511
- this.subscribeInternalFile(identifier, mediaClient);
512
- }
513
- }
514
- }, {
515
- key: "requestedDimensions",
516
- get: function get() {
517
- var dimensions = this.props.dimensions;
518
- var cardRef = this.state.cardRef;
519
- var options = {
520
- dimensions: dimensions,
521
- element: cardRef
522
- };
523
- var width = (0, _getDataURIDimension.getDataURIDimension)('width', options);
524
- var height = (0, _getDataURIDimension.getDataURIDimension)('height', options);
525
- return {
526
- width: width,
527
- height: height
528
- };
710
+ value: function updateStateForIdentifier(identifier) {
711
+ this.fireCommencedEvent();
712
+ this.subscribeInternalFile(identifier);
529
713
  }
530
714
  }, {
531
715
  key: "subscribeInternalFile",
532
- value: function subscribeInternalFile(identifier, mediaClient) {
716
+ value: function subscribeInternalFile(identifier) {
533
717
  var _this2 = this;
534
718
 
719
+ var mediaClient = this.props.mediaClient;
720
+ var isBannedLocalPreview = this.state.isBannedLocalPreview;
535
721
  var id = identifier.id,
536
722
  collectionName = identifier.collectionName,
537
723
  occurrenceKey = identifier.occurrenceKey;
724
+ this.unsubscribe();
538
725
  this.subscription = mediaClient.file.getFileState(id, {
539
726
  collectionName: collectionName,
540
727
  occurrenceKey: occurrenceKey
541
728
  }).subscribe({
542
- next: function () {
543
- var _next = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(fileState) {
544
- var thisCardStatusUpdateTimestamp, filePreviewStatus, status, cardPreview, error, wrappedError;
545
- return _regenerator.default.wrap(function _callee$(_context) {
546
- while (1) {
547
- switch (_context.prev = _context.next) {
548
- case 0:
549
- _this2.lastFileState = fileState;
550
- thisCardStatusUpdateTimestamp = (performance || Date).now();
551
- filePreviewStatus = (0, _getCardPreview.extractFilePreviewStatus)(fileState, _this2.props.featureFlags);
552
- status = (0, _getCardStatus.getCardStatus)(fileState.status, filePreviewStatus);
553
-
554
- _this2.safeSetState({
555
- fileState: fileState
556
- });
557
-
558
- if (!(0, _getCardPreview.shouldGetCardPreview)(status, filePreviewStatus)) {
559
- _context.next = 17;
560
- break;
561
- }
562
-
563
- _context.prev = 6;
564
- _context.next = 9;
565
- return (0, _getCardPreview.getCardPreview)(_this2.getCardPreviewParams(id, collectionName, fileState));
566
-
567
- case 9:
568
- cardPreview = _context.sent;
569
-
570
- if (['loading-preview', 'processing'].includes(status)) {
571
- status = 'complete';
572
- }
573
-
574
- _context.next = 17;
575
- break;
576
-
577
- case 13:
578
- _context.prev = 13;
579
- _context.t0 = _context["catch"](6);
580
- wrappedError = (0, _errors.ensureMediaCardError)('preview-fetch', _context.t0);
581
- /**
582
- * If remote preview fails, we set status 'error'
583
- * If the local preview fails (i.e, no remote preview available),
584
- * we can stay in the same status until there is a remote preview available
585
- * */
586
-
587
- if ((0, _errors.isRemotePreviewError)(wrappedError)) {
588
- status = 'error';
589
- error = wrappedError;
590
- }
591
-
592
- case 17:
593
- if (_this2.isLatestCardStatusUpdate(thisCardStatusUpdateTimestamp)) {
594
- // These status and progress must not override values representing more recent FileState
595
-
596
- /* next() start some await() delay in next() status & progress update
597
- * ------- ------------------ ------------------------
598
- * |----[1]FileState:uploading------>| |
599
- * | | |
600
- * |----[2]FileState:uploading------>| |
601
- * | | |
602
- * | |----[2]FileState:uploading------>| Update status to `uploading`
603
- * |----[3]FileState:processing----->| |
604
- * | |----[3]FileState:processing----->| Update status to `complete`
605
- * | | |
606
- * | |----[1]FileState:uploading------>| We do not want to update status to `uploading` again!
607
- *
608
- */
609
- if (status === 'error' && (0, _mediaClient.isErrorFileState)(fileState) && !error) {
610
- error = new _errors.MediaCardError('error-file-state', new Error(fileState.message));
611
- }
612
-
613
- _this2.safeSetState({
614
- status: status,
615
- cardPreview: cardPreview,
616
- progress: status === 'uploading' && fileState.status === 'uploading' ? fileState.progress : 1,
617
- error: status === 'error' && error ? error : undefined
618
- });
619
-
620
- _this2.lastCardStatusUpdateTimestamp = thisCardStatusUpdateTimestamp;
621
- }
622
-
623
- case 18:
624
- case "end":
625
- return _context.stop();
626
- }
627
- }
628
- }, _callee, null, [[6, 13]]);
629
- }));
729
+ next: function next(fileState) {
730
+ var featureFlags = _this2.props.featureFlags;
731
+ var newState = (0, _cardState.getCardStateFromFileState)(fileState, isBannedLocalPreview, featureFlags);
630
732
 
631
- function next(_x) {
632
- return _next.apply(this, arguments);
633
- }
634
-
635
- return next;
636
- }(),
733
+ _this2.safeSetState(newState);
734
+ },
637
735
  error: function error(e) {
638
- // If file state subscription decides that the card is complete
639
- // and later there is an error, we won't change the card's status.
640
- if (_this2.state.status === 'complete') {
641
- return;
642
- }
643
-
644
- var errorReason = _this2.fileAttributes.fileStatus === 'uploading' ? 'upload' : 'metadata-fetch';
736
+ var errorReason = _this2.state.status === 'uploading' ? 'upload' : 'metadata-fetch';
645
737
  var error = new _errors.MediaCardError(errorReason, e);
646
738
 
647
739
  _this2.safeSetState({
648
740
  error: error,
649
741
  status: 'error'
650
742
  });
651
-
652
- _this2.lastCardStatusUpdateTimestamp = (performance || Date).now();
653
743
  }
654
744
  });
655
745
  }
746
+ }, {
747
+ key: "requestedDimensions",
748
+ get: function get() {
749
+ var dimensions = this.props.dimensions;
750
+
751
+ var _ref3 = this.state || {},
752
+ element = _ref3.cardRef;
753
+
754
+ return (0, _getDataURIDimension.getRequestedDimensions)({
755
+ dimensions: dimensions,
756
+ element: element
757
+ });
758
+ }
656
759
  }, {
657
760
  key: "metadata",
658
761
  get: function get() {
659
- var identifier = this.props.identifier;
660
- return (0, _mediaClient.isFileIdentifier)(identifier) ? this.state.fileState ? (0, _metadata.getFileDetails)(this.state.fileState) : {
661
- id: identifier.id
662
- } : {
663
- id: identifier.mediaItemType,
664
- name: identifier.name || identifier.dataURI,
665
- mediaType: 'image'
666
- };
762
+ var _this$state4;
763
+
764
+ return (0, _metadata.getFileDetails)(this.props.identifier, (_this$state4 = this.state) === null || _this$state4 === void 0 ? void 0 : _this$state4.fileState);
667
765
  }
668
766
  }, {
669
767
  key: "fileAttributes",
670
768
  get: function get() {
671
- var _this$lastFileState;
769
+ var _this$state5, _this$state5$fileStat;
672
770
 
673
- return (0, _analytics.getFileAttributes)(this.metadata, (_this$lastFileState = this.lastFileState) === null || _this$lastFileState === void 0 ? void 0 : _this$lastFileState.status);
771
+ return (0, _analytics.getFileAttributes)(this.metadata, (_this$state5 = this.state) === null || _this$state5 === void 0 ? void 0 : (_this$state5$fileStat = _this$state5.fileState) === null || _this$state5$fileStat === void 0 ? void 0 : _this$state5$fileStat.status);
772
+ }
773
+ }, {
774
+ key: "fireOperationalEvent",
775
+ value: function fireOperationalEvent() {
776
+ var _this$state6 = this.state,
777
+ status = _this$state6.status,
778
+ error = _this$state6.error;
779
+ var createAnalyticsEvent = this.props.createAnalyticsEvent;
780
+ createAnalyticsEvent && (0, _cardAnalytics.fireOperationalEvent)(createAnalyticsEvent, status, this.fileAttributes, this.getPerformanceAttributes(), error);
674
781
  }
675
782
  }, {
676
783
  key: "fireCommencedEvent",
677
784
  value: function fireCommencedEvent() {
678
- if (!this.state.isCardVisible) {
679
- return;
680
- }
681
-
785
+ this.timeElapsedTillCommenced = performance.now();
682
786
  var createAnalyticsEvent = this.props.createAnalyticsEvent;
683
- var payload = (0, _analytics.getRenderCommencedEventPayload)(this.fileAttributes);
684
- (0, _analytics.fireMediaCardEvent)(payload, createAnalyticsEvent);
787
+ createAnalyticsEvent && (0, _cardAnalytics.fireCommencedEvent)(createAnalyticsEvent, this.fileAttributes, {
788
+ overall: {
789
+ durationSincePageStart: this.timeElapsedTillCommenced
790
+ }
791
+ });
685
792
  }
686
793
  }, {
687
794
  key: "actions",
688
795
  get: function get() {
689
796
  var _this3 = this;
690
797
 
691
- var _this$props10 = this.props,
692
- _this$props10$actions = _this$props10.actions,
693
- actions = _this$props10$actions === void 0 ? [] : _this$props10$actions,
694
- identifier = _this$props10.identifier,
695
- shouldEnableDownloadButton = _this$props10.shouldEnableDownloadButton;
798
+ var _this$props12 = this.props,
799
+ _this$props12$actions = _this$props12.actions,
800
+ actions = _this$props12$actions === void 0 ? [] : _this$props12$actions,
801
+ identifier = _this$props12.identifier,
802
+ shouldEnableDownloadButton = _this$props12.shouldEnableDownloadButton;
696
803
  var status = this.state.status;
697
804
  var metadata = this.metadata;
698
805
 
@@ -714,20 +821,15 @@ var CardBase = /*#__PURE__*/function (_Component) {
714
821
  }, {
715
822
  key: "render",
716
823
  value: function render() {
717
- var _this$lastFileState2;
718
-
719
- var _this$state3 = this.state,
720
- isPlayingFile = _this$state3.isPlayingFile,
721
- mediaViewerSelectedItem = _this$state3.mediaViewerSelectedItem;
824
+ var _this$state7 = this.state,
825
+ isPlayingFile = _this$state7.isPlayingFile,
826
+ mediaViewerSelectedItem = _this$state7.mediaViewerSelectedItem;
722
827
 
723
828
  var innerContent = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, isPlayingFile ? this.renderInlinePlayer() : this.renderCard(), mediaViewerSelectedItem ? this.renderMediaViewer() : null);
724
829
 
725
- var content = this.context.intl ? innerContent : /*#__PURE__*/_react.default.createElement(_reactIntl.IntlProvider, {
830
+ return this.context.intl ? innerContent : /*#__PURE__*/_react.default.createElement(_reactIntl.IntlProvider, {
726
831
  locale: "en"
727
832
  }, innerContent);
728
- return /*#__PURE__*/_react.default.createElement(_fileAttributesContext.FileAttributesProvider, {
729
- data: (0, _analytics.getFileAttributes)(this.metadata, (_this$lastFileState2 = this.lastFileState) === null || _this$lastFileState2 === void 0 ? void 0 : _this$lastFileState2.status)
730
- }, content);
731
833
  }
732
834
  }]);
733
835
  return CardBase;
@@ -746,23 +848,6 @@ exports.CardBase = CardBase;
746
848
  // Required to detect a parent IntlProvider
747
849
  intl: _reactIntl.intlShape
748
850
  });
749
- (0, _defineProperty2.default)(CardBase, "getDerivedStateFromProps", function (props) {
750
- var identifier = props.identifier;
751
-
752
- if (identifier.mediaItemType === 'external-image') {
753
- var dataURI = identifier.dataURI;
754
- return {
755
- status: 'complete',
756
- cardPreview: {
757
- dataURI: dataURI,
758
- orientation: 1,
759
- source: 'external'
760
- }
761
- };
762
- }
763
-
764
- return null;
765
- });
766
851
  var Card = (0, _mediaCommon.withMediaAnalyticsContext)({
767
852
  packageVersion: _version.version,
768
853
  packageName: _version.name,