Skip to content

Commit

Permalink
Introduce a custom isDirty method.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
boonebgorges committed Nov 19, 2015
1 parent ce87363 commit 831e171
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions assets/js/hooks-wp-fee.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<span class="unapproved-comment-count" title="' + SocialPaperI18n.unapproved_comment_alt + '">' + SocialPaperI18n.unapproved_comment_count * 1 + '</span>';
Expand Down Expand Up @@ -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;
}
} );

/**
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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
Expand Down

1 comment on commit 831e171

@boonebgorges
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.