# 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.
@RedactorPlugins ||= {}
# API to override Redactor options
# @api public
scrivito.editors.html_editor =
redactor:
options: {}
$ ->
# Plugin for closing the editor with a button in the toolbar.
RedactorPlugins.scrivito_editors_close = ->
init: ->
button = @button.addFirst('close', 'Close')
@button.addCallback button, -> closeEditor @
# Stores redactor options, custom settings and configures callbacks.
scrivito.editors.html_editor.redactor.default_options =
buttons: [
'formatting', 'bold', 'italic', 'deleted', 'underline',
'unorderedlist', 'orderedlist', 'table', 'image', 'link', 'html',
]
buttonSource: true
cleanSpaces: false
cleanOnPaste: false
convertLinks: false
imageUpload: true # dummy, to enable scrivito_editors_image_manager
linkTooltip: false # disabled, causes unexpected blur on redactor 9 and 10
paragraphize: false
placeholder: false
plugins: [
'scrivito_editors_close'
'scrivito_editors_image_manager'
'scrivito_editors_link_manager'
'table'
]
replaceDivs: false
tabifier: false
blurCallback: -> closeEditor @ if @_everHadFocus
changeCallback: -> saveContents @
destroyCallback: -> saveContents @
dropdownShowCallback: -> saveContents @
focusCallback: -> @_everHadFocus = true if @_isInitialized
modalOpenedCallback: -> saveContents @
keyupCallback: (event) ->
key = event.keyCode || event.which
if key == 27
event.stopPropagation()
closeEditor @
else
saveContents @
initCallback: ->
cmsField = $(@.$element)
content = cmsField.scrivito('content')
@_initialScrivitoContent = content
# @code.get() is empty during init, so we get all HTML via selections
@selection.selectAll()
htmlWithSelection = @selection.getHtml()
@selection.removeMarkers()
html = @selection.getHtml()
if html != content
@code.set(content)
@selection.selectAll()
html = @selection.getHtml()
@focus.setEnd()
else
@code.set(htmlWithSelection)
@selection.restore()
@_initialRedactorContent = html
@_isInitialized = true
startCallback: ->
@_everHadFocus = undefined
@_initialRedactorContent = undefined
@_initialScrivitoContent = undefined
@_isInitialized = undefined
if document.getSelection().anchorNode
# see http://imperavi.com/redactor/examples/click-to-edit/
@insert.node @selection.getMarker(), false
# Close editor (implies save)
closeEditor = (editor) ->
cmsField = $(editor.$element)
editor.core.destroy()
cmsField.removeClass('scrivito_editors_redactor')
cmsField.trigger('scrivito_editors:blur')
# Saves the current editor content to the CMS.
saveContents = (editor) ->
cmsField = editor.$element
content = getCurrentContent(editor)
if content != cmsField.scrivito('content')
cmsField.scrivito('save', content).done ->
cmsField.trigger('save.scrivito_editors')
else
$.Deferred().resolve()
getCurrentContent = (editor) ->
content = editor.code.get()
if content == editor._initialRedactorContent
content = editor._initialScrivitoContent
content
getOptions = ->
$.extend {}, scrivito.editors.html_editor.redactor.default_options,
scrivito.editors.html_editor.redactor.options
# 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('scrivito_editors_redactor')
cmsField
.redactor(getOptions())
.addClass('scrivito_editors_redactor')
scrivito.on 'load', ->
if scrivito.in_editable_view()
addOnclickRedactorHandlers()