Class: TinyMCE::Rails::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/tinymce/rails/configuration.rb

Defined Under Namespace

Classes: Function

Constant Summary collapse

FUNCTION_REGEX =
/^function\s*\(/
RELATIVE_PATH_REGEX =
/^(\/|\.{1,2})\S*/
COMMA =
",".freeze
SPACE =
" ".freeze
SEMICOLON =
";".freeze
OPTION_SEPARATORS =
{
  "plugins"                       => COMMA,
  "custom_elements"               => COMMA,
  "entities"                      => COMMA,
  "extended_valid_elements"       => COMMA,
  "font_formats"                  => SEMICOLON,
  "fontsize_formats"              => COMMA,
  "invalid_elements"              => COMMA,
  "block_formats"                 => SEMICOLON,
  "valid_children"                => COMMA,
  "valid_elements"                => COMMA,
  "body_id"                       => COMMA,
  "body_class"                    => COMMA,
  "content_css"                   => COMMA,
  "tabfocus_elements"             => COMMA,
  "table_clone_elements"          => SPACE,
  "paste_word_valid_elements"     => COMMA,
  "paste_webkit_styles"           => SPACE,
  "paste_retain_style_properties" => SPACE,
  "spellchecker_languages"        => COMMA
}
OPTION_TRANSFORMERS =
{
  # Check for files provided in the content_css option to replace them with their actual path.
  # If no corresponding stylesheet is found for a file, it will remain unchanged.
  "content_css" => ->(value) {
    helpers = ActionView::Base.new(ActionView::LookupContext.new([]), {}, nil)
    separator = OPTION_SEPARATORS["content_css"]

    value.split(separator).map { |file|
      next file if RELATIVE_PATH_REGEX =~ file
      helpers.stylesheet_path(file.strip) || file
    }.join(separator)
  }
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Configuration

Returns a new instance of Configuration.



63
64
65
# File 'lib/tinymce/rails/configuration.rb', line 63

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



61
62
63
# File 'lib/tinymce/rails/configuration.rb', line 61

def options
  @options
end

Class Method Details

.defaultsObject



11
12
13
14
15
16
# File 'lib/tinymce/rails/configuration.rb', line 11

def self.defaults
  {
    "selector"     => "textarea.tinymce",
    "cache_suffix" => "?v=#{VERSION}"
  }
end

.new_with_defaults(options = {}) ⇒ Object



67
68
69
70
71
# File 'lib/tinymce/rails/configuration.rb', line 67

def self.new_with_defaults(options={})
  config = new(defaults)
  config = config.merge(options) if options
  config
end

Instance Method Details

#merge(options) ⇒ Object



88
89
90
# File 'lib/tinymce/rails/configuration.rb', line 88

def merge(options)
  self.class.new(self.options.merge(options))
end

#options_for_tinymceObject

Converts options into a TinyMCE-friendly format.

1. Joins array values using OPTION_SEPARATORS
2. Converts JavaScript function() strings to Function objects
3. Applies transformations from OPTION_TRANSFORMERS


84
85
86
# File 'lib/tinymce/rails/configuration.rb', line 84

def options_for_tinymce
  preprocess_options(options)
end

#to_javascriptObject

Converts options into a String representing a JavaScript object that can be passed directly to tinyMCE.init



75
76
77
# File 'lib/tinymce/rails/configuration.rb', line 75

def to_javascript
  options_to_javascript(options_for_tinymce)
end