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

Draft
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/block-preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function BlockPreview( {
...originalSettings,
focusMode: false, // Disable "Spotlight mode".
__unstableIsPreviewMode: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this a getter and deprecate it or something?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can avoid passing all these __unstableIsPreviewMode configs and instead have a deprecated "property getter" in the default settings that fallbacks to isPreviewMode

Copy link
Member Author

@zaguiini zaguiini Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As much as I love that idea, it seems incredibly complex to do it right.

We're using the SETTINGS_DEFAULTS object in many places (defaults.native, the reducer, the Editor store), and the spread operator does not include the prototype, meaning the getter is not copied. Also, overriding the settings in this file, for example, would also result in the getter not being copied.

So, I think this duplication is better. Unfortunately, we cannot display the deprecation notice because we're not using the getter function. However, a possible workaround would be to add the getter function exclusively to the sub-registries (in this file and the others that override the original settings) and keep the reducers (block editor store, editor store) intact. I believe we can even remove the __unstableIsPreviewMode property from it directly if we're feeling adventurous.

isPreviewMode: true,
} ),
[ originalSettings ]
);
Expand Down Expand Up @@ -125,6 +126,7 @@ export function useBlockPreview( { blocks, props = {}, layout } ) {
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
8 changes: 8 additions & 0 deletions packages/block-editor/src/store/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,16 @@ export const SETTINGS_DEFAULTS = {
__mobileEnablePageTemplates: false,
__experimentalBlockPatterns: [],
__experimentalBlockPatternCategories: [],

/**
* Use isPreviewMode instead.
*
* @deprecated
*/
__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
// the block inspector to animate transitions between what it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export function useSpecificEditorSettings() {
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
__unstableIsPreviewMode: canvasMode === 'view',
isPreviewMode: canvasMode === 'view',
};
}, [
settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default function usePatternSettings() {
...restStoredSettings,
__experimentalBlockPatterns: blockPatterns,
__unstableIsPreviewMode: true,
isPreviewMode: true,
};
}, [ storedSettings, blockPatterns ] );

Expand Down
6 changes: 5 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,11 @@ function Revisions( { userConfig, blocks } ) {
[]
);
const settings = useMemo(
() => ( { ...originalSettings, __unstableIsPreviewMode: true } ),
() => ( {
...originalSettings,
__unstableIsPreviewMode: true,
isPreviewMode: true,
} ),
[ originalSettings ]
);

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

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

Expand Down Expand Up @@ -330,6 +334,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 @@ -323,7 +323,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 @@ -86,6 +86,7 @@ const BLOCK_EDITOR_SETTINGS = [
'widgetTypesToHideFromLegacyWidgetBlock',
'__unstableHasCustomAppender',
'__unstableIsPreviewMode',
'isPreviewMode', // TODO: Reorder this when removing __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 @@ -167,7 +167,7 @@ function VisualEditor( {
deviceType: getDeviceType(),
isFocusedEntity: !! editorSettings.onNavigateToPreviousEntityRecord,
postType: postTypeSlug,
isPreview: editorSettings.__unstableIsPreviewMode,
isPreview: editorSettings.isPreviewMode,
};
}, [] );
const { isCleanNewPost } = useSelect( editorStore );
Expand Down
Loading