From 9b84d4665c1a36a7df8fe3b69a681ce755318509 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Tue, 7 Jun 2022 20:46:39 +0200
Subject: [PATCH] image

---
 .../communication/block/template/base.rb      |  2 -
 .../communication/block/template/chapter.rb   |  2 +
 .../blocks/components/image/_edit.html.erb    | 88 +++++++------------
 .../blocks/components/image/_preview.html.erb |  4 -
 .../blocks/components/image/_static.html.erb  |  6 --
 .../components/rich_text/_edit.html.erb       |  3 +-
 .../blocks/components/string/_edit.html.erb   |  3 +-
 .../blocks/components/text/_edit.html.erb     |  3 +-
 .../blocks/templates/chapter/_edit.html.erb   | 21 ++++-
 .../templates/chapter/_preview.html.erb       |  2 +
 .../blocks/templates/chapter/_static.html.erb |  2 +
 .../blocks/templates/gallery/_edit.html.erb   | 12 +--
 config/locales/communication/en.yml           | 19 ++--
 13 files changed, 77 insertions(+), 90 deletions(-)

diff --git a/app/models/communication/block/template/base.rb b/app/models/communication/block/template/base.rb
index 4d869b184..03ea74c82 100644
--- a/app/models/communication/block/template/base.rb
+++ b/app/models/communication/block/template/base.rb
@@ -23,8 +23,6 @@ class Communication::Block::Template::Base
 
   def self.has_image(property)
     has_component property, :image
-    has_component "#{property}_alt".to_sym, :string
-    has_component "#{property}_credit".to_sym, :string
   end
 
   def self.has_layouts(list)
diff --git a/app/models/communication/block/template/chapter.rb b/app/models/communication/block/template/chapter.rb
index a744759c1..e0d9a26b1 100644
--- a/app/models/communication/block/template/chapter.rb
+++ b/app/models/communication/block/template/chapter.rb
@@ -2,6 +2,8 @@ class Communication::Block::Template::Chapter < Communication::Block::Template::
   has_rich_text :text
   has_rich_text :notes
   has_image :image
+  has_string :image_alt
+  has_rich_text :image_credit
 
   def build_git_dependencies
     add_dependency image&.blob
diff --git a/app/views/admin/communication/blocks/components/image/_edit.html.erb b/app/views/admin/communication/blocks/components/image/_edit.html.erb
index 9da7edf0e..803c28f5a 100644
--- a/app/views/admin/communication/blocks/components/image/_edit.html.erb
+++ b/app/views/admin/communication/blocks/components/image/_edit.html.erb
@@ -1,57 +1,37 @@
-<div class="card">
-  <div class="card-header">
-    <%= t "admin.communication.blocks.templates.#{@block.template_kind}.edit.#{property}.label" %>
-  </div>
-  <div class="card-body">
-    <div class="row">
-      <div class="col-xl-3 col-6 mb-3">
-        <div v-if="!data.<%= property %>">
-          <label  class="form-label"
-                  for="<%= property %>">
-            <%= t 'admin.communication.blocks.components.image.input.label' %>
-          </label>
-          <input  class="form-control"
-                  type="file"
-                  accept="image/*"
-                  @change="onFileImageChange( $event, data, '<%= property %>' )"
-                  id="<%= property %>">
-        </div>
-        <div v-if="data.<%= property %>">
-          <img  :src="getImageUrl(data.<%= property %>)"
-                class="img-fluid"
-                />
-          <br>
-        </div>
-      </div>
-      <div class="col-xl-9">
-        <label  class="form-label"
-                for="<%= property %>_alt">
-          <%= t 'admin.communication.blocks.components.image.alt.label' %>
-        </label>
-        <input  id="<%= property %>_alt"
-                type="text"
-                class="form-control mb-3"
-                v-model="data.<%= property %>_alt"
-                placeholder="<%= t 'admin.communication.blocks.components.image.alt.placeholder' %>" />
-
-        <label  class="form-label"
-                for="<%= property %>_credit">
-          <%= t 'admin.communication.blocks.components.image.credit.label' %>
-        </label>
-        <input  id="<%= property %>_credit"
-                type="text"
-                class="form-control mb-3"
-                v-model="data.<%= property %>_credit"
-                placeholder="<%= t 'admin.communication.blocks.components.image.credit.placeholder' %>" />
+<%
+element ||= false
+if element
+  model = 'element'
+  dom_id = "'element-' + index + '-#{property}'"
+else
+  model = 'data'
+  dom_id = "#{property}"
+end
+label ||= t 'admin.communication.blocks.components.image.input.label'
+remove ||= t 'admin.communication.blocks.components.image.input.remove'
+%>
+<div class="mb-3">
 
