@atlaskit/media-card 77.2.3 → 77.4.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.
- package/CHANGELOG.md +17 -0
- package/dist/cjs/card/card.js +1 -1
- package/dist/cjs/card/getCardPreview/cache.js +1 -1
- package/dist/cjs/card/media-card-analytics-error-boundary.js +1 -1
- package/dist/cjs/card/ui/imageRenderer/imageRenderer.js +6 -3
- package/dist/cjs/card/v2/cardV2.js +18 -1037
- package/dist/cjs/card/v2/externalImageCard.js +299 -0
- package/dist/cjs/card/v2/fileCard.js +1001 -0
- package/dist/cjs/errors.js +33 -55
- package/dist/cjs/inline/loader.js +1 -1
- package/dist/cjs/utils/ufoExperiences.js +1 -1
- package/dist/cjs/utils/useCurrentValueRef.js +12 -0
- package/dist/cjs/utils/usePrevious.js +14 -0
- package/dist/es2019/card/card.js +1 -1
- package/dist/es2019/card/getCardPreview/cache.js +2 -0
- package/dist/es2019/card/media-card-analytics-error-boundary.js +1 -1
- package/dist/es2019/card/ui/imageRenderer/imageRenderer.js +6 -3
- package/dist/es2019/card/v2/cardV2.js +17 -1021
- package/dist/es2019/card/v2/externalImageCard.js +261 -0
- package/dist/es2019/card/v2/fileCard.js +881 -0
- package/dist/es2019/errors.js +1 -17
- package/dist/es2019/inline/loader.js +1 -1
- package/dist/es2019/utils/ufoExperiences.js +1 -1
- package/dist/es2019/utils/useCurrentValueRef.js +6 -0
- package/dist/es2019/utils/usePrevious.js +8 -0
- package/dist/esm/card/card.js +1 -1
- package/dist/esm/card/getCardPreview/cache.js +2 -0
- package/dist/esm/card/media-card-analytics-error-boundary.js +1 -1
- package/dist/esm/card/ui/imageRenderer/imageRenderer.js +6 -3
- package/dist/esm/card/v2/cardV2.js +19 -1036
- package/dist/esm/card/v2/externalImageCard.js +289 -0
- package/dist/esm/card/v2/fileCard.js +991 -0
- package/dist/esm/errors.js +31 -55
- package/dist/esm/inline/loader.js +1 -1
- package/dist/esm/utils/ufoExperiences.js +1 -1
- package/dist/esm/utils/useCurrentValueRef.js +6 -0
- package/dist/esm/utils/usePrevious.js +8 -0
- package/dist/types/card/v2/cardV2.d.ts +4 -60
- package/dist/types/card/v2/externalImageCard.d.ts +14 -0
- package/dist/types/card/v2/fileCard.d.ts +19 -0
- package/dist/types/errors.d.ts +1 -7
- package/dist/types/utils/useCurrentValueRef.d.ts +2 -0
- package/dist/types/utils/usePrevious.d.ts +1 -0
- package/dist/types-ts4.5/card/v2/cardV2.d.ts +4 -60
- package/dist/types-ts4.5/card/v2/externalImageCard.d.ts +14 -0
- package/dist/types-ts4.5/card/v2/fileCard.d.ts +19 -0
- package/dist/types-ts4.5/errors.d.ts +1 -7
- package/dist/types-ts4.5/utils/useCurrentValueRef.d.ts +2 -0
- package/dist/types-ts4.5/utils/usePrevious.d.ts +1 -0
- package/package.json +5 -5
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
2
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
3
|
+
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; }
|
|
4
|
+
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; }
|
|
5
|
+
import { globalMediaEventEmitter } from '@atlaskit/media-client';
|
|
6
|
+
import { getRandomHex } from '@atlaskit/media-common';
|
|
7
|
+
import { MediaViewer } from '@atlaskit/media-viewer';
|
|
8
|
+
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
|
9
|
+
import ReactDOM from 'react-dom';
|
|
10
|
+
import { ImageLoadError } from '../../errors';
|
|
11
|
+
import getDocument from '../../utils/document';
|
|
12
|
+
import { generateUniqueId } from '../../utils/generateUniqueId';
|
|
13
|
+
import { getMediaCardCursor } from '../../utils/getMediaCardCursor';
|
|
14
|
+
import { abortUfoExperience, completeUfoExperience, startUfoExperience } from '../../utils/ufoExperiences';
|
|
15
|
+
import { useCurrentValueRef } from '../../utils/useCurrentValueRef';
|
|
16
|
+
import { usePrevious } from '../../utils/usePrevious';
|
|
17
|
+
import { fireCommencedEvent, fireCopiedEvent, fireOperationalEvent, fireScreenEvent } from '../cardAnalytics';
|
|
18
|
+
import { CardViewV2 } from './cardViewV2';
|
|
19
|
+
export var ExternalImageCard = function ExternalImageCard(_ref) {
|
|
20
|
+
var mediaClient = _ref.mediaClient,
|
|
21
|
+
_ref$appearance = _ref.appearance,
|
|
22
|
+
appearance = _ref$appearance === void 0 ? 'auto' : _ref$appearance,
|
|
23
|
+
_ref$resizeMode = _ref.resizeMode,
|
|
24
|
+
resizeMode = _ref$resizeMode === void 0 ? 'crop' : _ref$resizeMode,
|
|
25
|
+
_ref$disableOverlay = _ref.disableOverlay,
|
|
26
|
+
disableOverlay = _ref$disableOverlay === void 0 ? false : _ref$disableOverlay,
|
|
27
|
+
_ref$featureFlags = _ref.featureFlags,
|
|
28
|
+
featureFlags = _ref$featureFlags === void 0 ? {} : _ref$featureFlags,
|
|
29
|
+
identifier = _ref.identifier,
|
|
30
|
+
dimensions = _ref.dimensions,
|
|
31
|
+
contextId = _ref.contextId,
|
|
32
|
+
alt = _ref.alt,
|
|
33
|
+
actions = _ref.actions,
|
|
34
|
+
shouldOpenMediaViewer = _ref.shouldOpenMediaViewer,
|
|
35
|
+
selectable = _ref.selectable,
|
|
36
|
+
selected = _ref.selected,
|
|
37
|
+
testId = _ref.testId,
|
|
38
|
+
titleBoxBgColor = _ref.titleBoxBgColor,
|
|
39
|
+
titleBoxIcon = _ref.titleBoxIcon,
|
|
40
|
+
shouldHideTooltip = _ref.shouldHideTooltip,
|
|
41
|
+
mediaViewerItems = _ref.mediaViewerItems,
|
|
42
|
+
_onClick = _ref.onClick,
|
|
43
|
+
_onMouseEnter = _ref.onMouseEnter,
|
|
44
|
+
createAnalyticsEvent = _ref.createAnalyticsEvent;
|
|
45
|
+
var internalOccurrenceKey = useMemo(function () {
|
|
46
|
+
return generateUniqueId();
|
|
47
|
+
}, []);
|
|
48
|
+
var timeElapsedTillCommenced = useMemo(function () {
|
|
49
|
+
return performance.now();
|
|
50
|
+
}, []);
|
|
51
|
+
|
|
52
|
+
// Generate unique traceId for file
|
|
53
|
+
var traceContext = useMemo(function () {
|
|
54
|
+
return {
|
|
55
|
+
traceId: getRandomHex(8)
|
|
56
|
+
};
|
|
57
|
+
}, []);
|
|
58
|
+
var _useState = useState(null),
|
|
59
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
60
|
+
cardElement = _useState2[0],
|
|
61
|
+
setCardElement = _useState2[1];
|
|
62
|
+
var fileStateFlagsRef = useRef({
|
|
63
|
+
wasStatusUploading: false,
|
|
64
|
+
wasStatusProcessing: false
|
|
65
|
+
});
|
|
66
|
+
var fireCommencedEventRef = useCurrentValueRef(function () {
|
|
67
|
+
createAnalyticsEvent && fireCommencedEvent(createAnalyticsEvent, fileAttributes, {
|
|
68
|
+
overall: {
|
|
69
|
+
durationSincePageStart: timeElapsedTillCommenced
|
|
70
|
+
}
|
|
71
|
+
}, traceContext);
|
|
72
|
+
startUfoExperience(internalOccurrenceKey);
|
|
73
|
+
});
|
|
74
|
+
var _useState3 = useState('loading-preview'),
|
|
75
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
76
|
+
status = _useState4[0],
|
|
77
|
+
setStatus = _useState4[1];
|
|
78
|
+
useEffect(function () {
|
|
79
|
+
fireCommencedEventRef.current();
|
|
80
|
+
setStatus('loading-preview');
|
|
81
|
+
}, [fireCommencedEventRef, identifier]);
|
|
82
|
+
var cardPreview = useMemo(function () {
|
|
83
|
+
return {
|
|
84
|
+
dataURI: identifier.dataURI,
|
|
85
|
+
orientation: 1,
|
|
86
|
+
source: 'external'
|
|
87
|
+
};
|
|
88
|
+
}, [identifier.dataURI]);
|
|
89
|
+
var metadata = {
|
|
90
|
+
id: identifier.mediaItemType,
|
|
91
|
+
name: identifier.name || identifier.dataURI,
|
|
92
|
+
mediaType: 'image'
|
|
93
|
+
};
|
|
94
|
+
var fileAttributes = {
|
|
95
|
+
fileMediatype: 'image',
|
|
96
|
+
fileId: metadata.id
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// for analytics
|
|
100
|
+
var ssrReliability = {
|
|
101
|
+
server: {
|
|
102
|
+
status: 'unknown'
|
|
103
|
+
},
|
|
104
|
+
client: {
|
|
105
|
+
status: 'unknown'
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
var _useState5 = useState(false),
|
|
109
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
|
110
|
+
previewDidRender = _useState6[0],
|
|
111
|
+
setPreviewDidRender = _useState6[1];
|
|
112
|
+
var _useState7 = useState(),
|
|
113
|
+
_useState8 = _slicedToArray(_useState7, 2),
|
|
114
|
+
error = _useState8[0],
|
|
115
|
+
setError = _useState8[1];
|
|
116
|
+
var _useState9 = useState(null),
|
|
117
|
+
_useState10 = _slicedToArray(_useState9, 2),
|
|
118
|
+
mediaViewerSelectedItem = _useState10[0],
|
|
119
|
+
setMediaViewerSelectedItem = _useState10[1];
|
|
120
|
+
|
|
121
|
+
//----------------------------------------------------------------//
|
|
122
|
+
//---------------------- Analytics ------------------------------//
|
|
123
|
+
//----------------------------------------------------------------//
|
|
124
|
+
|
|
125
|
+
var fireOperationalEventRef = useCurrentValueRef(function () {
|
|
126
|
+
var timeElapsedTillEvent = performance.now();
|
|
127
|
+
var durationSinceCommenced = timeElapsedTillEvent - timeElapsedTillCommenced;
|
|
128
|
+
var performanceAttributes = {
|
|
129
|
+
overall: {
|
|
130
|
+
durationSincePageStart: timeElapsedTillEvent,
|
|
131
|
+
durationSinceCommenced: durationSinceCommenced
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
createAnalyticsEvent && fireOperationalEvent(createAnalyticsEvent, status, fileAttributes, performanceAttributes, ssrReliability, error, traceContext, undefined);
|
|
135
|
+
completeUfoExperience(internalOccurrenceKey, status, fileAttributes, fileStateFlagsRef.current, ssrReliability, error);
|
|
136
|
+
});
|
|
137
|
+
var fireScreenEventRef = useCurrentValueRef(function () {
|
|
138
|
+
createAnalyticsEvent && fireScreenEvent(createAnalyticsEvent, fileAttributes);
|
|
139
|
+
});
|
|
140
|
+
var fireAbortedEventRef = useCurrentValueRef(function () {
|
|
141
|
+
// UFO won't abort if it's already in a final state (succeeded, failed, aborted, etc)
|
|
142
|
+
abortUfoExperience(internalOccurrenceKey, {
|
|
143
|
+
fileAttributes: fileAttributes,
|
|
144
|
+
fileStateFlags: fileStateFlagsRef === null || fileStateFlagsRef === void 0 ? void 0 : fileStateFlagsRef.current,
|
|
145
|
+
ssrReliability: ssrReliability
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
//----------------------------------------------------------------//
|
|
150
|
+
//------------------------ useEffects ----------------------------//
|
|
151
|
+
//----------------------------------------------------------------//
|
|
152
|
+
|
|
153
|
+
useEffect(function () {
|
|
154
|
+
var _getDocument;
|
|
155
|
+
var fireCopiedCardEvent = function fireCopiedCardEvent() {
|
|
156
|
+
cardElement && createAnalyticsEvent && fireCopiedEvent(createAnalyticsEvent, metadata.id, cardElement);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// we add a listener for each of the cards on the page
|
|
160
|
+
// and then check if the triggered listener is from the card
|
|
161
|
+
// that contains a div in current window.getSelection()
|
|
162
|
+
// won't work in IE11
|
|
163
|
+
(_getDocument = getDocument()) === null || _getDocument === void 0 || _getDocument.addEventListener('copy', fireCopiedCardEvent);
|
|
164
|
+
return function () {
|
|
165
|
+
var _getDocument2;
|
|
166
|
+
(_getDocument2 = getDocument()) === null || _getDocument2 === void 0 || _getDocument2.removeEventListener('copy', fireCopiedCardEvent);
|
|
167
|
+
};
|
|
168
|
+
}, [cardElement, createAnalyticsEvent, metadata.id]);
|
|
169
|
+
var prevStatus = usePrevious(status);
|
|
170
|
+
useEffect(function () {
|
|
171
|
+
if (prevStatus !== undefined && status !== prevStatus) {
|
|
172
|
+
fireOperationalEventRef.current();
|
|
173
|
+
}
|
|
174
|
+
}, [fireOperationalEventRef, prevStatus, status]);
|
|
175
|
+
useEffect(function () {
|
|
176
|
+
if (previewDidRender && status === 'loading-preview') {
|
|
177
|
+
setStatus('complete');
|
|
178
|
+
}
|
|
179
|
+
}, [previewDidRender, status]);
|
|
180
|
+
useEffect(function () {
|
|
181
|
+
if (prevStatus !== undefined && status !== prevStatus) {
|
|
182
|
+
if (status === 'complete') {
|
|
183
|
+
fireScreenEventRef.current();
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}, [fireScreenEventRef, prevStatus, status]);
|
|
187
|
+
useEffect(function () {
|
|
188
|
+
return function () {
|
|
189
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
190
|
+
fireAbortedEventRef.current();
|
|
191
|
+
};
|
|
192
|
+
}, [fireAbortedEventRef]);
|
|
193
|
+
|
|
194
|
+
//----------------------------------------------------------------//
|
|
195
|
+
//---------------------- Render Functions ------------------------//
|
|
196
|
+
//----------------------------------------------------------------//
|
|
197
|
+
|
|
198
|
+
var renderMediaViewer = function renderMediaViewer() {
|
|
199
|
+
if (!mediaViewerSelectedItem) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
return /*#__PURE__*/ReactDOM.createPortal( /*#__PURE__*/React.createElement(MediaViewer, {
|
|
203
|
+
collectionName: '',
|
|
204
|
+
items: mediaViewerItems || [],
|
|
205
|
+
mediaClientConfig: mediaClient.config,
|
|
206
|
+
selectedItem: mediaViewerSelectedItem,
|
|
207
|
+
onClose: function onClose() {
|
|
208
|
+
setMediaViewerSelectedItem(null);
|
|
209
|
+
},
|
|
210
|
+
contextId: contextId,
|
|
211
|
+
featureFlags: featureFlags
|
|
212
|
+
}), document.body);
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
//----------------------------------------------------------------//
|
|
216
|
+
//-------------------------- RENDER ------------------------------//
|
|
217
|
+
//----------------------------------------------------------------//
|
|
218
|
+
|
|
219
|
+
var mediaCardCursor = getMediaCardCursor(false, !!shouldOpenMediaViewer, status === 'error', !!cardPreview, metadata.mediaType);
|
|
220
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(CardViewV2, {
|
|
221
|
+
status: status,
|
|
222
|
+
error: error,
|
|
223
|
+
mediaItemType: identifier.mediaItemType,
|
|
224
|
+
metadata: metadata,
|
|
225
|
+
cardPreview: cardPreview,
|
|
226
|
+
alt: alt,
|
|
227
|
+
appearance: appearance,
|
|
228
|
+
resizeMode: resizeMode,
|
|
229
|
+
dimensions: dimensions,
|
|
230
|
+
actions: actions,
|
|
231
|
+
selectable: selectable,
|
|
232
|
+
selected: selected,
|
|
233
|
+
onClick: function onClick(event, analyticsEvent) {
|
|
234
|
+
if (_onClick) {
|
|
235
|
+
var cardEvent = {
|
|
236
|
+
event: event,
|
|
237
|
+
mediaItemDetails: metadata
|
|
238
|
+
};
|
|
239
|
+
_onClick(cardEvent, analyticsEvent);
|
|
240
|
+
}
|
|
241
|
+
if (shouldOpenMediaViewer) {
|
|
242
|
+
setMediaViewerSelectedItem({
|
|
243
|
+
mediaItemType: 'external-image',
|
|
244
|
+
dataURI: identifier.dataURI,
|
|
245
|
+
name: identifier.name
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
},
|
|
249
|
+
onMouseEnter: function onMouseEnter(event) {
|
|
250
|
+
_onMouseEnter === null || _onMouseEnter === void 0 || _onMouseEnter({
|
|
251
|
+
event: event,
|
|
252
|
+
mediaItemDetails: metadata
|
|
253
|
+
});
|
|
254
|
+
},
|
|
255
|
+
disableOverlay: disableOverlay,
|
|
256
|
+
onDisplayImage: function onDisplayImage() {
|
|
257
|
+
var payloadPart = {
|
|
258
|
+
fileId: identifier.dataURI,
|
|
259
|
+
isUserCollection: false
|
|
260
|
+
};
|
|
261
|
+
globalMediaEventEmitter.emit('media-viewed', _objectSpread({
|
|
262
|
+
viewingLevel: 'minimal'
|
|
263
|
+
}, payloadPart));
|
|
264
|
+
},
|
|
265
|
+
innerRef: setCardElement,
|
|
266
|
+
testId: testId,
|
|
267
|
+
featureFlags: featureFlags,
|
|
268
|
+
titleBoxBgColor: titleBoxBgColor,
|
|
269
|
+
titleBoxIcon: titleBoxIcon,
|
|
270
|
+
onImageError: function onImageError(newCardPreview) {
|
|
271
|
+
// If the dataURI has been replaced, we can dismiss this error
|
|
272
|
+
if ((newCardPreview === null || newCardPreview === void 0 ? void 0 : newCardPreview.dataURI) !== (cardPreview === null || cardPreview === void 0 ? void 0 : cardPreview.dataURI)) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
var error = new ImageLoadError(newCardPreview === null || newCardPreview === void 0 ? void 0 : newCardPreview.source);
|
|
276
|
+
setStatus('error');
|
|
277
|
+
setError(error);
|
|
278
|
+
},
|
|
279
|
+
onImageLoad: function onImageLoad(newCardPreview) {
|
|
280
|
+
// If the dataURI has been replaced, we can dismiss this callback
|
|
281
|
+
if ((newCardPreview === null || newCardPreview === void 0 ? void 0 : newCardPreview.dataURI) !== (cardPreview === null || cardPreview === void 0 ? void 0 : cardPreview.dataURI)) {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
setPreviewDidRender(true);
|
|
285
|
+
},
|
|
286
|
+
mediaCardCursor: mediaCardCursor,
|
|
287
|
+
shouldHideTooltip: shouldHideTooltip
|
|
288
|
+
}), mediaViewerSelectedItem ? renderMediaViewer() : null);
|
|
289
|
+
};
|