205 lines
6.2 KiB
JavaScript
Raw Normal View History

2019-10-22 15:21:01 +02:00
import React, { forwardRef, useState } from 'react';
2019-10-24 15:28:47 +02:00
import { get } from 'lodash';
2019-10-18 15:27:45 +02:00
import PropTypes from 'prop-types';
import Carret from './Carret';
import DraggedField from '../DraggedField';
2019-10-28 22:30:45 +01:00
import DynamicZoneWrapper from './DynamicZoneWrapper';
2019-10-22 15:21:01 +02:00
import PreviewCarret from '../PreviewCarret';
2019-10-18 15:27:45 +02:00
import Wrapper from './Wrapper';
2019-10-28 22:30:45 +01:00
import DynamicComponent from './DynamicComponent';
2019-10-18 15:27:45 +02:00
const DraggedFieldWithPreview = forwardRef(
2019-10-22 15:21:01 +02:00
(
2019-10-22 17:24:11 +02:00
{
2019-10-23 11:36:46 +02:00
goTo,
2019-10-28 11:08:26 +01:00
componentUid,
componentLayouts,
2019-10-28 22:30:45 +01:00
dynamicZoneComponents,
2019-10-23 10:53:49 +02:00
isDragging,
2019-10-24 15:45:50 +02:00
label,
2019-10-22 17:24:11 +02:00
name,
onClickEdit,
onClickRemove,
selectedItem,
showLeftCarret,
showRightCarret,
size,
2019-10-23 11:11:30 +02:00
style,
2019-10-22 17:24:11 +02:00
type,
},
2019-10-22 15:21:01 +02:00
refs
) => {
2019-10-18 15:27:45 +02:00
const isHidden = name === '_TEMP_';
2019-10-22 15:21:01 +02:00
const [dragStart, setDragStart] = useState(false);
2019-10-28 22:30:45 +01:00
const [isOverDynamicZone, setIsOverDynamicZone] = useState(false);
2019-10-23 10:53:49 +02:00
const opacity = isDragging ? 0.2 : 1;
2019-10-22 15:21:01 +02:00
const isFullSize = size === 12;
const display = isFullSize && dragStart ? 'none' : '';
const width = isFullSize && dragStart ? 0 : '100%';
2019-10-28 11:08:26 +01:00
const higherFields = [
'json',
'text',
'file',
'media',
'component',
'richtext',
2019-10-28 22:30:45 +01:00
'dynamiczone',
2019-10-28 11:08:26 +01:00
];
2019-10-24 15:28:47 +02:00
const withLongerHeight = higherFields.includes(type) && !dragStart;
2019-10-28 11:08:26 +01:00
const componentData = get(componentLayouts, [componentUid], {});
const componentLayout = get(componentData, ['layouts', 'edit'], []);
2019-10-24 15:28:47 +02:00
const getWrapperWitdh = colNum => `${(1 / 12) * colNum * 100}%`;
2019-10-18 15:27:45 +02:00
return (
2019-10-22 15:21:01 +02:00
<div
2019-10-24 15:28:47 +02:00
style={{ width: getWrapperWitdh(size) }}
2019-10-22 15:21:01 +02:00
onDrag={() => {
if (isFullSize && !dragStart) {
setDragStart(true);
}
}}
onDragEnd={() => {
if (isFullSize) {
setDragStart(false);
}
}}
>
2019-10-23 11:11:30 +02:00
<Wrapper
ref={refs.dropRef}
withLongerHeight={withLongerHeight}
style={style}
>
2019-10-22 15:21:01 +02:00
{dragStart && isFullSize && (
<PreviewCarret style={{ marginRight: '-10px' }} />
)}
<>
2019-10-24 15:28:47 +02:00
{showLeftCarret && <Carret />}
2019-10-18 15:27:45 +02:00
2019-10-23 10:53:49 +02:00
<div className="sub" style={{ width, opacity }}>
2019-10-22 15:21:01 +02:00
<DraggedField
2019-10-23 11:36:46 +02:00
goTo={goTo}
2019-10-28 11:08:26 +01:00
componentUid={componentUid}
2019-10-22 15:21:01 +02:00
isHidden={isHidden}
2019-10-28 22:30:45 +01:00
isOverDynamicZone={isOverDynamicZone}
2019-10-24 15:45:50 +02:00
label={label}
2019-10-22 15:21:01 +02:00
name={name}
2019-10-22 17:24:11 +02:00
onClick={onClickEdit}
2019-10-22 15:21:01 +02:00
onRemove={onClickRemove}
2019-10-23 11:36:46 +02:00
ref={refs.dragRef}
2019-10-22 17:24:11 +02:00
selectedItem={selectedItem}
2019-10-24 15:28:47 +02:00
style={{ display, marginRight: 0, paddingRight: 0 }}
2019-10-22 17:24:11 +02:00
type={type}
2019-10-22 15:21:01 +02:00
withLongerHeight={withLongerHeight}
2019-10-24 15:28:47 +02:00
>
2019-10-28 11:08:26 +01:00
{type === 'component' &&
componentLayout.map((row, i) => {
2019-10-24 15:28:47 +02:00
const marginBottom =
2019-10-28 11:08:26 +01:00
i === componentLayout.length - 1 ? '29px' : '';
2019-10-24 15:28:47 +02:00
const marginTop = i === 0 ? '5px' : '';
return (
<div
style={{
display: 'flex',
marginBottom,
marginTop,
}}
key={i}
>
{row.map(field => {
const fieldType = get(
2019-10-28 11:08:26 +01:00
componentData,
2019-10-24 15:28:47 +02:00
['schema', 'attributes', field.name, 'type'],
''
);
2019-10-24 15:45:50 +02:00
const label = get(
2019-10-28 11:08:26 +01:00
componentData,
2019-10-24 15:45:50 +02:00
['metadatas', field.name, 'edit', 'label'],
''
);
2019-10-24 15:28:47 +02:00
return (
<div
key={field.name}
style={{
width: getWrapperWitdh(field.size),
marginBottom: '6px',
}}
>
<DraggedField
2019-10-24 15:45:50 +02:00
label={label}
2019-10-24 15:28:47 +02:00
name={field.name}
isSub
withLongerHeight={higherFields.includes(
fieldType
)}
></DraggedField>
</div>
);
})}
</div>
);
})}
2019-10-28 22:30:45 +01:00
{type === 'dynamiczone' && (
<DynamicZoneWrapper>
{dynamicZoneComponents.map(compo => {
return (
<DynamicComponent
key={compo}
componentUid={compo}
setIsOverDynamicZone={setIsOverDynamicZone}
/>
);
})}
</DynamicZoneWrapper>
)}
2019-10-24 15:28:47 +02:00
</DraggedField>
2019-10-22 15:21:01 +02:00
</div>
2019-10-24 15:28:47 +02:00
{showRightCarret && <Carret right />}
2019-10-22 15:21:01 +02:00
</>
2019-10-18 15:27:45 +02:00
</Wrapper>
</div>
);
}
);
2019-10-22 10:18:01 +02:00
DraggedFieldWithPreview.defaultProps = {
2019-10-24 15:28:47 +02:00
goTo: () => {},
2019-10-28 11:08:26 +01:00
componentLayouts: {},
componentUid: null,
2019-10-28 22:30:45 +01:00
dynamicZoneComponents: [],
2019-10-23 10:53:49 +02:00
isDragging: false,
2019-10-24 15:45:50 +02:00
label: '',
2019-10-22 17:24:11 +02:00
onClickEdit: () => {},
2019-10-22 15:21:01 +02:00
onClickRemove: () => {},
2019-10-22 17:24:11 +02:00
selectedItem: '',
2019-10-22 10:18:01 +02:00
showLeftCarret: false,
showRightCarret: false,
size: 1,
2019-10-23 11:11:30 +02:00
style: {},
2019-10-22 15:21:01 +02:00
type: 'string',
2019-10-22 10:18:01 +02:00
};
DraggedFieldWithPreview.propTypes = {
2019-10-23 11:36:46 +02:00
goTo: PropTypes.func,
2019-10-28 11:08:26 +01:00
componentLayouts: PropTypes.object,
componentUid: PropTypes.string,
2019-10-28 22:30:45 +01:00
dynamicZoneComponents: PropTypes.array,
2019-10-23 10:53:49 +02:00
isDragging: PropTypes.bool,
2019-10-24 15:45:50 +02:00
label: PropTypes.string,
2019-10-22 10:18:01 +02:00
name: PropTypes.string.isRequired,
2019-10-22 17:24:11 +02:00
onClickEdit: PropTypes.func,
2019-10-22 15:21:01 +02:00
onClickRemove: PropTypes.func,
2019-10-22 17:24:11 +02:00
selectedItem: PropTypes.string,
2019-10-22 10:18:01 +02:00
showLeftCarret: PropTypes.bool,
showRightCarret: PropTypes.bool,
size: PropTypes.number,
2019-10-23 11:11:30 +02:00
style: PropTypes.object,
2019-10-22 15:21:01 +02:00
type: PropTypes.string,
2019-10-22 10:18:01 +02:00
};
2019-11-26 16:15:59 +01:00
DraggedFieldWithPreview.displayName = 'DraggedFieldWithPreview';
2019-10-18 15:27:45 +02:00
export default DraggedFieldWithPreview;