Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(image-upload): add acceptedFileTypes option #305

Conversation

dancormier
Copy link
Contributor

@dancormier dancormier commented Apr 12, 2024

This PR targets #304 and updates the image upload code to use the acceptedFileTypes option to:

  • Populate the file input's accept attribute
  • Create the "Supported file types" string
  • Provide the file types to display in validation message

TODO

  • Add tests for updated UI

@dancormier dancormier added the enhancement New feature or request label Apr 12, 2024
@dancormier dancormier changed the base branch from klustig/dynamic-accepted-file-types to main April 12, 2024 22:27
@dancormier dancormier changed the base branch from main to klustig/dynamic-accepted-file-types April 12, 2024 22:28
Copy link
Collaborator

@kristinalustig kristinalustig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mostly just have a few questions for my own education/curiosity but I do think these updates meet our requirements. Once we've gone through those we can merge this into my original PR.

@@ -141,12 +144,9 @@ export class ImageUploader extends PluginInterfaceView<
this.uploadField = document.createElement("input");
this.uploadField.type = "file";
this.uploadField.className = "js-image-uploader-input v-visible-sr";
this.uploadField.accept = "image/*";
this.uploadField.accept = acceptedFileTypes?.join(", ");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this make it so that it no longer reads with an "or" in the penultimate position?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property populates the accept attribute on the input itself by taking the array provided from the acceptedFileTypes option and joining values to create a string.

With the default acceptedFileTypes value, we get this:

<input  accept="image/jpeg, image/png, image/gif">

<div class="flex--item">${acceptedFileTypeString}</div>
<div class="flex--item">(Max size 2 MiB)</div>
</div>
</label>, drag & drop<span class="js-external-url-trigger-container d-none">, <button type="button" class="s-btn s-btn__link js-external-url-trigger">enter a link</button></span>, or paste an image.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these functionally different? They look the same to me when the string is displayed or not. Your correction seems like it's too complex for what we're trying to accomplish, but I may be missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is purely a whitespace change. There was an extra space character at the end of the line that isn't necessary. I'm not sure why GitHub isn't showing it that way, but this is the diff for the change of line 158 (shortened for clarity):

-                 </label>[…] or paste an image. 
+                 </label>[…] or paste an image.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol thanks github!

return acceptedTypesString;
uploadCaptionString = acceptedTypes
.join(", ")
.replace(/image\//g, "");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why even have the image bit if we're just removing it any time we're displaying it? In this context it will always be an image.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're removing image/ when we're displaying it, but not always when we're using it. I figured it made the most sense to stick to the MIME type to imply the connection to the file input's accept attribute. I'd be fine with inverting this if you think the API would be simpler to instead have the consumer provide a list of file extensions.

Let me know what you think and I'd be happy to make changes if you'd like.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured it made the most sense to stick to the MIME type to imply the connection to the file input's accept attribute.

FWIW sticking to the MIME type is a good call in my opinion. It will be easier for us to convey to the consumers how this option field should be populated - Furthermore people familiar with the HTML spec of input type="file" will have pretty much a zero learning curve. 🙂

@dancormier dancormier marked this pull request as ready for review April 15, 2024 16:46
@dancormier dancormier changed the title Dcormier/dynamic accepted file types feat(image-upload): add acceptedFileTypes option Apr 15, 2024
@kristinalustig kristinalustig merged commit 5cdca73 into klustig/dynamic-accepted-file-types Apr 18, 2024
1 check passed
@kristinalustig kristinalustig deleted the dcormier/dynamic-accepted-file-types branch April 18, 2024 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants