From d85e20ca5da6708a0bd9ba1d7dd8be9af0ea2c6a Mon Sep 17 00:00:00 2001 From: James Nylen Date: Thu, 4 May 2017 05:55:49 -0500 Subject: [PATCH] Add basic 'dirty state' handling (#610) --- editor/header/saved-state/index.js | 30 +++++++++++++++++++++++++----- editor/state.js | 16 ++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/editor/header/saved-state/index.js b/editor/header/saved-state/index.js index 126b1d4498c211..0d6640070ebf00 100644 --- a/editor/header/saved-state/index.js +++ b/editor/header/saved-state/index.js @@ -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 ( -
- - { wp.i18n.__( 'Saved' ) } +
+ + { text }
); } -export default SavedState; +export default connect( + ( state ) => ( { + isDirty: state.blocks.dirty, + } ) +)( SavedState ); diff --git a/editor/state.js b/editor/state.js index 6158db4212dba7..ccba7ee7c1f58a 100644 --- a/editor/state.js +++ b/editor/state.js @@ -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':