Skip to content

Commit

Permalink
Block Editor: Remove history handling as managed by Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Feb 13, 2019
1 parent 8a9363a commit 990aa38
Show file tree
Hide file tree
Showing 18 changed files with 1,768 additions and 1,792 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -776,17 +776,6 @@ Whether redo history exists.

## Actions

### __unstableInitBlocks

Returns an action object used in signalling that blocks state should be
intialized using a specified array of blocks,

This action reset the undo/redo history

*Parameters*

* blocks: Array of blocks.

### resetBlocks

Returns an action object used in signalling that blocks state should be
Expand Down Expand Up @@ -1059,20 +1048,6 @@ Returns an action object used in signalling that the editor settings have been u

* settings: Updated settings

### redo

Returns an action object used in signalling that undo history should
restore last popped state.

### undo

Returns an action object used in signalling that undo history should pop.

### createUndoLevel

Returns an action object used in signalling that undo history record should
be created.

### __unstableSaveReusableBlock

Returns an action object used in signalling that a temporary reusable blocks have been saved
Expand Down
39 changes: 39 additions & 0 deletions docs/designers-developers/developers/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@

## Selectors

### hasEditorUndo

Returns true if any past editor history snapshots exist, or false otherwise.

*Parameters*

* state: Global application state.

### hasEditorRedo

Returns true if any future editor history snapshots exist, or false
otherwise.

*Parameters*

* state: Global application state.

*Returns*

Whether redo history exists.

### isEditedPostNew

Returns true if the currently edited post is yet to be saved, or false if
Expand All @@ -11,6 +32,10 @@ the post has been saved.

* state: Global application state.

*Returns*

Whether the post is new.

### hasChangedContent

Returns true if content includes unsaved changes, or false otherwise.
Expand Down Expand Up @@ -767,6 +792,20 @@ Returns an action object used in signalling that the post should autosave.

* options: Extra flags to identify the autosave.

### redo

Returns an action object used in signalling that undo history should
restore last popped state.

### undo

Returns an action object used in signalling that undo history should pop.

### createUndoLevel

Returns an action object used in signalling that undo history record should
be created.

### updatePostLock

