Class: Redactor4Input

Inherits:
SimpleForm::Inputs::TextInput
  • Object
show all
Defined in:
app/inputs/redactor4_input.rb

Overview

Custom SimpleForm input for Redactor 4 WYSIWYG editor
Uses Stimulus controller for lazy loading and proper Turbo lifecycle handling

Usage:
<%= f.input :body, as: :redactor4 %>
<%= f.input :body, as: :redactor4, mode: :blog %> # Blog mode with AI Tools
<%= f.input :body, as: :redactor4, mode: :email %> # Email template mode
<%= f.input :body, as: :redactor4, ai: true %> # Enable AI Tools explicitly

Options:

  • mode: Editor mode (:classic, :blog, :email) - default: :classic
  • ai: Enable AI Tools (default: false, auto-enabled for :blog mode)
  • min_height: Custom minimum height (default: '300px')
  • max_height: Custom maximum height (e.g. '70vh', 'false' for unlimited)
  • air: Enable air mode (floating toolbar)

Modes:

  • :classic - Standard editor without AI
  • :blog - Blog post editor with AI Tools enabled
  • :email - Email template editor with merge tags, clips, etc.

Instance Method Summary collapse

Instance Method Details

#input(wrapper_options = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/inputs/redactor4_input.rb', line 25

def input(wrapper_options = nil)
  merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)

  # Extract our custom options before passing to the text_area
  mode = merged_input_options.delete(:mode)&.to_sym || :classic
  enable_ai = merged_input_options.delete(:ai)

  # Add Stimulus target attribute
  merged_input_options[:data] ||= {}
  merged_input_options[:data][:redactor4_init_target] = 'editor'

  # Add form-control class for Bootstrap styling
  merged_input_options[:class] = Array(merged_input_options[:class])
  merged_input_options[:class] << 'form-control' unless merged_input_options[:class].include?('form-control')

  # Build Stimulus controller data attributes
  controller_data = { controller: 'redactor4-init' }

  # Set mode-specific values
  case mode
  when :email
    controller_data[:redactor4_init_email_value] = true
  when :blog
    controller_data[:redactor4_init_blog_value] = true
    # Blog mode automatically enables AI unless explicitly disabled
    enable_ai = true if enable_ai.nil?
  end

  # Explicit AI override (can force AI on in classic mode or off in blog mode)
  if enable_ai == true && mode != :blog
    controller_data[:redactor4_init_blog_value] = true # AI requires blog mode
  end

  # Support for custom options passed via input_html
  controller_data[:redactor4_init_min_height_value] = merged_input_options.delete(:min_height) if merged_input_options[:min_height]

  if merged_input_options.key?(:max_height)
    mh = merged_input_options.delete(:max_height)
    controller_data[:redactor4_init_max_height_value] = (mh == false ? 'false' : mh.to_s)
  end

  controller_data[:redactor4_init_air_value] = merged_input_options.delete(:air) if merged_input_options[:air]

  # Source code (HTML) editing - enabled for:
  # - Blog mode (always enabled for content editors)
  # - Admins (full access)
  # - Marketing managers (need to paste/edit raw HTML for blog posts)
  can_use_source = mode == :blog ||
                   template.&.is_admin? ||
                   template.&.is_marketing_manager?
  controller_data[:redactor4_init_source_value] = can_use_source || false

  # Wrap textarea in a div with the Stimulus controller
  template.tag.div(data: controller_data) do
    @builder.text_area(attribute_name, merged_input_options)
  end
end

#input_html_classesObject



83
84
85
# File 'app/inputs/redactor4_input.rb', line 83

def input_html_classes
  super
end