diff --git a/lib/gollum/public/gollum/javascript/editor/gollum.editor.js b/lib/gollum/public/gollum/javascript/editor/gollum.editor.js index c68fc41b..3ec550a5 100755 --- a/lib/gollum/public/gollum/javascript/editor/gollum.editor.js +++ b/lib/gollum/public/gollum/javascript/editor/gollum.editor.js @@ -17,6 +17,8 @@ NoDefinitionsFor: [] }; var ActiveOptions = {}; + var autoSaveTimer = null; + var storageKey = 'gollum_autorecover_' + window.location; function isRTL(s){ var ltrChars = 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF'+'\u2C00-\uFB1C\uFDFE-\uFE6F\uFEFD-\uFFFF', @@ -32,6 +34,27 @@ window.ace_editor.renderer.updateFull(); } + function autoSaveHandler() { + // Autosave + if (autoSaveTimer) { + // Reset the timer because we just changed the text + clearTimeout(autoSaveTimer); + } + + $('#gollum-saved-msg').text('Saving...'); + + // Wait 2 seconds, then actualy save the text to local storage + autoSaveTimer = setTimeout(function() { + localStorage.setItem(storageKey, window.ace_editor.getSession().getValue()); + // Save any subpage editor text that might exist + $('#gollum-editor-header, #gollum-editor-footer, #gollum-editor-sidebar').each(function(_, el) { + var spStorageKey = storageKey + el.id.replace('gollum-editor-', '_'); + localStorage.setItem(spStorageKey, el.value); + }); + $('#gollum-saved-msg').text('Saved recovery text'); + }, 2000); + } + /** * $.GollumEditor * @@ -51,20 +74,24 @@ }).insertAfter(textarea); textarea.css('display', 'none'); - var autoSaveTimer = null; - // NOTE: This requires the page to have only one 'gollum-editor-body'. var editor = ace.edit(editDiv[0], {rtlText: true}); window.ace_editor = editor; // Check to see if we have any autosaved text and show a message to // restore it if present. - var storageKey = 'gollum_autorecover_' + window.location; var savedText = localStorage.getItem(storageKey); if (savedText) { $('#gollum-autorecover-button').click(function(e) { editor.getSession().setValue(savedText); + // Restore subpage editor values too, if they exist + ['header', 'footer', 'sidebar'].forEach(function(i) { + var sbSavedText = localStorage.getItem(storageKey + '_' + i); + if (sbSavedText) { + $('#gollum-editor-' + i).val(sbSavedText); + } + }); $('#gollum-autorecover-msg')[0].hidden = true; e.preventDefault(); }); @@ -80,22 +107,13 @@ editor.getSession().on('change', function(){ textarea.val(editor.getSession().getValue()); - - // Autosave - if (autoSaveTimer) { - // Reset the timer because we just changed the text - clearTimeout(autoSaveTimer); - } - - $('#gollum-saved-msg').text('Saving...'); - - // Wait 2 seconds, then actualy save the text to local storage - autoSaveTimer = setTimeout(function() { - localStorage.setItem(storageKey, editor.getSession().getValue()); - $('#gollum-saved-msg').text('Saved recovery text'); - }, 2000); + autoSaveHandler(); }); + // Autosave for the header, footer and sidebar + $('#gollum-editor-header, #gollum-editor-footer, #gollum-editor-sidebar') + .on('change keyup paste', autoSaveHandler); + if (isRTL(editor.getSession().getLine(0))) { switchRtl(true); } @@ -148,6 +166,10 @@ $("#gollum-editor-submit, #gollum-editor-cancel").click(function() { var storageKey = 'gollum_autorecover_' + window.location; localStorage.removeItem(storageKey); + // Clear subpage editor values too, if they exist + ['header', 'footer', 'sidebar'].forEach(function(i) { + localStorage.removeItem(storageKey + '_' + i); + }); }); debug('GollumEditor loading');