| 1 |
import { store as blockEditorStore } from '@wordpress/block-editor'; |
| 2 |
import { subscribe, useSelect } from '@wordpress/data'; |
| 3 |
import { store as editorStore } from '@wordpress/editor'; |
| 4 |
import { useEffect, useState } from '@wordpress/element'; |
| 5 |
|
| 6 |
//** This hook checks if the editor is interactive yet */ |
| 7 |
export const useEditorReady = () => { |
| 8 |
const [isEditorReady, setIsEditorReady] = useState(false); |
| 9 |
const blocksReady = useSelect( |
| 10 |
(select) => |
| 11 |
select(blockEditorStore).__unstableIsEditorReady || |
| 12 |
select(blockEditorStore).getBlockCount() > 0 || |
| 13 |
select(blockEditorStore).getSelectedBlockClientId(), |
| 14 |
); |
| 15 |
const editorReady = useSelect( |
| 16 |
(select) => |
| 17 |
select(editorStore).__unstableIsEditorReady || |
| 18 |
select(editorStore).isCleanNewPost(), |
| 19 |
); |
| 20 |
// TODO: do we need to wait on the iframe? |
| 21 |
|
| 22 |
useEffect(() => { |
| 23 |
const unsubscribe = subscribe(() => { |
| 24 |
if (blocksReady || editorReady) { |
| 25 |
setIsEditorReady(true); |
| 26 |
unsubscribe(); |
| 27 |
} |
| 28 |
}); |
| 29 |
}, [blocksReady, editorReady]); |
| 30 |
|
| 31 |
return isEditorReady; |
| 32 |
}; |
| 33 |
|