abhaynikam/react-trix-rte

Is there a way to change the RAILS_DIRECT_UPLOADS_URL?

Closed this issue ยท 8 comments

mech commented

Hi,

Great Trix integration with React. Want to ask if there is a way to change the URL for RAILS_DIRECT_UPLOADS_URL since our Rails API server is on another host.

Regards

@mech RAILS_DIRECT_UPLOADS_URL is a constant relative URL right now. I like the idea to send a prop to customize the URL. Do you want to work on it? We are about to release a new version of the gem. This would be a great addition ๐Ÿ˜Š

mech commented

Sure, let me try it ๐Ÿ˜€

mech commented

Hi, I have made a pull request at #44

However, I want to ask you a question. When I drag a file to the Trix editor, it does not seems to react, is there any additional step to perform?

However, I want to ask you a question. When I drag a file to the Trix editor, it does not seem to react, is there any additional step to perform?

Yeah. We need to add onAttachmentAdd listener and handle the file addition. In the case of Rails, importing @rails/actiontext adds listener here: https://github.com/rails/rails/blob/main/actiontext/app/javascript/actiontext/index.js

mech commented

Tks for the info. Just for those looking into it. Here's what I did:

import Trix from "trix";
import { ReactTrixRTEInput } from "react-trix-rte";
import "@rails/actiontext";

At the Rails API end, need to do much more effort:

  1. Setup config.action_controller.default_url_options to combat the example.org issue
  2. skip_before_action :verify_authenticity_token for all ActiveStorage controller if you do not need CSRF
  3. Make use of _blob.html.erb if you want a downloadable PDF link

Looking forward to get the pull request merged ๐Ÿ˜€

Thanks, @mech โค๏ธ . Yeah in the Rails application importing "@rails/actiontext"; is important. ActionText form builder also relies on the same to handle the document attach.

We are close on closing this issue. Thank you @mech for your contribution ๐Ÿ˜Š

mech commented

I saw your Rails contribution on this rails/rails#39113 did not receive anymore attention. I am wondering if I can do this at the config/application.rb instead?

config.after_initialize do
  ActionText::Attachments::TrixConversion.class_eval do
    def to_trix_attachment(content = trix_attachment_content)
      attributes = full_attributes.dup
      attributes["content"] = content if content
      attributes["url"] = Rails.application.routes.url_helpers.rails_blob_url(preview_image.blob, only_path: false) if previewable_attachable? && previewable?
      ActionText::TrixAttachment.from_attributes(attributes)
    end
  end
end

Will using class_eval be the right way to sort of address it ATM?

Fixed in #44