Returns an action object used to lock the editor.
Expand Down
50 changes: 30 additions & 20 deletions packages/block-editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,40 @@ import { withDispatch, withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';

class BlockEditorProvider extends Component {
constructor( props ) {
super( ...arguments );
props.updateEditorSettings( props.settings );
props.initBlocks( props.value );
this.persistedValue = props.select( 'core/block-editor' ).getBlocks();
componentDidMount() {
this.props.updateEditorSettings( this.props.settings );
this.props.resetBlocks( this.props.value );

this.isSyncingBlockValue = true;
}

componentDidUpdate( prevProps ) {
if ( this.props.settings !== prevProps.settings ) {
this.props.updateEditorSettings( this.props.settings );
const {
settings,
updateEditorSettings,
value,
resetBlocks,
blocks,
onChange,
} = this.props;

if ( settings !== prevProps.settings ) {
updateEditorSettings( settings );
}

if (
this.props.blocks !== prevProps.blocks &&
this.props.blocks !== this.persistedValue
) {
this.persistedValue = this.props.blocks;
this.props.onChange( this.props.blocks );
if ( this.isSyncingBlockValue ) {
this.isSyncingBlockValue = false;
} else if ( blocks !== prevProps.blocks ) {
onChange( blocks );
this.isSyncingBlockValue = true;
} else if ( value !== prevProps.value ) {
resetBlocks( value );
this.isSyncingBlockValue = true;
}
}

render() {
const {
children,
} = this.props;
const { children } = this.props;

return (
<SlotFillProvider>
Expand All @@ -45,20 +54,21 @@ class BlockEditorProvider extends Component {

export default compose( [
withSelect( ( select ) => {
const { getBlocks } = select( 'core/block-editor' );

return {
blocks: select( 'core/block-editor' ).getBlocks(),
select,
blocks: getBlocks(),
};
} ),
withDispatch( ( dispatch ) => {
const {
updateEditorSettings,
__unstableInitBlocks: initBlocks,
resetBlocks,
} = dispatch( 'core/block-editor' );

return {
updateEditorSettings,
initBlocks,
resetBlocks,
};
} ),
] )( BlockEditorProvider );
46 changes: 0 additions & 46 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@ import { castArray } from 'lodash';
*/
import { getDefaultBlockName, createBlock } from '@wordpress/blocks';

/**
* Returns an action object used in signalling that blocks state should be
* intialized using a specified array of blocks,
*
* This action reset the undo/redo history
*
* @param {Array} blocks Array of blocks.
*
* @return {Object} Action object.
*/
export function __unstableInitBlocks( blocks ) {
return {
type: 'INIT_BLOCKS',
blocks,
};
}

/**
* Returns an action object used in signalling that blocks state should be
* reset to the specified array of blocks, taking precedence over any other
Expand Down Expand Up @@ -526,35 +509,6 @@ export function updateEditorSettings( settings ) {
};
}

/**
* Returns an action object used in signalling that undo history should
* restore last popped state.
*
* @return {Object} Action object.
*/
export function redo() {
return { type: 'REDO' };
}

/**
* Returns an action object used in signalling that undo history should pop.
*
* @return {Object} Action object.
*/
export function undo() {
return { type: 'UNDO' };
}

/**
* Returns an action object used in signalling that undo history record should
* be created.
*
* @return {Object} Action object.
*/
export function createUndoLevel() {
return { type: 'CREATE_UNDO_LEVEL' };
}

/**
* Returns an action object used in signalling that a temporary reusable blocks have been saved
* in order to switch its temporary id with the real id.
Expand Down
72 changes: 10 additions & 62 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
omit,
without,
mapValues,
keys,
isEqual,
isEmpty,
get,
Expand All @@ -24,7 +23,6 @@ import { isReusableBlock } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import withHistory from '../utils/with-history';
import {
PREFERENCES_DEFAULTS,
EDITOR_SETTINGS_DEFAULTS,
Expand Down Expand Up @@ -143,55 +141,6 @@ function getMutateSafeObject( original, working ) {
return working;
}

/**
* Returns true if the two object arguments have the same keys, or false
* otherwise.
*
* @param {Object} a First object.
* @param {Object} b Second object.
*
* @return {boolean} Whether the two objects have the same keys.
*/
export function hasSameKeys( a, b ) {
return isEqual( keys( a ), keys( b ) );
}

/**
* Returns true if, given the currently dispatching action and the previously
* dispatched action, the two actions are updating the same block attribute, or
* false otherwise.
*
* @param {Object} action Currently dispatching action.
* @param {Object} previousAction Previously dispatched action.
*
* @return {boolean} Whether actions are updating the same block attribute.
*/
export function isUpdatingSameBlockAttribute( action, previousAction ) {
return (
action.type === 'UPDATE_BLOCK_ATTRIBUTES' &&
action.clientId === previousAction.clientId &&
hasSameKeys( action.attributes, previousAction.attributes )
);
}

/**
* Returns true if, given the currently dispatching action and the previously
* dispatched action, the two actions are modifying the same property such that
* undo history should be batched.
*
* @param {Object} action Currently dispatching action.
* @param {Object} previousAction Previously dispatched action.
*
* @return {boolean} Whether to overwrite present state.
*/
export function shouldOverwriteState( action, previousAction ) {
if ( ! previousAction || action.type !== previousAction.type ) {
return false;
}

return isUpdatingSameBlockAttribute( action, previousAction );
}

/**
* Higher-order reducer targeting the combined editor reducer, augmenting
* block client IDs in remove action to include cascade of inner blocks.
Expand Down Expand Up @@ -227,10 +176,7 @@ const withInnerBlocksRemoveCascade = ( reducer ) => ( state, action ) => {
* @return {Function} Enhanced reducer function.
*/
const withBlockReset = ( reducer ) => ( state, action ) => {
if (
state &&
( action.type === 'RESET_BLOCKS' || action.type === 'INIT_BLOCKS' )
) {
if ( state && action.type === 'RESET_BLOCKS' ) {
const visibleClientIds = getNestedBlockClientIds( state.order );
return {
...state,
Expand Down Expand Up @@ -307,13 +253,6 @@ export const editor = flow( [
combineReducers,

withInnerBlocksRemoveCascade,

// Track undo history, starting at editor initialization.
withHistory( {
resetTypes: [ 'INIT_BLOCKS' ],
ignoreTypes: [ 'RECEIVE_BLOCKS' ],
shouldOverwriteState,
} ),
] )( {
blocks: flow(
combineReducers,
Expand All @@ -322,6 +261,9 @@ export const editor = flow( [
)( {
byClientId( state = {}, action ) {
switch ( action.type ) {
case 'RESET_BLOCKS':
return getFlattenedBlocksWithoutAttributes( action.blocks );

case 'RECEIVE_BLOCKS':
return {
...state,
Expand Down Expand Up @@ -373,6 +315,9 @@ export const editor = flow( [

attributes( state = {}, action ) {
switch ( action.type ) {
case 'RESET_BLOCKS':
return getFlattenedBlockAttributes( action.blocks );

case 'RECEIVE_BLOCKS':
return {
...state,
Expand Down Expand Up @@ -446,6 +391,9 @@ export const editor = flow( [

order( state = {}, action ) {
switch ( action.type ) {
case 'RESET_BLOCKS':
return mapBlockOrder( action.blocks );

case 'RECEIVE_BLOCKS':
return {
...state,
Expand Down
Loading

0 comments on commit 990aa38

Please sign in to comment.