-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Add data component for editor initialization to replace __u…
…nstableInitialize action. (fixes #15403) (#15444) Create EditorInitializer component and implement for various things to initialize as the editor is loaded. This replaces the `__unstableInitialize` refactor done in #14740.
- Loading branch information
Showing
11 changed files
with
413 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/edit-post/src/components/editor-initialization/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect } from '@wordpress/element'; | ||
import { useDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
useAdjustSidebarListener, | ||
useBlockSelectionListener, | ||
useUpdatePostLinkListener, | ||
} from './listener-hooks'; | ||
|
||
/** | ||
* Data component used for initializing the editor and re-initializes | ||
* when postId changes or on unmount. | ||
* | ||
* @param {number} postId The id of the post. | ||
* @return {null} This is a data component so does not render any ui. | ||
*/ | ||
export default function( { postId } ) { | ||
useAdjustSidebarListener( postId ); | ||
useBlockSelectionListener( postId ); | ||
useUpdatePostLinkListener( postId ); | ||
const { triggerGuide } = useDispatch( 'core/nux' ); | ||
useEffect( () => { | ||
triggerGuide( [ | ||
'core/editor.inserter', | ||
'core/editor.settings', | ||
'core/editor.preview', | ||
'core/editor.publish', | ||
] ); | ||
}, [ triggerGuide ] ); | ||
return null; | ||
} |
106 changes: 106 additions & 0 deletions
106
packages/edit-post/src/components/editor-initialization/listener-hooks.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { useEffect, useRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
STORE_KEY, | ||
VIEW_AS_LINK_SELECTOR, | ||
VIEW_AS_PREVIEW_LINK_SELECTOR, | ||
} from '../../store/constants'; | ||
|
||
/** | ||
* This listener hook monitors for block selection and triggers the appropriate | ||
* sidebar state. | ||
* | ||
* @param {number} postId The current post id. | ||
*/ | ||
export const useBlockSelectionListener = ( postId ) => { | ||
const { | ||
hasBlockSelection, | ||
isEditorSidebarOpened, | ||
} = useSelect( | ||
( select ) => ( { | ||
hasBlockSelection: !! select( | ||
'core/block-editor' | ||
).getBlockSelectionStart(), | ||
isEditorSidebarOpened: select( STORE_KEY ).isEditorSidebarOpened(), | ||
} ), | ||
[ postId ] | ||
); | ||
|
||
const { openGeneralSidebar } = useDispatch( STORE_KEY ); | ||
|
||
useEffect( () => { | ||
if ( ! isEditorSidebarOpened ) { | ||
return; | ||
} | ||
if ( hasBlockSelection ) { | ||
openGeneralSidebar( 'edit-post/block' ); | ||
} else { | ||
openGeneralSidebar( 'edit-post/document' ); | ||
} | ||
}, [ hasBlockSelection, isEditorSidebarOpened ] ); | ||
}; | ||
|
||
/** | ||
* This listener hook is used to monitor viewport size and adjust the sidebar | ||
* accordingly. | ||
* | ||
* @param {number} postId The current post id. | ||
*/ | ||
export const useAdjustSidebarListener = ( postId ) => { | ||
const { isSmall, sidebarToReOpenOnExpand } = useSelect( | ||
( select ) => ( { | ||
isSmall: select( 'core/viewport' ).isViewportMatch( '< medium' ), | ||
sidebarToReOpenOnExpand: select( STORE_KEY ).getActiveGeneralSidebarName(), | ||
} ), | ||
[ postId ] | ||
); | ||
|
||
const { openGeneralSidebar, closeGeneralSidebar } = useDispatch( STORE_KEY ); | ||
|
||
const previousOpenedSidebar = useRef( '' ); | ||
|
||
useEffect( () => { | ||
if ( isSmall && sidebarToReOpenOnExpand ) { | ||
previousOpenedSidebar.current = sidebarToReOpenOnExpand; | ||
closeGeneralSidebar(); | ||
} else if ( ! isSmall && previousOpenedSidebar.current ) { | ||
openGeneralSidebar( previousOpenedSidebar.current ); | ||
previousOpenedSidebar.current = ''; | ||
} | ||
}, [ isSmall, sidebarToReOpenOnExpand ] ); | ||
}; | ||
|
||
/** | ||
* This listener hook monitors any change in permalink and updates the view | ||
* post link in the admin bar. | ||
* | ||
* @param {number} postId | ||
*/ | ||
export const useUpdatePostLinkListener = ( postId ) => { | ||
const { newPermalink } = useSelect( | ||
( select ) => ( { | ||
newPermalink: select( 'core/editor' ).getCurrentPost().link, | ||
} ), | ||
[ postId ] | ||
); | ||
const nodeToUpdate = useRef(); | ||
|
||
useEffect( () => { | ||
nodeToUpdate.current = document.querySelector( VIEW_AS_PREVIEW_LINK_SELECTOR ) || | ||
document.querySelector( VIEW_AS_LINK_SELECTOR ); | ||
}, [ postId ] ); | ||
|
||
useEffect( () => { | ||
if ( ! newPermalink || ! nodeToUpdate.current ) { | ||
return; | ||
} | ||
nodeToUpdate.current.setAttribute( 'href', newPermalink ); | ||
}, [ newPermalink ] ); | ||
}; |
Oops, something went wrong.