Skip to content

Commit

Permalink
Add compat mode for freeform block
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Dec 1, 2022
1 parent 75de669 commit be43ef0
Showing 1 changed file with 62 additions and 31 deletions.
93 changes: 62 additions & 31 deletions packages/block-library/src/freeform/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { debounce } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { ToolbarGroup } from '@wordpress/components';
import { useEffect, useRef } from '@wordpress/element';
import { debounce, useRefEffect } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
import { ToolbarButton, ToolbarGroup } from '@wordpress/components';
import { RawHTML, useEffect, useRef, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { BACKSPACE, DELETE, F10, isKeyboardEvent } from '@wordpress/keycodes';

Expand All @@ -36,17 +36,57 @@ function isTmceEmpty( editor ) {
return /^\n?$/.test( body.innerText || body.textContent );
}

export default function ClassicEdit( {
export default function FreeformEdit( props ) {
const {
clientId,
attributes: { content },
} = props;
const canRemove = useSelect(
( select ) => select( blockEditorStore ).canRemoveBlock( clientId ),
[ clientId ]
);
const { toggleBlockMode } = useDispatch( blockEditorStore );
const [ isIframed, setIsIframed ] = useState( false );
const ref = useRefEffect( ( element ) => {
setIsIframed( element.ownerDocument !== document );
}, [] );

return (
<>
{ canRemove && (
<BlockControls>
<ToolbarGroup>
<ConvertToBlocksButton clientId={ clientId } />
{ isIframed && (
<ToolbarButton
onClick={ () => {
toggleBlockMode( clientId );
} }
>
{ __( 'Edit as HTML' ) }
</ToolbarButton>
) }
</ToolbarGroup>
</BlockControls>
) }
<div { ...useBlockProps( { ref } ) }>
{ isIframed ? (
<RawHTML>{ content }</RawHTML>
) : (
<ClassicEdit { ...props } />
) }
</div>
</>
);
}

function ClassicEdit( {
clientId,
attributes: { content },
setAttributes,
onReplace,
} ) {
const { getMultiSelectedBlockClientIds } = useSelect( blockEditorStore );
const canRemove = useSelect(
( select ) => select( blockEditorStore ).canRemoveBlock( clientId ),
[ clientId ]
);
const didMount = useRef( false );

useEffect( () => {
Expand Down Expand Up @@ -225,28 +265,19 @@ export default function ClassicEdit( {
/* eslint-disable jsx-a11y/no-static-element-interactions */
return (
<>
{ canRemove && (
<BlockControls>
<ToolbarGroup>
<ConvertToBlocksButton clientId={ clientId } />
</ToolbarGroup>
</BlockControls>
) }
<div { ...useBlockProps() }>
<div
key="toolbar"
id={ `toolbar-${ clientId }` }
className="block-library-classic__toolbar"
onClick={ focus }
data-placeholder={ __( 'Classic' ) }
onKeyDown={ onToolbarKeyDown }
/>
<div
key="editor"
id={ `editor-${ clientId }` }
className="wp-block-freeform block-library-rich-text__tinymce"
/>
</div>
<div
key="toolbar"
id={ `toolbar-${ clientId }` }
className="block-library-classic__toolbar"
onClick={ focus }
data-placeholder={ __( 'Classic' ) }
onKeyDown={ onToolbarKeyDown }
/>
<div
key="editor"
id={ `editor-${ clientId }` }
className="wp-block-freeform block-library-rich-text__tinymce"
/>
</>
);
/* eslint-enable jsx-a11y/no-static-element-interactions */
Expand Down

0 comments on commit be43ef0

Please sign in to comment.