@webiny/app 0.0.0-mt-2 → 0.0.0-unstable.06b2ede40f
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/App.d.ts +26 -0
- package/App.js +95 -0
- package/App.js.map +1 -0
- package/AppContainer.d.ts +23 -0
- package/AppContainer.js +9 -0
- package/AppContainer.js.map +1 -0
- package/apollo-client/InMemoryCache.d.ts +4 -3
- package/apollo-client/InMemoryCache.js +13 -52
- package/apollo-client/InMemoryCache.js.map +1 -0
- package/apollo-client/IntrospectionFragmentMatcher.d.ts +11 -0
- package/apollo-client/IntrospectionFragmentMatcher.js +45 -0
- package/apollo-client/IntrospectionFragmentMatcher.js.map +1 -0
- package/components/Image.d.ts +3 -3
- package/components/Image.js +12 -17
- package/components/Image.js.map +1 -0
- package/components/Routes.d.ts +2 -2
- package/components/Routes.js +20 -19
- package/components/Routes.js.map +1 -0
- package/components/View.d.ts +1 -1
- package/components/View.js +13 -17
- package/components/View.js.map +1 -0
- package/components/index.d.ts +1 -0
- package/components/index.js +4 -1
- package/components/index.js.map +1 -0
- package/config/RouterConfig/Route.d.ts +30 -0
- package/config/RouterConfig/Route.js +42 -0
- package/config/RouterConfig/Route.js.map +1 -0
- package/config/RouterConfig.d.ts +32 -0
- package/config/RouterConfig.js +18 -0
- package/config/RouterConfig.js.map +1 -0
- package/config.d.ts +8 -0
- package/config.js +25 -0
- package/config.js.map +1 -0
- package/contexts/Ui/index.d.ts +5 -8
- package/contexts/Ui/index.js +27 -64
- package/contexts/Ui/index.js.map +1 -0
- package/core/DebounceRender.d.ts +12 -0
- package/core/DebounceRender.js +31 -0
- package/core/DebounceRender.js.map +1 -0
- package/core/Plugin.d.ts +8 -0
- package/core/Plugin.js +14 -0
- package/core/Plugin.js.map +1 -0
- package/core/Plugins.d.ts +15 -0
- package/core/Plugins.js +39 -0
- package/core/Plugins.js.map +1 -0
- package/core/Provider.d.ts +8 -0
- package/core/Provider.js +18 -0
- package/core/Provider.js.map +1 -0
- package/core/Routes.d.ts +6 -0
- package/core/Routes.js +34 -0
- package/core/Routes.js.map +1 -0
- package/core/createProvider.d.ts +10 -0
- package/core/createProvider.js +9 -0
- package/core/createProvider.js.map +1 -0
- package/core/createProviderPlugin.d.ts +8 -0
- package/core/createProviderPlugin.js +17 -0
- package/core/createProviderPlugin.js.map +1 -0
- package/hooks/useAutocomplete/index.js +3 -1
- package/hooks/useAutocomplete/index.js.map +1 -0
- package/hooks/useAutocomplete/useAutocomplete.d.ts +6 -1
- package/hooks/useAutocomplete/useAutocomplete.js +11 -15
- package/hooks/useAutocomplete/useAutocomplete.js.map +1 -0
- package/hooks/useDataList/functions/getData.d.ts +1 -1
- package/hooks/useDataList/functions/getData.js +3 -3
- package/hooks/useDataList/functions/getData.js.map +1 -0
- package/hooks/useDataList/functions/getError.d.ts +1 -1
- package/hooks/useDataList/functions/getError.js +3 -3
- package/hooks/useDataList/functions/getError.js.map +1 -0
- package/hooks/useDataList/functions/getMeta.d.ts +1 -1
- package/hooks/useDataList/functions/getMeta.js +3 -3
- package/hooks/useDataList/functions/getMeta.js.map +1 -0
- package/hooks/useDataList/functions/index.js +3 -1
- package/hooks/useDataList/functions/index.js.map +1 -0
- package/hooks/useDataList/functions/searchDataByKey.d.ts +1 -1
- package/hooks/useDataList/functions/searchDataByKey.js +5 -8
- package/hooks/useDataList/functions/searchDataByKey.js.map +1 -0
- package/hooks/useDataList/index.js +3 -1
- package/hooks/useDataList/index.js.map +1 -0
- package/hooks/useDataList/useDataList.d.ts +6 -6
- package/hooks/useDataList/useDataList.js +80 -90
- package/hooks/useDataList/useDataList.js.map +1 -0
- package/hooks/useDataList/utils/index.js +3 -1
- package/hooks/useDataList/utils/index.js.map +1 -0
- package/hooks/useDataList/utils/prepareLoadListParams.d.ts +9 -3
- package/hooks/useDataList/utils/prepareLoadListParams.js +35 -31
- package/hooks/useDataList/utils/prepareLoadListParams.js.map +1 -0
- package/hooks/useDataList/utils/redirectToRouteWithQueryParams.d.ts +13 -4
- package/hooks/useDataList/utils/redirectToRouteWithQueryParams.js +13 -9
- package/hooks/useDataList/utils/redirectToRouteWithQueryParams.js.map +1 -0
- package/hooks/useDataList/utils/types.d.ts +21 -15
- package/hooks/useDataList/utils/types.js +3 -1
- package/hooks/useDataList/utils/types.js.map +1 -0
- package/hooks/useHandler.d.ts +11 -1
- package/hooks/useHandler.js +13 -6
- package/hooks/useHandler.js.map +1 -0
- package/hooks/useHandlers.d.ts +4 -4
- package/hooks/useHandlers.js +15 -11
- package/hooks/useHandlers.js.map +1 -0
- package/hooks/useRegisterLegacyPlugin.d.ts +2 -0
- package/hooks/useRegisterLegacyPlugin.js +11 -0
- package/hooks/useRegisterLegacyPlugin.js.map +1 -0
- package/hooks/useUi.d.ts +1 -1
- package/hooks/useUi.js +5 -3
- package/hooks/useUi.js.map +1 -0
- package/i18n/i18n.js +3 -1
- package/i18n/i18n.js.map +1 -0
- package/i18n/index.js +3 -1
- package/i18n/index.js.map +1 -0
- package/index.d.ts +17 -0
- package/index.js +20 -0
- package/index.js.map +1 -0
- package/package.json +27 -27
- package/plugins/AddQuerySelectionPlugin.d.ts +10 -3
- package/plugins/AddQuerySelectionPlugin.js +63 -92
- package/plugins/AddQuerySelectionPlugin.js.map +1 -0
- package/plugins/ApolloCacheObjectIdPlugin.d.ts +8 -8
- package/plugins/ApolloCacheObjectIdPlugin.js +12 -36
- package/plugins/ApolloCacheObjectIdPlugin.js.map +1 -0
- package/plugins/ApolloDynamicLink.d.ts +3 -1
- package/plugins/ApolloDynamicLink.js +19 -50
- package/plugins/ApolloDynamicLink.js.map +1 -0
- package/plugins/ApolloLinkPlugin.d.ts +6 -6
- package/plugins/ApolloLinkPlugin.js +18 -39
- package/plugins/ApolloLinkPlugin.js.map +1 -0
- package/plugins/ConsoleLinkPlugin.js +19 -40
- package/plugins/ConsoleLinkPlugin.js.map +1 -0
- package/plugins/LocaleHeaderLinkPlugin.d.ts +3 -7
- package/plugins/LocaleHeaderLinkPlugin.js +22 -51
- package/plugins/LocaleHeaderLinkPlugin.js.map +1 -0
- package/plugins/NetworkErrorLinkPlugin/ErrorOverlay.d.ts +13 -5
- package/plugins/NetworkErrorLinkPlugin/ErrorOverlay.js +21 -31
- package/plugins/NetworkErrorLinkPlugin/ErrorOverlay.js.map +1 -0
- package/plugins/NetworkErrorLinkPlugin/StyledComponents.d.ts +8 -2
- package/plugins/NetworkErrorLinkPlugin/StyledComponents.js +76 -7
- package/plugins/NetworkErrorLinkPlugin/StyledComponents.js.map +1 -0
- package/plugins/NetworkErrorLinkPlugin/createErrorOverlay.d.ts +6 -4
- package/plugins/NetworkErrorLinkPlugin/createErrorOverlay.js +20 -16
- package/plugins/NetworkErrorLinkPlugin/createErrorOverlay.js.map +1 -0
- package/plugins/NetworkErrorLinkPlugin.js +20 -35
- package/plugins/NetworkErrorLinkPlugin.js.map +1 -0
- package/plugins/OmitTypenameLinkPlugin.js +11 -31
- package/plugins/OmitTypenameLinkPlugin.js.map +1 -0
- package/plugins/RoutePlugin.d.ts +3 -3
- package/plugins/RoutePlugin.js +12 -32
- package/plugins/RoutePlugin.js.map +1 -0
- package/plugins/TenantHeaderLinkPlugin.d.ts +3 -7
- package/plugins/TenantHeaderLinkPlugin.js +29 -61
- package/plugins/TenantHeaderLinkPlugin.js.map +1 -0
- package/plugins/ViewPlugin.d.ts +5 -5
- package/plugins/ViewPlugin.js +15 -37
- package/plugins/ViewPlugin.js.map +1 -0
- package/plugins/image.d.ts +1 -1
- package/plugins/image.js +65 -85
- package/plugins/image.js.map +1 -0
- package/plugins/index.d.ts +3 -3
- package/plugins/index.js +35 -42
- package/plugins/index.js.map +1 -0
- package/react-butterfiles/Files.d.ts +69 -0
- package/react-butterfiles/Files.js +227 -0
- package/react-butterfiles/Files.js.map +1 -0
- package/react-butterfiles/index.d.ts +2 -0
- package/react-butterfiles/index.js +4 -0
- package/react-butterfiles/index.js.map +1 -0
- package/react-butterfiles/utils/generateId.d.ts +1 -0
- package/react-butterfiles/utils/generateId.js +5 -0
- package/react-butterfiles/utils/generateId.js.map +1 -0
- package/react-butterfiles/utils/readFileContent.d.ts +1 -0
- package/react-butterfiles/utils/readFileContent.js +15 -0
- package/react-butterfiles/utils/readFileContent.js.map +1 -0
- package/renderApp.d.ts +2 -0
- package/renderApp.js +9 -0
- package/renderApp.js.map +1 -0
- package/types.d.ts +29 -24
- package/types.js +13 -1
- package/types.js.map +1 -0
- package/utils/createGenericContext.d.ts +13 -0
- package/utils/createGenericContext.js +25 -0
- package/utils/createGenericContext.js.map +1 -0
- package/utils/createHashing.d.ts +2 -0
- package/utils/createHashing.js +18 -0
- package/utils/createHashing.js.map +1 -0
- package/utils/getApiUrl.d.ts +1 -0
- package/utils/getApiUrl.js +5 -0
- package/utils/getApiUrl.js.map +1 -0
- package/utils/getGqlApiUrl.d.ts +1 -0
- package/utils/getGqlApiUrl.js +6 -0
- package/utils/getGqlApiUrl.js.map +1 -0
- package/utils/getHeadlessCmsGqlApiUrl.d.ts +5 -0
- package/utils/getHeadlessCmsGqlApiUrl.js +12 -0
- package/utils/getHeadlessCmsGqlApiUrl.js.map +1 -0
- package/utils/getLocaleCode.d.ts +6 -0
- package/utils/getLocaleCode.js +33 -0
- package/utils/getLocaleCode.js.map +1 -0
- package/utils/getPrerenderId.d.ts +6 -0
- package/utils/getPrerenderId.js +5 -0
- package/utils/getPrerenderId.js.map +1 -0
- package/utils/getTenantId.d.ts +6 -0
- package/utils/getTenantId.js +45 -0
- package/utils/getTenantId.js.map +1 -0
- package/utils/index.d.ts +10 -0
- package/utils/index.js +12 -0
- package/utils/index.js.map +1 -0
- package/utils/isLocalhost.d.ts +1 -0
- package/utils/isLocalhost.js +7 -0
- package/utils/isLocalhost.js.map +1 -0
- package/utils/isPrerendering.d.ts +1 -0
- package/utils/isPrerendering.js +5 -0
- package/utils/isPrerendering.js.map +1 -0
- package/utils/legacyPluginToReactComponent.d.ts +7 -0
- package/utils/legacyPluginToReactComponent.js +14 -0
- package/utils/legacyPluginToReactComponent.js.map +1 -0
package/plugins/image.js
CHANGED
|
@@ -1,107 +1,86 @@
|
|
|
1
|
-
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
|
|
2
|
-
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
|
|
3
|
-
var _excluded = ["transform", "srcSet"];
|
|
4
1
|
import React from "react";
|
|
5
2
|
import { Image } from "@webiny/ui/Image";
|
|
6
|
-
|
|
3
|
+
const SUPPORTED_IMAGE_RESIZE_WIDTHS = [100, 300, 500, 750, 1000, 1500, 2500];
|
|
4
|
+
|
|
7
5
|
/**
|
|
8
6
|
* Width of the image should not be just any random number. For optimization reasons,
|
|
9
7
|
* we only allow the ones listed in SUPPORTED_IMAGE_RESIZE_WIDTHS list (Webiny Cloud supports only these).
|
|
10
8
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
var i = SUPPORTED_IMAGE_RESIZE_WIDTHS.length;
|
|
15
|
-
|
|
9
|
+
const getSupportedImageResizeWidth = width => {
|
|
10
|
+
let output = SUPPORTED_IMAGE_RESIZE_WIDTHS[0];
|
|
11
|
+
let i = SUPPORTED_IMAGE_RESIZE_WIDTHS.length;
|
|
16
12
|
while (i >= 0) {
|
|
17
13
|
if (width === SUPPORTED_IMAGE_RESIZE_WIDTHS[i]) {
|
|
18
14
|
output = SUPPORTED_IMAGE_RESIZE_WIDTHS[i];
|
|
19
15
|
break;
|
|
20
16
|
}
|
|
21
|
-
|
|
22
17
|
if (width > SUPPORTED_IMAGE_RESIZE_WIDTHS[i]) {
|
|
23
18
|
// Use next larger width. If there isn't any, use current.
|
|
24
19
|
output = SUPPORTED_IMAGE_RESIZE_WIDTHS[i + 1];
|
|
25
|
-
|
|
26
20
|
if (!output) {
|
|
27
21
|
output = SUPPORTED_IMAGE_RESIZE_WIDTHS[i];
|
|
28
22
|
}
|
|
29
|
-
|
|
30
23
|
break;
|
|
31
24
|
}
|
|
32
|
-
|
|
33
25
|
i--;
|
|
34
26
|
}
|
|
35
|
-
|
|
36
27
|
return output;
|
|
37
28
|
};
|
|
38
29
|
/**
|
|
39
30
|
* Currently we only allow "width" as a transform option.
|
|
40
31
|
* @param args
|
|
41
32
|
*/
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (width > 0) {
|
|
51
|
-
output.width = getSupportedImageResizeWidth(width);
|
|
52
|
-
}
|
|
33
|
+
const sanitizeTransformArgs = args => {
|
|
34
|
+
const output = {};
|
|
35
|
+
if (!args || args.width === undefined || args.width === null) {
|
|
36
|
+
return output;
|
|
37
|
+
}
|
|
38
|
+
const width = parseInt(args.width);
|
|
39
|
+
if (width > 0) {
|
|
40
|
+
output.width = getSupportedImageResizeWidth(width);
|
|
53
41
|
}
|
|
54
|
-
|
|
55
42
|
return output;
|
|
56
43
|
};
|
|
57
|
-
|
|
58
|
-
|
|
44
|
+
const getSizes = width => {
|
|
45
|
+
if (typeof width !== "string") {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
59
48
|
// Check if width was set as percentage, with "%" in the value.
|
|
60
|
-
if (
|
|
61
|
-
return
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
return "".concat(parseInt(width), "vw");
|
|
67
|
-
} // Check if width was set as relative, with "em" in the value.
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (typeof width === "string" && width.endsWith("em")) {
|
|
71
|
-
return "".concat(parseInt(width), "em");
|
|
49
|
+
if (width.endsWith("%")) {
|
|
50
|
+
return `${parseInt(width)}vw`;
|
|
51
|
+
}
|
|
52
|
+
// Check if width was set as viewport width, with "vw" in the value.
|
|
53
|
+
if (width.endsWith("vw")) {
|
|
54
|
+
return `${parseInt(width)}vw`;
|
|
72
55
|
}
|
|
73
56
|
|
|
74
|
-
|
|
57
|
+
// Check if width was set as relative, with "em" in the value.
|
|
58
|
+
if (width.endsWith("em")) {
|
|
59
|
+
return `${parseInt(width)}em`;
|
|
60
|
+
}
|
|
61
|
+
return undefined;
|
|
75
62
|
};
|
|
76
|
-
|
|
77
|
-
var isFixedImageWidth = function isFixedImageWidth(width) {
|
|
63
|
+
const isFixedImageWidth = width => {
|
|
78
64
|
if (Number.isFinite(width)) {
|
|
79
65
|
return true;
|
|
80
66
|
}
|
|
81
|
-
|
|
82
67
|
if (typeof width === "string" && width.endsWith("px")) {
|
|
83
68
|
return true;
|
|
84
69
|
}
|
|
85
|
-
|
|
86
70
|
return false;
|
|
87
71
|
};
|
|
88
|
-
|
|
89
|
-
var getSrcSetAutoSizes = function getSrcSetAutoSizes(max) {
|
|
72
|
+
const getSrcSetAutoSizes = max => {
|
|
90
73
|
max = isFixedImageWidth(max) ? parseInt("" + max) : 2500;
|
|
91
|
-
|
|
92
|
-
return SUPPORTED_IMAGE_RESIZE_WIDTHS.filter(
|
|
74
|
+
const maxWidth = getSupportedImageResizeWidth(max);
|
|
75
|
+
return SUPPORTED_IMAGE_RESIZE_WIDTHS.filter(supportedWidth => {
|
|
93
76
|
return supportedWidth <= maxWidth;
|
|
94
77
|
});
|
|
95
78
|
};
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return Object.keys(transform).map(function (key) {
|
|
99
|
-
return "".concat(key, "=").concat(transform[key]);
|
|
100
|
-
}).join("&");
|
|
79
|
+
const convertTransformToQueryParams = transform => {
|
|
80
|
+
return Object.keys(transform).map(key => `${key}=${transform[key]}`).join("&");
|
|
101
81
|
};
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
var imagePlugin = {
|
|
82
|
+
export default () => {
|
|
83
|
+
const imagePlugin = {
|
|
105
84
|
name: "image-component",
|
|
106
85
|
type: "image-component",
|
|
107
86
|
presets: {
|
|
@@ -109,51 +88,50 @@ export default (function () {
|
|
|
109
88
|
width: 300
|
|
110
89
|
}
|
|
111
90
|
},
|
|
112
|
-
getImageSrc:
|
|
91
|
+
getImageSrc: props => {
|
|
113
92
|
if (!props) {
|
|
114
93
|
return "";
|
|
115
94
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
95
|
+
const {
|
|
96
|
+
src,
|
|
97
|
+
transform
|
|
98
|
+
} = props;
|
|
120
99
|
if (!transform) {
|
|
121
100
|
return src;
|
|
122
101
|
}
|
|
123
|
-
|
|
124
102
|
if (!src || src.startsWith("data:") || src.endsWith("svg")) {
|
|
125
103
|
return src;
|
|
126
104
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
params = convertTransformToQueryParams(params);
|
|
105
|
+
const sanitizedParams = sanitizeTransformArgs(transform);
|
|
106
|
+
const params = convertTransformToQueryParams(sanitizedParams);
|
|
130
107
|
return src + "?" + params;
|
|
131
108
|
},
|
|
132
|
-
render
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
109
|
+
render(props) {
|
|
110
|
+
const {
|
|
111
|
+
transform,
|
|
112
|
+
srcSet: srcSetInitial,
|
|
113
|
+
...imageProps
|
|
114
|
+
} = props;
|
|
115
|
+
let srcSet = srcSetInitial;
|
|
116
|
+
let sizes;
|
|
117
|
+
const src = imageProps.src;
|
|
141
118
|
if (srcSet && srcSet === "auto") {
|
|
142
|
-
srcSet = {};
|
|
119
|
+
srcSet = {};
|
|
143
120
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
srcSetAutoWidths
|
|
121
|
+
// Check if image width was forced, and additionally if width was set as pixels, with "px" in the value.
|
|
122
|
+
const forcedWidth = props.width || props.style && props.style.width;
|
|
123
|
+
const srcSetAutoWidths = getSrcSetAutoSizes(forcedWidth);
|
|
124
|
+
srcSetAutoWidths.forEach(width => {
|
|
147
125
|
srcSet[width + "w"] = imagePlugin.getImageSrc({
|
|
148
|
-
src
|
|
149
|
-
transform:
|
|
150
|
-
|
|
151
|
-
|
|
126
|
+
src,
|
|
127
|
+
transform: {
|
|
128
|
+
...transform,
|
|
129
|
+
width
|
|
130
|
+
}
|
|
152
131
|
});
|
|
153
132
|
});
|
|
154
133
|
sizes = getSizes(forcedWidth);
|
|
155
134
|
}
|
|
156
|
-
|
|
157
135
|
return /*#__PURE__*/React.createElement(Image, Object.assign({}, imageProps, {
|
|
158
136
|
srcSet: srcSet,
|
|
159
137
|
src: src,
|
|
@@ -162,4 +140,6 @@ export default (function () {
|
|
|
162
140
|
}
|
|
163
141
|
};
|
|
164
142
|
return imagePlugin;
|
|
165
|
-
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
//# sourceMappingURL=image.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["React","Image","SUPPORTED_IMAGE_RESIZE_WIDTHS","getSupportedImageResizeWidth","width","output","i","length","sanitizeTransformArgs","args","undefined","parseInt","getSizes","endsWith","isFixedImageWidth","Number","isFinite","getSrcSetAutoSizes","max","maxWidth","filter","supportedWidth","convertTransformToQueryParams","transform","Object","keys","map","key","join","imagePlugin","name","type","presets","avatar","getImageSrc","props","src","startsWith","sanitizedParams","params","render","srcSet","srcSetInitial","imageProps","sizes","forcedWidth","style","srcSetAutoWidths","forEach","createElement","assign"],"sources":["image.tsx"],"sourcesContent":["import React from \"react\";\nimport { Image } from \"@webiny/ui/Image\";\nimport type { ImageComponentPlugin } from \"~/types\";\n\nconst SUPPORTED_IMAGE_RESIZE_WIDTHS = [100, 300, 500, 750, 1000, 1500, 2500];\n\n/**\n * Width of the image should not be just any random number. For optimization reasons,\n * we only allow the ones listed in SUPPORTED_IMAGE_RESIZE_WIDTHS list (Webiny Cloud supports only these).\n */\nconst getSupportedImageResizeWidth = (width: number) => {\n let output = SUPPORTED_IMAGE_RESIZE_WIDTHS[0];\n let i = SUPPORTED_IMAGE_RESIZE_WIDTHS.length;\n while (i >= 0) {\n if (width === SUPPORTED_IMAGE_RESIZE_WIDTHS[i]) {\n output = SUPPORTED_IMAGE_RESIZE_WIDTHS[i];\n break;\n }\n\n if (width > SUPPORTED_IMAGE_RESIZE_WIDTHS[i]) {\n // Use next larger width. If there isn't any, use current.\n output = SUPPORTED_IMAGE_RESIZE_WIDTHS[i + 1];\n if (!output) {\n output = SUPPORTED_IMAGE_RESIZE_WIDTHS[i];\n }\n break;\n }\n\n i--;\n }\n\n return output;\n};\n\ninterface SanitizeTransformArgsParams {\n width?: string;\n}\ninterface SanitizeTransformArgsResult {\n width?: number;\n}\n/**\n * Currently we only allow \"width\" as a transform option.\n * @param args\n */\nconst sanitizeTransformArgs = (args?: SanitizeTransformArgsParams): SanitizeTransformArgsResult => {\n const output: SanitizeTransformArgsResult = {};\n if (!args || args.width === undefined || args.width === null) {\n return output;\n }\n const width = parseInt(args.width);\n if (width > 0) {\n output.width = getSupportedImageResizeWidth(width);\n }\n\n return output;\n};\n\nconst getSizes = (width?: string | number): string | undefined => {\n if (typeof width !== \"string\") {\n return undefined;\n }\n // Check if width was set as percentage, with \"%\" in the value.\n if (width.endsWith(\"%\")) {\n return `${parseInt(width)}vw`;\n }\n // Check if width was set as viewport width, with \"vw\" in the value.\n if (width.endsWith(\"vw\")) {\n return `${parseInt(width)}vw`;\n }\n\n // Check if width was set as relative, with \"em\" in the value.\n if (width.endsWith(\"em\")) {\n return `${parseInt(width)}em`;\n }\n\n return undefined;\n};\n\nconst isFixedImageWidth = (width?: number | string) => {\n if (Number.isFinite(width)) {\n return true;\n }\n\n if (typeof width === \"string\" && width.endsWith(\"px\")) {\n return true;\n }\n return false;\n};\n\nconst getSrcSetAutoSizes = (max?: string | number) => {\n max = isFixedImageWidth(max) ? parseInt(\"\" + max) : 2500;\n const maxWidth = getSupportedImageResizeWidth(max);\n return SUPPORTED_IMAGE_RESIZE_WIDTHS.filter((supportedWidth: number) => {\n return supportedWidth <= maxWidth;\n });\n};\n\nconst convertTransformToQueryParams = (transform: Record<string, any>): string => {\n return Object.keys(transform)\n .map(key => `${key}=${transform[key]}`)\n .join(\"&\");\n};\n\nexport default () => {\n const imagePlugin: ImageComponentPlugin = {\n name: \"image-component\",\n type: \"image-component\",\n presets: {\n avatar: { width: 300 }\n },\n getImageSrc: (props?: Record<string, any>) => {\n if (!props) {\n return \"\";\n }\n\n const { src, transform } = props;\n if (!transform) {\n return src;\n }\n\n if (!src || src.startsWith(\"data:\") || src.endsWith(\"svg\")) {\n return src;\n }\n\n const sanitizedParams = sanitizeTransformArgs(transform);\n const params = convertTransformToQueryParams(sanitizedParams);\n return src + \"?\" + params;\n },\n render(props) {\n const { transform, srcSet: srcSetInitial, ...imageProps } = props;\n let srcSet: any = srcSetInitial;\n let sizes: string | undefined;\n const src = imageProps.src;\n if (srcSet && srcSet === \"auto\") {\n srcSet = {};\n\n // Check if image width was forced, and additionally if width was set as pixels, with \"px\" in the value.\n const forcedWidth = props.width || (props.style && props.style.width);\n const srcSetAutoWidths = getSrcSetAutoSizes(forcedWidth);\n srcSetAutoWidths.forEach(width => {\n srcSet[width + \"w\"] = imagePlugin.getImageSrc({\n src,\n transform: { ...transform, width }\n });\n });\n sizes = getSizes(forcedWidth);\n }\n\n return <Image {...imageProps} srcSet={srcSet} src={src} sizes={sizes} />;\n }\n };\n\n return imagePlugin;\n};\n"],"mappings":"AAAA,OAAOA,KAAK,MAAM,OAAO;AACzB,SAASC,KAAK,QAAQ,kBAAkB;AAGxC,MAAMC,6BAA6B,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC;;AAE5E;AACA;AACA;AACA;AACA,MAAMC,4BAA4B,GAAIC,KAAa,IAAK;EACpD,IAAIC,MAAM,GAAGH,6BAA6B,CAAC,CAAC,CAAC;EAC7C,IAAII,CAAC,GAAGJ,6BAA6B,CAACK,MAAM;EAC5C,OAAOD,CAAC,IAAI,CAAC,EAAE;IACX,IAAIF,KAAK,KAAKF,6BAA6B,CAACI,CAAC,CAAC,EAAE;MAC5CD,MAAM,GAAGH,6BAA6B,CA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,OAAOf,WAAW;AACtB,CAAC","ignoreList":[]}
|
package/plugins/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { ReactNode } from "react";
|
|
2
|
-
import { Plugin } from "@webiny/plugins/types";
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import type { Plugin } from "@webiny/plugins/types";
|
|
3
3
|
import imagePlugin from "./image";
|
|
4
4
|
export { imagePlugin };
|
|
5
|
-
|
|
5
|
+
type RenderPluginOptions<T> = {
|
|
6
6
|
wrapper?: boolean;
|
|
7
7
|
fn?: string;
|
|
8
8
|
filter?: (value: T, index: number, array: T[]) => boolean;
|
package/plugins/index.js
CHANGED
|
@@ -3,71 +3,64 @@ import warning from "warning";
|
|
|
3
3
|
import { plugins } from "@webiny/plugins";
|
|
4
4
|
import imagePlugin from "./image";
|
|
5
5
|
export { imagePlugin };
|
|
6
|
-
|
|
7
|
-
var PluginComponent = function PluginComponent(props) {
|
|
6
|
+
const PluginComponent = props => {
|
|
8
7
|
return props.children;
|
|
9
8
|
};
|
|
10
|
-
|
|
11
|
-
var PluginsComponent = function PluginsComponent(props) {
|
|
9
|
+
const PluginsComponent = props => {
|
|
12
10
|
return props.children;
|
|
13
11
|
};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
fn = _options$fn === void 0 ? "render" : _options$fn;
|
|
22
|
-
var plugin = plugins.byName(name);
|
|
23
|
-
warning(plugin, "No such plugin \"".concat(name, "\""));
|
|
24
|
-
|
|
12
|
+
export const renderPlugin = (name, params = {}, options = {}) => {
|
|
13
|
+
const {
|
|
14
|
+
wrapper = true,
|
|
15
|
+
fn = "render"
|
|
16
|
+
} = options;
|
|
17
|
+
const plugin = plugins.byName(name);
|
|
18
|
+
warning(plugin, `No such plugin "${name}"`);
|
|
25
19
|
if (!plugin) {
|
|
26
20
|
return null;
|
|
27
21
|
}
|
|
28
|
-
|
|
29
|
-
var content = plugin[fn](params);
|
|
30
|
-
|
|
22
|
+
const content = plugin[fn](params);
|
|
31
23
|
if (content) {
|
|
32
24
|
return wrapper ? /*#__PURE__*/React.createElement(PluginComponent, {
|
|
33
25
|
key: plugin.name,
|
|
34
26
|
name: name,
|
|
35
27
|
params: params,
|
|
36
28
|
fn: fn
|
|
37
|
-
}, content) : /*#__PURE__*/React.cloneElement(content, {
|
|
29
|
+
}, content) : (/*#__PURE__*/React.cloneElement(content, {
|
|
38
30
|
key: plugin.name
|
|
39
|
-
});
|
|
31
|
+
}));
|
|
40
32
|
}
|
|
41
|
-
|
|
42
33
|
return null;
|
|
43
34
|
};
|
|
44
|
-
export
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
35
|
+
export const renderPlugins = (type, params = {}, options = {}) => {
|
|
36
|
+
const {
|
|
37
|
+
wrapper = true,
|
|
38
|
+
fn = "render",
|
|
39
|
+
filter = v => v,
|
|
40
|
+
reverse
|
|
41
|
+
} = options;
|
|
42
|
+
const content = plugins.byType(type).filter(pl => {
|
|
43
|
+
/**
|
|
44
|
+
* TODO @ts-refactor Problem with possibility of a different subtype.
|
|
45
|
+
*/
|
|
46
|
+
// @ts-expect-error
|
|
47
|
+
return filter(pl);
|
|
48
|
+
})
|
|
49
|
+
/**
|
|
50
|
+
* We cast as string because renderPlugin checks for the plugin.name
|
|
51
|
+
*/.map(plugin => renderPlugin(plugin.name, params, {
|
|
52
|
+
wrapper,
|
|
53
|
+
fn
|
|
54
|
+
})).filter(Boolean);
|
|
63
55
|
if (reverse) {
|
|
64
56
|
content.reverse();
|
|
65
57
|
}
|
|
66
|
-
|
|
67
58
|
return wrapper ? /*#__PURE__*/React.createElement(PluginsComponent, {
|
|
68
59
|
type: type,
|
|
69
60
|
params: params,
|
|
70
61
|
fn: fn
|
|
71
62
|
}, content) : content;
|
|
72
63
|
};
|
|
73
|
-
export default [imagePlugin];
|
|
64
|
+
export default [imagePlugin];
|
|
65
|
+
|
|
66
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["React","warning","plugins","imagePlugin","PluginComponent","props","children","PluginsComponent","renderPlugin","name","params","options","wrapper","fn","plugin","byName","content","createElement","key","cloneElement","renderPlugins","type","filter","v","reverse","byType","pl","map","Boolean"],"sources":["index.tsx"],"sourcesContent":["import type { ReactNode } from \"react\";\nimport React from \"react\";\nimport warning from \"warning\";\nimport { plugins } from \"@webiny/plugins\";\nimport type { Plugin } from \"@webiny/plugins/types\";\nimport imagePlugin from \"./image\";\n\nexport { imagePlugin };\n\ntype RenderPluginOptions<T> = {\n wrapper?: boolean;\n fn?: string;\n filter?: (value: T, index: number, array: T[]) => boolean;\n reverse?: boolean;\n};\n\ninterface RenderPlugin {\n <T extends Plugin = Plugin>(name: string, params?: any, options?: RenderPluginOptions<T>):\n | ReactNode\n | ReactNode[];\n}\n\ninterface RenderPlugins {\n <T extends Plugin = Plugin>(type: string, params?: any, options?: RenderPluginOptions<T>):\n | ReactNode\n | ReactNode[];\n}\n\ninterface PluginComponentProps {\n name: string;\n params: Record<string, any>;\n fn: string;\n children: React.ReactNode;\n}\nconst PluginComponent = (props: PluginComponentProps) => {\n return props.children as React.ReactElement;\n};\ninterface PluginsComponentProps {\n type: string;\n params: Record<string, any>;\n fn: string;\n children: React.ReactNode;\n}\nconst PluginsComponent = (props: PluginsComponentProps) => {\n return props.children as React.ReactElement;\n};\n\nexport const renderPlugin: RenderPlugin = (name, params = {}, options = {}) => {\n const { wrapper = true, fn = \"render\" } = options;\n\n const plugin = plugins.byName(name);\n warning(plugin, `No such plugin \"${name}\"`);\n\n if (!plugin) {\n return null;\n }\n\n const content = plugin[fn](params);\n if (content) {\n return wrapper ? (\n <PluginComponent key={plugin.name} name={name} params={params} fn={fn}>\n {content}\n </PluginComponent>\n ) : (\n React.cloneElement(content, { key: plugin.name })\n );\n }\n return null;\n};\n\nexport const renderPlugins: RenderPlugins = (type, params = {}, options = {}) => {\n const { wrapper = true, fn = \"render\", filter = v => v, reverse } = options;\n\n const content = plugins\n .byType(type)\n .filter(pl => {\n /**\n * TODO @ts-refactor Problem with possibility of a different subtype.\n */\n // @ts-expect-error\n return filter(pl);\n })\n /**\n * We cast as string because renderPlugin checks for the plugin.name\n */\n .map(plugin => renderPlugin(plugin.name as string, params, { wrapper, fn }))\n .filter(Boolean);\n\n if (reverse) {\n content.reverse();\n }\n\n return wrapper ? (\n <PluginsComponent type={type} params={params} fn={fn}>\n {content}\n </PluginsComponent>\n ) : (\n content\n );\n};\n\nexport default [imagePlugin];\n"],"mappings":"AACA,OAAOA,KAAK,MAAM,OAAO;AACzB,OAAOC,OAAO,MAAM,SAAS;AAC7B,SAASC,OAAO,QAAQ,iBAAiB;AAEzC,OAAOC,WAAW;AAElB,SAASA,WAAW;AA2BpB,MAAMC,eAAe,GAAIC,KAA2B,IAAK;EACrD,OAAOA,KAAK,CAACC,QAAQ;AACzB,CAAC;AAOD,MAAMC,gBAAgB,GAAIF,KAA4B,IAAK;EACvD,OAAOA,KAAK,CAACC,QAAQ;AACzB,CAAC;AAED,OAAO,MAAME,YAA0B,GAAGA,CAACC,IAAI,EAAEC,MAAM,GAAG,CAAC,CAAC,EAAEC,OAAO,GAAG,CAAC,CAAC,KAAK;EAC3E,MAAM;IAAEC,OAAO,GAAG,IAAI;IAAEC,EAAE,GAAG;EAAS,CAAC,GAAGF,OAAO;EAEjD,MAAMG,MAAM,GAAGZ,OAAO,CAACa,MAAM,CAACN,IAAI,CAAC;EACnCR,OAAO,CAACa,MAAM,EAAE,mBAAmBL,IAAI,GAAG,CAAC;EAE3C,IAAI,CAACK,MAAM,EAAE;IACT,OAAO,IAAI;EACf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GAChDG,OACa,CAAC,GAEnBA,OACH;AACL,CAAC;AAED,eAAe,CAACb,WAAW,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
export type SelectedFile = {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
type: string;
|
|
6
|
+
size: number;
|
|
7
|
+
src: {
|
|
8
|
+
file: File;
|
|
9
|
+
base64: string | null;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
export type FileError = {
|
|
13
|
+
id: string;
|
|
14
|
+
type: "unsupportedFileType" | "maxSizeExceeded" | "multipleMaxSizeExceeded" | "multipleMaxCountExceeded" | "multipleNotAllowed";
|
|
15
|
+
index?: number;
|
|
16
|
+
file?: SelectedFile | File;
|
|
17
|
+
multipleFileSize?: number;
|
|
18
|
+
multipleMaxSize?: number;
|
|
19
|
+
multipleMaxCount?: number;
|
|
20
|
+
multipleCount?: number;
|
|
21
|
+
};
|
|
22
|
+
export type BrowseFilesParams = {
|
|
23
|
+
onSuccess?: (files: SelectedFile[]) => void;
|
|
24
|
+
onError?: (errors: FileError[], files: SelectedFile[]) => void;
|
|
25
|
+
};
|
|
26
|
+
export type RenderPropParams = {
|
|
27
|
+
browseFiles: (params: BrowseFilesParams) => void;
|
|
28
|
+
getDropZoneProps: (additionalProps: any) => any;
|
|
29
|
+
getLabelProps: (additionalProps: any) => any;
|
|
30
|
+
validateFiles: (files: SelectedFile[] | File[]) => FileError[];
|
|
31
|
+
};
|
|
32
|
+
export type FilesRules = {
|
|
33
|
+
accept: string[];
|
|
34
|
+
multiple: boolean;
|
|
35
|
+
maxSize: string;
|
|
36
|
+
multipleMaxSize: string;
|
|
37
|
+
multipleMaxCount: number | null;
|
|
38
|
+
convertToBase64: boolean;
|
|
39
|
+
onSuccess?: (files: SelectedFile[]) => void;
|
|
40
|
+
onError?: (errors: FileError[], files: SelectedFile[]) => void;
|
|
41
|
+
};
|
|
42
|
+
export type Props = FilesRules & {
|
|
43
|
+
children: (params: RenderPropParams) => React.ReactNode;
|
|
44
|
+
id?: string;
|
|
45
|
+
};
|
|
46
|
+
export declare class Files extends React.Component<Props> {
|
|
47
|
+
static defaultProps: {
|
|
48
|
+
accept: never[];
|
|
49
|
+
multiple: boolean;
|
|
50
|
+
maxSize: string;
|
|
51
|
+
multipleMaxSize: string;
|
|
52
|
+
multipleMaxCount: null;
|
|
53
|
+
convertToBase64: boolean;
|
|
54
|
+
};
|
|
55
|
+
input: HTMLInputElement | null;
|
|
56
|
+
browseFilesPassedParams: BrowseFilesParams | null;
|
|
57
|
+
id: string;
|
|
58
|
+
validateFiles: (files: SelectedFile[] | File[]) => FileError[];
|
|
59
|
+
processSelectedFiles: (eventFiles: Array<File>) => Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Extracted into a separate method just for testing purposes.
|
|
62
|
+
*/
|
|
63
|
+
onDropFilesHandler: ({ e, onSuccess, onError }: any) => Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Extracted into a separate method just for testing purposes.
|
|
66
|
+
*/
|
|
67
|
+
browseFilesHandler: ({ onSuccess, onError }: any) => void;
|
|
68
|
+
render(): React.JSX.Element;
|
|
69
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import bytes from "bytes";
|
|
3
|
+
import minimatch from "minimatch";
|
|
4
|
+
import { readFileContent } from "./utils/readFileContent";
|
|
5
|
+
import { generateId } from "./utils/generateId";
|
|
6
|
+
export class Files extends React.Component {
|
|
7
|
+
static defaultProps = {
|
|
8
|
+
accept: [],
|
|
9
|
+
multiple: false,
|
|
10
|
+
maxSize: "2mb",
|
|
11
|
+
multipleMaxSize: "10mb",
|
|
12
|
+
multipleMaxCount: null,
|
|
13
|
+
convertToBase64: false
|
|
14
|
+
};
|
|
15
|
+
input = null;
|
|
16
|
+
browseFilesPassedParams = null;
|
|
17
|
+
id = generateId();
|
|
18
|
+
validateFiles = files => {
|
|
19
|
+
const {
|
|
20
|
+
multiple,
|
|
21
|
+
multipleMaxSize,
|
|
22
|
+
multipleMaxCount,
|
|
23
|
+
accept,
|
|
24
|
+
maxSize
|
|
25
|
+
} = this.props;
|
|
26
|
+
const errors = [];
|
|
27
|
+
let multipleFileSize = 0;
|
|
28
|
+
if (!multiple && files.length > 1) {
|
|
29
|
+
errors.push({
|
|
30
|
+
id: generateId(),
|
|
31
|
+
type: "multipleNotAllowed"
|
|
32
|
+
});
|
|
33
|
+
return errors;
|
|
34
|
+
}
|
|
35
|
+
for (let index = 0; index < files.length; index++) {
|
|
36
|
+
const file = files[index];
|
|
37
|
+
if (Array.isArray(accept) && accept.length && !accept.some(type => minimatch(file.type, type))) {
|
|
38
|
+
errors.push({
|
|
39
|
+
id: generateId(),
|
|
40
|
+
index,
|
|
41
|
+
file,
|
|
42
|
+
type: "unsupportedFileType"
|
|
43
|
+
});
|
|
44
|
+
} else if (maxSize) {
|
|
45
|
+
const sizeAsBytes = bytes(maxSize);
|
|
46
|
+
if (sizeAsBytes && file.size > sizeAsBytes) {
|
|
47
|
+
errors.push({
|
|
48
|
+
id: generateId(),
|
|
49
|
+
index,
|
|
50
|
+
file,
|
|
51
|
+
type: "maxSizeExceeded"
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (multiple) {
|
|
56
|
+
multipleFileSize += file.size;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (multiple) {
|
|
60
|
+
const maxMultipleMaxSize = bytes(multipleMaxSize);
|
|
61
|
+
if (maxMultipleMaxSize && multipleMaxSize && multipleFileSize > maxMultipleMaxSize) {
|
|
62
|
+
errors.push({
|
|
63
|
+
id: generateId(),
|
|
64
|
+
type: "multipleMaxSizeExceeded",
|
|
65
|
+
multipleFileSize,
|
|
66
|
+
multipleMaxSize: maxMultipleMaxSize
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
if (multipleMaxCount && files.length > multipleMaxCount) {
|
|
70
|
+
errors.push({
|
|
71
|
+
id: generateId(),
|
|
72
|
+
type: "multipleMaxCountExceeded",
|
|
73
|
+
multipleCount: files.length,
|
|
74
|
+
multipleMaxCount
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return errors;
|
|
79
|
+
};
|
|
80
|
+
processSelectedFiles = async eventFiles => {
|
|
81
|
+
if (eventFiles.length === 0) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const {
|
|
85
|
+
convertToBase64,
|
|
86
|
+
onSuccess,
|
|
87
|
+
onError
|
|
88
|
+
} = this.props;
|
|
89
|
+
const {
|
|
90
|
+
browseFilesPassedParams
|
|
91
|
+
} = this;
|
|
92
|
+
const callbacks = {
|
|
93
|
+
onSuccess,
|
|
94
|
+
onError
|
|
95
|
+
};
|
|
96
|
+
if (browseFilesPassedParams && browseFilesPassedParams.onSuccess) {
|
|
97
|
+
callbacks.onSuccess = browseFilesPassedParams.onSuccess;
|
|
98
|
+
}
|
|
99
|
+
if (browseFilesPassedParams && browseFilesPassedParams.onError) {
|
|
100
|
+
callbacks.onError = browseFilesPassedParams.onError;
|
|
101
|
+
}
|
|
102
|
+
const files = [...eventFiles].map(file => {
|
|
103
|
+
return {
|
|
104
|
+
id: generateId(),
|
|
105
|
+
name: file.name,
|
|
106
|
+
type: file.type,
|
|
107
|
+
size: file.size,
|
|
108
|
+
src: {
|
|
109
|
+
file,
|
|
110
|
+
base64: null
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
const errors = this.validateFiles(files);
|
|
115
|
+
if (errors.length) {
|
|
116
|
+
callbacks.onError && callbacks.onError(errors, files);
|
|
117
|
+
} else {
|
|
118
|
+
if (convertToBase64) {
|
|
119
|
+
for (let i = 0; i < files.length; i++) {
|
|
120
|
+
const file = files[i].src.file;
|
|
121
|
+
files[i].src.base64 = await readFileContent(file);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
callbacks.onSuccess && callbacks.onSuccess(files);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Reset the browseFiles arguments.
|
|
128
|
+
if (this.input) {
|
|
129
|
+
this.input.value = "";
|
|
130
|
+
}
|
|
131
|
+
this.browseFilesPassedParams = null;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Extracted into a separate method just for testing purposes.
|
|
136
|
+
*/
|
|
137
|
+
onDropFilesHandler = async ({
|
|
138
|
+
e,
|
|
139
|
+
onSuccess,
|
|
140
|
+
onError
|
|
141
|
+
}) => {
|
|
142
|
+
this.browseFilesPassedParams = {
|
|
143
|
+
onSuccess,
|
|
144
|
+
onError
|
|
145
|
+
};
|
|
146
|
+
e.dataTransfer && e.dataTransfer.files && (await this.processSelectedFiles(e.dataTransfer.files));
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Extracted into a separate method just for testing purposes.
|
|
151
|
+
*/
|
|
152
|
+
browseFilesHandler = ({
|
|
153
|
+
onSuccess,
|
|
154
|
+
onError
|
|
155
|
+
}) => {
|
|
156
|
+
this.browseFilesPassedParams = {
|
|
157
|
+
onSuccess,
|
|
158
|
+
onError
|
|
159
|
+
};
|
|
160
|
+
this.input && this.input.click();
|
|
161
|
+
};
|
|
162
|
+
render() {
|
|
163
|
+
const {
|
|
164
|
+
multiple,
|
|
165
|
+
accept,
|
|
166
|
+
id
|
|
167
|
+
} = this.props;
|
|
168
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, this.props.children({
|
|
169
|
+
getLabelProps: props => {
|
|
170
|
+
return {
|
|
171
|
+
...props,
|
|
172
|
+
htmlFor: id || this.id
|
|
173
|
+
};
|
|
174
|
+
},
|
|
175
|
+
validateFiles: this.validateFiles,
|
|
176
|
+
browseFiles: ({
|
|
177
|
+
onSuccess,
|
|
178
|
+
onError
|
|
179
|
+
} = {}) => {
|
|
180
|
+
this.browseFilesHandler({
|
|
181
|
+
onSuccess,
|
|
182
|
+
onError
|
|
183
|
+
});
|
|
184
|
+
},
|
|
185
|
+
getDropZoneProps: ({
|
|
186
|
+
onSuccess,
|
|
187
|
+
onError,
|
|
188
|
+
onDragOver,
|
|
189
|
+
onDrop,
|
|
190
|
+
...rest
|
|
191
|
+
} = {}) => {
|
|
192
|
+
return {
|
|
193
|
+
...rest,
|
|
194
|
+
onDragOver: e => {
|
|
195
|
+
e.preventDefault();
|
|
196
|
+
typeof onDragOver === "function" && onDragOver();
|
|
197
|
+
},
|
|
198
|
+
onDrop: async e => {
|
|
199
|
+
e.preventDefault();
|
|
200
|
+
typeof onDrop === "function" && onDrop();
|
|
201
|
+
this.onDropFilesHandler({
|
|
202
|
+
e,
|
|
203
|
+
onSuccess,
|
|
204
|
+
onError
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
}), /*#__PURE__*/React.createElement("input", {
|
|
210
|
+
id: id || this.id,
|
|
211
|
+
ref: ref => {
|
|
212
|
+
if (ref) {
|
|
213
|
+
this.input = ref;
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
accept: accept.join(","),
|
|
217
|
+
style: {
|
|
218
|
+
display: "none"
|
|
219
|
+
},
|
|
220
|
+
type: "file",
|
|
221
|
+
multiple: multiple,
|
|
222
|
+
onChange: e => this.processSelectedFiles(e.target.files ?? [])
|
|
223
|
+
}));
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
//# sourceMappingURL=Files.js.map
|