@atlaskit/media-card 78.0.6 → 78.0.8

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 (50) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/cjs/card/card.js +1 -1
  3. package/dist/cjs/card/fileCard.js +2 -2
  4. package/dist/cjs/card/media-card-analytics-error-boundary.js +1 -1
  5. package/dist/cjs/card/svgView/helpers.js +2 -2
  6. package/dist/cjs/inline/loader.js +1 -1
  7. package/dist/cjs/types.js +16 -1
  8. package/dist/cjs/utils/ufoExperiences.js +1 -1
  9. package/dist/es2019/card/card.js +1 -1
  10. package/dist/es2019/card/fileCard.js +1 -1
  11. package/dist/es2019/card/media-card-analytics-error-boundary.js +1 -1
  12. package/dist/es2019/card/svgView/helpers.js +2 -2
  13. package/dist/es2019/inline/loader.js +1 -1
  14. package/dist/es2019/types.js +10 -0
  15. package/dist/es2019/utils/ufoExperiences.js +1 -1
  16. package/dist/esm/card/card.js +1 -1
  17. package/dist/esm/card/fileCard.js +1 -1
  18. package/dist/esm/card/media-card-analytics-error-boundary.js +1 -1
  19. package/dist/esm/card/svgView/helpers.js +2 -2
  20. package/dist/esm/inline/loader.js +1 -1
  21. package/dist/esm/types.js +14 -0
  22. package/dist/esm/utils/ufoExperiences.js +1 -1
  23. package/dist/types/types.d.ts +4 -0
  24. package/dist/types-ts4.5/types.d.ts +4 -0
  25. package/package.json +2 -2
  26. package/dist/cjs/card/cardState.js +0 -37
  27. package/dist/cjs/card/getCardPreview/cache.js +0 -39
  28. package/dist/cjs/card/getCardPreview/filePreviewStatus.js +0 -45
  29. package/dist/cjs/card/getCardPreview/helpers.js +0 -165
  30. package/dist/cjs/card/getCardPreview/index.js +0 -245
  31. package/dist/es2019/card/cardState.js +0 -30
  32. package/dist/es2019/card/getCardPreview/cache.js +0 -33
  33. package/dist/es2019/card/getCardPreview/filePreviewStatus.js +0 -43
  34. package/dist/es2019/card/getCardPreview/helpers.js +0 -74
  35. package/dist/es2019/card/getCardPreview/index.js +0 -170
  36. package/dist/esm/card/cardState.js +0 -32
  37. package/dist/esm/card/getCardPreview/cache.js +0 -35
  38. package/dist/esm/card/getCardPreview/filePreviewStatus.js +0 -40
  39. package/dist/esm/card/getCardPreview/helpers.js +0 -158
  40. package/dist/esm/card/getCardPreview/index.js +0 -213
  41. package/dist/types/card/cardState.d.ts +0 -9
  42. package/dist/types/card/getCardPreview/cache.d.ts +0 -21
  43. package/dist/types/card/getCardPreview/filePreviewStatus.d.ts +0 -4
  44. package/dist/types/card/getCardPreview/helpers.d.ts +0 -10
  45. package/dist/types/card/getCardPreview/index.d.ts +0 -53
  46. package/dist/types-ts4.5/card/cardState.d.ts +0 -9
  47. package/dist/types-ts4.5/card/getCardPreview/cache.d.ts +0 -21
  48. package/dist/types-ts4.5/card/getCardPreview/filePreviewStatus.d.ts +0 -4
  49. package/dist/types-ts4.5/card/getCardPreview/helpers.d.ts +0 -10
  50. package/dist/types-ts4.5/card/getCardPreview/index.d.ts +0 -53
