Merge remote-tracking branch 'upstream/pull/3045'

This commit is contained in:
Tom Hughes 2021-01-13 20:31:21 +00:00
commit 7f3b86e30b
6 changed files with 50 additions and 31 deletions

View file

@ -6,6 +6,6 @@
<h1><%= @title %></h1>
<% end %>
<%= form_for @diary_entry, :builder => RichtextFormBuilder, :url => diary_entry_path(current_user, @diary_entry), :html => { :method => :put } do |f| %>
<%= bootstrap_form_for @diary_entry, :url => diary_entry_path(current_user, @diary_entry), :html => { :method => :put } do |f| %>
<%= render :partial => "form", :locals => { :f => f } %>
<% end %>

View file

@ -6,6 +6,6 @@
<h1><%= @title %></h1>
<% end %>
<%= form_for @diary_entry, :builder => RichtextFormBuilder do |f| %>
<%= bootstrap_form_for @diary_entry do |f| %>
<%= render :partial => "form", :locals => { :f => f } %>
<% end %>

View file

@ -1,17 +1,14 @@
<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 text-break"></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 id="<%= id %>_container" class="form-row richtext_container">
<div id="<%= id %>_content" class="col-sm-8 mb-3 mb-sm-0 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 text-break"></div>
</div>
<div id="<%= id %>_help" class="col-sm-4 richtext_help">
<div class="card bg-light h-100">
<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>

View file

@ -0,0 +1,2 @@
# Include our custom RichtextField input method for `f.richtext_field` in forms
BootstrapForm::FormBuilder.include BootstrapForm::Inputs::RichtextField

View file

@ -0,0 +1,35 @@
# frozen_string_literal: true
# A custom richtext_field form group. By using form_group_builder we get to use
# the built-in methods for generating labels and help text.
module BootstrapForm
module Inputs
module RichtextField
extend ActiveSupport::Concern
include Base
# It's not clear to me why this needs to be duplicated from the upstream BootstrapForm::FormBuilder class
delegate :content_tag, :capture, :concat, :tag, :to => :@template
included do
def richtext_field_with_bootstrap(name, options = {})
id = "#{@object_name}_#{name}"
type = options.delete(:format) || "markdown"
form_group_builder(name, options) do
@template.render(:partial => "shared/richtext_field",
:locals => { :object => @object,
:attribute => name,
:object_name => @object_name,
:id => id,
:type => type,
:options => options,
:builder => self })
end
end
alias_method :richtext_field, :richtext_field_with_bootstrap
end
end
end
end

View file

@ -1,15 +0,0 @@
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