|
3 | 3 | */ |
4 | 4 | import { castArray } from 'lodash'; |
5 | 5 |
|
| 6 | +/** |
| 7 | + * Internal dependencies |
| 8 | + */ |
| 9 | +import { __unstableSubscribe } from './controls'; |
| 10 | +import { onChangeListener } from './utils'; |
| 11 | +import { STORE_KEY, VIEW_AS_LINK_SELECTOR } from './constants'; |
| 12 | + |
6 | 13 | /** |
7 | 14 | * Returns an action object used in signalling that the user opened an editor sidebar. |
8 | 15 | * |
@@ -232,3 +239,74 @@ export function metaBoxUpdatesSuccess() { |
232 | 239 | }; |
233 | 240 | } |
234 | 241 |
|
| 242 | +/** |
| 243 | + * Returns an action generator used to initialize some subscriptions for the |
| 244 | + * post editor: |
| 245 | + * |
| 246 | + * - subscription for toggling the `edit-post/block` general sidebar when a |
| 247 | + * block is selected. |
| 248 | + * - subscription for hiding/showing the sidebar depending on size of viewport. |
| 249 | + * - subscription for updating the "View Post" link in the admin bar when |
| 250 | + * permalink is updated. |
| 251 | + */ |
| 252 | +export function* __unstableInitialize() { |
| 253 | + // Select the block settings tab when the selected block changes |
| 254 | + yield __unstableSubscribe( ( registry ) => onChangeListener( |
| 255 | + () => !! registry.select( 'core/block-editor' ) |
| 256 | + .getBlockSelectionStart(), |
| 257 | + ( hasBlockSelection ) => { |
| 258 | + if ( ! registry.select( 'core/edit-post' ).isEditorSidebarOpened() ) { |
| 259 | + return; |
| 260 | + } |
| 261 | + if ( hasBlockSelection ) { |
| 262 | + registry.dispatch( STORE_KEY ) |
| 263 | + .openGeneralSidebar( 'edit-post/block' ); |
| 264 | + } else { |
| 265 | + registry.dispatch( STORE_KEY ) |
| 266 | + .openGeneralSidebar( 'edit-post/document' ); |
| 267 | + } |
| 268 | + } |
| 269 | + ) ); |
| 270 | + // hide/show the sidebar depending on size of viewport. |
| 271 | + yield __unstableSubscribe( ( registry ) => onChangeListener( |
| 272 | + () => registry.select( 'core/viewport' ) |
| 273 | + .isViewportMatch( '< medium' ), |
| 274 | + ( () => { |
| 275 | + let sidebarToReOpenOnExpand = null; |
| 276 | + return ( isSmall ) => { |
| 277 | + const { getActiveGeneralSidebarName } = registry.select( STORE_KEY ); |
| 278 | + const { |
| 279 | + closeGeneralSidebar: closeSidebar, |
| 280 | + openGeneralSidebar: openSidebar, |
| 281 | + } = registry.dispatch( STORE_KEY ); |
| 282 | + if ( isSmall ) { |
| 283 | + sidebarToReOpenOnExpand = getActiveGeneralSidebarName(); |
| 284 | + if ( sidebarToReOpenOnExpand ) { |
| 285 | + closeSidebar(); |
| 286 | + } |
| 287 | + } else if ( |
| 288 | + sidebarToReOpenOnExpand && |
| 289 | + ! getActiveGeneralSidebarName() |
| 290 | + ) { |
| 291 | + openSidebar( sidebarToReOpenOnExpand ); |
| 292 | + } |
| 293 | + }; |
| 294 | + } )(), |
| 295 | + true |
| 296 | + ) ); |
| 297 | + // Update View Post link in the admin bar when permalink is updated. |
| 298 | + yield __unstableSubscribe( ( registry ) => onChangeListener( |
| 299 | + () => registry.select( 'core/editor' ).getCurrentPost().link, |
| 300 | + ( newPermalink ) => { |
| 301 | + if ( ! newPermalink ) { |
| 302 | + return; |
| 303 | + } |
| 304 | + const nodeToUpdate = document.querySelector( VIEW_AS_LINK_SELECTOR ); |
| 305 | + if ( ! nodeToUpdate ) { |
| 306 | + return; |
| 307 | + } |
| 308 | + nodeToUpdate.setAttribute( 'href', newPermalink ); |
| 309 | + } |
| 310 | + ) ); |
| 311 | +} |
| 312 | + |
0 commit comments