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

Limit accept attachment file types, fix missing id and make Trix compatible with modals #1135

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

davilajose23
Copy link

@davilajose23 davilajose23 commented Feb 19, 2024

Context

While integrating Trix, I found a conflict if I wanted to attach files and Trix was inside a modal, as discussed in issue #1039. Additionally, I needed to restrict the attached files to only images. During debugging, I also noticed that the input element was missing an ID, a problem previously reported in issue #1088 by @chadrschroeder. To address this, I monkey-patched it in my implementation and decided to create this pull request.

Changes

  1. Add an option to limit the file types when attaching a file.
  2. Inserting the input file at the same level as the trix-editor instead of doing it at the end of the body
  3. Add the missing fileInputId based on Restore Missing fileInputId Property and Add acceptedFileTypes Property #1089

To solve 2 and 3 I had to use the editorController instance as an argument of the pickFiles function instead of just using the callback.
Let me know if you consider that not all the changes are needed or if there are any issues with the code.

How it works

Trix will still accept all file types as an attachment by default, but now a trix-attachment-accept attribute is available, which takes a comma-separated list of allowed file extensions or MIME types.

<trix-editor trix-attachment-accept="image/png, image/jpg, image/jpeg"></trix-editor>
<trix-editor trix-attachment-accept=".png, .jpg, .jpeg"></trix-editor>
<trix-editor trix-attachment-accept="image/*"></trix-editor>

@ghiculescu
Copy link

Or this one please @afcapel - the undefined ID is a pretty big nuisance :(

pickFiles(editorController) {
const editorElement = editorController.element
const acceptTypes = editorElement.getAttribute("trix-attachment-accept") || this.acceptedFileTypes
const input = makeElement("input", { type: "file", multiple: true, hidden: true, id: this.fileInputId, accept: acceptTypes })

Choose a reason for hiding this comment

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

Not for this PR specifically, but should we allow customizing whether it allows multiple files or not?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants