Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear undo history when initialising an editor #92

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/components/block-editor-contents/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import isPromise from 'is-promise';
import { Popover } from '@wordpress/components';
import { withDispatch, withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { useEffect } from '@wordpress/element';
import { useLayoutEffect } from '@wordpress/element';
import { parse, rawHandler } from '@wordpress/blocks';

/**
Expand Down Expand Up @@ -59,6 +59,7 @@ async function getInitialContent( settings, loader ) {
* @param {object[]} props.blocks
* @param {OnUpdate} props.updateBlocksWithoutUndo - Callback to update blocks
* @param {OnUpdate} props.updateBlocksWithUndo - Callback to update blocks
* @param {*} props.clearHistory - Callback to clear history
* @param {boolean} props.isEditing - Are we editing in this editor?
* @param {EditorMode} props.editorMode - Visual or code?
* @param {object} props.children - Child components
Expand All @@ -68,17 +69,19 @@ async function getInitialContent( settings, loader ) {
* @param {OnLoad} props.onLoad - Load initial blocks
*/
function BlockEditorContents( props ) {
const { blocks, updateBlocksWithoutUndo, updateBlocksWithUndo, selection, isEditing, editorMode } = props;
const { blocks, updateBlocksWithoutUndo, updateBlocksWithUndo, clearHistory, selection, isEditing, editorMode } = props;
const { children, settings, renderMoreMenu, onLoad } = props;

// Set initial content, if we have any, but only if there is no existing data in the editor (from elsewhere)
useEffect( () => {
useLayoutEffect( () => {
const loadData = async () => {
const initialContent = await getInitialContent( settings, onLoad );

if ( initialContent.length > 0 && ( ! blocks || blocks.length === 0 ) ) {
updateBlocksWithoutUndo( initialContent );
}

clearHistory();
};

loadData();
Expand Down Expand Up @@ -119,11 +122,12 @@ export default compose( [
};
} ),
withDispatch( ( dispatch ) => {
const { updateBlocksWithUndo, updateBlocksWithoutUndo } = dispatch( 'isolated/editor' );
const { updateBlocksWithUndo, updateBlocksWithoutUndo, clearHistory } = dispatch( 'isolated/editor' );

return {
updateBlocksWithUndo,
updateBlocksWithoutUndo,
clearHistory,
};
} ),
] )( BlockEditorContents );
3 changes: 3 additions & 0 deletions src/store/blocks/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const actions = {
redo() {
return ActionCreators.redo();
},
clearHistory() {
return ActionCreators.clearHistory();
},
/**
* Update blocks without undo history
* @param {object[]} blocks
Expand Down
1 change: 1 addition & 0 deletions src/store/blocks/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ const reducer = ( state = DEFAULT_STATE, action ) => {

export default undoable( reducer, {
groupBy,
ignoreInitialState: true,
} );