-        <a  class="btn btn-sm btn-danger"
-            v-on:click="data.<%= property %>=null"
-            v-if="data.<%= property %>">
-            <i class="fas fa-times"></i>
-          <%= t 'admin.communication.blocks.components.image.input.remove' %>
-        </a>
-
-      </div>
-    </div>
+  <div v-if="!<%= model %>.<%= property %>">
+    <label  class="form-label"
+            :for="<%= dom_id.html_safe %>">
+      <%= label %>
+    </label>
+    <input  class="form-control"
+            type="file"
+            accept="image/*"
+            @change="onFileImageChange( $event, <%= model %>, '<%= property %>' )"
+            :id="<%= dom_id.html_safe %>">
+  </div>
+  <div v-if="<%= model %>.<%= property %>">
+    <img  :src="getImageUrl(<%= model %>.<%= property %>)"
+          class="img-fluid mb-3"
+          />
   </div>
+  <a  class="btn btn-sm btn-danger"
+      v-on:click="<%= model %>.<%= property %>=null"
+      v-if="<%= model %>.<%= property %>">
+      <i class="fas fa-times"></i>
+    <%= remove %>
+  </a>
 </div>
diff --git a/app/views/admin/communication/blocks/components/image/_preview.html.erb b/app/views/admin/communication/blocks/components/image/_preview.html.erb
index bc4ff0d8f..76241efe7 100644
--- a/app/views/admin/communication/blocks/components/image/_preview.html.erb
+++ b/app/views/admin/communication/blocks/components/image/_preview.html.erb
@@ -1,11 +1,7 @@
 <%
 image = @block.template.public_send property
 blob = image['blob'] if image&.has_key? 'blob'
-alt = @block.template.public_send "#{property}_alt"
-credit = @block.template.public_send "#{property}_credit"
 %>
 <%= kamifusen_tag blob,
                   width: 600,
-                  alt: alt,
                   class: 'img-fluid' if blob %>
-<p class="small"><%= credit %></p>
diff --git a/app/views/admin/communication/blocks/components/image/_static.html.erb b/app/views/admin/communication/blocks/components/image/_static.html.erb
index 48a859ef7..de58b80a8 100644
--- a/app/views/admin/communication/blocks/components/image/_static.html.erb
+++ b/app/views/admin/communication/blocks/components/image/_static.html.erb
@@ -1,16 +1,10 @@
 <%
 image = @block.template.public_send(property)
 blob = image['blob'] if image&.has_key? 'blob'
-alt = @block.template.public_send "#{property}_alt"
-credit = @block.template.public_send "#{property}_credit"
 indentation = '      '
 %>
 <% if blob %>
 <%= indentation %>image:
 <%= indentation %>  id: "<%= blob.id %>"
 <%= indentation %>  file: "<%= blob.id %>"
-<%= indentation %>  alt: >-
-<%= indentation %>    <%= prepare_text_for_static alt %>
-<%= indentation %>  credit: >-
-<%= indentation %>    <%= prepare_text_for_static credit %>
 <% end %>
diff --git a/app/views/admin/communication/blocks/components/rich_text/_edit.html.erb b/app/views/admin/communication/blocks/components/rich_text/_edit.html.erb
index 4de0040c6..9209df1b5 100644
--- a/app/views/admin/communication/blocks/components/rich_text/_edit.html.erb
+++ b/app/views/admin/communication/blocks/components/rich_text/_edit.html.erb
@@ -1,13 +1,14 @@
 <%
 template_kind = @block.template_kind
 i18n_template = "admin.communication.blocks.templates.#{template_kind}.edit"
+element ||= false
 if element
   model = 'element'
   dom_id = "'element-' + index + '-#{property}'"
   i18n_component = "#{i18n_template}.element.#{property}"
 else
   model = 'data'
-  dom_id = property
+  dom_id = "#{property}"
   i18n_component = "#{i18n_template}.#{property}"
 end
 label ||= t "#{i18n_component}.label"
diff --git a/app/views/admin/communication/blocks/components/string/_edit.html.erb b/app/views/admin/communication/blocks/components/string/_edit.html.erb
index 7aaad01b0..6e771afbd 100644
--- a/app/views/admin/communication/blocks/components/string/_edit.html.erb
+++ b/app/views/admin/communication/blocks/components/string/_edit.html.erb
@@ -1,13 +1,14 @@
 <%
 template_kind = @block.template_kind
 i18n_template = "admin.communication.blocks.templates.#{template_kind}.edit"
+element ||= false
 if element
   model = 'element'
   dom_id = "'element-' + index + '-#{property}'"
   i18n_component = "#{i18n_template}.element.#{property}"
 else
   model = 'data'
-  dom_id = property
+  dom_id = "#{property}"
   i18n_component = "#{i18n_template}.#{property}"
 end
 label ||= t "#{i18n_component}.label"
diff --git a/app/views/admin/communication/blocks/components/text/_edit.html.erb b/app/views/admin/communication/blocks/components/text/_edit.html.erb
index 8d9645640..434a82345 100644
--- a/app/views/admin/communication/blocks/components/text/_edit.html.erb
+++ b/app/views/admin/communication/blocks/components/text/_edit.html.erb
@@ -1,13 +1,14 @@
 <%
 template_kind = @block.template_kind
 i18n_template = "admin.communication.blocks.templates.#{template_kind}.edit"
