Repository navigation
Blocks V3: keep undo and redo working for block fields - #578
Open
faisalahammad wants to merge 3 commits into
Open
faisalahammad wants to merge 3 commits into
faisalahammad wants to merge 3 commits into
Conversation
- Set hasAcfError only when it changes, as a non persistent update that sends only that key. - Hash a copy of the attributes so the store object is not changed. - Skip writing form data back while the form still has its loaded values, compared with the form's own first serialization. - Count native input and change events in the block form as user interaction. Stored block data is keyed by field name and the form serializes by field key, so the first form serialization always looked like a change and was written back, marking the post as dirty with no edit. Fixes WordPress#556
A new block has no stored data, so when the form still had its loaded values the fetch sent no field data and the server treated it as a first preview, skipping validation. Required fields on new blocks then no longer blocked publishing. Send the form values with the fetch, without storing them, so the server validates them like before.
- When the block data changes from outside the form (undo, redo or a script), load a new form for that data and re-mount it, instead of writing the old form values back. - Compare stored data with the form by value, mapping field names to keys through the "_name" entries or the block's fields. - Keep an edit typed while the new form is loading, and put focus back in the field that had it. - Only count edits made in a re-mounted form, and run the "unmount" action when the form goes away so fields like WYSIWYG clean up. - Write _acf_changed with undoIgnore so it does not add an empty undo step. The V3 form never followed the data attribute. After Ctrl+Z it wrote its own values back about 200 ms later, which undid the undo and cleared the redo stack. Fixes WordPress#577
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @wojtekpiskorz. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Undo and redo did not work for Blocks V3 fields. Ctrl+Z reverted the block's
dataattribute, but the V3 form never followsdata. About 200 ms later the form wrote its own values back, which undid the undo and cleared the redo stack. The history then got stuck at the SCF block, so edits made before it in other blocks could not be undone either.Depends on #571. This branch is stacked on it, so the first two commits here are from #571. The change for this issue is the last commit, 83b2e80. I will rebase once #571 is merged.
What changed
block-edit.jsdatachange made outside the form (undo, redo or a script). On an outside change it no longer writes the form back. It fetches the form and preview for the currentdataand re-mountsBlockFormwith akey. Focus and caret go back to the field that had them, unless focus has moved somewhere else.isSameBlockData()compares stored data with the form by value. Field names map to keys through the_nameentries, or through the block's fields when those entries are missing.block-form.jsisReloadedprop. A re-mounted form only counts edits made in it, so the Ctrl+Z that caused the reload is not taken as an edit, and field setup in the new form is not written back.unmountaction when the form goes away, so fields like WYSIWYG clean up. V2 already did this._acf-fields.js_acf_changedis written with{ undoIgnore: true }. Before, it added an empty undo step, so the first click on the header Undo button did nothing.How to test
tests/e2e/plugins/scf-test-v3-block.php. Add a paragraph and an "SCF V3 Block" with the titleHelloto a post. Save and reload.XYZat the end of the Title field in the sidebar, then wait a second.Hellofor a moment, thenHelloXYZcomes back and redo does nothing. After: the field, the preview anddatastay atHello, and focus stays in the field.HelloXYZcomes back. Type!, and the new edit is kept.wp.data.dispatch( 'core/block-editor' ).updateBlockAttributes()on the block with a newdatavalue. The sidebar field and the preview show the new value, and the value is not overwritten.Jest tests added in
block-edit.test.jsandblock-form.test.js. The new undo tests fail without this change. Full suite: 980 passed.Known limits:
Closes #577
Use of AI Tools
Claude Code was used to help write the fix and tests. I reviewed and tested the changes before submitting.