Repository navigation
Blocks V3: stop marking the post as changed on load - #571
faisalahammad wants to merge 2 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
|
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: @zerodigitaluk. 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. |
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.
|
Thanks for the contribution and the effort here! 🙏 A heads-up on how this repo works: a large part of SCF's codebase (field types, Flexible Content, Blocks v3, local fields, forms, value formatting, and most of includes/ and assets/src/js/) is regularly synced from an upstream codebase. Any change we make there, whether a bug fix or a new feature, gets overwritten or causes hard merge conflicts on the next sync. That includes features behind a beta flag. Fixes for those areas arrive through the sync itself, so we can't accept PRs that touch that code, even good ones. |
Opening a post or template with a Blocks V3 block marked it as changed, with no edit. This came from two updates SCF made to every block on load:
hasAcfErrorwas set on every mount, as a persistent change, even when it was already falsy.data. Stored data is keyed by field name (with_name→ field key) and the form serializes by field key, so the string compare never matched and the same values were written again after the first click anywhere in the editor.What changed
block-edit.jshasAcfErroris only set when it changes. That update is marked not persistent and sends only{ hasAcfError }, so the next real edit is not folded into it (isUpdatingSameBlockAttribute).generateAttributesHash()hashes a copy. Before, it deletedhasAcfErrorand added_acf_contexton the store's own attributes object.setAttributes, it only refreshes the preview. This compares the form with itself, so no name to key mapping or line break handling is needed, and it works for repeaters and groups too.serializeBlockForm()helper, used by the three places that serialized the form.block-form.jsonUntouchedChangeprop. A form change before any interaction (fields setting up) is reported once, so the kept state follows it.input/changeevents now count as interaction, so an edit like a context menu paste is never taken as setup.How to test
tests/e2e/plugins/scf-test-v3-block.phpworks.wp.data.select( 'core/editor' ).isEditedPostDirty()in the console. Before:true. After:false.false, and leaving the page shows no "Leave site?" prompt.Jest tests added in
block-edit.test.jsandblock-form.test.js. The new BlockEdit tests fail on trunk and pass with this change. Full suite: 967 passed.Closes #556
Use of AI Tools
Claude Code was used to help write the fix and tests. I reviewed and tested the changes before submitting.