-
Notifications
You must be signed in to change notification settings - Fork 31
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
base: master
Are you sure you want to change the base?
Changes from 5 commits
74932e9
853d7cd
f56c313
ef51a6b
1b83374
38396d5
a23de3c
91380a1
1e67af5
5e2f858
7cc59f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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 { redo as redoIcon } from '@wordpress/icons'; | ||
|
||
|
||
function HistoryRedo( { hasRedo, redo, ...props } ) { | ||
return ( | ||
<Button | ||
{ ...props } | ||
icon={ redoIcon } | ||
label={ __( 'Red' ) } | ||
shortcut={ displayShortcut.primary( 'z' ) } | ||
getdave marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// If there are no redo 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={ ! hasRedo } | ||
onClick={ hasRedo ? redo : undefined } | ||
className="editor-history__redo" | ||
/> | ||
); | ||
} | ||
|
||
const EnhancedHistoryRedo = compose( [ | ||
withSelect( ( select ) => ( { | ||
hasRedo: select( 'getdavesbe' ).hasRedo(), | ||
} ) ), | ||
withDispatch( ( dispatch ) => ( { | ||
redo: dispatch( 'getdavesbe' ).redo, | ||
} ) ), | ||
] )( HistoryRedo ); | ||
|
||
export default EnhancedHistoryRedo; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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' ).hasUndo(), | ||
} ) ), | ||
withDispatch( ( dispatch ) => ( { | ||
undo: dispatch( 'getdavesbe' ).undo, | ||
} ) ), | ||
] )( HistoryUndo ); | ||
|
||
export default EnhancedHistoryUndo; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const UPDATE_BLOCKS = "UPDATE_BLOCKS"; | ||
export const PERSIST_BLOCKS = "PERSIST_BLOCKS"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { dispatch } from '@wordpress/data-controls'; | ||
import { UPDATE_BLOCKS, PERSIST_BLOCKS } from "./action-types"; | ||
import { ActionCreators } from "redux-undo"; | ||
|
||
|
||
export function undo() { | ||
return ActionCreators.undo(); | ||
} | ||
|
||
export function redo() { | ||
return ActionCreators.redo(); | ||
} | ||
|
||
export function updateBlocks( blocks, persist = false ) { | ||
return { | ||
type: persist ? PERSIST_BLOCKS : UPDATE_BLOCKS, | ||
blocks, | ||
}; | ||
} | ||
|
||
|
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
import undoable, { groupByActionTypes, includeAction } from "redux-undo"; | ||
|
||
|
||
|
||
import { UPDATE_BLOCKS, PERSIST_BLOCKS } from "./action-types"; | ||
|
||
function blocksReducer(state = [], action) { | ||
switch (action.type) { | ||
case UPDATE_BLOCKS: | ||
case PERSIST_BLOCKS: | ||
const { blocks } = action; | ||
|
||
return { | ||
blocks, | ||
}; | ||
} | ||
|
||
return state; | ||
} | ||
|
||
export default undoable(blocksReducer, { | ||
filter: includeAction(PERSIST_BLOCKS), | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createRegistrySelector } from '@wordpress/data'; | ||
|
||
|
||
|
||
|
||
|
||
|
||
export const getBlocks = ( state ) => { | ||
return state.present.blocks || []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've updated this to allow for a default/fallback set of blocks, so that can reload blocks previously worked on;
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep although grabbing for storage is a side effect so should probably be handled in a control action. Still working that one out. |
||
} | ||
|
||
export const hasUndo = (state) => { | ||
return state.past?.length; | ||
}; | ||
|
||
export const hasRedo = (state) => { | ||
return state.future?.length; | ||
}; | ||
|
Uh oh!
There was an error while loading. Please reload this page.