]> git.openstreetmap.org Git - rails.git/commitdiff
Refactor diary entry forms to use a custom form builder
authorAndy Allan <git@gravitystorm.co.uk>
Thu, 24 Dec 2020 13:31:04 +0000 (13:31 +0000)
committerAndy Allan <git@gravitystorm.co.uk>
Thu, 24 Dec 2020 13:32:41 +0000 (13:32 +0000)
app/assets/stylesheets/common.scss
app/views/diary_entries/_form.html.erb
app/views/diary_entries/edit.html.erb
app/views/diary_entries/new.html.erb
app/views/shared/_html_help.erb [new file with mode: 0644]
app/views/shared/_markdown_help.html.erb [new file with mode: 0644]
app/views/shared/_richtext_field.html.erb [new file with mode: 0644]
config/locales/en.yml
lib/richtext_form_builder.rb [new file with mode: 0644]

index 7ae0577402301d1f30e4e213f7bfe0fbc6a88cb5..d8369e3ce3bfb40ddcae194981e8f16b20f128f1 100644 (file)
@@ -1281,8 +1281,6 @@ tr.turn:hover {
 
 .diary_entries {
   #map {
-    position: relative;
-    width: 90%;
     height: 400px;
     border: 1px solid $grey;
     display: none;
index 9585b09eb635d1697133edeff8df7fc957f6fd32..ed7164e88b1f4d89830dad9756094ba943a1f20f 100644 (file)
@@ -1,35 +1,19 @@
-<div class="diary_entry standard-form">
-  <fieldset>
-    <div class='standard-form-row'>
-      <label class="standard-label"><%= t ".subject" -%></label>
-      <%= f.text_field :title, :class => "richtext_title" %>
-    </div>
-    <div class='standard-form-row'>
-      <label class="standard-label"><%= t ".body" -%></label>
-      <%= richtext_area :diary_entry, :body, :cols => 80, :rows => 20, :format => @diary_entry.body_format %>
+<%= f.text_field :title %>
+<%= f.richtext_field :body, :cols => 80, :rows => 20, :format => @diary_entry.body_format %>
+<%= f.collection_select :language_code, Language.order(:english_name), :code, :name %>
+
+<fieldset>
+  <legend><%= t ".location" -%></legend>
+
+  <%= tag.div "", :id => "map", :data => { :lat => @lat, :lon => @lon, :zoom => @zoom } %>
+
+  <div class="form-row">
+    <%= f.text_field :latitude, :wrapper_class => "col-sm-4", :id => "latitude" %>
+    <%= f.text_field :longitude, :wrapper_class => "col-sm-4", :id => "longitude" %>
+    <div class="col-sm-4">
+      <label><a href="#" id="usemap"><%= t ".use_map_link" -%></a></label>
     </div>
-    <div class='standard-form-row'>
-      <label class="standard-label"><%= t ".language" -%></label>
-      <%= f.collection_select :language_code, Language.order(:english_name), :code, :name %>
   </div>
-  </fieldset>
-  <fieldset class='location'>
-    <label class="standard-label"><%= t ".location" -%></label>
-    <%= tag.div "", :id => "map", :data => { :lat => @lat, :lon => @lon, :zoom => @zoom } %>
-    <div class='standard-form-row clearfix'>
-      <div class='form-column'>
-        <label class="secondary standard-label"><%= t ".latitude" -%></label>
-        <%= f.text_field :latitude, :size => 20, :id => "latitude" %>
-      </div>
-      <div class='form-column'>
-        <label class="secondary standard-label"><%= t ".longitude" -%></label>
-        <%= f.text_field :longitude, :size => 20, :id => "longitude" %>
-      </div>
-      <div class='form-column'>
-        <a href="#" id="usemap"><%= t ".use_map_link" -%></a>
-      </div>
-    </div>
-  </fieldset>
+</fieldset>
 
-  <%= f.submit %>
-</div>
+<%= f.primary %>
index 5ea6193280c357e5d55e2036df03aad1da49bdfe..9d03e774ab27fdf2dcee2f07c4ba4392daf772de 100644 (file)
@@ -6,8 +6,6 @@
   <h1><%= @title %></h1>
 <% end %>
 
-<%= error_messages_for "diary_entry" %>
-
-<%= form_for @diary_entry, :url => diary_entry_path(current_user, @diary_entry), :html => { :method => :put } do |f| %>
+<%= form_for @diary_entry, :builder => RichtextFormBuilder, :url => diary_entry_path(current_user, @diary_entry), :html => { :method => :put } do |f| %>
   <%= render :partial => "form", :locals => { :f => f } %>
 <% end %>
index dfe69f22a203e795f93d41d2bd5cd3aceac0aab9..9d8ad61708b5104ff8f42c8251babd3a382bf850 100644 (file)
@@ -6,8 +6,6 @@
   <h1><%= @title %></h1>
 <% end %>
 
-<%= error_messages_for "diary_entry" %>
-
-<%= form_for @diary_entry do |f| %>
+<%= form_for @diary_entry, :builder => RichtextFormBuilder do |f| %>
   <%= render :partial => "form", :locals => { :f => f } %>
 <% end %>
diff --git a/app/views/shared/_html_help.erb b/app/views/shared/_html_help.erb
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/app/views/shared/_markdown_help.html.erb b/app/views/shared/_markdown_help.html.erb
new file mode 100644 (file)
index 0000000..5bd8c49
--- /dev/null
@@ -0,0 +1,19 @@
+<h5><%= t ".title_html" %></h5>
+<dl>
+  <dt><%= t ".headings" %></dt>
+  <dd># <%= t ".heading" %><br>
+      ## <%= t ".subheading" %></dd>
+  <dt><%= t ".unordered" %></dt>
+  <dd>* <%= t ".first" %><br>
+     * <%= t ".second" %></dd>
+
+  <dt><%= t ".ordered" %></dt>
+  <dd>1. <%= t ".first" %><br>
+     2. <%= t ".second" %></dd>
+
+  <dt><%= t ".link" %></dt>
+  <dd>[<%= t ".text" %>](<%= t ".url" %>)</dd>
+
+  <dt><%= t ".image" %></dt>
+  <dd>![<%= t ".alt" %>](<%= t ".url" %>)</dd>
+</dl>
diff --git a/app/views/shared/_richtext_field.html.erb b/app/views/shared/_richtext_field.html.erb
new file mode 100644 (file)
index 0000000..eef9823
--- /dev/null
@@ -0,0 +1,18 @@
+<div class="form-group">
+  <label><%= object.class.human_attribute_name(attribute) %></label>
+  <div id="<%= id %>_container" class="form-row richtext_container">
+    <div id="<%= id %>_content" class="col-sm-8 mb-3 richtext_content">
+      <%= builder.text_area(attribute, options.merge(:wrapper => false, "data-preview-url" => preview_url(:type => type))) %>
+      <div id="<%= id %>_preview" class="richtext_preview richtext"></div>
+    </div>
+    <div id="<%= id %>_help" class="col-sm-4 richtext_help">
+      <div class="card bg-light">
+        <div class="card-body">
+          <%= render :partial => "shared/#{type}_help" %>
+          <%= submit_tag t(".edit"), :id => "#{id}_doedit", :class => "richtext_doedit btn btn-primary", :disabled => true %>
+          <%= submit_tag t(".preview"), :id => "#{id}_dopreview", :class => "richtext_dopreview btn btn-primary" %>
+        </div>
+      </div>
+    </div>
+  </div>
+</div>
index 21ea7e106a40f8b940c8e8e41de753a5e76aac8c..ebbe71b2ceb32c01ef3cc1b7b1a933556c2776f8 100644 (file)
@@ -399,13 +399,8 @@ en:
     new:
       title: New Diary Entry
     form:
-      subject: "Subject:"
-      body: "Body:"
-      language: "Language:"
-      location: "Location:"
-      latitude: "Latitude:"
-      longitude: "Longitude:"
-      use_map_link: "use map"
+      location: Location
+      use_map_link: Use Map
     index:
       title: "Users' diaries"
       title_friends: "Friends' diaries"
@@ -1578,6 +1573,24 @@ en:
       as_unread: "Message marked as unread"
     destroy:
       destroyed: "Message deleted"
+  shared:
+    markdown_help:
+      title_html: Parsed with <a href="https://kramdown.gettalong.org/quickref.html">kramdown</a>
+      headings: Headings
+      heading: Heading
+      subheading: Subheading
+      unordered: Unordered list
+      ordered: Ordered list
+      first: First item
+      second: Second item
+      link: Link
+      text: Text
+      image: Image
+      alt: Alt text
+      url: URL
+    richtext_field:
+      edit: Edit
+      preview: Preview
   site:
     about:
       next: Next
diff --git a/lib/richtext_form_builder.rb b/lib/richtext_form_builder.rb
new file mode 100644 (file)
index 0000000..4198f38
--- /dev/null
@@ -0,0 +1,15 @@
+class RichtextFormBuilder < BootstrapForm::FormBuilder
+  def richtext_field(attribute, options = {})
+    id = "#{@object_name}_#{attribute}"
+    type = options.delete(:format) || "markdown"
+
+    @template.render(:partial => "shared/richtext_field",
+                     :locals => { :object => @object,
+                                  :attribute => attribute,
+                                  :object_name => @object_name,
+                                  :id => id,
+                                  :type => type,
+                                  :options => options,
+                                  :builder => self })
+  end
+end