Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize isPreviewMode flag #66149

Merged
merged 12 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,7 @@ function BlockListBlockProvider( props ) {
const attributes = getBlockAttributes( clientId );
const { name: blockName, isValid } = blockWithoutAttributes;
const blockType = getBlockType( blockName );
const { supportsLayout, __unstableIsPreviewMode: isPreviewMode } =
getSettings();
const { supportsLayout, isPreviewMode } = getSettings();
const hasLightBlockWrapper = blockType?.apiVersion > 1;
const previewContext = {
isPreviewMode,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function Items( {

const _order = getBlockOrder( rootClientId );

if ( getSettings().__unstableIsPreviewMode ) {
if ( getSettings().isPreviewMode ) {
return {
order: _order,
selectedBlocks: EMPTY_ARRAY,
Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/block-preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function BlockPreview( {
() => ( {
...originalSettings,
focusMode: false, // Disable "Spotlight mode".
__unstableIsPreviewMode: true,
isPreviewMode: true,
} ),
[ originalSettings ]
);
Expand Down Expand Up @@ -124,7 +124,7 @@ export function useBlockPreview( { blocks, props = {}, layout } ) {
...originalSettings,
styles: undefined, // Clear styles included by the parent settings, as they are already output by the parent's EditorStyles.
focusMode: false, // Disable "Spotlight mode".
__unstableIsPreviewMode: true,
isPreviewMode: true,
} ),
[ originalSettings ]
);
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function Iframe( {
const settings = getSettings();
return {
resolvedAssets: settings.__unstableResolvedAssets,
isPreviewMode: settings.__unstableIsPreviewMode,
isPreviewMode: settings.isPreviewMode,
};
}, [] );
const { styles = '', scripts = '' } = resolvedAssets;
Expand Down
4 changes: 1 addition & 3 deletions packages/block-editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ export const ExperimentalBlockEditorProvider = withRegistryProvider(

return (
<SlotFillProvider passthrough>
{ ! settings?.__unstableIsPreviewMode && (
<KeyboardShortcuts.Register />
) }
{ ! settings?.isPreviewMode && <KeyboardShortcuts.Register /> }
<BlockRefsProvider>{ children }</BlockRefsProvider>
</SlotFillProvider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { render } from '@testing-library/react';
/**
* WordPress dependencies
*/
import { useRegistry } from '@wordpress/data';
import { useRegistry, useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -21,6 +21,20 @@ const HasEditorSetting = ( props ) => {
return <p>Test.</p>;
};

const PreviewModeGetter = () => {
const previewModeKeys = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const settings = getSettings();
return {
// This property will be removed in the future. There is a test that asserts we're throwing a deprecation warning.
__unstableIsPreviewMode: settings.__unstableIsPreviewMode,
isPreviewMode: settings.isPreviewMode,
};
}, [] );

return <>{ JSON.stringify( previewModeKeys ) }</>;
};

describe( 'BlockEditorProvider', () => {
let registry;
const setRegistry = ( reg ) => {
Expand Down Expand Up @@ -58,6 +72,34 @@ describe( 'BlockEditorProvider', () => {
const settings = registry.select( blockEditorStore ).getSettings();
expect( settings ).toHaveProperty( 'stableSetting' );
} );
it( 'preserves deprecated getters incoming from the settings reducer', async () => {
const consoleWarn = jest
.spyOn( global.console, 'warn' )
.mockImplementation();

const { container } = render(
<BlockEditorProvider
settings={ {
isPreviewMode: true,
} }
>
<PreviewModeGetter />
</BlockEditorProvider>
);

expect( container ).toHaveTextContent(
JSON.stringify( {
__unstableIsPreviewMode: true,
isPreviewMode: true,
} )
);

expect( consoleWarn ).toHaveBeenCalledWith(
'__unstableIsPreviewMode is deprecated since version 6.8. Please use isPreviewMode instead.'
);

consoleWarn.mockRestore();
} );
} );

describe( 'ExperimentalBlockEditorProvider', () => {
Expand Down
18 changes: 17 additions & 1 deletion packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,23 @@ export function updateBlockListSettings( clientId, settings ) {
* @return {Object} Action object
*/
export function updateSettings( settings ) {
return __experimentalUpdateSettings( settings, {
let updatedSettings = settings;

if ( Object.hasOwn( updatedSettings, '__unstableIsPreviewMode' ) ) {
zaguiini marked this conversation as resolved.
Show resolved Hide resolved
deprecated(
"__unstableIsPreviewMode argument in wp.data.dispatch('core/block-editor').updateSettings",
{
since: '6.8',
alternative: 'isPreviewMode',
}
);

updatedSettings = { ...updatedSettings };
updatedSettings.isPreviewMode = updatedSettings.__unstableIsPreviewMode;
delete updatedSettings.__unstableIsPreviewMode;
}

return __experimentalUpdateSettings( updatedSettings, {
stripExperimentalSettings: true,
} );
}
Expand Down
3 changes: 2 additions & 1 deletion packages/block-editor/src/store/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ export const SETTINGS_DEFAULTS = {
__mobileEnablePageTemplates: false,
__experimentalBlockPatterns: [],
__experimentalBlockPatternCategories: [],
__unstableIsPreviewMode: false,

isPreviewMode: false,

// These settings will be completely revamped in the future.
// The goal is to evolve this into an API which will instruct
Expand Down
36 changes: 25 additions & 11 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fastDeepEqual from 'fast-deep-equal/es6';
*/
import { pipe } from '@wordpress/compose';
import { combineReducers, select } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
import { store as blocksStore } from '@wordpress/blocks';
/**
* Internal dependencies
Expand Down Expand Up @@ -1660,17 +1661,30 @@ export function template( state = { isValid: true }, action ) {
*/
export function settings( state = SETTINGS_DEFAULTS, action ) {
switch ( action.type ) {
case 'UPDATE_SETTINGS':
if ( action.reset ) {
return {
...SETTINGS_DEFAULTS,
...action.settings,
};
}
return {
...state,
...action.settings,
};
case 'UPDATE_SETTINGS': {
zaguiini marked this conversation as resolved.
Show resolved Hide resolved
const updatedSettings = action.reset
? {
...SETTINGS_DEFAULTS,
...action.settings,
}
: {
...state,
...action.settings,
};

Object.defineProperty( updatedSettings, '__unstableIsPreviewMode', {
get() {
deprecated( '__unstableIsPreviewMode', {
since: '6.8',
alternative: 'isPreviewMode',
} );

return this.isPreviewMode;
},
} );

return updatedSettings;
}
}

return state;
Expand Down
41 changes: 41 additions & 0 deletions packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,47 @@ describe( 'actions', () => {
} );
} );

describe( 'updateSettings', () => {
it( 'warns when setting the deprecated __unstableIsPreviewMode property and sets the stable property instead', () => {
const consoleWarn = jest
.spyOn( global.console, 'warn' )
.mockImplementation();

const store = createRegistry().registerStore(
blockEditorStoreName,
{
actions,
selectors,
reducer,
}
);

store.dispatch(
updateSettings( {
__unstableIsPreviewMode: true,
} )
);

expect( consoleWarn ).toHaveBeenCalledWith(
"__unstableIsPreviewMode argument in wp.data.dispatch('core/block-editor').updateSettings is deprecated since version 6.8. Please use isPreviewMode instead."
);

consoleWarn.mockClear();

expect( store.getState().settings.__unstableIsPreviewMode ).toBe(
true
);

expect( store.getState().settings.isPreviewMode ).toBe( true );

expect( consoleWarn ).toHaveBeenCalledWith(
'__unstableIsPreviewMode is deprecated since version 6.8. Please use isPreviewMode instead.'
);

consoleWarn.mockRestore();
} );
} );

describe( 'registerInserterMediaCategory', () => {
describe( 'should log errors when invalid', () => {
it( 'valid object', () => {
Expand Down
23 changes: 23 additions & 0 deletions packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
insertionPoint,
template,
blockListSettings,
settings,
lastBlockAttributesChange,
lastBlockInserted,
blockEditingModes,
Expand Down Expand Up @@ -3070,6 +3071,28 @@ describe( 'state', () => {
} );
} );

describe( 'settings', () => {
it( 'should warn about __unstableIsPreviewMode deprecation', () => {
const consoleWarn = jest
.spyOn( global.console, 'warn' )
.mockImplementation();

const settingsObject = settings( undefined, {
type: 'UPDATE_SETTINGS',
reset: true,
} );

expect( settingsObject.__unstableIsPreviewMode ).toBeDefined();
expect( settingsObject.isPreviewMode ).toBeDefined();

expect( consoleWarn ).toHaveBeenCalledWith(
'__unstableIsPreviewMode is deprecated since version 6.8. Please use isPreviewMode instead.'
);

consoleWarn.mockRestore();
} );
} );

describe( 'blockListSettings', () => {
it( 'should add new settings', () => {
const original = deepFreeze( {} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function useSpecificEditorSettings() {
defaultRenderingMode,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
__unstableIsPreviewMode: canvas === 'view',
isPreviewMode: canvas === 'view',
};
}, [
settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function usePatternSettings() {
return {
...restStoredSettings,
__experimentalBlockPatterns: blockPatterns,
__unstableIsPreviewMode: true,
isPreviewMode: true,
};
}, [ storedSettings, blockPatterns ] );

Expand Down
5 changes: 4 additions & 1 deletion packages/edit-site/src/components/revisions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ function Revisions( { userConfig, blocks } ) {
[]
);
const settings = useMemo(
() => ( { ...originalSettings, __unstableIsPreviewMode: true } ),
() => ( {
...originalSettings,
isPreviewMode: true,
} ),
[ originalSettings ]
);

Expand Down
7 changes: 5 additions & 2 deletions packages/edit-site/src/components/style-book/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ function StyleBook( {
);

const settings = useMemo(
() => ( { ...originalSettings, __unstableIsPreviewMode: true } ),
() => ( {
...originalSettings,
isPreviewMode: true,
} ),
[ originalSettings ]
);

Expand Down Expand Up @@ -328,7 +331,7 @@ const Example = ( { id, title, blocks, isSelected, onClick } ) => {
() => ( {
...originalSettings,
focusMode: false, // Disable "Spotlight mode".
__unstableIsPreviewMode: true,
isPreviewMode: true,
} ),
[ originalSettings ]
);
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function useEditorCommandLoader() {
isDistractionFree: get( 'core', 'distractionFree' ),
isFocusMode: get( 'core', 'focusMode' ),
isTopToolbar: get( 'core', 'fixedToolbar' ),
isPreviewMode: getSettings().__unstableIsPreviewMode,
isPreviewMode: getSettings().isPreviewMode,
isViewable: getPostType( getCurrentPostType() )?.viewable ?? false,
isCodeEditingEnabled: getEditorSettings().codeEditingEnabled,
isRichEditingEnabled: getEditorSettings().richEditingEnabled,
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/editor-interface/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default function EditorInterface( {
isInserterOpened: select( editorStore ).isInserterOpened(),
isListViewOpened: select( editorStore ).isListViewOpened(),
isDistractionFree: get( 'core', 'distractionFree' ),
isPreviewMode: editorSettings.__unstableIsPreviewMode,
isPreviewMode: editorSettings.isPreviewMode,
showBlockBreadcrumbs: get( 'core', 'showBlockBreadcrumbs' ),
// translators: Default label for the Document in the Block Breadcrumb.
documentLabel: postTypeLabel || _x( 'Document', 'noun' ),
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export const ExperimentalEditorProvider = withRegistryProvider(
useSubRegistry={ false }
>
{ children }
{ ! settings.__unstableIsPreviewMode && (
{ ! settings.isPreviewMode && (
<>
<PatternsMenuItems />
<TemplatePartMenuItems />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const BLOCK_EDITOR_SETTINGS = [
'imageDimensions',
'imageEditing',
'imageSizes',
'isPreviewMode',
'isRTL',
'locale',
'maxWidth',
Expand All @@ -85,7 +86,6 @@ const BLOCK_EDITOR_SETTINGS = [
'supportsLayout',
'widgetTypesToHideFromLegacyWidgetBlock',
'__unstableHasCustomAppender',
'__unstableIsPreviewMode',
'__unstableResolvedAssets',
'__unstableIsBlockBasedTheme',
];
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function VisualEditor( {
deviceType: getDeviceType(),
isFocusedEntity: !! editorSettings.onNavigateToPreviousEntityRecord,
postType: postTypeSlug,
isPreview: editorSettings.__unstableIsPreviewMode,
isPreview: editorSettings.isPreviewMode,
};
}, [] );
const { isCleanNewPost } = useSelect( editorStore );
Expand Down
Loading