Class: Inkpen::MarkdownMode

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

Overview

PORO representing markdown mode configuration.

MarkdownMode enables switching between WYSIWYG, raw markdown, and split view editing modes. When enabled, a mode toggle appears allowing users to edit content as markdown source.

Examples:

Basic usage

markdown_mode = Inkpen::MarkdownMode.new(enabled: true)

With split view default

markdown_mode = Inkpen::MarkdownMode.new(
  enabled: true,
  default_mode: :split,
  sync_delay: 500
)

In editor

editor = Inkpen::Editor.new(
  name: "post[body]",
  markdown_mode: Inkpen::MarkdownMode.new(enabled: true)
)

See Also:

Author:

  • Nauman Tariq

Since:

  • 0.5.0

Constant Summary collapse

MODES =

Valid editing modes.

Returns:

  • (Array<Symbol>)

Since:

  • 0.5.0

%i[wysiwyg markdown split].freeze
TOGGLE_PLACEMENTS =

Valid placements for the mode toggle.

Returns:

  • (Array<Symbol>)

Since:

  • 0.5.0

%i[top inline].freeze
DEFAULT_SYNC_DELAY =

Default sync delay in milliseconds for split view.

Returns:

  • (Integer)

Since:

  • 0.5.0

300

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enabled: false, default_mode: :wysiwyg, show_toggle: true, toggle_placement: :top, toolbar_button: false, sync_delay: DEFAULT_SYNC_DELAY, keyboard_shortcuts: true) ⇒ MarkdownMode

Initialize a new markdown mode configuration.

Parameters:

  • enabled (Boolean) (defaults to: false)

    whether markdown mode is enabled

  • default_mode (Symbol) (defaults to: :wysiwyg)

    initial mode (:wysiwyg, :markdown, :split)

  • show_toggle (Boolean) (defaults to: true)

    show mode toggle button

  • toggle_placement (Symbol) (defaults to: :top)

    mode toggle placement (:top, :inline)

  • toolbar_button (Boolean) (defaults to: false)

    show markdown toggle in formatting toolbar

  • sync_delay (Integer) (defaults to: DEFAULT_SYNC_DELAY)

    debounce delay for split sync (ms)

  • keyboard_shortcuts (Boolean) (defaults to: true)

    enable keyboard shortcuts

Since:

  • 0.5.0



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/inkpen/markdown_mode.rb', line 83

def initialize(
  enabled: false,
  default_mode: :wysiwyg,
  show_toggle: true,
  toggle_placement: :top,
  toolbar_button: false,
  sync_delay: DEFAULT_SYNC_DELAY,
  keyboard_shortcuts: true
)
  @enabled = enabled
  @default_mode = validate_mode(default_mode)
  @show_toggle = show_toggle
  @toggle_placement = validate_toggle_placement(toggle_placement)
  @toolbar_button = toolbar_button
  @sync_delay = sync_delay.to_i
  @keyboard_shortcuts = keyboard_shortcuts
end

Instance Attribute Details

#default_modeSymbol (readonly)

Returns the default editing mode.

Returns:

  • (Symbol)

    the default editing mode



70
71
72
# File 'lib/inkpen/markdown_mode.rb', line 70

def default_mode
  @default_mode
end

#enabledBoolean (readonly)

Returns whether markdown mode is enabled.

Returns:

  • (Boolean)

    whether markdown mode is enabled



70
# File 'lib/inkpen/markdown_mode.rb', line 70

attr_reader :default_mode, :show_toggle, :toggle_placement, :toolbar_button, :sync_delay, :keyboard_shortcuts

#keyboard_shortcutsObject (readonly)

Since:

  • 0.5.0



70
# File 'lib/inkpen/markdown_mode.rb', line 70

attr_reader :default_mode, :show_toggle, :toggle_placement, :toolbar_button, :sync_delay, :keyboard_shortcuts

#show_toggleBoolean (readonly)

Returns whether to show the mode toggle button.

Returns:

  • (Boolean)

    whether to show the mode toggle button



70
# File 'lib/inkpen/markdown_mode.rb', line 70

attr_reader :default_mode, :show_toggle, :toggle_placement, :toolbar_button, :sync_delay, :keyboard_shortcuts

