// This file integrates contenteditable for string attributes. 'use strict'; function onKey(event, proxy) { var cmsField = proxy.jQueryElement(); var key = event.keyCode || event.which; switch (key) { case 13: // Enter if (!isNewlineAllowed(cmsField)) { event.preventDefault(); cmsField.blur(); } break; case 27: // Esc event.stopPropagation(); cmsField.blur(); } } function onInput(proxy) { save(proxy); } function onFocus(proxy) { prepareForEditing(proxy.jQueryElement()); } function onClick(proxy) { fixSelectionForFirefox(proxy.jQueryElement()); } function onBlur(proxy) { save(proxy).done(function () { return proxy.trigger('scrivito_editors:blur'); }); finishEditing(proxy); prepareForEditing(proxy.jQueryElement()); } function save(proxy) { var content = getCurrentContent(proxy.jQueryElement()); return proxy.save(content).done(function () { return proxy.trigger('scrivito_editors:save'); }); } function getCurrentContent(cmsField) { cleanUp(cmsField); return cmsField[0].innerText.replace(/\r\n/g, '\n').replace(/\n+$/, ''); } function cleanUp(cmsField) { var siblings = cmsFieldAndPastedContent(cmsField); var pasted = siblings.not(cmsField); if (pasted.length > 0) { pasted.remove(); cmsField.text(siblings.text()); } } function cmsFieldAndPastedContent(cmsField) { var siblingsBefore = cmsField.data('scrivito_editors_siblings_before_edit'); var siblings = cmsField.siblings(); var needsReset = !siblingsBefore || siblings.filter(siblingsBefore).length < siblingsBefore.length; if (needsReset) { return cmsField.data('scrivito_editors_siblings_before_edit', siblings); } return cmsField.siblings().addBack().not(siblingsBefore); } function isNewlineAllowed(cmsField) { if (cmsField.data('scrivitoEditorsMultiline') === true) { return true; } if (cmsField.data('scrivitoEditorsMultiline') === false) { return false; } return cmsField.css('white-space').match(/pre/); } function prepareForEditing(cmsField) { if (isNewlineAllowed(cmsField) && !cmsField.data('scrivito_editors_prepared_for_editing')) { cmsField.data('scrivito_editors_prepared_for_editing', true); var html = cmsField.html(); var htmlNl2Br = html.replace(/\r?\n/g, '
'); if (html !== htmlNl2Br) { cmsField.html(htmlNl2Br); } } } function finishEditing(proxy) { var cmsField = proxy.jQueryElement(); cmsField.data('scrivito_editors_prepared_for_editing', false); cmsField.text(proxy.content()); } var DOUBLE_CLICK_MS = 300; function fixSelectionForFirefox(cmsField) { // see https://bugzilla.mozilla.org/show_bug.cgi?id=389348 var sel = document.getSelection(); if (!sel.anchorNode || !sel.focusNode) { return; } if (!cmsField.find(sel.anchorNode).addBack(sel.anchorNode).length || !cmsField.find(sel.focusNode).addBack(sel.focusNode).length) { sel.selectAllChildren(cmsField[0]); } } function _activate(proxy) { var cmsField = proxy.jQueryElement(); scrivito.editors.applyPlaceholder(cmsField); cmsField.attr('contenteditable', 'true').blur(function () { return onBlur(proxy); }).click(function () { return onClick(proxy); }).focus(function () { return onFocus(proxy); }).keypress(function (event) { return onKey(event, proxy); }).keyup(function (event) { return onKey(event, proxy); }); if (cmsField.attr('data-scrivito-editors-autosave') !== 'false') { cmsField.on('cut input keypress keyup paste', function () { return onInput(proxy); }); } prepareForEditing(cmsField); cmsField.on('click', function (event) { cmsField.attr('contenteditable', 'true'); cleanUp(cmsField); // Prevent editable link strings from being opened on click, but allow an enforcing double click if (event.timeStamp - cmsField.data('scrivito_editors_last_click') >= DOUBLE_CLICK_MS) { event.preventDefault(); } cmsField.data('scrivito_editors_last_click', event.timeStamp); }); } scrivito.editors.string_editor = { can_edit: function can_edit(element) { return $(element).is('[data-scrivito-field-type=string]'); }, activate: function activate(element) { _activate(new scrivito.editors.DomContentProxy(element)); }, _activateWithProxy: function _activateWithProxy(proxy) { _activate(proxy); } }; scrivito.on('load', function () { return scrivito.define_editor('string', scrivito.editors.string_editor); });