Class: Inkpen::Editor

Inherits:
Object
  • Object
show all
Defined in:
lib/inkpen/editor.rb

Overview

PORO representing an editor instance.

The Editor class encapsulates all configuration for a single TipTap editor instance. It generates the necessary data attributes for the Stimulus controller and handles form integration.

Examples:

Basic usage

editor = Inkpen::Editor.new(name: "post[body]")

With all options

editor = Inkpen::Editor.new(
  name: "post[body]",
  value: @post.body,
  toolbar: :floating,
  extensions: [:bold, :italic, :slash_commands],
  extension_config: {
    mention: { searchUrl: "/editor/mentions.json" }
  },
  placeholder: "Start writing...",
  autosave: true,
  autosave_interval: 5000,
  min_height: "200px",
  max_height: "600px"
)

See Also:

Author:

  • Nauman Tariq

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, value: nil, **options) ⇒ Editor

Initialize a new editor instance.

Parameters:

  • name (String)

    the form field name (required)

  • value (String, nil) (defaults to: nil)

    initial HTML content

  • options (Hash)

    additional configuration options

Options Hash (**options):

  • :toolbar (Symbol) — default: :floating

    toolbar style

  • :sticky_toolbar (StickyToolbar)

    sticky toolbar config

  • :markdown_mode (MarkdownMode)

    markdown mode config

  • :extensions (Array<Symbol>)

    list of extensions

  • :extension_config (Hash)

    per-extension config

  • :placeholder (String)

    placeholder text

  • :autosave (Boolean)

    enable autosave

  • :autosave_interval (Integer)

    autosave interval (ms)

  • :min_height (String)

    minimum height

  • :max_height (String)

    maximum height

  • :html (Hash)

    additional HTML attributes

Since:

  • 0.1.0



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/inkpen/editor.rb', line 102

def initialize(name:, value: nil, **options)
  @name = name
  @value = value
  @toolbar = options.fetch(:toolbar, Inkpen.configuration.toolbar)
  @sticky_toolbar = options.fetch(:sticky_toolbar, nil)
  @markdown_mode = options.fetch(:markdown_mode, nil)
  @extensions = options.fetch(:extensions, Inkpen.configuration.extensions)
  @extension_config = options.fetch(:extension_config, {})
  @placeholder = options.fetch(:placeholder, Inkpen.configuration.placeholder)
  @autosave = options.fetch(:autosave, Inkpen.configuration.autosave)
  @autosave_interval = options.fetch(:autosave_interval, Inkpen.configuration.autosave_interval)
  @min_height = options.fetch(:min_height, Inkpen.configuration.min_height)
  @max_height = options.fetch(:max_height, Inkpen.configuration.max_height)
  @html_attributes = options.fetch(:html, {})
end

Instance Attribute Details

#autosaveBoolean (readonly)

Returns whether autosave is enabled.

Returns:

  • (Boolean)

    whether autosave is enabled



79
80
81
# File 'lib/inkpen/editor.rb', line 79

attr_reader :name, :value, :toolbar, :sticky_toolbar, :markdown_mode, :extensions,
:extension_config, :placeholder, :autosave, :autosave_interval,
:min_height, :max_height, :html_attributes

#autosave_intervalInteger (readonly)

Returns autosave interval in milliseconds.

Returns:

  • (Integer)

    autosave interval in milliseconds



79
80
81
# File 'lib/inkpen/editor.rb', line 79

attr_reader :name, :value, :toolbar, :sticky_toolbar, :markdown_mode, :extensions,
:extension_config, :placeholder, :autosave, :autosave_interval,
:min_height, :max_height, :html_attributes

#extension_configHash (readonly)

Returns per-extension configuration options.

Returns:

  • (Hash)

    per-extension configuration options



79
80
81
# File 'lib/inkpen/editor.rb', line 79

attr_reader :name, :value, :toolbar, :sticky_toolbar, :markdown_mode, :extensions,
:extension_config, :placeholder, :autosave, :autosave_interval,
:min_height, :max_height, :html_attributes

#extensionsArray<Symbol> (readonly)

Returns list of enabled extensions.

Returns:

  • (Array<Symbol>)

    list of enabled extensions



79
80
81
# File 'lib/inkpen/editor.rb', line 79

attr_reader :name, :value, :toolbar, :sticky_toolbar, :markdown_mode, :extensions,
:extension_config, :placeholder, :autosave, :autosave_interval,
:min_height, :max_height, :html_attributes

#html_attributesObject (readonly)

Since:

  • 0.1.0



79
80
81
# File 'lib/inkpen/editor.rb', line 79

attr_reader :name, :value, :toolbar, :sticky_toolbar, :markdown_mode, :extensions,
:extension_config, :placeholder, :autosave, :autosave_interval,
:min_height, :max_height, :html_attributes

#markdown_modeMarkdownMode? (readonly)

Returns markdown mode configuration.

Returns:



79
80
81
# File 'lib/inkpen/editor.rb', line 79

attr_reader :name, :value, :toolbar, :sticky_toolbar, :markdown_mode, :extensions,
:extension_config, :placeholder, :autosave, :autosave_interval,
:min_height, :max_height, :html_attributes

#max_heightString? (readonly)

Returns maximum editor height (CSS value).

Returns:

  • (String, nil)

    maximum editor height (CSS value)



79
80
81
# File 'lib/inkpen/editor.rb', line 79

