Skip to content

Commit 003ebf8

Browse files
authored
fix (default block): save api fetch can be called multiple to infinite times (#3355)
Co-authored-by: [email protected] <>
1 parent 94770d6 commit 003ebf8

File tree

1 file changed

+69
-67
lines changed

1 file changed

+69
-67
lines changed

src/plugins/save-block/custom-block-styles-editor.js

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { __, sprintf } from '@wordpress/i18n'
2222
import { getBlockFromExample, createBlocksFromInnerBlocksTemplate } from '@wordpress/blocks'
2323

2424
// Saving can be triggered multiple times by the editor, throttle to the first trigger only.
25-
let isSaving = false
25+
let isPerformingSave = false
2626

2727
// Gets the first block that has a block name.
2828
const findBlock = ( blockName, blocks ) => {
@@ -38,87 +38,89 @@ const findBlock = ( blockName, blocks ) => {
3838
return foundBlock
3939
}
4040

41-
// Listen to editor updates when editing a 'stackable_temp_post'
42-
subscribe( () => {
43-
const postType = select( 'core/editor' )?.getCurrentPostType()
44-
if ( postType && postType === 'stackable_temp_post' ) {
45-
// The isSavingPost can trigger true multiple times, throttle it
46-
// manually so we only do our saving once.
47-
const isCurrentlySaving = select( 'core/editor' )?.isSavingPost()
48-
if ( isCurrentlySaving && ! isSaving ) {
49-
isSaving = true
50-
} else {
51-
isSaving = false
52-
}
41+
const performSave = () => {
42+
const {
43+
stk_block_name: blockName,
44+
stk_block_title: blockTitle,
45+
stk_style_slug: styleSlug,
46+
} = select( 'core/editor' ).getEditedPostAttribute( 'meta' )
47+
48+
// The user can have multiple blocks, just pick the first one that we're really editing.
49+
const blocks = select( 'core/block-editor' ).getBlocks()
50+
const blockToSave = findBlock( blockName, blocks )
51+
52+
// If there is no block, error.
53+
if ( ! blockToSave ) {
54+
setTimeout( () => { // We need to delay this for this to show up.
55+
dispatch( 'core/notices' ).createNotice(
56+
'success',
57+
sprintf( __( 'Error saving block, please make sure you only have a %s block', i18n ), blockTitle ),
58+
{
59+
type: 'snackbar',
60+
isDismissible: true,
61+
id: 'stk-block-style-saved', // This is the "save post" notice id, override the "Please wait" message.
62+
}
63+
)
64+
}, 100 )
65+
return
66+
}
5367

54-
if ( ! isSaving ) {
55-
return
56-
}
68+
const clientId = blockToSave.clientId
69+
const {
70+
getBlockSave,
71+
getBlockAttributes,
72+
getBlockInnerBlocks,
73+
} = saveBlockStyleFuncs( select )
5774

58-
const {
59-
stk_block_name: blockName,
60-
stk_block_title: blockTitle,
61-
stk_style_slug: styleSlug,
62-
} = select( 'core/editor' ).getEditedPostAttribute( 'meta' )
75+
const attributes = getBlockAttributes( clientId )
76+
const innerBlocks = getBlockInnerBlocks( clientId )
77+
const blockSave = getBlockSave( clientId )
6378

64-
// The user can have multiple blocks, just pick the first one that we're really editing.
65-
const blocks = select( 'core/block-editor' ).getBlocks()
66-
const blockToSave = findBlock( blockName, blocks )
79+
const {
80+
updateBlockStyle,
81+
} = dispatch( 'stackable/block-styles' )
82+
83+
// The style name is the post title (if default).
84+
const postTitle = select( 'core/editor' ).getPostEdits().title || select( 'core/editor' ).getCurrentPost().title
85+
const styleName = styleSlug === 'default' ? __( 'Default', i18n ) : postTitle
86+
87+
// Save the block style.
88+
return updateBlockStyle( blockName, attributes, innerBlocks, blockSave, styleName, styleSlug )
89+
.then( () => {
90+
setTimeout( () => {
91+
const message = styleSlug === 'default'
92+
? sprintf( __( 'Default %s Block saved', i18n ), blockTitle )
93+
: sprintf( __( '%s Block style saved', i18n ), blockTitle )
6794

68-
// If there is no block, error.
69-
if ( ! blockToSave ) {
70-
setTimeout( () => { // We need to delay this for this to show up.
7195
dispatch( 'core/notices' ).createNotice(
7296
'success',
73-
sprintf( __( 'Error saving block, please make sure you only have a %s block', i18n ), blockTitle ),
97+
message,
7498
{
7599
type: 'snackbar',
76100
isDismissible: true,
77-
id: 'SAVE_POST_NOTICE_ID', // This is the "save post" notice id, override the "Please wait" message.
101+
id: 'stk-block-style-save-done', // This is the "save post" notice id, override the "Please wait" message.
78102
}
79103
)
80104
}, 100 )
81-
return
82-
}
83-
84-
const clientId = blockToSave.clientId
85-
const {
86-
getBlockSave,
87-
getBlockAttributes,
88-
getBlockInnerBlocks,
89-
} = saveBlockStyleFuncs( select )
90-
91-
const attributes = getBlockAttributes( clientId )
92-
const innerBlocks = getBlockInnerBlocks( clientId )
93-
const blockSave = getBlockSave( clientId )
94-
95-
const {
96-
updateBlockStyle,
97-
} = dispatch( 'stackable/block-styles' )
98-
99-
// The style name is the post title (if default).
100-
const postTitle = select( 'core/editor' ).getPostEdits().title || select( 'core/editor' ).getCurrentPost().title
101-
const styleName = styleSlug === 'default' ? __( 'Default', i18n ) : postTitle
105+
} )
106+
}
102107

103-
// Save the block style.
104-
updateBlockStyle( blockName, attributes, innerBlocks, blockSave, styleName, styleSlug )
105-
.then( () => {
108+
// Listen to editor updates when editing a 'stackable_temp_post'
109+
subscribe( () => {
110+
const postType = select( 'core/editor' )?.getCurrentPostType()
111+
if ( postType && postType === 'stackable_temp_post' ) {
112+
// The isSavingPost can trigger true multiple times, throttle it
113+
// manually so we only do our saving once.
114+
const isCurrentlySaving = select( 'core/editor' )?.isSavingPost()
115+
if ( isCurrentlySaving && isPerformingSave === false ) {
116+
isPerformingSave = true
117+
// Prevent this happening again and again.
118+
performSave()?.finally( () => {
106119
setTimeout( () => {
107-
const message = styleSlug === 'default'
108-
? sprintf( __( 'Default %s Block saved', i18n ), blockTitle )
109-
: sprintf( __( '%s Block style saved', i18n ), blockTitle )
110-
111-
dispatch( 'core/notices' ).createNotice(
112-
'success',
113-
message,
114-
{
115-
type: 'snackbar',
116-
isDismissible: true,
117-
id: 'SAVE_POST_NOTICE_ID', // This is the "save post" notice id, override the "Please wait" message.
118-
}
119-
)
120-
}, 100 )
120+
isPerformingSave = false
121+
}, 1000 ) // A long delay since this can re-trigger
121122
} )
123+
}
122124
}
123125
} )
124126

0 commit comments

Comments
 (0)