-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Block Editor: Make initial inner blocks non-dirtying. #19521
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -350,15 +350,18 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => { | |
*/ | ||
function withPersistentBlockChange( reducer ) { | ||
let lastAction; | ||
let markNextChangeAsNotPersistent = false; | ||
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. Should we name this with the "is" prefix? Something like isNextActionExplicitlyNotPersistent? 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. That is harder to read. |
||
|
||
return ( state, action ) => { | ||
let nextState = reducer( state, action ); | ||
|
||
const isExplicitPersistentChange = action.type === 'MARK_LAST_CHANGE_AS_PERSISTENT'; | ||
const isExplicitPersistentChange = action.type === 'MARK_LAST_CHANGE_AS_PERSISTENT' || markNextChangeAsNotPersistent; | ||
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. This flag seems a little bit confusing because it is true if the change is explicitly persistent or explicitly not persistent, right? The name seems to indicate the flag is true only when the change is explicit persistent, but not when the change is explicitly not persistent. 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.
It's an "explicit persistent change". The change could be to |
||
|
||
// Defer to previous state value (or default) unless changing or | ||
// explicitly marking as persistent. | ||
if ( state === nextState && ! isExplicitPersistentChange ) { | ||
markNextChangeAsNotPersistent = action.type === 'MARK_NEXT_CHANGE_AS_NOT_PERSISTENT'; | ||
|
||
const nextIsPersistentChange = get( state, [ 'isPersistentChange' ], true ); | ||
if ( state.isPersistentChange === nextIsPersistentChange ) { | ||
return state; | ||
|
@@ -372,16 +375,16 @@ function withPersistentBlockChange( reducer ) { | |
|
||
nextState = { | ||
...nextState, | ||
isPersistentChange: ( | ||
isExplicitPersistentChange || | ||
! isUpdatingSameBlockAttribute( action, lastAction ) | ||
), | ||
isPersistentChange: isExplicitPersistentChange ? | ||
! markNextChangeAsNotPersistent : | ||
! isUpdatingSameBlockAttribute( action, lastAction ), | ||
}; | ||
|
||
// In comparing against the previous action, consider only those which | ||
// would have qualified as one which would have been ignored or not | ||
// have resulted in a changed state. | ||
lastAction = action; | ||
markNextChangeAsNotPersistent = action.type === 'MARK_NEXT_CHANGE_AS_NOT_PERSISTENT'; | ||
|
||
return nextState; | ||
}; | ||
|
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.
To mark a change as persistent, we use MARK_LAST_CHANGE_AS_PERSISTENT to mark the previous change as persistent, and we trow the action after the action we want to mark. While to explicitly mark a change as not persistent, we use MARK_NEXT_CHANGE_AS_NOT_PERSISTENT, and we trow it before the action we want to mark.
Would it be possible to make the usage of the actions more similar and trow both actions before or after the actions we want to mark?
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.
No, because of usage nuance:
In our framework, when you want a persistent change to not be persistent, it's so that the non-dirtying change handler is called from the block editor. If you fire an edit, and then fire a
MARK_NEXT_CHANGE_AS_NOT_PERSISTENT
, nothing guarantees that the wrong change handler wasn't already called before theMARK_NEXT_CHANGE_AS_NOT_PERSISTENT
.MARK_LAST_CHANGE_AS_PERSISTENT
is different, because when using it, it's ok that the non-dirtying handler might have already been called, it will just call the dirtying one again with the same values.