attr_reader :name, :value, :toolbar, :sticky_toolbar, :markdown_mode, :extensions,
:extension_config, :placeholder, :autosave, :autosave_interval,
:min_height, :max_height, :html_attributes

#min_heightString? (readonly)

Returns minimum editor height (CSS value).

Returns:

  • (String, nil)

    minimum editor height (CSS value)



79
80
81
# File 'lib/inkpen/editor.rb', line 79

attr_reader :name, :value, :toolbar, :sticky_toolbar, :markdown_mode, :extensions,
:extension_config, :placeholder, :autosave, :autosave_interval,
:min_height, :max_height, :html_attributes

#nameString (readonly)

Returns the form field name (e.g., “post”).

Returns:

  • (String)

    the form field name (e.g., “post”)



79
80
81
# File 'lib/inkpen/editor.rb', line 79

def name
  @name
end

#placeholderString (readonly)

Returns placeholder text when editor is empty.

Returns:

  • (String)

    placeholder text when editor is empty



79
80
81
# File 'lib/inkpen/editor.rb', line 79

attr_reader :name, :value, :toolbar, :sticky_toolbar, :markdown_mode, :extensions,
:extension_config, :placeholder, :autosave, :autosave_interval,
:min_height, :max_height, :html_attributes

#sticky_toolbarStickyToolbar? (readonly)

Returns sticky toolbar configuration.

Returns:



79
80
81
# File 'lib/inkpen/editor.rb', line 79

attr_reader :name, :value, :toolbar, :sticky_toolbar, :markdown_mode, :extensions,
:extension_config, :placeholder, :autosave, :autosave_interval,
:min_height, :max_height, :html_attributes

#toolbarSymbol (readonly)

Returns toolbar style (:floating, :fixed, :none).

Returns:

  • (Symbol)

    toolbar style (:floating, :fixed, :none)



79
80
81
# File 'lib/inkpen/editor.rb', line 79

attr_reader :name, :value, :toolbar, :sticky_toolbar, :markdown_mode, :extensions,
:extension_config, :placeholder, :autosave, :autosave_interval,
:min_height, :max_height, :html_attributes

#valueString? (readonly)

Returns the initial HTML content.

Returns:

  • (String, nil)

    the initial HTML content



79
80
81
# File 'lib/inkpen/editor.rb', line 79

attr_reader :name, :value, :toolbar, :sticky_toolbar, :markdown_mode, :extensions,
:extension_config, :placeholder, :autosave, :autosave_interval,
:min_height, :max_height, :html_attributes

Instance Method Details

#data_attributesHash

Generate data attributes for the Stimulus controller.

Returns a hash with nested :data key for proper Rails tag.attributes handling.

Returns:

  • (Hash)

    data attributes hash

Since:

  • 0.1.0



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/inkpen/editor.rb', line 126

def data_attributes
  controllers = ["inkpen--editor"]
  controllers << "inkpen--sticky-toolbar" if sticky_toolbar&.enabled?

  attrs = {
    data: {
      controller: controllers.join(" "),
      "inkpen--editor-extensions-value" => extensions.to_json,
      "inkpen--editor-extension-config-value" => extension_config.to_json,
      "inkpen--editor-toolbar-value" => toolbar.to_s,
      "inkpen--editor-placeholder-value" => placeholder,
      "inkpen--editor-autosave-value" => autosave.to_s,
      "inkpen--editor-autosave-interval-value" => autosave_interval.to_s
    }
  }

  # Merge sticky toolbar data attributes if enabled
  if sticky_toolbar&.enabled?
    attrs[:data].merge!(sticky_toolbar.data_attributes)
  end

  # Merge markdown mode data attributes if enabled
  if markdown_mode&.enabled?
    attrs[:data].merge!(markdown_mode.data_attributes)
  end

  attrs
end

#extension_enabled?(name) ⇒ Boolean

Check if a specific extension is enabled.

Examples:

editor.extension_enabled?(:slash_commands) # => true

Parameters:

  • name (Symbol, String)

    the extension name to check

Returns:

  • (Boolean)

    true if the extension is enabled

Since:

  • 0.1.0



176
177
178
# File 'lib/inkpen/editor.rb', line 176

def extension_enabled?(name)
  extensions.include?(name.to_sym)
end

#input_idString

Generate a safe HTML ID from the field name.

Converts bracket notation to underscores for valid HTML IDs.

Examples:

editor = Editor.new(name: "post[body]")
editor.input_id # => "post_body"

Returns:

  • (String)

    safe HTML ID

Since:

  • 0.1.0



200
201
202
# File 'lib/inkpen/editor.rb', line 200

def input_id
  name.to_s.gsub(/[\[\]]/, "_").gsub(/_+/, "_").chomp("_")
end

#input_nameString

Get the input field name for form submission.

Returns:

  • (String)

    the form field name

Since:

  • 0.1.0



185
186
187
# File 'lib/inkpen/editor.rb', line 185

def input_name
  name
end

#style_attributesString

Generate inline styles for the editor container.

Returns:

  • (String)

    CSS style string

Since:

  • 0.1.0



160
161
162
163
164
165
# File 'lib/inkpen/editor.rb', line 160

def style_attributes
  styles = []
  styles << "min-height: #{min_height}" if min_height
  styles << "max-height: #{max_height}" if max_height
  styles.join("; ")
end