#sync_delayInteger (readonly)

Returns debounce delay for split view sync (ms).

Returns:

  • (Integer)

    debounce delay for split view sync (ms)



70
# File 'lib/inkpen/markdown_mode.rb', line 70

attr_reader :default_mode, :show_toggle, :toggle_placement, :toolbar_button, :sync_delay, :keyboard_shortcuts

#toggle_placementSymbol (readonly)

Returns where to place the mode toggle (:top, :inline).

Returns:

  • (Symbol)

    where to place the mode toggle (:top, :inline)



70
# File 'lib/inkpen/markdown_mode.rb', line 70

attr_reader :default_mode, :show_toggle, :toggle_placement, :toolbar_button, :sync_delay, :keyboard_shortcuts

#toolbar_buttonBoolean (readonly)

Returns whether to show markdown toggle button in main toolbar.

Returns:

  • (Boolean)

    whether to show markdown toggle button in main toolbar



70
# File 'lib/inkpen/markdown_mode.rb', line 70

attr_reader :default_mode, :show_toggle, :toggle_placement, :toolbar_button, :sync_delay, :keyboard_shortcuts

Instance Method Details

#data_attributesHash

Generate data attributes for Stimulus controller.

Returns:

  • (Hash)

    data attributes hash

Since:

  • 0.5.0



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/inkpen/markdown_mode.rb', line 151

def data_attributes
  {
    "inkpen--editor-markdown-enabled-value" => enabled?.to_s,
    "inkpen--editor-markdown-mode-value" => default_mode.to_s,
    "inkpen--editor-markdown-show-toggle-value" => show_toggle.to_s,
    "inkpen--editor-markdown-toggle-placement-value" => toggle_placement.to_s,
    "inkpen--editor-markdown-toolbar-button-value" => toolbar_button.to_s,
    "inkpen--editor-markdown-sync-delay-value" => sync_delay.to_s,
    "inkpen--editor-markdown-shortcuts-value" => keyboard_shortcuts.to_s
  }
end

#enabled?Boolean

Check if markdown mode is enabled.

Returns:

  • (Boolean)

    true if enabled

Since:

  • 0.5.0



106
107
108
# File 'lib/inkpen/markdown_mode.rb', line 106

def enabled?
  @enabled
end

#inline_toggle?Boolean

Check if toggle should render inline with fixed toolbar.

Returns:

  • (Boolean)

    true if toggle placement is inline

Since:

  • 0.5.0



142
143
144
# File 'lib/inkpen/markdown_mode.rb', line 142

def inline_toggle?
  toggle_placement == :inline
end

#markdown_default?Boolean

Check if the default mode is markdown.

Returns:

  • (Boolean)

    true if default is markdown

Since:

  • 0.5.0



124
125
126
# File 'lib/inkpen/markdown_mode.rb', line 124

def markdown_default?
  default_mode == :markdown
end

#split_default?Boolean

Check if the default mode is split view.

Returns:

  • (Boolean)

    true if default is split

Since:

  • 0.5.0



133
134
135
# File 'lib/inkpen/markdown_mode.rb', line 133

def split_default?
  default_mode == :split
end

#to_configHash

Convert to configuration hash for JavaScript.

Returns:

  • (Hash)

    configuration hash

Since:

  • 0.5.0



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/inkpen/markdown_mode.rb', line 168

def to_config
  {
    enabled: enabled?,
    defaultMode: default_mode.to_s,
    showToggle: show_toggle,
    togglePlacement: toggle_placement.to_s,
    toolbarButton: toolbar_button,
    syncDelay: sync_delay,
    keyboardShortcuts: keyboard_shortcuts
  }
end

#to_json(*args) ⇒ String

Convert to JSON representation.

Returns:

  • (String)

    JSON string

Since:

  • 0.5.0



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

def to_json(*args)
  to_config.to_json(*args)
end

#wysiwyg_default?Boolean

Check if the default mode is WYSIWYG.

Returns:

  • (Boolean)

    true if default is WYSIWYG

Since:

  • 0.5.0



115
116
117
# File 'lib/inkpen/markdown_mode.rb', line 115

def wysiwyg_default?
  default_mode == :wysiwyg
end