@@ -5,16 +5,27 @@ import { store as editorStore } from '@wordpress/editor';
5
5
import { store as noticesStore } from '@wordpress/notices' ;
6
6
7
7
export const useContentModelNameLengthRestrictor = ( ) => {
8
- const { editPost } = useDispatch ( editorStore ) ;
8
+ const { editPost, lockPostSaving, unlockPostSaving } =
9
+ useDispatch ( editorStore ) ;
9
10
const { createNotice } = useDispatch ( noticesStore ) ;
10
11
11
12
const title = useSelect ( ( select ) =>
12
13
select ( editorStore ) . getEditedPostAttribute ( 'title' )
13
14
) ;
14
15
15
16
useEffect ( ( ) => {
16
- if ( title && title . length > 20 ) {
17
- editPost ( { title : title . substring ( 0 , 20 ) } ) ;
17
+ const trimmedTitle = title ?. trim ( ) ?? '' ;
18
+
19
+ if ( trimmedTitle . length === 0 ) {
20
+ lockPostSaving ( 'title-empty-lock' ) ;
21
+ return ;
22
+ }
23
+
24
+ unlockPostSaving ( 'title-empty-lock' ) ;
25
+
26
+ editPost ( { title : trimmedTitle . substring ( 0 , 20 ) } ) ;
27
+
28
+ if ( trimmedTitle . length > 20 ) {
18
29
createNotice (
19
30
'warning' ,
20
31
__ ( 'Title is limited to 20 characters.' ) ,
@@ -24,5 +35,5 @@ export const useContentModelNameLengthRestrictor = () => {
24
35
}
25
36
) ;
26
37
}
27
- } , [ title , editPost , createNotice ] ) ;
38
+ } , [ title , lockPostSaving , unlockPostSaving , editPost , createNotice ] ) ;
28
39
} ;
0 commit comments