Skip to content

Commit 1afb101

Browse files
Fix being able to select fnt files on ios (#8843)
1 parent 5c86948 commit 1afb101

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

newIDE/app/src/ResourcesList/FileToCloudProjectResourceUploader.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,20 @@ const getAcceptedMimeTypes = (resourceKind: ResourceKind): string[] => {
7878
return resourceKindToInputAcceptedMimes[resourceKind] || [];
7979
};
8080

81+
const usesFilePseudoMime = (resourceKind: ResourceKind): boolean => {
82+
const acceptedMimes = getAcceptedMimeTypes(resourceKind);
83+
return acceptedMimes.length === 1 && acceptedMimes[0] === 'file';
84+
};
85+
8186
export const getInputAcceptedMimesAndExtensions = (
8287
resourceKind: ResourceKind
8388
): string => {
89+
// iOS Safari/WKWebView maps each extension of the `accept` attribute to a mime type,
90+
// dropping the unrecognized ones. If at least one is recognized (like .xml), the picker
91+
// is restricted to it, greying out the others (like .fnt). So when the 'file' pseudo-mime
92+
// is used, don't send any extension: validation happens post-picking.
93+
if (usesFilePseudoMime(resourceKind)) return 'file';
94+
8495
const acceptedExtensions = getAcceptedExtensions(resourceKind);
8596
const acceptedMimes = getAcceptedMimeTypes(resourceKind);
8697

@@ -205,11 +216,9 @@ export const FileToCloudProjectResourceUploader = ({
205216

206217
const shouldValidateFilePostPicking = React.useMemo(
207218
() => {
208-
const acceptedMimeTypes = getAcceptedMimeTypes(options.resourceKind);
209-
// Safari does not use file extensions to filter files pre-picking and
210-
// Safari also does not recognize all mime types. So if the only accepted
211-
// mime type is 'file', the file validation should happen post-picking.
212-
return acceptedMimeTypes.length === 1 && acceptedMimeTypes[0] === 'file';
219+
// If the only accepted mime type is the 'file' pseudo-mime, no filtering
220+
// happens pre-picking, so the file validation should happen post-picking.
221+
return usesFilePseudoMime(options.resourceKind);
213222
},
214223
[options.resourceKind]
215224
);

newIDE/app/src/ResourcesList/FileToCloudProjectResourceUploader.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('FileToCloudProjectResourceUploader', () => {
99
);
1010
expect(
1111
getInputAcceptedMimesAndExtensions('bitmapFont')
12-
).toMatchInlineSnapshot(`"file,.fnt,.xml"`);
12+
).toMatchInlineSnapshot(`"file"`);
1313
expect(getInputAcceptedMimesAndExtensions('font')).toMatchInlineSnapshot(
1414
`"font/ttf,font/otf,.ttf,.otf"`
1515
);
@@ -29,10 +29,10 @@ describe('FileToCloudProjectResourceUploader', () => {
2929
`"video/mp4,video/webm,.mp4,.webm"`
3030
);
3131
expect(getInputAcceptedMimesAndExtensions('model3D')).toMatchInlineSnapshot(
32-
`"file,.glb"`
32+
`"file"`
3333
);
3434
expect(getInputAcceptedMimesAndExtensions('atlas')).toMatchInlineSnapshot(
35-
`"file,.atlas"`
35+
`"file"`
3636
);
3737
expect(getInputAcceptedMimesAndExtensions('spine')).toMatchInlineSnapshot(
3838
`"application/json,.json"`

0 commit comments

Comments
 (0)