Skip to content

Commit

Permalink
wpcom-block-editor-nux: Support getting the canvas mode from the quer…
Browse files Browse the repository at this point in the history
…y string after GB 19.6 (#40045)

* Introduce @wordpress/router

* Get canvas mode from the URL search parameter

* changelog

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/11677336292

Upstream-Ref: Automattic/jetpack@e8c0297
  • Loading branch information
arthur791004 authored and matticbot committed Nov 5, 2024
1 parent e67c752 commit 7c409d8
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This is an alpha version! The changes listed here are not final.
- Fix PHPUnit coverage warnings.
- Help Center: fixed api typo
- We now check if JP_CONNECTION_INITIAL_STATE is defined before accessing it when using Global Styles'
- wpcom-block-editor: Support getting the canvas mode from the query string after GB 19.6

## [5.65.0] - 2024-10-29
### Added
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"@wordpress/icons": "10.9.0",
"@wordpress/plugins": "7.9.0",
"@wordpress/private-apis": "^1.8.1",
"@wordpress/router": "^1.8.11",
"@wordpress/url": "4.9.0",
"clsx": "2.1.1",
"debug": "4.3.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('jetpack-connection', 'lodash', 'moment', 'react', 'react-dom', 'react-jsx-runtime', 'wp-api-fetch', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-data-controls', 'wp-dom', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-notices', 'wp-nux', 'wp-plugins', 'wp-polyfill', 'wp-primitives', 'wp-private-apis', 'wp-url'), 'version' => '84c0373645699d975b72');
<?php return array('dependencies' => array('jetpack-connection', 'lodash', 'moment', 'react', 'react-dom', 'react-jsx-runtime', 'wp-api-fetch', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-data-controls', 'wp-dom', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-notices', 'wp-nux', 'wp-plugins', 'wp-polyfill', 'wp-primitives', 'wp-private-apis', 'wp-router', 'wp-url'), 'version' => 'acd9efa73a7249ceeaef');
4 changes: 2 additions & 2 deletions src/build/wpcom-block-editor-nux/wpcom-block-editor-nux.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/common/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as useCanvasMode } from './use-canvas-mode';
export { default as useLocation } from './use-location';
20 changes: 20 additions & 0 deletions src/common/hooks/use-canvas-mode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useSelect } from '@wordpress/data';
import useLocation from './use-location';

const useCanvasMode = () => {
const location = useLocation();

return useSelect(
select => {
// The canvas mode is limited to the site editor.
if ( ! select( 'core/edit-site' ) ) {
return null;
}

return new URLSearchParams( location?.search ).get( 'canvas' ) || 'view';
},
[ location?.search ]
);
};

export default useCanvasMode;
15 changes: 15 additions & 0 deletions src/common/hooks/use-location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as router from '@wordpress/router';
import { getUnlock } from '../utils';

const unlock = getUnlock();

const routerPrivateApis = router?.privateApis;

let useLocation: () => Location | null = () => null;

// The routerPrivateApis may be unavailable.
if ( unlock && routerPrivateApis && unlock( routerPrivateApis ) ) {
useLocation = unlock( routerPrivateApis ).useLocation;
}

export default useLocation;
21 changes: 21 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';

export const getUnlock = () => {
/**
* Sometimes Gutenberg doesn't allow you to re-register the module and throws an error.
* FIXME: The new version allow it by default, but we might need to ensure that all the site has the new version.
* @see https://github.com/Automattic/wp-calypso/pull/79663
*/
let unlock: ( object: any ) => any | undefined; // eslint-disable-line @typescript-eslint/no-explicit-any
try {
unlock = __dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.',
'@wordpress/edit-site'
).unlock;
return unlock;
} catch ( error ) {
// eslint-disable-next-line no-console
console.error( 'Error: Unable to get the unlock api. Reason: %s', error );
return undefined;
}
};
58 changes: 17 additions & 41 deletions src/features/wpcom-block-editor-nux/src/block-editor-nux.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useDispatch, useSelect } from '@wordpress/data';
import { useEffect, useState } from '@wordpress/element';
import { applyFilters } from '@wordpress/hooks';
import { registerPlugin } from '@wordpress/plugins';
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';
import { getQueryArg } from '@wordpress/url';
import { useCanvasMode } from '../../../common/hooks';
import {
HasSeenSellerCelebrationModalProvider,
HasSeenVideoCelebrationModalProvider,
Expand All @@ -22,22 +22,6 @@ import VideoPressCelebrationModal from './video-celebration-modal';
import WpcomNux from './welcome-modal/wpcom-nux';
import LaunchWpcomWelcomeTour from './welcome-tour/tour-launch';

/**
* Sometimes Gutenberg doesn't allow you to re-register the module and throws an error.
* FIXME: The new version allow it by default, but we might need to ensure that all the site has the new version.
* @see https://github.com/Automattic/wp-calypso/pull/79663
*/
let unlock;
try {
unlock = __dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.',
'@wordpress/edit-site'
).unlock;
} catch ( error ) {
// eslint-disable-next-line no-console
console.error( 'Error: Unable to get the unlock api. Reason: %s', error );
}

/**
* The WelcomeTour component
*/
Expand All @@ -46,31 +30,23 @@ function WelcomeTour() {
getQueryArg( window.location.href, 'showDraftPostModal' )
);

const {
show,
isLoaded,
variant,
isManuallyOpened,
isNewPageLayoutModalOpen,
siteEditorCanvasMode,
} = useSelect( select => {
const welcomeGuideStoreSelect = select( 'automattic/wpcom-welcome-guide' );
const starterPageLayoutsStoreSelect = select( 'automattic/starter-page-layouts' );
let canvasMode;
if ( unlock && select( 'core/edit-site' ) ) {
canvasMode =
select( 'core/edit-site' ) && unlock( select( 'core/edit-site' ) ).getCanvasMode();
}
const { show, isLoaded, variant, isManuallyOpened, isNewPageLayoutModalOpen } = useSelect(
select => {
const welcomeGuideStoreSelect = select( 'automattic/wpcom-welcome-guide' );
const starterPageLayoutsStoreSelect = select( 'automattic/starter-page-layouts' );

return {
show: welcomeGuideStoreSelect.isWelcomeGuideShown(),
isLoaded: welcomeGuideStoreSelect.isWelcomeGuideStatusLoaded(),
variant: welcomeGuideStoreSelect.getWelcomeGuideVariant(),
isManuallyOpened: welcomeGuideStoreSelect.isWelcomeGuideManuallyOpened(),
isNewPageLayoutModalOpen: starterPageLayoutsStoreSelect?.isOpen(), // Handle the case where SPT is not initalized.
};
},
[]
);

return {
show: welcomeGuideStoreSelect.isWelcomeGuideShown(),
isLoaded: welcomeGuideStoreSelect.isWelcomeGuideStatusLoaded(),
variant: welcomeGuideStoreSelect.getWelcomeGuideVariant(),
isManuallyOpened: welcomeGuideStoreSelect.isWelcomeGuideManuallyOpened(),
isNewPageLayoutModalOpen: starterPageLayoutsStoreSelect?.isOpen(), // Handle the case where SPT is not initalized.
siteEditorCanvasMode: canvasMode,
};
}, [] );
const siteEditorCanvasMode = useCanvasMode();

const setOpenState = useDispatch( 'automattic/starter-page-layouts' )?.setOpenState;

Expand Down

0 comments on commit 7c409d8

Please sign in to comment.