+element ||= false
 if element
   model = 'element'
   dom_id = "'element-' + index + '-#{property}'"
   i18n_component = "#{i18n_template}.element.#{property}"
 else
   model = 'data'
-  dom_id = property
+  dom_id = "#{property}"
   i18n_component = "#{i18n_template}.#{property}"
 end
 label ||= t "#{i18n_component}.label"
diff --git a/app/views/admin/communication/blocks/templates/chapter/_edit.html.erb b/app/views/admin/communication/blocks/templates/chapter/_edit.html.erb
index c455ecd9b..a2e775537 100644
--- a/app/views/admin/communication/blocks/templates/chapter/_edit.html.erb
+++ b/app/views/admin/communication/blocks/templates/chapter/_edit.html.erb
@@ -4,6 +4,25 @@
     <%= block_component_edit :notes %>
   </div>
   <div class="col-md-6">
-    <%= block_component_edit :image %>
+    <div class="card">
+      <div class="card-header">
+        <%= t "admin.communication.blocks.templates.chapter.edit.image.label" %>
+      </div>
+      <div class="card-body">
+        <%= block_component_edit :image %>
+        <%= block_component_edit :image_alt,
+              label: t('admin.communication.blocks.components.image.alt.label'),
+              placeholder: t('admin.communication.blocks.components.image.alt.placeholder') %>
+        <%= block_component_edit :image_credit,
+              label: t('admin.communication.blocks.components.image.credit.label'),
+              placeholder: t('admin.communication.blocks.components.image.credit.placeholder') %>
+        <div class="row">
+          <div class="col-xl-3 col-6 mb-3">
+          </div>
+          <div class="col-xl-9">
+          </div>
+        </div>
+      </div>
+    </div>
   </div>
 </div>
diff --git a/app/views/admin/communication/blocks/templates/chapter/_preview.html.erb b/app/views/admin/communication/blocks/templates/chapter/_preview.html.erb
index 9a02f6ae9..53312438f 100644
--- a/app/views/admin/communication/blocks/templates/chapter/_preview.html.erb
+++ b/app/views/admin/communication/blocks/templates/chapter/_preview.html.erb
@@ -1,3 +1,5 @@
 <%= block_component_preview :image %>
+<%= block_component_preview :image_alt %>
+<%= block_component_preview :image_credit %>
 <%= block_component_preview :notes %>
 <%= block_component_preview :text %>
diff --git a/app/views/admin/communication/blocks/templates/chapter/_static.html.erb b/app/views/admin/communication/blocks/templates/chapter/_static.html.erb
index fc5903037..af81bc62d 100644
--- a/app/views/admin/communication/blocks/templates/chapter/_static.html.erb
+++ b/app/views/admin/communication/blocks/templates/chapter/_static.html.erb
@@ -1,3 +1,5 @@
 <%= block_component_static :text %>
 <%= block_component_static :notes %>
 <%= block_component_static :image %>
+<%= block_component_static :image_alt %>
+<%= block_component_static :image_credit %>
diff --git a/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb b/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb
index 66fe346e6..2005bce92 100644
--- a/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb
+++ b/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb
@@ -24,18 +24,8 @@ element = @block.template.default_element
           </a>
         </div>
       </div>
-      <img :src="getImageUrl(element.file)" class="img-fluid d-block" v-if="element.file.id" />
       <div class="card-body">
-        <label  class="form-label"
-                :for="'element-' + index + '-file'">
-          <%= t '.image_label' %>
-        </label>
-        <input  class="form-control mb-3"
-                type="file"
-                accept="image/*"
-                @change="onFileImageChange( $event, element, 'file' )"
-                :id="'element-' + index + '-file'">
-
+        <%= block_component_edit :image, element: element %>
         <%= block_component_edit :alt, element: element %>
         <%= block_component_edit :credit, element: element %>
         <%= block_component_edit :text, element: element %>
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index 99515360a..bfd4da71d 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -220,15 +220,16 @@ en:
                 label: Carousel
                 description: Images are big, with a previous / next navigation and a constant height.
             edit:
-              add_image: Add image
-              image_label: Image (.png, .jpg)
-              alt_label: Alternative text
-              alt_placeholder: Enter text description
-              credit_label: Credit
-              credit_placeholder: Enter image's credit here
-              text_label: Text
-              text_placeholder: Enter text
-              remove_image: Remove image
+              element:
+                text:
+                  label: Text
+                  placeholder: Enter text
+                alt:
+                  label: Alternative text
+                  placeholder: Enter text description
+                credit:
+                  label: Credit
+                  placeholder: Enter image's credit here
           image:
             description: An image with its alternative description, a credit and a text.
           key_figures:
-- 
GitLab