Class: Inkpen::Editor
- Inherits:
-
Object
- Object
- Inkpen::Editor
- 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.
Instance Attribute Summary collapse
-
#autosave ⇒ Boolean
readonly
Whether autosave is enabled.
-
#autosave_interval ⇒ Integer
readonly
Autosave interval in milliseconds.
-
#extension_config ⇒ Hash
readonly
Per-extension configuration options.
-
#extensions ⇒ Array<Symbol>
readonly
List of enabled extensions.
- #html_attributes ⇒ Object readonly
-
#markdown_mode ⇒ MarkdownMode?
readonly
Markdown mode configuration.
-
#max_height ⇒ String?
readonly
Maximum editor height (CSS value).
-
#min_height ⇒ String?
readonly
Minimum editor height (CSS value).
-
#name ⇒ String
readonly
The form field name (e.g., “post”).
-
#placeholder ⇒ String
readonly
Placeholder text when editor is empty.
-
#sticky_toolbar ⇒ StickyToolbar?
readonly
Sticky toolbar configuration.
-
#toolbar ⇒ Symbol
readonly
Toolbar style (:floating, :fixed, :none).
-
#value ⇒ String?
readonly
The initial HTML content.
Instance Method Summary collapse
-
#data_attributes ⇒ Hash
Generate data attributes for the Stimulus controller.
-
#extension_enabled?(name) ⇒ Boolean
Check if a specific extension is enabled.
-
#initialize(name:, value: nil, **options) ⇒ Editor
constructor
Initialize a new editor instance.
-
#input_id ⇒ String
Generate a safe HTML ID from the field name.
-
#input_name ⇒ String
Get the input field name for form submission.
-
#style_attributes ⇒ String
Generate inline styles for the editor container.
Constructor Details
#initialize(name:, value: nil, **options) ⇒ Editor
Initialize a new editor instance.
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, **) @name = name @value = value @toolbar = .fetch(:toolbar, Inkpen.configuration.) @sticky_toolbar = .fetch(:sticky_toolbar, nil) @markdown_mode = .fetch(:markdown_mode, nil) @extensions = .fetch(:extensions, Inkpen.configuration.extensions) @extension_config = .fetch(:extension_config, {}) @placeholder = .fetch(:placeholder, Inkpen.configuration.placeholder) @autosave = .fetch(:autosave, Inkpen.configuration.autosave) @autosave_interval = .fetch(:autosave_interval, Inkpen.configuration.autosave_interval) @min_height = .fetch(:min_height, Inkpen.configuration.min_height) @max_height = .fetch(:max_height, Inkpen.configuration.max_height) @html_attributes = .fetch(:html, {}) end |
Instance Attribute Details
#autosave ⇒ Boolean (readonly)
Returns 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_interval ⇒ Integer (readonly)
Returns 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_config ⇒ Hash (readonly)
Returns 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 |
#extensions ⇒ Array<Symbol> (readonly)
Returns 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_attributes ⇒ Object (readonly)
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_mode ⇒ MarkdownMode? (readonly)
Returns markdown mode configuration.
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_height ⇒ String? (readonly)
Returns 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_height ⇒ String? (readonly)
Returns 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 |
#name ⇒ String (readonly)
Returns the form field name (e.g., “post”).
79 80 81 |
# File 'lib/inkpen/editor.rb', line 79 def name @name end |
#placeholder ⇒ String (readonly)
Returns 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_toolbar ⇒ StickyToolbar? (readonly)
Returns sticky toolbar configuration.
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 |
#toolbar ⇒ Symbol (readonly)
Returns 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 |
#value ⇒ String? (readonly)
Returns 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_attributes ⇒ Hash
Generate data attributes for the Stimulus controller.
Returns a hash with nested :data key for proper Rails tag.attributes handling.
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 &.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" => .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 &.enabled? attrs[:data].merge!(.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.
176 177 178 |
# File 'lib/inkpen/editor.rb', line 176 def extension_enabled?(name) extensions.include?(name.to_sym) end |
#input_id ⇒ String
Generate a safe HTML ID from the field name.
Converts bracket notation to underscores for valid HTML IDs.
200 201 202 |
# File 'lib/inkpen/editor.rb', line 200 def input_id name.to_s.gsub(/[\[\]]/, "_").gsub(/_+/, "_").chomp("_") end |
#input_name ⇒ String
Get the input field name for form submission.
185 186 187 |
# File 'lib/inkpen/editor.rb', line 185 def input_name name end |
#style_attributes ⇒ String
Generate inline styles for the editor container.
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 |