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

Refactor Blocks/LeadImage/Edit component #4959

Merged
merged 20 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
515bf3e
Refactor LeadIMage/Edit.jsx
Tishasoumya-02 Jul 7, 2023
205eed4
/news
Tishasoumya-02 Jul 8, 2023
b564f7e
Merge branch 'master' into block-leadimage-refactor
Tishasoumya-02 Jul 12, 2023
d2df452
Resolve conflict
Tishasoumya-02 Jul 31, 2023
ffb1f90
Merge branch 'block-leadimage-refactor' of https://github.com/plone/v…
Tishasoumya-02 Jul 31, 2023
da30467
storybook
Tishasoumya-02 Aug 2, 2023
3bd2775
storybook
Tishasoumya-02 Aug 2, 2023
33877f0
Merge branch 'master' into block-leadimage-refactor
nileshgulia1 Aug 7, 2023
67c8109
LeadImage story
Tishasoumya-02 Aug 8, 2023
2716399
Merge branch 'block-leadimage-refactor' of https://github.com/plone/v…
Tishasoumya-02 Aug 8, 2023
9a082b3
chore: prettier fix
nileshgulia1 Aug 8, 2023
2fc3ead
Added actions to components
Tishasoumya-02 Aug 9, 2023
8b3d095
Merge branch 'block-leadimage-refactor' of https://github.com/plone/v…
Tishasoumya-02 Aug 9, 2023
9345dbe
Merge branch 'master' into block-leadimage-refactor
nileshgulia1 Aug 28, 2023
201cfe5
improve code
Tishasoumya-02 Aug 30, 2023
68c1463
Merge branch 'block-leadimage-refactor' of https://github.com/plone/v…
Tishasoumya-02 Aug 30, 2023
31837bd
Merge branch 'main' into block-leadimage-refactor
Tishasoumya-02 Aug 12, 2024
3f7b054
Remove storybook tests and replace /news
Tishasoumya-02 Aug 12, 2024
ee12f4c
remove react memo, call the function immediately after defining it an…
Tishasoumya-02 Aug 13, 2024
0e2f35f
Merge branch 'main' into block-leadimage-refactor
Tishasoumya-02 Aug 13, 2024
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
1 change: 1 addition & 0 deletions news/4959.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactor Blocks/LeadImage/Edit component -@Tishasoumya-02
163 changes: 57 additions & 106 deletions src/components/manage/Blocks/LeadImage/Edit.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
/**
* Edit image block.
* @module components/manage/Blocks/Image/Edit
*/

import React, { Component } from 'react';
import React, { useMemo } from 'react';
import PropTypes from 'prop-types';
import { compose } from 'redux';
import { defineMessages, injectIntl } from 'react-intl';
import { defineMessages, useIntl } from 'react-intl';
import cx from 'classnames';
import { Message } from 'semantic-ui-react';
import { isEqual } from 'lodash';

import { LeadImageSidebar, SidebarPortal } from '@plone/volto/components';
import { flattenToAppURL } from '@plone/volto/helpers';

import imageBlockSVG from '@plone/volto/components/manage/Blocks/Image/block-image.svg';

const messages = defineMessages({
Expand All @@ -23,102 +15,61 @@ const messages = defineMessages({
},
});

/**
* Edit image block class.
* @class Edit
* @extends Component
*/
class Edit extends Component {
/**
* Property types.
* @property {Object} propTypes Property types.
* @static
*/
static propTypes = {
properties: PropTypes.objectOf(PropTypes.any).isRequired,
selected: PropTypes.bool.isRequired,
block: PropTypes.string.isRequired,
index: PropTypes.number.isRequired,
data: PropTypes.objectOf(PropTypes.any).isRequired,
pathname: PropTypes.string.isRequired,
onChangeBlock: PropTypes.func.isRequired,
openObjectBrowser: PropTypes.func.isRequired,
};

/**
* Align block handler
* @method onAlignBlock
* @param {string} align Alignment option
* @returns {undefined}
*/
onAlignBlock(align) {
this.props.onChangeBlock(this.props.block, {
...this.props.data,
align,
});
}
const Edit = React.memo((props) => {
const intl = useIntl();
const { data, properties, selected } = props;

/**
* @param {*} nextProps
* @returns {boolean}
* @memberof Edit
*/
shouldComponentUpdate(nextProps) {
return (
this.props.selected ||
nextProps.selected ||
!isEqual(this.props.data, nextProps.data)
);
}
const placeholder = useMemo(
Tishasoumya-02 marked this conversation as resolved.
Show resolved Hide resolved
() =>
data.placeholder ||
intl.formatMessage(messages.ImageBlockInputPlaceholder),
[data, intl],
);

node = React.createRef();

/**
* Render method.
* @method render
* @returns {string} Markup for the component.
*/
render() {
const { data, properties } = this.props;
const placeholder =
this.props.data.placeholder ||
this.props.intl.formatMessage(messages.ImageBlockInputPlaceholder);

return (
<div
className={cx(
'block image align',
{
center: !Boolean(data.align),
},
data.align,
)}
>
{!properties.image && (
<Message>
<center>
<img src={imageBlockSVG} alt="" />
<div className="message-text">{placeholder}</div>
</center>
</Message>
)}
{properties.image && (
<img
className={cx({ 'full-width': data.align === 'full' })}
src={
properties.image.data
? `data:${properties.image['content-type']};base64,${properties.image.data}`
: flattenToAppURL(properties.image.download)
}
alt={data.image_caption || ''}
/>
)}
<SidebarPortal selected={this.props.selected}>
<LeadImageSidebar {...this.props} />
</SidebarPortal>
</div>
);
}
}
return (
<div
className={cx(
'block image align',
{
center: !Boolean(data.align),
},
data.align,
)}
>
{!properties.image && (
<Message>
<center>
<img src={imageBlockSVG} alt="" />
<div className="message-text">{placeholder}</div>
</center>
</Message>
)}
{properties.image && (
<img
className={cx({ 'full-width': data.align === 'full' })}
src={
properties.image.data
? `data:${properties.image['content-type']};base64,${properties.image.data}`
: flattenToAppURL(properties.image.download)
}
alt={data.image_caption || ''}
/>
)}
<SidebarPortal selected={selected}>
<LeadImageSidebar {...props} />
</SidebarPortal>
</div>
);
});
Edit.propTypes = {
properties: PropTypes.objectOf(PropTypes.any).isRequired,
selected: PropTypes.bool.isRequired,
block: PropTypes.string.isRequired,
index: PropTypes.number.isRequired,
data: PropTypes.objectOf(PropTypes.any).isRequired,
pathname: PropTypes.string.isRequired,
onChangeBlock: PropTypes.func.isRequired,
openObjectBrowser: PropTypes.func.isRequired,
};

export default compose(injectIntl)(Edit);
export default Edit;