Skip to content

Commit

Permalink
HTML Block: Force HTML preview in view mode (WordPress#66440)
Browse files Browse the repository at this point in the history
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: ndiego <ndiego@git.wordpress.org>
Co-authored-by: talldan <talldanwp@git.wordpress.org>
  • Loading branch information
6 people authored and karthick-murugan committed Nov 13, 2024
1 parent caa1d96 commit b2e9eba
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
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;
}, [] );

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

0 comments on commit b2e9eba

Please sign in to comment.