Skip to content
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
118 changes: 62 additions & 56 deletions packages/block-library/src/html/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,94 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useContext, useState } from '@wordpress/element';
import { useState } from '@wordpress/element';
import {
BlockControls,
PlainText,
BlockIcon,
InspectorControls,
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import {
ToolbarButton,
Disabled,
ToolbarGroup,
VisuallyHidden,
Placeholder,
Button,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useInstanceId } from '@wordpress/compose';
import { code } from '@wordpress/icons';

/**
* Internal dependencies
*/
import Preview from './preview';
import HTMLEditModal from './modal';

export default function HTMLEdit( { attributes, setAttributes, isSelected } ) {
const [ isPreview, setIsPreview ] = useState();
const isDisabled = useContext( Disabled.Context );

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

const isPreviewMode = useSelect( ( select ) => {
return select( blockEditorStore ).getSettings().isPreviewMode;
}, [] );

function switchToPreview() {
setIsPreview( true );
}

function switchToHTML() {
setIsPreview( false );
}

const [ isModalOpen, setIsModalOpen ] = useState( false );
const blockProps = useBlockProps( {
className: 'block-library-html__edit',
'aria-describedby': isPreview ? instanceId : undefined,
} );

// Show placeholder when content is empty
if ( ! attributes.content?.trim() ) {
return (
<div { ...blockProps }>
<Placeholder
icon={ <BlockIcon icon={ code } /> }
label={ __( 'Custom HTML' ) }
instructions={ __(
'Add custom HTML code and preview how it looks.'
) }
>
<Button
__next40pxDefaultSize
variant="primary"
onClick={ () => setIsModalOpen( true ) }
>
{ __( 'Edit HTML' ) }
</Button>
</Placeholder>
<HTMLEditModal
isOpen={ isModalOpen }
onRequestClose={ () => setIsModalOpen( false ) }
content={ attributes.content }
setAttributes={ setAttributes }
/>
</div>
);
}

return (
<div { ...blockProps }>
<BlockControls>
<ToolbarGroup>
<ToolbarButton
isPressed={ ! isPreview }
onClick={ switchToHTML }
>
HTML
</ToolbarButton>
<ToolbarButton
isPressed={ isPreview }
onClick={ switchToPreview }
>
{ __( 'Preview' ) }
<ToolbarButton onClick={ () => setIsModalOpen( true ) }>
{ __( 'Edit code' ) }
</ToolbarButton>
</ToolbarGroup>
</BlockControls>
{ isPreview || isPreviewMode || isDisabled ? (
<>
<Preview
content={ attributes.content }
isSelected={ isSelected }
/>
<VisuallyHidden id={ instanceId }>
{ __(
'HTML preview is not yet fully accessible. Please switch screen reader to virtualized mode to navigate the below iFrame.'
) }
</VisuallyHidden>
</>
) : (
<PlainText
value={ attributes.content }
onChange={ ( content ) => setAttributes( { content } ) }
placeholder={ __( 'Write HTML…' ) }
aria-label={ __( 'HTML' ) }
/>
) }
<InspectorControls>
<VStack
className="block-editor-block-inspector-edit-contents"
expanded
>
<Button
className="block-editor-block-inspector-edit-contents__button"
__next40pxDefaultSize
variant="secondary"
onClick={ () => setIsModalOpen( true ) }
>
{ __( 'Edit code' ) }
</Button>
</VStack>
</InspectorControls>
<Preview content={ attributes.content } isSelected={ isSelected } />
<HTMLEditModal
isOpen={ isModalOpen }
onRequestClose={ () => setIsModalOpen( false ) }
content={ attributes.content }
setAttributes={ setAttributes }
/>
</div>
);
}
79 changes: 69 additions & 10 deletions packages/block-library/src/html/editor.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@use "@wordpress/base-styles/mixins" as *;
@use "@wordpress/base-styles/variables" as *;
@use "@wordpress/base-styles/colors" as *;

.block-library-html__edit {
.block-library-html__preview-overlay {
Expand All @@ -8,16 +10,73 @@
top: 0;
left: 0;
}
}

// Modal styles
.block-library-html__modal {
// Make modal content scrollable
.components-modal__content {
display: flex;
flex-direction: column;
padding: 0;
min-height: 70vh;
}

.components-modal__children-container {
height: 100%;
padding: $grid-unit-20;
}
}

.block-library-html__modal-tabs {
height: 100%;
}

.block-library-html__modal-tab {
height: 100%;
display: flex;
flex-direction: column;
margin: 0;
box-sizing: border-box;
border: 1px solid $gray-200;
border-radius: 2px;
padding: $grid-unit-20;
font-family: $editor-html-font;
}

.block-library-html__modal-editor {
width: 100%;
height: 100%;
flex: 1;
// Reset textarea styles to inherit from pre
border: none;
background: transparent;
padding: 0;
font-family: inherit;
font-size: inherit;
line-height: inherit;
color: inherit;
resize: none;
// HTML input is always LTR regardless of language.
/*rtl:ignore*/
direction: ltr;
overflow-x: auto;
box-sizing: border-box;

// The editing view for the HTML block is equivalent to block UI.
// Therefore we increase specificity to avoid theme styles bleeding in.
.block-editor-plain-text {
display: block;
box-sizing: border-box;
max-height: 250px;
@include editor-input-reset();
// HTML input is always LTR regardless of language.
/*rtl:ignore*/
direction: ltr;
&:focus {
outline: none;
box-shadow: none;
}
}

.block-library-html__preview {
display: flex;
align-items: center;
justify-content: center;
padding: $grid-unit-60;
}

.block-library-html__modal-actions {
margin-top: $grid-unit-20;
padding: 0 $grid-unit-40 $grid-unit-40;
}
Loading