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

Paste and upload image into Trix editor from Clipboard #1278

Conversation

wakproductions
Copy link
Contributor

Context

As an author using the Trix rich text editor, I would like to insert images into my page content by copy/paste or drag-and-drop directly into the editor.

Changes proposed in this pull request

Reads the Trix file-accept event to read the details of any pasted file and:

  1. Automatically uploads the file data into the media picker
  2. Inserts the newly added file into the Trix editor

Guidance to review

Give it a spin!

spina-copy-paste-image-demo

<%=t 'spina.media_library.upload_from_device' %>
</button>
<% end %>
<%= render Spina::Forms::FileUploadComponent.new(origin: 'media-picker', media_folder: @media_folder, turbo_frame: 'media_picker') %>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored the media picker file upload functionality into its own component, which is reused by the automatic file uploader. There is similar code on the media library page which could be refactored to use this component as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only thing getting in the way of converting the upload form on app/views/spina/admin/images/index.html.erb so that it uses the same component is the styling of the button, which I'd either have to make consistent or do a block to allow dependency injection for customization of its appearance.
image
image

Spina::Forms::EditorInsertImagesMetaComponent
.new(images: @images, trix_target_id: params[:trix_target_id])
.render_in(view_context)
)
else
render turbo_stream: turbo_stream.prepend("images", partial: "image", collection: @images)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes use of existing ImagesController to perform the same processing logic, just rerouting the output depending on where the upload originated from:

  • Media Picker Page
  • Media Libary Page
  • Auto File Upload Component

@@ -13,6 +13,8 @@

<!-- Make sure all modals are rendered in this frame -->
<%= turbo_frame_tag "modal" %>

<%= render Spina::Forms::AutoFileUploadComponent.new %>
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 sits at the bottom of every page like the modal section and just listens for the auto-file-upload:start event to do its thing. It takes the file which should be included in that event, puts it into the file component form, and automates a Turbo request to ImagesController.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it necessary to put this in the layout? Now it's also rendered on pages without Trix where it's not being used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to include that within page parts that just use Trix, but that did not work because it uses a hidden <form> to submit the dropped file. The form did not work because the entire page is wrapped within its own <form> and you can't have a form nested within a form.

I could not come up with a simple way to put the AutoFileUploadComponent outside of the page <form> and have it intelligent enough to know whether Trix was used somewhere in that form or not.

@Bramjetten
Copy link
Contributor

👏👏👏

@Bramjetten
Copy link
Contributor

Anything in particular that needs some reviewing before we merge this into Spina?

@wakproductions
Copy link
Contributor Author

Anything in particular that needs some reviewing before we merge this into Spina?

I don't think so. It works as much as I've been able to test it. I will make a followup PR tweaking the event handlers in spina-blog so that you can use this over there as well.

I tried to write a Capybara test for this, but simulating the paste into the headless browser did not seem possible.

</div>
<%#= render Spina::Forms::FileUploadComponent.new(id: f.object.object_id, css_classes: 'hidden', turbo_frame: 'page_content') %>
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops. That's an artifact of my earlier attempt to contain the auto upload form to the Trix-related components only. Didn't work here because of the form that encloses the whole page content editing sections. I'll remove this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's removed


insertImages() {
const imagesMetadata = this.element.querySelectorAll('div.trix-insert-image')
console.log('Inserting images', imagesMetadata)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe remove this console.log call

Copy link
Contributor Author

Choose a reason for hiding this comment

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

gone

@Bramjetten Bramjetten merged commit f7e3712 into SpinaCMS:main Aug 1, 2023
1 check passed
@Bramjetten
Copy link
Contributor

Thank you very much @wakproductions! 👏

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.

2 participants