From 831e171ce68b9c0402bb05bc8cbcd34a12524b4d Mon Sep 17 00:00:00 2001 From: Boone B Gorges Date: Thu, 19 Nov 2015 10:40:33 -0600 Subject: [PATCH] Introduce a custom `isDirty` method. Instead of comparing content, we set an `isDirty` flag whenever a change is detected. The flag gets set back to `false` on `fee-after-save`. This technique can result in some false positives, as when you change content but then undo that change. But it has a couple of benefits that outweigh this downside: * It warns the user about unsaved changes on the Settings panel, which FEE doesn't know about. * It allows us to modify the content of the editor at will, without worrying about FEE's `isDirty` checks. See #77, #71, #42. --- assets/js/hooks-wp-fee.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/assets/js/hooks-wp-fee.js b/assets/js/hooks-wp-fee.js index a61f570..51cfb1f 100644 --- a/assets/js/hooks-wp-fee.js +++ b/assets/js/hooks-wp-fee.js @@ -303,6 +303,11 @@ jQuery(document).ready( function($) { toggle_sidebar(); } ); + // When settings are changed, set the isDirty flag. + $sidebar.find( 'input,textarea' ).on( 'change input', function() { + SocialPaper.isDirty = true; + } ); + // If the current post has unapproved comments, show a count. if ( SocialPaperI18n.unapproved_comment_count > 0 ) { var unapproved_span = '' + SocialPaperI18n.unapproved_comment_count * 1 + ''; @@ -343,6 +348,12 @@ jQuery(document).ready( function($) { if ( st_message ) { addNotice( st_message, 'updated', true ); } + + // Define our own `isDirty()` method. + SocialPaper.isDirty = false; + wp.fee.isDirty = function() { + return SocialPaper.isDirty; + } } ); /** @@ -390,6 +401,12 @@ jQuery(document).ready( function($) { var $entry_content = $( '.fee-content' ); var slug_editor, current_title, current_slug; $.each( window.tinymce.editors, function( i, ed ) { + + // Set the isDirty flag whenever content changes. + ed.onChange.add( function() { + SocialPaper.isDirty = true; + } ); + if ( ed.id == $entry_content.attr( 'id' ).replace( /\-content\-/, '-mce-' ) ) { ed.on( 'keydown', function( ed, l ) { maybe_scroll_window( this ); @@ -560,6 +577,9 @@ jQuery(document).ready( function($) { * Hook into WP FEE after save */ $(document).on( 'fee-after-save', function( event ) { + // Editor is now clean. + SocialPaper.isDirty = false; + // Dynamically do some stuff after a paper is first published if ( -1 !== event.currentTarget.URL.indexOf( '#edit=true' ) && 'publish' === wp.fee.postOnServer.post_status ) { // Change the current URL to the full paper URL using HTML5 history