@@ -1,245 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- Object.defineProperty(exports, "extractFilePreviewStatus", {
8
- enumerable: true,
9
- get: function get() {
10
- return _filePreviewStatus.extractFilePreviewStatus;
11
- }
12
- });
13
- exports.getCardPreview = exports.fetchAndCacheRemotePreview = void 0;
14
- Object.defineProperty(exports, "getCardPreviewFromBackend", {
15
- enumerable: true,
16
- get: function get() {
17
- return _helpers.getCardPreviewFromBackend;
18
- }
19
- });
20
- exports.getCardPreviewFromCache = void 0;
21
- Object.defineProperty(exports, "getCardPreviewFromFilePreview", {
22
- enumerable: true,
23
- get: function get() {
24
- return _helpers.getCardPreviewFromFilePreview;
25
- }
26
- });
27
- exports.isSSRServerPreview = exports.isSSRPreview = exports.isSSRDataPreview = exports.isSSRClientPreview = exports.isLocalPreview = exports.getSSRCardPreview = exports.getFilePreviewFromFileState = void 0;
28
- Object.defineProperty(exports, "isSupportedLocalPreview", {
29
- enumerable: true,
30
- get: function get() {
31
- return _helpers.isSupportedLocalPreview;
32
- }
33
- });
34
- exports.shouldResolvePreview = exports.removeCardPreviewFromCache = void 0;
35
- var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
36
- var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
37
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
38
- var _mediaClient = require("@atlaskit/media-client");
39
- var _mediaCommon = require("@atlaskit/media-common");
40
- var _cache = _interopRequireDefault(require("./cache"));
41
- var _helpers = require("./helpers");
42
- var _errors = require("../../errors");
43
- var _dimensionComparer = require("../../utils/dimensionComparer");
44
- var _filePreviewStatus = require("./filePreviewStatus");
45
- 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; }
46
- 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) { (0, _defineProperty2.default)(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; }
47
- var getCardPreviewFromCache = exports.getCardPreviewFromCache = _cache.default.get;
48
- var removeCardPreviewFromCache = exports.removeCardPreviewFromCache = _cache.default.remove;
49
-
50
- /**
51
- * Will return the preview if available and supported by the browser
52
- * See extractFilePreviewStatus "hasLocalPreview" logic
53
- */
54
- var getFilePreviewFromFileState = exports.getFilePreviewFromFileState = function getFilePreviewFromFileState(fileState) {
55
- return 'mimeType' in fileState && (0, _mediaCommon.isMimeTypeSupportedByBrowser)(fileState.mimeType) && (0, _mediaClient.isPreviewableFileState)(fileState) ? fileState.preview : undefined;
56
- };
57
- var extendAndCachePreview = function extendAndCachePreview(id, mode, preview, mediaBlobUrlAttrs) {
58
- var source;
59
- switch (preview.source) {
60
- case 'local':
61
- source = 'cache-local';
62
- break;
63
- case 'remote':
64
- source = 'cache-remote';
65
- break;
66
- case 'ssr-server':
67
- source = 'cache-ssr-server';
68
- break;
69
- case 'ssr-client':
70
- source = 'cache-ssr-client';
71
- break;
72
- default:
73
- source = preview.source;
74
- }
75
- // We want to embed some meta context into dataURI for Copy/Paste to work.
76
- var dataURI = mediaBlobUrlAttrs ? (0, _mediaClient.addFileAttrsToUrl)(preview.dataURI, mediaBlobUrlAttrs) : preview.dataURI;
77
- // We store new cardPreview into cache
78
- _cache.default.set(id, mode, _objectSpread(_objectSpread({}, preview), {}, {
79
- source: source,
80
- dataURI: dataURI
81
- }));
82
- return _objectSpread(_objectSpread({}, preview), {}, {
83
- dataURI: dataURI
84
- });
85
- };
86
-
87
- /**
88
- * This function will try to return a Card preview, either from cache, local preview or remote preview.
89
- * It should only be called if there is a chance to get either a remote or a local preview, or both.
90
- * This, in order to ensure there is always going to be a valid return value OR throw an error if the process fails.
91
- * It is worth noting that local preview failures break the process if there is no remote preview available.
92
- * In that case we throw an error immediately.
93
- * Otherwise, if the local preview fails but there is a remote preview available, the failure does not break the process.
94
- * In that case, we still want to report the local preview error to the caller, for feature realiability track.
95
- * hence the use of the optional callback onLocalPreviewError
96
- */
97
- var getCardPreview = exports.getCardPreview = /*#__PURE__*/function () {
98
- var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(_ref) {
99
- var mediaClient, id, _ref$dimensions, dimensions, filePreview, onLocalPreviewError, isRemotePreviewReady, imageUrlParams, mediaBlobUrlAttrs, traceContext, mode, cachedPreview, dimensionsAreBigger, localPreview, remotePreview;
100
- return _regenerator.default.wrap(function _callee$(_context) {
101
- while (1) switch (_context.prev = _context.next) {
102
- case 0:
103
- mediaClient = _ref.mediaClient, id = _ref.id, _ref$dimensions = _ref.dimensions, dimensions = _ref$dimensions === void 0 ? {} : _ref$dimensions, filePreview = _ref.filePreview, onLocalPreviewError = _ref.onLocalPreviewError, isRemotePreviewReady = _ref.isRemotePreviewReady, imageUrlParams = _ref.imageUrlParams, mediaBlobUrlAttrs = _ref.mediaBlobUrlAttrs, traceContext = _ref.traceContext;
104
- mode = imageUrlParams.mode;
105
- cachedPreview = _cache.default.get(id, mode);
106
- dimensionsAreBigger = (0, _dimensionComparer.isBigger)(cachedPreview === null || cachedPreview === void 0 ? void 0 : cachedPreview.dimensions, dimensions);
107
- if (!(cachedPreview && !dimensionsAreBigger)) {
108
- _context.next = 6;
109
- break;
110
- }
111
- return _context.abrupt("return", cachedPreview);
112
- case 6:
113
- _context.prev = 6;
114
- if (!filePreview) {
115
- _context.next = 12;
116
- break;
117
- }
118
- _context.next = 10;
119
- return (0, _helpers.getCardPreviewFromFilePreview)(filePreview);
120
- case 10:
121
- localPreview = _context.sent;
122
- return _context.abrupt("return", extendAndCachePreview(id, mode, _objectSpread(_objectSpread({}, localPreview), {}, {
123
- dimensions: dimensions
124
- }), mediaBlobUrlAttrs));
125
- case 12:
126
- _context.next = 19;
127
- break;
128
- case 14:
129
- _context.prev = 14;
130
- _context.t0 = _context["catch"](6);
131
- /**
132
- * We report the error if:
133
- * - local preview is supported and fails
134
- * - local preview is unsupported and remote preview is NOT READY
135
- * i.e. the function was called for "no reason".
136
- * We DON'T report the error if:
137
- * - local preview is unsupported and remote preview IS READY
138
- * i.e. local preview is available and not supported,
139
- * but we are after the remote preview instead.
140
- */
141
- if (!(0, _errors.isUnsupportedLocalPreviewError)(_context.t0) || (0, _errors.isUnsupportedLocalPreviewError)(_context.t0) && !isRemotePreviewReady) {
142
- onLocalPreviewError && onLocalPreviewError(_context.t0);
143
- }
144
- /**
145
- * No matter the reason why the local preview failed, we break the process
146
- * if there is no remote preview available
147
- */
148
- if (isRemotePreviewReady) {
149
- _context.next = 19;
150
- break;
151
- }
152
- throw _context.t0;
153
- case 19:
154
- if (isRemotePreviewReady) {
155
- _context.next = 21;
156
- break;
157
- }
158
- throw new _errors.MediaCardError('remote-preview-not-ready');
159
- case 21:
160
- _context.next = 23;
161
- return fetchAndCacheRemotePreview(mediaClient, id, dimensions, imageUrlParams, mediaBlobUrlAttrs, traceContext);
162
- case 23:
163
- remotePreview = _context.sent;
164
- return _context.abrupt("return", remotePreview);
165
- case 25:
166
- case "end":
167
- return _context.stop();
168
- }
169
- }, _callee, null, [[6, 14]]);
170
- }));
171
- return function getCardPreview(_x) {
172
- return _ref2.apply(this, arguments);
173
- };
174
- }();
175
- var shouldResolvePreview = exports.shouldResolvePreview = function shouldResolvePreview(_ref3) {
176
- var status = _ref3.status,
177
- fileState = _ref3.fileState,
178
- prevDimensions = _ref3.prevDimensions,
179
- dimensions = _ref3.dimensions,
180
- hasCardPreview = _ref3.hasCardPreview,
181
- isBannedLocalPreview = _ref3.isBannedLocalPreview,
182
- wasResolvedUpfrontPreview = _ref3.wasResolvedUpfrontPreview;
183
- var statusIsPreviewable = (0, _filePreviewStatus.isPreviewableStatus)(status, (0, _filePreviewStatus.extractFilePreviewStatus)(fileState, isBannedLocalPreview));
184
- var dimensionsAreBigger = (0, _dimensionComparer.isBigger)(prevDimensions, dimensions);
185
- // We should not fetch the preview if the upfront one hasn't been resolved yet (it could be resolving now), even if there are new dimensions.
186
- return wasResolvedUpfrontPreview && statusIsPreviewable && (!hasCardPreview || dimensionsAreBigger);
187
- };
188
- var getSSRCardPreview = exports.getSSRCardPreview = function getSSRCardPreview(ssr, mediaClient, id, params, mediaBlobUrlAttrs) {
189
- var dataURI;
190
- try {
191
- var rawDataURI = mediaClient.getImageUrlSync(id, params);
192
- // We want to embed some meta context into dataURI for Copy/Paste to work.
193
- dataURI = mediaBlobUrlAttrs ? (0, _mediaClient.addFileAttrsToUrl)(rawDataURI, mediaBlobUrlAttrs) : rawDataURI;
194
- var source = ssr === 'client' ? 'ssr-client' : 'ssr-server';
195
- return {
196
- dataURI: dataURI,
197
- source: source,
198
- orientation: 1
199
- };
200
- } catch (e) {
201
- var reason = ssr === 'server' ? 'ssr-server-uri' : 'ssr-client-uri';
202
- throw new _errors.SsrPreviewError(reason, e instanceof Error ? e : undefined);
203
- }
204
- };
205
- var isLocalPreview = exports.isLocalPreview = function isLocalPreview(preview) {
206
- var localSources = ['local', 'cache-local'];
207
- return localSources.includes(preview.source);
208
- };
209
- var isSSRPreview = exports.isSSRPreview = function isSSRPreview(preview) {
210
- return isSSRClientPreview(preview) || isSSRServerPreview(preview) || isSSRDataPreview(preview);
211
- };
212
- var isSSRServerPreview = exports.isSSRServerPreview = function isSSRServerPreview(preview) {
213
- var ssrClientSources = ['ssr-server', 'cache-ssr-server'];
214
- return ssrClientSources.includes(preview.source);
215
- };
216
- var isSSRClientPreview = exports.isSSRClientPreview = function isSSRClientPreview(preview) {
217
- var ssrClientSources = ['ssr-client', 'cache-ssr-client'];
218
- return ssrClientSources.includes(preview.source);
219
- };
220
- var isSSRDataPreview = exports.isSSRDataPreview = function isSSRDataPreview(preview) {
221
- return preview.source === 'ssr-data';
222
- };
223
- var fetchAndCacheRemotePreview = exports.fetchAndCacheRemotePreview = /*#__PURE__*/function () {
224
- var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(mediaClient, id, dimensions, params, mediaBlobUrlAttrs, traceContext) {
225
- var remotePreview;
226
- return _regenerator.default.wrap(function _callee2$(_context2) {
227
- while (1) switch (_context2.prev = _context2.next) {
228
- case 0:
229
- _context2.next = 2;
230
- return (0, _helpers.getCardPreviewFromBackend)(mediaClient, id, params, traceContext);
231
- case 2:
232
- remotePreview = _context2.sent;
233
- return _context2.abrupt("return", extendAndCachePreview(id, params.mode, _objectSpread(_objectSpread({}, remotePreview), {}, {
234
- dimensions: dimensions
235
- }), mediaBlobUrlAttrs));
236
- case 4:
237
- case "end":
238
- return _context2.stop();
239
- }
240
- }, _callee2);
241
- }));
242
- return function fetchAndCacheRemotePreview(_x2, _x3, _x4, _x5, _x6, _x7) {
243
- return _ref4.apply(this, arguments);
244
- };
245
- }();
@@ -1,30 +0,0 @@
1
- import { isErrorFileState } from '@atlaskit/media-client';
2
- import { MediaCardError } from '../errors';
3
- import { getCardStatus, isFinalCardStatus } from './getCardStatus';
4
- import { extractFilePreviewStatus } from './getCardPreview';
5
-
6
- /**
7
- * From docs: "Both state and props received by the updater function are guaranteed to be up-to-date.
8
- * The output of the updater is shallowly merged with state."
9
- */
10
- export const createStateUpdater = (newState, fireErrorEvent) => prevState => {
11
- // Only override if previous status is non-final or new status is 'complete'
12
- if (!!newState.status && isFinalCardStatus(prevState.status) && newState.status !== 'complete') {
13
- // Log the error if the new state is not going to store it.
14
- // i.e. this is a non critical error
15
- !!newState.error && fireErrorEvent(newState.error);
16
- return prevState;
17
- }
18
- return newState;
19
- };
20
- export const getCardStateFromFileState = (fileState, isBannedLocalPreview) => {
21
- const status = getCardStatus(fileState.status, extractFilePreviewStatus(fileState, isBannedLocalPreview));
22
- const error = status === 'error' && isErrorFileState(fileState) ? new MediaCardError('error-file-state', new Error(fileState.message)) : undefined;
23
- const progress = status === 'uploading' && fileState.status === 'uploading' ? fileState.progress : 1;
24
- return {
25
- fileState,
26
- status,
27
- progress,
28
- error
29
- };
30
- };
@@ -1,33 +0,0 @@
1
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
- import { createObjectURLCache } from '../../utils/objectURLCache';
3
-
4
- // Dimensions are used to create a key.
5
- // Cache is invalidated when different dimensions are provided.
6
-
7
- export const getCacheKey = (id, mode) => {
8
- const resizeMode = mode || 'crop';
9
- return [id, resizeMode].join('-');
10
- };
11
- export class CardPreviewCacheImpl {
12
- constructor(previewCache) {
13
- _defineProperty(this, "get", (id, mode) => {
14
- const cacheKey = getCacheKey(id, mode);
15
- return this.previewCache.get(cacheKey);
16
- });
17
- _defineProperty(this, "set", (id, mode, cardPreview) => {
18
- const cacheKey = getCacheKey(id, mode);
19
- this.previewCache.set(cacheKey, cardPreview);
20
- });
21
- _defineProperty(this, "remove", (id, mode) => {
22
- const cacheKey = getCacheKey(id, mode);
23
- this.previewCache.remove(cacheKey);
24
- });
25
- _defineProperty(this, "clear", () => {
26
- this.previewCache.clear();
27
- });
28
- this.previewCache = previewCache;
29
- }
30
- }
31
-
32
- // eslint-disable-next-line import/no-anonymous-default-export
33
- export default new CardPreviewCacheImpl(createObjectURLCache());
@@ -1,43 +0,0 @@
1
- import { isMimeTypeSupportedByBrowser } from '@atlaskit/media-common';
2
- import { isPreviewableFileState, isPreviewableType, isImageRepresentationReady } from '@atlaskit/media-client';
3
- import { isSupportedLocalPreview } from './helpers';
4
-
5
- // TODO: align these checks with helpers from Media Client
6
- // https://product-fabric.atlassian.net/browse/BMPT-1300
7
- export const extractFilePreviewStatus = (fileState, isBannedLocalPreview) => {
8
- const hasFilesize = 'size' in fileState && !!fileState.size;
9
- const {
10
- mediaType
11
- } = 'mediaType' in fileState && fileState || {};
12
- const {
13
- mimeType
14
- } = 'mimeType' in fileState && fileState || {};
15
- const isPreviewable = !!mediaType && isPreviewableType(mediaType);
16
-
17
- // Local preview is available only if it's supported by browser and supported by Media Card (isSupportedLocalPreview)
18
- // For example, SVGs are mime type NOT supported by browser but media type supported by Media Card (image)
19
- // Then, local Preview NOT available
20
- const hasLocalPreview = !isBannedLocalPreview && isPreviewableFileState(fileState) && isSupportedLocalPreview(mediaType) && !!mimeType && isMimeTypeSupportedByBrowser(mimeType);
21
- const hasRemotePreview = isImageRepresentationReady(fileState);
22
- const hasPreview = hasLocalPreview || hasRemotePreview;
23
- const isSupportedByBrowser = !!mimeType && isMimeTypeSupportedByBrowser(mimeType);
24
- return {
25
- hasFilesize,
26
- isPreviewable,
27
- hasPreview,
28
- isSupportedByBrowser
29
- };
30
- };
31
-
32
- // CXP-2723 TODO: Review this in relation to removing status from the hook
33
- export const isPreviewableStatus = (cardStatus, {
34
- isPreviewable,
35
- hasPreview,
36
- isSupportedByBrowser
37
- }) => {
38
- return hasPreview && isPreviewable && (cardStatus === 'complete' || cardStatus === 'loading-preview' || cardStatus === 'uploading' ||
39
- // For Video, we can have local or remote preview while processing.
40
- // Then, we only want to show the thumbnail if the file is supported by the browser,
41
- // this way we prevent playing unsupported videos that are not procesed
42
- cardStatus === 'processing' && isSupportedByBrowser);
43
- };
@@ -1,74 +0,0 @@
1
- import { takeSnapshot } from '../../utils/videoSnapshot';
2
- import { getMediaTypeFromMimeType } from '@atlaskit/media-common';
3
- import { getOrientation } from '@atlaskit/media-ui';
4
- import { LocalPreviewError, RemotePreviewError } from '../../errors';
5
- /**
6
- * This method tells the support for the media
7
- * types covered in getCardPreviewFromFilePreview
8
- */
9
- export const isSupportedLocalPreview = mediaType => mediaType === 'image' || mediaType === 'video';
10
- const getImageLocalPreview = async value => {
11
- try {
12
- const orientation = await getOrientation(value);
13
- const dataURI = URL.createObjectURL(value);
14
- return {
15
- dataURI,
16
- orientation,
17
- source: 'local'
18
- };
19
- } catch (e) {
20
- throw new LocalPreviewError('local-preview-image', e instanceof Error ? e : undefined);
21
- }
22
- };
23
- const getVideoLocalPreview = async value => {
24
- try {
25
- const dataURI = await takeSnapshot(value);
26
- return {
27
- dataURI,
28
- orientation: 1,
29
- source: 'local'
30
- };
31
- } catch (e) {
32
- throw new LocalPreviewError('local-preview-video', e instanceof Error ? e : undefined);
33
- }
34
- };
35
- export const getCardPreviewFromFilePreview = async filePreview => {
36
- let value;
37
- try {
38
- const resolvedFilePreview = await filePreview;
39
- value = resolvedFilePreview.value;
40
- } catch (e) {
41
- throw new LocalPreviewError('local-preview-rejected', e instanceof Error ? e : undefined);
42
- }
43
- if (typeof value === 'string') {
44
- return {
45
- dataURI: value,
46
- orientation: 1,
47
- source: 'local'
48
- };
49
- } else if (value instanceof Blob) {
50
- const {
51
- type
52
- } = value;
53
- const mediaType = getMediaTypeFromMimeType(type);
54
- switch (mediaType) {
55
- case 'image':
56
- return getImageLocalPreview(value);
57
- case 'video':
58
- return getVideoLocalPreview(value);
59
- }
60
- }
61
- throw new LocalPreviewError('local-preview-unsupported');
62
- };
63
- export const getCardPreviewFromBackend = async (mediaClient, id, params, traceContext) => {
64
- try {
65
- const blob = await mediaClient.getImage(id, params, undefined, undefined, traceContext);
66
- return {
67
- dataURI: URL.createObjectURL(blob),
68
- orientation: 1,
69
- source: 'remote'
70
- };
71
- } catch (e) {
72
- throw new RemotePreviewError('remote-preview-fetch', e instanceof Error ? e : undefined);
73
- }
74
- };
@@ -1,170 +0,0 @@
1
- import { isPreviewableFileState, addFileAttrsToUrl } from '@atlaskit/media-client';
2
- import { isMimeTypeSupportedByBrowser } from '@atlaskit/media-common';
3
- import cardPreviewCache from './cache';
4
- import { getCardPreviewFromFilePreview, getCardPreviewFromBackend } from './helpers';
5
- import { MediaCardError, SsrPreviewError, isUnsupportedLocalPreviewError } from '../../errors';
6
- import { isBigger } from '../../utils/dimensionComparer';
7
- import { extractFilePreviewStatus, isPreviewableStatus } from './filePreviewStatus';
8
- export { getCardPreviewFromFilePreview, getCardPreviewFromBackend, isSupportedLocalPreview } from './helpers';
9
- export { extractFilePreviewStatus } from './filePreviewStatus';
10
- export const getCardPreviewFromCache = cardPreviewCache.get;
11
- export const removeCardPreviewFromCache = cardPreviewCache.remove;
12
-
13
- /**
14
- * Will return the preview if available and supported by the browser
15
- * See extractFilePreviewStatus "hasLocalPreview" logic
16
- */
17
- export const getFilePreviewFromFileState = fileState => 'mimeType' in fileState && isMimeTypeSupportedByBrowser(fileState.mimeType) && isPreviewableFileState(fileState) ? fileState.preview : undefined;
18
- const extendAndCachePreview = (id, mode, preview, mediaBlobUrlAttrs) => {
19
- let source;
20
- switch (preview.source) {
21
- case 'local':
22
- source = 'cache-local';
23
- break;
24
- case 'remote':
25
- source = 'cache-remote';
26
- break;
27
- case 'ssr-server':
28
- source = 'cache-ssr-server';
29
- break;
30
- case 'ssr-client':
31
- source = 'cache-ssr-client';
32
- break;
33
- default:
34
- source = preview.source;
35
- }
36
- // We want to embed some meta context into dataURI for Copy/Paste to work.
37
- const dataURI = mediaBlobUrlAttrs ? addFileAttrsToUrl(preview.dataURI, mediaBlobUrlAttrs) : preview.dataURI;
38
- // We store new cardPreview into cache
39
- cardPreviewCache.set(id, mode, {
40
- ...preview,
41
- source,
42
- dataURI
43
- });
44
- return {
45
- ...preview,
46
- dataURI
47
- };
48
- };
49
-
50
- /**
51
- * This function will try to return a Card preview, either from cache, local preview or remote preview.
52
- * It should only be called if there is a chance to get either a remote or a local preview, or both.
53
- * This, in order to ensure there is always going to be a valid return value OR throw an error if the process fails.
54
- * It is worth noting that local preview failures break the process if there is no remote preview available.
55
- * In that case we throw an error immediately.
56
- * Otherwise, if the local preview fails but there is a remote preview available, the failure does not break the process.
57
- * In that case, we still want to report the local preview error to the caller, for feature realiability track.
58
- * hence the use of the optional callback onLocalPreviewError
59
- */
60
- export const getCardPreview = async ({
61
- mediaClient,
62
- id,
63
- dimensions = {},
64
- filePreview,
65
- onLocalPreviewError,
66
- isRemotePreviewReady,
67
- imageUrlParams,
68
- mediaBlobUrlAttrs,
69
- traceContext
70
- }) => {
71
- const mode = imageUrlParams.mode;
72
- const cachedPreview = cardPreviewCache.get(id, mode);
73
- const dimensionsAreBigger = isBigger(cachedPreview === null || cachedPreview === void 0 ? void 0 : cachedPreview.dimensions, dimensions);
74
- if (cachedPreview && !dimensionsAreBigger) {
75
- return cachedPreview;
76
- }
77
- try {
78
- if (filePreview) {
79
- const localPreview = await getCardPreviewFromFilePreview(filePreview);
80
- return extendAndCachePreview(id, mode, {
81
- ...localPreview,
82
- dimensions
83
- }, mediaBlobUrlAttrs);
84
- }
85
- } catch (e) {
86
- /**
87
- * We report the error if:
88
- * - local preview is supported and fails
89
- * - local preview is unsupported and remote preview is NOT READY
90
- * i.e. the function was called for "no reason".
91
- * We DON'T report the error if:
92
- * - local preview is unsupported and remote preview IS READY
93
- * i.e. local preview is available and not supported,
94
- * but we are after the remote preview instead.
95
- */
96
- if (!isUnsupportedLocalPreviewError(e) || isUnsupportedLocalPreviewError(e) && !isRemotePreviewReady) {
97
- onLocalPreviewError && onLocalPreviewError(e);
98
- }
99
- /**
100
- * No matter the reason why the local preview failed, we break the process
101
- * if there is no remote preview available
102
- */
103
- if (!isRemotePreviewReady) {
104
- throw e;
105
- }
106
- }
107
- if (!isRemotePreviewReady) {
108
- /**
109
- * We throw this in case this function has been called
110
- * without checking isRemotePreviewReady first.
111
- * If remote preview is not ready, the call to getCardPreviewFromBackend
112
- * will generate a console error due to a 404 code
113
- */
114
- throw new MediaCardError('remote-preview-not-ready');
115
- }
116
- const remotePreview = await fetchAndCacheRemotePreview(mediaClient, id, dimensions, imageUrlParams, mediaBlobUrlAttrs, traceContext);
117
- return remotePreview;
118
- };
119
- export const shouldResolvePreview = ({
120
- status,
121
- fileState,
122
- prevDimensions,
123
- dimensions,
124
- hasCardPreview,
125
- isBannedLocalPreview,
126
- wasResolvedUpfrontPreview
127
- }) => {
128
- const statusIsPreviewable = isPreviewableStatus(status, extractFilePreviewStatus(fileState, isBannedLocalPreview));
129
- const dimensionsAreBigger = isBigger(prevDimensions, dimensions);
130
- // We should not fetch the preview if the upfront one hasn't been resolved yet (it could be resolving now), even if there are new dimensions.
131
- return wasResolvedUpfrontPreview && statusIsPreviewable && (!hasCardPreview || dimensionsAreBigger);
132
- };
133
- export const getSSRCardPreview = (ssr, mediaClient, id, params, mediaBlobUrlAttrs) => {
134
- let dataURI;
135
- try {
136
- const rawDataURI = mediaClient.getImageUrlSync(id, params);
137
- // We want to embed some meta context into dataURI for Copy/Paste to work.
138
- dataURI = mediaBlobUrlAttrs ? addFileAttrsToUrl(rawDataURI, mediaBlobUrlAttrs) : rawDataURI;
139
- const source = ssr === 'client' ? 'ssr-client' : 'ssr-server';
140
- return {
141
- dataURI,
142
- source,
143
- orientation: 1
144
- };
145
- } catch (e) {
146
- const reason = ssr === 'server' ? 'ssr-server-uri' : 'ssr-client-uri';
147
- throw new SsrPreviewError(reason, e instanceof Error ? e : undefined);
148
- }
149
- };
150
- export const isLocalPreview = preview => {
151
- const localSources = ['local', 'cache-local'];
152
- return localSources.includes(preview.source);
153
- };
154
- export const isSSRPreview = preview => isSSRClientPreview(preview) || isSSRServerPreview(preview) || isSSRDataPreview(preview);
155
- export const isSSRServerPreview = preview => {
156
- const ssrClientSources = ['ssr-server', 'cache-ssr-server'];
157
- return ssrClientSources.includes(preview.source);
158
- };
159
- export const isSSRClientPreview = preview => {
160
- const ssrClientSources = ['ssr-client', 'cache-ssr-client'];
161
- return ssrClientSources.includes(preview.source);
162
- };
163
- export const isSSRDataPreview = preview => preview.source === 'ssr-data';
164
- export const fetchAndCacheRemotePreview = async (mediaClient, id, dimensions, params, mediaBlobUrlAttrs, traceContext) => {
165
- const remotePreview = await getCardPreviewFromBackend(mediaClient, id, params, traceContext);
166
- return extendAndCachePreview(id, params.mode, {
167
- ...remotePreview,
168
- dimensions
169
- }, mediaBlobUrlAttrs);
170
- };
@@ -1,32 +0,0 @@
1
- import { isErrorFileState } from '@atlaskit/media-client';
2
- import { MediaCardError } from '../errors';
3
- import { getCardStatus, isFinalCardStatus } from './getCardStatus';
4
- import { extractFilePreviewStatus } from './getCardPreview';
5
-
6
- /**
7
- * From docs: "Both state and props received by the updater function are guaranteed to be up-to-date.
8
- * The output of the updater is shallowly merged with state."
9
- */
10
- export var createStateUpdater = function createStateUpdater(newState, fireErrorEvent) {
11
- return function (prevState) {
12
- // Only override if previous status is non-final or new status is 'complete'
13
- if (!!newState.status && isFinalCardStatus(prevState.status) && newState.status !== 'complete') {
14
- // Log the error if the new state is not going to store it.
15
- // i.e. this is a non critical error
16
- !!newState.error && fireErrorEvent(newState.error);
17
- return prevState;
18
- }
19
- return newState;
20
- };
21
- };
22
- export var getCardStateFromFileState = function getCardStateFromFileState(fileState, isBannedLocalPreview) {
23
- var status = getCardStatus(fileState.status, extractFilePreviewStatus(fileState, isBannedLocalPreview));
24
- var error = status === 'error' && isErrorFileState(fileState) ? new MediaCardError('error-file-state', new Error(fileState.message)) : undefined;
25
- var progress = status === 'uploading' && fileState.status === 'uploading' ? fileState.progress : 1;
26
- return {
27
- fileState: fileState,
28
- status: status,
29
- progress: progress,
30
- error: error
31
- };
32
- };
@@ -1,35 +0,0 @@
1
- import _createClass from "@babel/runtime/helpers/createClass";
2
- import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
4
- import { createObjectURLCache } from '../../utils/objectURLCache';
5
-
6
- // Dimensions are used to create a key.
7
- // Cache is invalidated when different dimensions are provided.
8
-
9
- export var getCacheKey = function getCacheKey(id, mode) {
10
- var resizeMode = mode || 'crop';
11
- return [id, resizeMode].join('-');
12
- };
13
- export var CardPreviewCacheImpl = /*#__PURE__*/_createClass(function CardPreviewCacheImpl(previewCache) {
14
- var _this = this;
15
- _classCallCheck(this, CardPreviewCacheImpl);
16
- _defineProperty(this, "get", function (id, mode) {
17
- var cacheKey = getCacheKey(id, mode);
18
- return _this.previewCache.get(cacheKey);
19
- });
20
- _defineProperty(this, "set", function (id, mode, cardPreview) {
21
- var cacheKey = getCacheKey(id, mode);
22
- _this.previewCache.set(cacheKey, cardPreview);
23
- });
24
- _defineProperty(this, "remove", function (id, mode) {
25
- var cacheKey = getCacheKey(id, mode);
26
- _this.previewCache.remove(cacheKey);
27
- });
28
- _defineProperty(this, "clear", function () {
29
- _this.previewCache.clear();
30
- });
31
- this.previewCache = previewCache;
32
- });
33
-
34
- // eslint-disable-next-line import/no-anonymous-default-export
35
- export default new CardPreviewCacheImpl(createObjectURLCache());