Module: SMEditor::Rails::FormHelper

Defined in:
lib/smeditor/rails/form_helper.rb

Overview

View helper that mounts a self-contained SMEditor inside a Rails form. The gem ships the browser bundle and default theme itself; host apps do not need npm, React, esbuild, importmap pins, or jsbundling-rails.

Instance Method Summary collapse

Instance Method Details

#smeditor_assets ⇒ Object

Render the packaged JS/CSS once for this view context. This makes the form helper work without editing the host layout. Apps that prefer to include assets in can call smeditor_assets there and disable auto inclusion with config.auto_include_assets = false.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/smeditor/rails/form_helper.rb', line 13

def smeditor_assets
  if defined?(@_smeditor_assets_rendered) && @_smeditor_assets_rendered
    "".html_safe
  else
    @_smeditor_assets_rendered = true
    safe_join([
      stylesheet_link_tag("smeditor", "data-turbo-track": "reload"),
      javascript_include_tag("smeditor", defer: true, "data-turbo-track": "reload"),
    ])
  end
end

#smeditor_editor(form, method, options = {}) ⇒ Object

form - a Rails FormBuilder method - the model attribute (stored as an HTML string) options - :kit ("starter" | "full"), :class, :placeholder, :theme ("light" | "dark"), :upload_url, :label, :include_assets, :min_height, :tablet_min_height, :mobile_min_height



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
# File 'lib/smeditor/rails/form_helper.rb', line 31

def smeditor_editor(form, method, options = {})
  object = form.object
  current = object.respond_to?(method) ? object.public_send(method) : nil
  kit = (options[:kit] || SMEditor.config.default_kit).to_s
  theme = smeditor_theme(options.fetch(:theme, SMEditor.config.default_theme))
  upload_url = options.fetch(:upload_url, smeditor_upload_url)
  include_assets = options.fetch(:include_assets, SMEditor.config.auto_include_assets)
  stylesheet_url = asset_path("smeditor.css")
  min_height = smeditor_css_length(options.fetch(:min_height, SMEditor.config.min_height), :min_height)
  tablet_min_height = smeditor_css_length(options.fetch(:tablet_min_height, SMEditor.config.tablet_min_height), :tablet_min_height)
  mobile_min_height = smeditor_css_length(options.fetch(:mobile_min_height, SMEditor.config.mobile_min_height), :mobile_min_height)

  hidden = form.hidden_field(
    method,
    value: current.to_s,
    data: { smeditor_input: true },
  )

  mount = (
    :div,
    "",
    class: "smeditor-mount",
    data: {
      smeditor: true,
      smeditor_kit: kit,
      smeditor_theme: theme,
      smeditor_placeholder: options[:placeholder],
      smeditor_upload_url: upload_url,
      smeditor_label: options[:label],
      smeditor_stylesheet: stylesheet_url,
      smeditor_min_height: min_height,
      smeditor_tablet_min_height: tablet_min_height,
      smeditor_mobile_min_height: mobile_min_height,
    }.compact,
  )

  wrapper_class = ["smeditor-field", options[:class]].compact.join(" ")
  field = (:div, safe_join([hidden, mount]), class: wrapper_class)

  include_assets ? safe_join([smeditor_assets, field]) : field
end