$ -> # This file integrates contenteditable for text attributes. # It provides multiline editing support. onKey = (event) -> cmsField = $(event.currentTarget) key = event.keyCode || event.which switch key when 27 # Esc event.stopPropagation() cmsField.blur() onInput = (event) -> cmsField = $(event.currentTarget) save(cmsField, false) onBlur = (event) -> cmsField = $(event.currentTarget) save(cmsField, true).done -> if cmsField.attr('data-reload') == 'true' cmsField.scrivito('reload') save = (cmsField, andClose) -> cleanUp(cmsField) clone = cmsFieldAndPastedContent(cmsField).clone() clone.find('div:not(:has(br))').before('\n') clone.find('br').replaceWith('\n') content = clone.text() clone.remove() if andClose cmsField.text(content) # Save only if the content has changed. if content != cmsField.scrivito('content') cmsField.scrivito('save', content).done -> cmsField.trigger('save.scrivito_editors') else $.Deferred().resolve() cleanUp = (cmsField) -> siblings = cmsFieldAndPastedContent(cmsField) pasted = siblings.not(cmsField) if pasted.length > 0 pasted.remove() cmsField.text(siblings.text()) cmsFieldAndPastedContent = (cmsField) -> siblingsBefore = cmsField.data('scrivito_editors_siblings_before_edit') siblings = cmsField.siblings() needsReset = !siblingsBefore || siblings.filter(siblingsBefore).length < siblingsBefore.length if needsReset cmsField.data('scrivito_editors_siblings_before_edit', siblings) else cmsField.siblings().addBack().not(siblingsBefore) DOUBLE_CLICK_MS = 300 initialize = -> $(':root').on 'mouseenter', '[data-scrivito-field-type="text"]:not([data-editor]), [data-editor~="text"]', (event) -> cmsField = $(@) unless cmsField.attr('contenteditable')? html = cmsField.html() html_nl2br = html.replace(/\n/g, '
') if html != html_nl2br cmsField.html(html_nl2br) cmsField .attr('contenteditable', true) .blur(onBlur) .keypress(onKey) .keyup(onKey) .on('input', onInput) $(':root').on 'click', '[data-scrivito-field-type="text"]:not([data-editor]), [data-editor~="text"]', (event) -> cmsField = $(@) cleanUp(cmsField) # Prevent editable link strings from being opened on click, but allow an enforcing double click unless event.timeStamp - cmsField.data('scrivito_editors_last_click') < DOUBLE_CLICK_MS event.preventDefault() cmsField.data('scrivito_editors_last_click', event.timeStamp) scrivito.on 'load', -> if scrivito.in_editable_view() initialize()