Skip to content

Commit

Permalink
Fix issue where pattern override values reset when saving (#61023)
Browse files Browse the repository at this point in the history
Co-authored-by: talldan <talldanwp@git.wordpress.org>
Co-authored-by: kevin940726 <kevin940726@git.wordpress.org>
  • Loading branch information
3 people committed Apr 26, 2024
1 parent 190795d commit 08a8156
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,9 @@ function ReusableBlockEdit( {
);
const isMissing = hasResolved && ! record;

// The initial value of the `content` attribute.
const initialContent = useRef( content );
// The value of the `content` attribute, stored in a `ref` to avoid triggering the effect
// that runs `applyInitialContentValuesToInnerBlocks` unnecessarily.
const contentRef = useRef( content );

// The default content values from the original pattern for overridable attributes.
// Set by the `applyInitialContentValuesToInnerBlocks` function.
Expand Down Expand Up @@ -349,7 +350,7 @@ function ReusableBlockEdit( {
// Build a map of clientIds to the old nano id system to provide back compat.
legacyIdMap.current = getLegacyIdMap(
initialBlocks,
initialContent.current
contentRef.current
);
defaultContent.current = {};
const originalEditingMode = getBlockEditingMode( patternClientId );
Expand All @@ -360,7 +361,7 @@ function ReusableBlockEdit( {
const blocks = hasPatternOverridesSource
? applyInitialContentValuesToInnerBlocks(
initialBlocks,
initialContent.current,
contentRef.current,
defaultContent.current,
legacyIdMap.current
)
Expand Down Expand Up @@ -417,13 +418,15 @@ function ReusableBlockEdit( {
if ( blocks !== prevBlocks ) {
prevBlocks = blocks;
syncDerivedUpdates( () => {
const updatedContent = getContentValuesFromInnerBlocks(
blocks,
defaultContent.current,
legacyIdMap.current
);
setAttributes( {
content: getContentValuesFromInnerBlocks(
blocks,
defaultContent.current,
legacyIdMap.current
),
content: updatedContent,
} );
contentRef.current = updatedContent;
} );
}
}, blockEditorStore );
Expand Down

1 comment on commit 08a8156

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 08a8156.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/8844698470
📝 Reported issues:

Please sign in to comment.