Skip to content

Commit

Permalink
add LecagyWrapper to Edit component
Browse files Browse the repository at this point in the history
  • Loading branch information
danalvrz committed Jun 5, 2024
1 parent cda2910 commit 93c582f
Showing 1 changed file with 64 additions and 49 deletions.
113 changes: 64 additions & 49 deletions packages/volto-heading-block/src/components/Edit.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import ContentEditable from 'react-contenteditable';
import { withBlockExtensions } from '@plone/volto/helpers';
import { SidebarPortal } from '@plone/volto/components';
import { SidebarPortal, MaybeWrap } from '@plone/volto/components';
import HeadingSidebar from './Sidebar';
import config from '@plone/volto/registry';
import cx from 'classnames';

// Source: https://stackoverflow.com/questions/5796718/html-entity-decode
var decodeHTMLEntities = (function () {
Expand Down Expand Up @@ -60,56 +61,70 @@ class HeadingEdit extends React.Component {
};

render = () => {
const { block, data, onChangeBlock, selected } = this.props;
const show_alignment = config.blocks?.blocksConfig?.heading?.show_alignment;
const { block, data, onChangeBlock, selected, blocksConfig, className } =
this.props;
const show_alignment = blocksConfig?.heading?.show_alignment;
const isBlockModelv3 = blocksConfig?.heading?.blockModel === 3;

const LegacyWrapper = ({ className, children }) => (
<div className={cx('block heading', className)}>{children}</div>
);

return (
<div className="block heading">
<div className="heading-wrapper">
<ContentEditable
innerRef={this.editable}
className="editable"
style={show_alignment ? { textAlign: data.alignment } : {}}
tagName={data.tag ?? 'h2'}
html={this.state.html} // innerHTML of the editable div
onChange={this.handleChange} // handle innerHTML change
onKeyDown={(e) => {
if (e.key === 'Enter') {
e.preventDefault();
e.stopPropagation();
this.props.onSelectBlock(
this.props.onAddBlock(
config.settings.defaultBlockType,
this.props.index + 1,
),
);
} else if (e.key === 'ArrowUp') {
e.preventDefault();
e.stopPropagation();
this.props.onFocusPreviousBlock(
this.props.block,
this.editable.current,
);
} else if (e.key === 'ArrowDown') {
e.preventDefault();
e.stopPropagation();
this.props.onFocusNextBlock(
this.props.block,
this.editable.current,
);
}
}}
/>
<SidebarPortal selected={selected}>
<HeadingSidebar
{...this.props}
data={data}
block={block}
onChangeBlock={onChangeBlock}
/>
</SidebarPortal>
</div>
</div>
<>
{data && (
<MaybeWrap
condition={!isBlockModelv3}
className={className}
as={LegacyWrapper}
>
<div className="heading-wrapper">
<ContentEditable
innerRef={this.editable}
className="editable"
style={show_alignment ? { textAlign: data.alignment } : {}}
tagName={data.tag ?? 'h2'}
html={this.state.html} // innerHTML of the editable div
onChange={this.handleChange} // handle innerHTML change
onKeyDown={(e) => {
if (e.key === 'Enter') {
e.preventDefault();
e.stopPropagation();
this.props.onSelectBlock(
this.props.onAddBlock(
config.settings.defaultBlockType,
this.props.index + 1,
),
);
} else if (e.key === 'ArrowUp') {
e.preventDefault();
e.stopPropagation();
this.props.onFocusPreviousBlock(
this.props.block,
this.editable.current,
);
} else if (e.key === 'ArrowDown') {
e.preventDefault();
e.stopPropagation();
this.props.onFocusNextBlock(
this.props.block,
this.editable.current,
);
}
}}
/>
<SidebarPortal selected={selected}>
<HeadingSidebar
{...this.props}
data={data}
block={block}
onChangeBlock={onChangeBlock}
/>
</SidebarPortal>
</div>
</MaybeWrap>
)}
</>
);
};
}
Expand Down

0 comments on commit 93c582f

Please sign in to comment.