Skip to content

Commit

Permalink
Add basic 'dirty state' handling (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
nylen authored May 4, 2017
1 parent 180e8f7 commit d85e20c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
30 changes: 25 additions & 5 deletions editor/header/saved-state/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';
import classNames from 'classnames';

/**
* Internal dependencies
*/
import './style.scss';
import Dashicon from '../../components/dashicon';

function SavedState() {
function SavedState( { isDirty } ) {
const classes = classNames( 'editor-saved-state', {
'is-dirty': isDirty,
} );
const icon = isDirty
? 'warning'
: 'saved';
const text = isDirty
? wp.i18n.__( 'Unsaved changes' )
: wp.i18n.__( 'Saved' );

return (
<div className="editor-saved-state">
<Dashicon icon="saved" />
{ wp.i18n.__( 'Saved' ) }
<div className={ classes }>
<Dashicon icon={ icon } />
{ text }
</div>
);
}

export default SavedState;
export default connect(
( state ) => ( {
isDirty: state.blocks.dirty,
} )
)( SavedState );
16 changes: 16 additions & 0 deletions editor/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ import { combineUndoableReducers } from 'utils/undoable-reducer';
* @return {Object} Updated state
*/
export const blocks = combineUndoableReducers( {
dirty( state = false, action ) {
switch ( action.type ) {
case 'RESET_BLOCKS':
return false;

case 'UPDATE_BLOCK':
case 'INSERT_BLOCK':
case 'MOVE_BLOCK_DOWN':
case 'MOVE_BLOCK_UP':
case 'REPLACE_BLOCKS':
case 'REMOVE_BLOCK':
return true;
}

return state;
},
byUid( state = {}, action ) {
switch ( action.type ) {
case 'RESET_BLOCKS':
Expand Down

0 comments on commit d85e20c

Please sign in to comment.