# Configuration and behavior of Redactor html editor. The editor is used for all html CMS
# attributes and provides autosave on top of the default Redactor settings.
# Check if the namespace for plugins exists and create it otherwise.
unless @RedactorPlugins?
@RedactorPlugins = {}
@RedactorPlugins.over_aggressive_cleanup_fix = init: -> @cleanSavePreCode = (html) -> html
# Plugin for closing the editor with a button in the toolbar.
@RedactorPlugins.close =
init: ->
@buttonAddFirst('close', 'Close', @closeAction)
closeAction: (buttonName, buttonDOM, buttonObj, event) ->
@.callback('blur')
$ ->
# Stores redactor options, custom settings and configures callbacks.
redactorOptions = ->
return {} =
cleanSpaces: false
convertLinks: false
formattingPre: false
paragraphy: false
tidyHtml: false
dropdownShowCallback: -> @_dropdownOpen = true
dropdownHideCallback: -> @_dropdownOpen = false
modalOpenedCallback: -> @_modalOpen = true
modalClosedCallback: -> @_modalOpen = false
focusCallback: -> @_hasFocus = true
buttons: [
'formatting', 'bold', 'italic', 'deleted', 'underline',
'unorderedlist', 'orderedlist', 'table', 'link', 'html',
]
# This options allows to configure what plugins are loaded. Plugins need to be defined in the
# +RedactorPlugins+ namespace.
plugins: ['close', 'over_aggressive_cleanup_fix']
# This option allows you to set whether Redactor gets cursor focus on load or not.
# http://imperavi.com/redactor/docs/settings/#set-focus
focus: true
# With this option turned on, Redactor will automatically replace divs to paragraphs.
# http://imperavi.com/redactor/docs/settings/#set-convertDivs
convertDivs: false
# This callback fires every time when content changes in Redactor.
# http://imperavi.com/redactor/docs/callbacks/#callback-changeCallback
changeCallback: ->
saveContents(@)
# This callback is triggered when Redactor loses focus.
# http://imperavi.com/redactor/docs/callbacks/#callback-blurCallback
blurCallback: ->
editor = @
editor._hasFocus = false
unless @_dropdownOpen || @_modalOpen
# workaround for issue with context menu (e. g. unlink) being neither modal nor dropdown
setTimeout ->
unless editor._hasFocus
saveContents(editor)
editor.destroy()
, 250
# This callback is triggered when a key is released.
# http://imperavi.com/redactor/docs/callbacks/#callback-keyupCallback
keyupCallback: (event) ->
event.stopPropagation()
key = event.keyCode || event.which
if key == 27
@.callback('blur')
else
saveContents(@)
# This callback allows to get pasted code after clean on paste.
# http://imperavi.com/redactor/docs/callbacks/#callback-pasteAfterCallback
pasteAfterCallback: (html) ->
saveContents(@)
html
# Saves the current editor content to the CMS.
saveContents = (editor) ->
cmsField = editor.$element
content = editor.get()
if content != cmsField.scrivito('content')
cmsField.scrivito('save', content).done ->
cmsField.trigger('save.scrivito_editors')
else
$.Deferred().resolve()
# Registers Redactor for all CMS html attributes found in the given scope of the DOM element.
addOnclickRedactorHandlers = ->
$(':root').on 'click.scrivito_editors', '[data-scrivito-field-type="html"]:not([data-editor]), [data-editor~="html"]', (event) ->
event.preventDefault()
cmsField = $(@)
unless cmsField.hasClass('redactor_editor')
cmsField.redactor(redactorOptions())
cmsField.redactor('set', cmsField.scrivito('content'), false)
cmsField.redactor('focus')
scrivito.on 'load', ->
if scrivito.in_editable_view()
addOnclickRedactorHandlers()