-
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
Open
getdave
wants to merge
11
commits into
master
Choose a base branch
from
try/undo-redo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
74932e9
Initial attempt at Undo/Redo
getdave 853d7cd
Add store to webpack known paths
getdave f56c313
Add Redux Undo package
getdave ef51a6b
Implement a basic undo redo system
getdave 1b83374
Include interface skeleton base styles to enable clicking on undo/redo
getdave 38396d5
Update src/components/header/redo.js
getdave a23de3c
Update src/components/header/redo.js
getdave 91380a1
Add persistance via data controls
getdave 1e67af5
Move resolvers to dedicated file
getdave 5e2f858
Make action creators clearer
getdave 7cc59f4
Remove rogue dispatch
getdave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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={ __( 'Redo' ) } | ||
shortcut={ displayShortcut.primary( 'x' ) } | ||
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const UPDATE_BLOCKS = "UPDATE_BLOCKS"; | ||
export const PERSIST_BLOCKS = "PERSIST_BLOCKS"; | ||
export const FETCH_BLOCKS_FROM_STORAGE = "FETCH_BLOCKS_FROM_STORAGE"; | ||
export const PERSIST_BLOCKS_TO_STORAGE = "PERSIST_BLOCKS_TO_STORAGE"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
UPDATE_BLOCKS, | ||
PERSIST_BLOCKS, | ||
FETCH_BLOCKS_FROM_STORAGE, | ||
PERSIST_BLOCKS_TO_STORAGE, | ||
} from "./action-types"; | ||
import { ActionCreators as ReduxUndo } from "redux-undo"; | ||
|
||
|
||
export function undo() { | ||
return ReduxUndo.undo(); | ||
} | ||
|
||
export function redo() { | ||
return ReduxUndo.redo(); | ||
} | ||
|
||
export function *updateBlocks( blocks, persist = false ) { | ||
|
||
if( persist ) { | ||
yield persistBlocksToStorage(blocks); | ||
} | ||
|
||
return { | ||
type: persist ? PERSIST_BLOCKS : UPDATE_BLOCKS, | ||
blocks, | ||
}; | ||
} | ||
|
||
export function fetchBlocksFromStorage() { | ||
return { | ||
type: FETCH_BLOCKS_FROM_STORAGE, | ||
}; | ||
}; | ||
|
||
export function persistBlocksToStorage(blocks) { | ||
return { | ||
type: PERSIST_BLOCKS_TO_STORAGE, | ||
blocks, | ||
}; | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { | ||
FETCH_BLOCKS_FROM_STORAGE, | ||
PERSIST_BLOCKS_TO_STORAGE, | ||
} from "./action-types"; | ||
import { serialize } from "@wordpress/blocks"; | ||
|
||
export default { | ||
[PERSIST_BLOCKS_TO_STORAGE](action) { | ||
return new Promise((resolve, reject) => { | ||
window.localStorage.setItem("getdavesbeBlocks", serialize(action.blocks)); | ||
resolve(action.blocks); | ||
}); | ||
}, | ||
[FETCH_BLOCKS_FROM_STORAGE]() { | ||
return new Promise((resolve, reject) => { | ||
const storedBlocks = | ||
window.localStorage.getItem("getdavesbeBlocks") || []; | ||
resolve(storedBlocks); | ||
}); | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { registerStore } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import reducer from './reducer'; | ||
import * as selectors from './selectors'; | ||
import * as actions from './actions'; | ||
import * as resolvers from "./resolvers"; | ||
import controls from './controls'; | ||
|
||
/** | ||
* Module Constants | ||
*/ | ||
const MODULE_KEY = 'getdavesbe'; | ||
|
||
const store = registerStore(MODULE_KEY, { | ||
reducer, | ||
selectors, | ||
actions, | ||
controls, | ||
resolvers, | ||
}); | ||
|
||
window.getDaveStore = store; | ||
|
||
export default store; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { parse } from "@wordpress/blocks"; | ||
import { fetchBlocksFromStorage, updateBlocks } from "./actions"; | ||
|
||
export function *getBlocks() { | ||
const rawBlocks = yield fetchBlocksFromStorage(); | ||
const persist = false; | ||
const blocks = parse(rawBlocks); | ||
yield updateBlocks(blocks, persist); | ||
return blocks; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 || []; | ||
} | ||
|
||
export const hasUndo = (state) => { | ||
return state.past?.length; | ||
}; | ||
|
||
export const hasRedo = (state) => { | ||
return state.future?.length; | ||
}; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The 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.