@@ -22,7 +22,7 @@ import { __, sprintf } from '@wordpress/i18n'
22
22
import { getBlockFromExample , createBlocksFromInnerBlocksTemplate } from '@wordpress/blocks'
23
23
24
24
// Saving can be triggered multiple times by the editor, throttle to the first trigger only.
25
- let isSaving = false
25
+ let isPerformingSave = false
26
26
27
27
// Gets the first block that has a block name.
28
28
const findBlock = ( blockName , blocks ) => {
@@ -38,87 +38,89 @@ const findBlock = ( blockName, blocks ) => {
38
38
return foundBlock
39
39
}
40
40
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
+ }
53
67
54
- if ( ! isSaving ) {
55
- return
56
- }
68
+ const clientId = blockToSave . clientId
69
+ const {
70
+ getBlockSave,
71
+ getBlockAttributes,
72
+ getBlockInnerBlocks,
73
+ } = saveBlockStyleFuncs ( select )
57
74
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 )
63
78
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 )
67
94
68
- // If there is no block, error.
69
- if ( ! blockToSave ) {
70
- setTimeout ( ( ) => { // We need to delay this for this to show up.
71
95
dispatch ( 'core/notices' ) . createNotice (
72
96
'success' ,
73
- sprintf ( __ ( 'Error saving block, please make sure you only have a %s block' , i18n ) , blockTitle ) ,
97
+ message ,
74
98
{
75
99
type : 'snackbar' ,
76
100
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.
78
102
}
79
103
)
80
104
} , 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
+ }
102
107
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 ( ( ) => {
106
119
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
121
122
} )
123
+ }
122
124
}
123
125
} )
124
126
0 commit comments