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

HTML Block: Force HTML preview in view mode #66440

Merged
merged 2 commits into from
Oct 25, 2024
Merged
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
8 changes: 7 additions & 1 deletion packages/block-library/src/html/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import {
BlockControls,
PlainText,
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import {
ToolbarButton,
Disabled,
ToolbarGroup,
VisuallyHidden,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useInstanceId } from '@wordpress/compose';

/**
Expand All @@ -27,6 +29,10 @@ export default function HTMLEdit( { attributes, setAttributes, isSelected } ) {

const instanceId = useInstanceId( HTMLEdit, 'html-edit-desc' );

const isPreviewMode = useSelect( ( select ) => {
return select( blockEditorStore ).getSettings().isPreviewMode;
}, [] );
Comment on lines +32 to +34
Copy link
Member

Choose a reason for hiding this comment

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

The isPreviewMode is also available via the private key in the block edit context. However, considering the block library code is probably the most copied code in the repo, using a selector makes more sense.


function switchToPreview() {
setIsPreview( true );
}
Expand Down Expand Up @@ -58,7 +64,7 @@ export default function HTMLEdit( { attributes, setAttributes, isSelected } ) {
</ToolbarButton>
</ToolbarGroup>
</BlockControls>
{ isPreview || isDisabled ? (
{ isPreview || isPreviewMode || isDisabled ? (
<>
<Preview
content={ attributes.content }
Expand Down
5 changes: 3 additions & 2 deletions packages/block-library/src/html/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ const DEFAULT_STYLES = `

export default function HTMLEditPreview( { content, isSelected } ) {
const settingStyles = useSelect(
( select ) => select( blockEditorStore ).getSettings().styles
( select ) => select( blockEditorStore ).getSettings().styles,
[]
);

const styles = useMemo(
() => [
DEFAULT_STYLES,
...transformStyles(
settingStyles.filter( ( style ) => style.css )
( settingStyles ?? [] ).filter( ( style ) => style.css )
),
],
[ settingStyles ]
Expand Down
Loading