Skip to content

Experiment with adding undo/redo #15

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Initial attempt at Undo/Redo
  • Loading branch information
getdave committed Sep 15, 2020
commit 74932e9c654ea1356e00e587f5e9e71e259b15e4
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
},
"devDependencies": {
"@wordpress/env": "^2.0.0",
"@wordpress/data-controls": "^1.14.0",
"@wordpress/eslint-plugin": "^7.2.0",
"@wordpress/keycodes": "^2.13.0",
"@wordpress/scripts": "^12.2.0",
"autoprefixer": "^9.8.6",
"css-loader": "^4.3.0",
Expand Down
9 changes: 9 additions & 0 deletions src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { EditorHistoryRedo } from '@wordpress/editor';

import HistoryUndo from './undo';

export default function Header() {
function handleUndo() {
console.log( 'undo' );
}
return (
<div
className="getdavesbe-header"
Expand All @@ -14,6 +20,9 @@ export default function Header() {
<h1 className="getdavesbe-header__title">
{ __( 'Standalone Block Editor', 'getdavesbe' ) }
</h1>

<HistoryUndo />
<EditorHistoryRedo />
</div>
);
}
34 changes: 34 additions & 0 deletions src/components/header/undo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { displayShortcut } from '@wordpress/keycodes';
import { undo as undoIcon } from '@wordpress/icons';

function HistoryUndo( { hasUndo, undo, ...props } ) {
return (
<Button
{ ...props }
icon={ undoIcon }
label={ __( 'Undo' ) }
shortcut={ displayShortcut.primary( 'z' ) }
// If there are no undo levels we don't want to actually disable this
// button, because it will remove focus for keyboard users.
// See: https://github.com/WordPress/gutenberg/issues/3486
aria-disabled={ ! hasUndo }
onClick={ hasUndo ? undo : undefined }
className="editor-history__undo"
/>
);
}

const EnhancedHistoryUndo = compose( [
withSelect( ( select ) => ( {
hasUndo: select( 'getdavesbe' ).hasEditorUndo(),
} ) ),
withDispatch( ( dispatch ) => ( {
undo: dispatch( 'getdavesbe' ).undo,
} ) ),
] )( HistoryUndo );

export default EnhancedHistoryUndo;
10 changes: 7 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import { render } from '@wordpress/element';
import { registerCoreBlocks } from '@wordpress/block-library';
import Editor from './editor';

import './store';

import './styles.scss';

domReady( function() {
domReady( function () {
const settings = window.getdaveSbeSettings || {};
registerCoreBlocks();
render( <Editor settings={ settings } />, document.getElementById( 'getdave-sbe-block-editor' ) );
render(
<Editor settings={ settings } />,
document.getElementById( 'getdave-sbe-block-editor' )
);
} );

20 changes: 20 additions & 0 deletions src/store/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { dispatch } from '@wordpress/data-controls';

/**
* Returns an action object used in signalling that undo history should
* restore last popped state.
*
* @yield {Object} Action object.
*/
export function* redo() {
yield dispatch( 'core', 'redo' );
}

/**
* Returns an action object used in signalling that undo history should pop.
*
* @yield {Object} Action object.
*/
export function* undo() {
yield dispatch( 'core', 'undo' );
}
26 changes: 26 additions & 0 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* WordPress dependencies
*/
import { registerStore } from '@wordpress/data';

/**
* Internal dependencies
*/
import reducer from './reducer';
import * as selectors from './selectors';
import * as actions from './actions';

/**
* Module Constants
*/
const MODULE_KEY = 'getdavesbe';

const store = registerStore( MODULE_KEY, {
reducer,
selectors,
actions,
} );

window.getDaveStore = store;

export default store;
3 changes: 3 additions & 0 deletions src/store/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function ( state = [], action ) {
return state;
}
24 changes: 24 additions & 0 deletions src/store/selectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createRegistrySelector } from '@wordpress/data';

/**
* Returns true if any past editor history snapshots exist, or false otherwise.
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether undo history exists.
*/
export const hasEditorUndo = createRegistrySelector( ( select ) => () => {
return select( 'core' ).hasUndo();
} );

/**
* Returns true if any future editor history snapshots exist, or false
* otherwise.
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether redo history exists.
*/
export const hasEditorRedo = createRegistrySelector( ( select ) => () => {
return select( 'core' ).hasRedo();
} );