module EL::Ace

Public Class Methods

included(base) click to toggle source
# File lib/el-ace/el-ace.rb, line 5
def self.included base
  base.mount_controller base.const_set(:ELAceController, Class.new(E) {
    include EL::AceHelpers

    map 'el-ace-controller'
    engine :Slim
    view_prefix '/'
    view_fullpath File.expand_path('../templates', __FILE__).freeze
    layout false

    def editor
      @textarea_id, @opts = env[:TEXTAREA_ID].to_s, env[:EDITOR_OPTS].dup
      @editor_id = normalize_editor_id(@textarea_id)
      @textarea_id = ('#' << @textarea_id).freeze
      @snippets = normalize_snippets(@opts.delete(:snippets))
      render
    end

    def get_assets(*)
      env['PATH_INFO'] = env['PATH_INFO'].to_s.sub(self.class::ASSETS_REGEXP, '')
      send_files self.class::ASSETS_PATH
    end
  })

  base.class_exec do

    private
    # @param [String] textarea_id
    # @param [Hash] opts
    # @option opts [String] :mode
    # @option opts [String] :file file name to guess mode by
    # @option opts [Boolean] :readonly
    # @option opts [Array] :snippets
    # @option opts [String|Proc] :toolbar_prepend
    #   a String or a Proc returning a String to be prepended to toolbar
    # @option opts [String|Proc] :toolbar_append
    #   a String or a Proc returning a String to be appended to toolbar
    # @option opts [String]  :save_button_selector
    #   a DOM selector for save button. by default .saveButton used.
    #   el-ace will update the content of textarea when save button hovered.
    #   this could also be done by listening onchange event,
    #   but page may become huge and slow on bigger documents.
    def ace textarea_id, opts = {}
      fetch self.class::ELAceController, :editor, params do |env|
        env.update TEXTAREA_ID: textarea_id, EDITOR_OPTS: opts
      end
    end
  end
end

Public Instance Methods

ace(textarea_id, opts = {}) click to toggle source

@param [String] textarea_id @param [Hash] opts @option opts [String] :mode @option opts [String] :file file name to guess mode by @option opts [Boolean] :readonly @option opts [Array] :snippets @option opts [String|Proc] :toolbar_prepend

a String or a Proc returning a String to be prepended to toolbar

@option opts [String|Proc] :toolbar_append

a String or a Proc returning a String to be appended to toolbar

@option opts [String] :save_button_selector

a DOM selector for save button. by default .saveButton used.
el-ace will update the content of textarea when save button hovered.
this could also be done by listening onchange event,
but page may become huge and slow on bigger documents.
# File lib/el-ace/el-ace.rb, line 47
def ace textarea_id, opts = {}
  fetch self.class::ELAceController, :editor, params do |env|
    env.update TEXTAREA_ID: textarea_id, EDITOR_OPTS: opts
  end
end
editor() click to toggle source
# File lib/el-ace/el-ace.rb, line 15
def editor
  @textarea_id, @opts = env[:TEXTAREA_ID].to_s, env[:EDITOR_OPTS].dup
  @editor_id = normalize_editor_id(@textarea_id)
  @textarea_id = ('#' << @textarea_id).freeze
  @snippets = normalize_snippets(@opts.delete(:snippets))
  render
end
get_assets(*) click to toggle source
# File lib/el-ace/el-ace.rb, line 23
def get_assets(*)
  env['PATH_INFO'] = env['PATH_INFO'].to_s.sub(self.class::ASSETS_REGEXP, '')
  send_files self.class::ASSETS_PATH
end