# ----------------------------------------------------------------------------- # Author: Alexander Kravets , # Slate Studio (http://www.slatestudio.com) # # Coding Guide: # https://github.com/thoughtbot/guides/tree/master/style/coffeescript # ----------------------------------------------------------------------------- # ----------------------------------------------------------------------------- # INPUT MARKDOWN # ----------------------------------------------------------------------------- # Markdown input supports syntax highlighting and optional compilation to html. # # Config options: # label - Input label # aceOptions - Custom options for overriding default ones # htmlFieldName - Input name for generated HTML content # # Input config example: # body_md: { type: 'markdown', label: 'Article', htmlFieldName: 'body_html' } # # Dependencies: #= require vendor/marked #= require vendor/ace #= require vendor/mode-markdown # # ----------------------------------------------------------------------------- class @InputMarkdown extends InputString # PRIVATE =============================================== _add_input: -> if @config.htmlFieldName @$inputHtml =$ "" if @object @$inputHtml.val(@object[@config.htmlFieldName]) @$el.append @$inputHtml @$input =$ "" @$el.append @$input @$editor =$ "
" @$el.append @$editor _update_inputs: -> md_source = @session.getValue() @$input.val(md_source) @$input.trigger('change') if @$inputHtml html = marked(md_source) @$inputHtml.val(html) @$inputHtml.trigger('change') # PUBLIC ================================================ initialize: -> @config.beforeInitialize?(this) @editor = ace.edit(@$editor.get(0)) @editor.$blockScrolling = Infinity @session = @editor.getSession() @session.setValue(@$input.val()) @session.setUseWrapMode(true) @session.setMode("ace/mode/markdown") # options: https://github.com/ajaxorg/ace/wiki/Configuring-Ace @editor.setOptions autoScrollEditorIntoView: true minLines: 5 maxLines: Infinity showLineNumbers: false showGutter: false highlightActiveLine: false showPrintMargin: false @session.on 'change', (e) => @_update_inputs() @_update_inputs() @config.onInitialize?(this) updateValue: (@value) -> @session.setValue(@value) @_update_inputs() hash: (hash={}) -> hash[@config.htmlFieldName] = @$inputHtml.val() hash[@config.klassName] = @$input.val() return hash chr.formInputs['markdown'] = InputMarkdown