-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add useStateWithHistory hook and use to show a block editor with undo/redo #54377
Conversation
Size Change: +2.36 kB (0%) Total Size: 1.52 MB
ℹ️ View Unchanged
|
This PR should be ready for final review now. |
// eslint-disable-next-line jsx-a11y/no-static-element-interactions | ||
<div | ||
className="editor-box" | ||
onKeyDown={ ( event ) => event.stopPropagation() } |
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.
Why?
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.
Because we have some global keyboard shortcuts that conflict playground's shortcuts (try typing in the editor). Feels like we may want to call "preventDefault" when typing random characters in the editor but it's out of scope here.
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.
Adding this to the code example doesn't feel right. Should maybe move to a general playground template
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 think there's a real bug in our components, I'm not sure where though because when you type within regular inputs the events are ignored by playwright but when you type in the block editor, the events reach the playground. So I'd rather not hide it for now.
*/ | ||
export default function useStateWithHistory< T >( initialValue: T ) { | ||
const manager = useRef( createUndoManager() ); | ||
const [ value, setValue ] = useState( initialValue ); |
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.
Why can't the internal state be equal to that of the under manager?
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 don't understand?
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.
Sorry, I thought the state was exposed, but I guess not
let history = []; |
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.
Why no give access to the state though, if it's a low level thing? Then you don't need an intermediate state.
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'm not sure, it feels like an internal representation of a history. I think regardless of whether we expose it or not, we need an intermediate state, in some occasions we don't record undo/redo steps. (undoIgnore option)
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.
Seems fine to me.
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.
Looks much better now 🙂
Flaky tests detected in cd69c61. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6184988253
|
Related to #53874
What?
Undo/Redo has been a common struggle for folks implementing a custom block editor. This PR introduces a
useStateWithHistory
to the compose package.Also, I'm adding a new "playground" story to Storybook to show a small block editor using this hook to implement undo/redo.
Testing Instructions
1- Run storybook
npm run storybook:dev
.2- Open the "Playground with undo/redo" story.
3- Check that you can use the editor and click undo/redo buttons