core/misc/form.autosave.js | 2 +- core/modules/ckeditor/js/ckeditor.js | 15 +++++++++++++++ core/modules/editor/editor.module | 1 + core/modules/editor/js/editor.js | 6 ++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/core/misc/form.autosave.js b/core/misc/form.autosave.js index 95b3e29..e53d121 100644 --- a/core/misc/form.autosave.js +++ b/core/misc/form.autosave.js @@ -17,7 +17,7 @@ Drupal.behaviors.formAutoSave = { // Attach garlic. .garlic({ inputs: 'input[type!=file][type!=hidden][type!=submit][type!=button], textarea, select', - events: [ 'textInput', 'input', 'change', 'keypress', 'paste', 'focus' ], + events: [ 'editorSerializedAfterChange', 'textInput', 'input', 'change', 'keypress', 'paste', 'focus' ], expires: 86400 * 30, conflictManager: { enabled: false diff --git a/core/modules/ckeditor/js/ckeditor.js b/core/modules/ckeditor/js/ckeditor.js index b8318be..6d67c9f 100644 --- a/core/modules/ckeditor/js/ckeditor.js +++ b/core/modules/ckeditor/js/ckeditor.js @@ -29,6 +29,21 @@ Drupal.editors.ckeditor = { } } return !!editor; + }, + + onChange: function (element, callback) { + var editor = CKEDITOR.dom.element.get(element).getEditor(); + if (editor) { + var changed = function () { + callback(editor.getData()); + }; + // @todo Make this more elegant once http://dev.ckeditor.com/ticket/9794 + // is fixed. + editor.on('key', changed); + editor.on('paste', changed); + editor.on('afterCommandExec', changed); + } + return !!editor; } }; diff --git a/core/modules/editor/editor.module b/core/modules/editor/editor.module index 6db6210..8603793 100644 --- a/core/modules/editor/editor.module +++ b/core/modules/editor/editor.module @@ -74,6 +74,7 @@ function editor_library_info() { 'dependencies' => array( array('system', 'jquery'), array('system', 'drupal'), + array('system', 'drupal.debounce'), array('system', 'drupalSettings'), array('system', 'jquery.once'), ), diff --git a/core/modules/editor/js/editor.js b/core/modules/editor/js/editor.js index 32da290..160bd69 100644 --- a/core/modules/editor/js/editor.js +++ b/core/modules/editor/js/editor.js @@ -97,6 +97,12 @@ Drupal.behaviors.editor = { Drupal.editorAttach = function (field, format) { if (format.editor) { Drupal.editors[format.editor].attach(field, format); + + // Serialize changes in the editor back to the textarea, at most every .5 s. + Drupal.editors[format.editor].onChange(field, Drupal.debounce(function () { + Drupal.editorDetach(field, format, 'serialize'); + $(field).trigger('editorSerializedAfterChange'); + }, 500)); } };