Skip to content

Commit

Permalink
improve the quality
Browse files Browse the repository at this point in the history
  • Loading branch information
dobri1408 committed Apr 1, 2024
1 parent 2c31ecd commit d6bf215
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
25 changes: 20 additions & 5 deletions src/components/Blocks/Hero/Edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { BlocksForm } from '@plone/volto/components';
import EditBlockWrapper from './EditBlockWrapper';
import { v4 as uuid } from 'uuid';

import { emptyBlocksForm } from '@plone/volto/helpers';
import { isEmpty } from 'lodash';
import {
BlockDataForm,
Expand Down Expand Up @@ -51,6 +50,7 @@ export default function Edit(props) {
metadata = null,
} = props;
const { copyright, copyrightIcon, copyrightPosition } = data;

const copyrightPrefix = config.blocks.blocksConfig.hero.copyrightPrefix || '';
const schema = React.useMemo(() => {
if (isFunction(HeroBlockSchema)) {
Expand All @@ -61,6 +61,7 @@ export default function Edit(props) {

const blockState = {};
const data_blocks = data?.data?.blocks;

if (data?.text || isEmpty(data_blocks)) {
let dataWithoutText = { ...data };
if (dataWithoutText) delete dataWithoutText.text;
Expand All @@ -72,28 +73,42 @@ export default function Edit(props) {
blocks: {
[id]: {
'@type': 'slate',
value: data.text,
value: { ...data.text },
plaintext: data.text?.[0].children?.[0].text,
},
},
blocks_layout: { items: [id] },
}
: emptyBlocksForm(),
: {
blocks: {
[id]: {
'@type': 'slate',
value: [{ type: 'h2', children: [{ text: '' }] }],
plaintext: '',
},
},
blocks_layout: { items: [id] },
},
});
}

return (
<>
<BodyClass className="with-hero-block" />

<Hero {...data}>
<Hero
{...data}
onClick={(e) => {
if (e.target.className.includes('hero')) setSelectedBlock(null);
}}
>
<Hero.Text {...data}>
<BlocksForm
metadata={properties || metadata}
properties={data.data || {}}
manage={false}
allowedBlocks={'slate'}
selectedBlock={selected ? selectedBlock : null}
selectedBlock={selectedBlock}
title={data.placeholder}
onSelectBlock={(s, e) => {
setSelectedBlock(s);
Expand Down
7 changes: 6 additions & 1 deletion src/components/Blocks/Hero/Hero.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function Hero({
spaced = false,
inverted = true,
styles,
onClick = () => {},
...props
}) {
const image = getFieldURL(props.image);
Expand All @@ -35,6 +36,7 @@ function Hero({

const bgImgRef = React.useRef();
const onScreen = useFirstVisited(bgImgRef);

return (
<div
className={cx(
Expand All @@ -49,6 +51,8 @@ function Hero({
'full-height': fullHeight,
},
)}
role="banner"
onClick={onClick}
>
<div
className={cx(
Expand Down Expand Up @@ -101,9 +105,10 @@ Hero.Text = ({ children, quoted, styles }) => {
'hero-block-text',
`color-fg-${textVariant}`,
`text-${textAlign}`,
quoted ? 'quoted-wrapper' : '',
)}
>
<div className={quoted ? 'quoted-wrapper' : ''}>{children}</div>
{children}
</div>
);
};
Expand Down
6 changes: 0 additions & 6 deletions src/components/Blocks/Hero/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const HeroBlockSchema = () => {
'quoted',
'spaced',
'inverted',
'isMultiline',
'buttonLabel',
'buttonLink',
'overlay',
Expand Down Expand Up @@ -65,11 +64,6 @@ export const HeroBlockSchema = () => {
type: 'boolean',
defaultValue: true,
},
isMultiline: {
title: 'Multiline',
type: 'boolean',
defaultValue: false,
},
buttonLabel: {
title: 'Button label',
widget: 'textarea',
Expand Down
33 changes: 33 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { serializeNodes } from '@plone/volto-slate/editor/render';
import isArray from 'lodash/isArray';
import isObject from 'lodash/isObject';
import isString from 'lodash/isString';
import { v4 as uuid } from 'uuid';
import { isInternalURL, flattenToAppURL } from '@plone/volto/helpers';

export const getFieldURL = (data) => {
Expand All @@ -26,6 +27,38 @@ const createEmptyHeader = () => {
};
};

export const addEmptyHeaderSlateBlockToData = (
data,
index,
setSelectedBlock,
) => {
console.log(index);
const id = uuid();
setSelectedBlock(id);
return {
...data,
blocks: {
...data.blocks,
[id]: {
'@type': 'slate',
value: [{ type: 'h2', children: [{ text: '' }] }],
plaintext: '',
},
},
blocks_layout: {
...data.blocks_layout,
items: [
...data.blocks_layout.items.slice(0, index),
id,
...data.blocks_layout.items.slice(
index,
data.blocks_layout.items.length,
),
],
},
};
};

export const createSlateHeader = (text) => {
return isArray(text) ? text : [createEmptyHeader()];
};
Expand Down

0 comments on commit d6bf215

Please sign in to comment.