diff --git a/app/javascript/controllers/upload_preview_controller.js b/app/javascript/controllers/upload_preview_controller.js index 4ce38ae9a..14cc5bfee 100644 --- a/app/javascript/controllers/upload_preview_controller.js +++ b/app/javascript/controllers/upload_preview_controller.js @@ -1,20 +1,14 @@ import { Controller } from "@hotwired/stimulus" export default class extends Controller { - static values = { defaultImage: String } - static targets = [ "image", "input", "button" ] + static targets = [ "image", "input" ] previewImage() { const file = this.inputTarget.files[0] if (file) { - this.imageTarget.src = URL.createObjectURL(this.inputTarget.files[0]); - this.imageTarget.onload = () => { URL.revokeObjectURL(this.imageTarget.src) } + this.imageTarget.src = URL.createObjectURL(file) + this.imageTarget.onload = () => URL.revokeObjectURL(this.imageTarget.src) } } - - clear() { - this.imageTarget.src = this.defaultImageValue - this.buttonTarget.style.visibility = "hidden" - } } diff --git a/app/views/bubbles/_image.html.erb b/app/views/bubbles/_image.html.erb index 522871aeb..829733c15 100644 --- a/app/views/bubbles/_image.html.erb +++ b/app/views/bubbles/_image.html.erb @@ -1,10 +1,10 @@
- <%= image_tag bubble.image.attached? ? bubble.image : "", data: { upload_preview_target: "image" } %> + <%= image_tag bubble.image.presence || "", data: { upload_preview_target: "image" } %>
<% if bubble.image.attached? %> - <%= button_to bubble_image_path(bubble), method: :delete, class: "btn btn--plain", style: "--btn-icon-size: 3em; margin-block-start: -0.5em; margin-inline-end: -0.2em", data: { action: "click->upload-preview#clear", upload_preview_target: "button" } do %> + <%= button_to bubble_image_path(bubble), method: :delete, class: "btn btn--plain", style: "--btn-icon-size: 3em; margin-block-start: -0.5em; margin-inline-end: -0.2em" do %> Remove image <% end %> @@ -12,8 +12,9 @@ <%= form_with model: bubble, data: { controller: "form" } do |form| %> <% end %>