Simplify dnd context

This commit is contained in:
soupette 2019-10-29 11:19:20 +01:00 committed by Alexandre Bodin
parent 14222973f4
commit 410266c311
11 changed files with 41 additions and 59 deletions

View File

@ -1,5 +1,6 @@
import React from 'react'; import React from 'react';
import { useDragLayer } from 'react-dnd'; import { useDragLayer } from 'react-dnd';
import { LayoutDndProvider } from '../../contexts/LayoutDnd';
import ItemTypes from '../../utils/ItemTypes'; import ItemTypes from '../../utils/ItemTypes';
@ -87,14 +88,16 @@ const CustomDragLayer = () => {
} }
return ( return (
<div style={layerStyles}> <LayoutDndProvider>
<div <div style={layerStyles}>
style={getItemStyles(initialOffset, currentOffset, mouseOffset)} <div
className="col-md-2" style={getItemStyles(initialOffset, currentOffset, mouseOffset)}
> className="col-md-2"
{renderItem()} >
{renderItem()}
</div>
</div> </div>
</div> </LayoutDndProvider>
); );
}; };

View File

@ -5,6 +5,7 @@ import { isEmpty } from 'lodash';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { Grab, GrabLarge, Pencil, Remove } from '@buffetjs/icons'; import { Grab, GrabLarge, Pencil, Remove } from '@buffetjs/icons';
import pluginId from '../../pluginId'; import pluginId from '../../pluginId';
import { useLayoutDnd } from '../../contexts/LayoutDnd';
import GrabWrapper from './GrabWrapper'; import GrabWrapper from './GrabWrapper';
import Link from './Link'; import Link from './Link';
import NameWrapper from './NameWrapper'; import NameWrapper from './NameWrapper';
@ -20,7 +21,6 @@ const DraggedField = forwardRef(
goTo, goTo,
componentUid, componentUid,
isDragging, isDragging,
isDraggingSibling,
isHidden, isHidden,
isOverDynamicZone, isOverDynamicZone,
isSub, isSub,
@ -35,6 +35,7 @@ const DraggedField = forwardRef(
}, },
ref ref
) => { ) => {
const { isDraggingSibling } = useLayoutDnd();
const [isOverRemove, setIsOverRemove] = useState(false); const [isOverRemove, setIsOverRemove] = useState(false);
const [isOverEditBlock, setIsOverEditBlock] = useState(false); const [isOverEditBlock, setIsOverEditBlock] = useState(false);
const opacity = isDragging ? 0.2 : 1; const opacity = isDragging ? 0.2 : 1;
@ -160,7 +161,6 @@ DraggedField.defaultProps = {
goTo: () => {}, goTo: () => {},
componentUid: null, componentUid: null,
isDragging: false, isDragging: false,
isDraggingSibling: false,
isHidden: false, isHidden: false,
isOverDynamicZone: false, isOverDynamicZone: false,
isSub: false, isSub: false,
@ -179,7 +179,6 @@ DraggedField.propTypes = {
goTo: PropTypes.func, goTo: PropTypes.func,
componentUid: PropTypes.string, componentUid: PropTypes.string,
isDragging: PropTypes.bool, isDragging: PropTypes.bool,
isDraggingSibling: PropTypes.bool,
isHidden: PropTypes.bool, isHidden: PropTypes.bool,
isOverDynamicZone: PropTypes.bool, isOverDynamicZone: PropTypes.bool,
isSub: PropTypes.bool, isSub: PropTypes.bool,

View File

@ -17,7 +17,6 @@ const DraggedFieldWithPreview = forwardRef(
componentLayouts, componentLayouts,
dynamicZoneComponents, dynamicZoneComponents,
isDragging, isDragging,
isDraggingSibling,
label, label,
name, name,
onClickEdit, onClickEdit,
@ -83,7 +82,6 @@ const DraggedFieldWithPreview = forwardRef(
goTo={goTo} goTo={goTo}
componentUid={componentUid} componentUid={componentUid}
isHidden={isHidden} isHidden={isHidden}
isDraggingSibling={isDraggingSibling}
isOverDynamicZone={isOverDynamicZone} isOverDynamicZone={isOverDynamicZone}
label={label} label={label}
name={name} name={name}
@ -173,7 +171,6 @@ DraggedFieldWithPreview.defaultProps = {
componentUid: null, componentUid: null,
dynamicZoneComponents: [], dynamicZoneComponents: [],
isDragging: false, isDragging: false,
isDraggingSibling: false,
label: '', label: '',
onClickEdit: () => {}, onClickEdit: () => {},
onClickRemove: () => {}, onClickRemove: () => {},
@ -191,7 +188,6 @@ DraggedFieldWithPreview.propTypes = {
componentUid: PropTypes.string, componentUid: PropTypes.string,
dynamicZoneComponents: PropTypes.array, dynamicZoneComponents: PropTypes.array,
isDragging: PropTypes.bool, isDragging: PropTypes.bool,
isDraggingSibling: PropTypes.bool,
label: PropTypes.string, label: PropTypes.string,
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
onClickEdit: PropTypes.func, onClickEdit: PropTypes.func,

View File

@ -12,14 +12,12 @@ import ItemTypes from '../../utils/ItemTypes';
const Item = ({ const Item = ({
componentUid, componentUid,
dynamicZoneComponents, dynamicZoneComponents,
isDraggingSibling,
itemIndex, itemIndex,
moveItem, moveItem,
moveRow, moveRow,
name, name,
removeField, removeField,
rowIndex, rowIndex,
setIsDraggingSibling,
size, size,
type, type,
}) => { }) => {
@ -29,6 +27,7 @@ const Item = ({
metadatas, metadatas,
setEditFieldToSelect, setEditFieldToSelect,
selectedItemName, selectedItemName,
setIsDraggingSibling,
} = useLayoutDnd(); } = useLayoutDnd();
const dragRef = useRef(null); const dragRef = useRef(null);
const dropRef = useRef(null); const dropRef = useRef(null);
@ -214,7 +213,6 @@ const Item = ({
componentLayouts={componentLayouts} componentLayouts={componentLayouts}
dynamicZoneComponents={dynamicZoneComponents} dynamicZoneComponents={dynamicZoneComponents}
isDragging={isDragging} isDragging={isDragging}
isDraggingSibling={isDraggingSibling}
label={get(metadatas, [name, 'edit', 'label'], '')} label={get(metadatas, [name, 'edit', 'label'], '')}
name={name} name={name}
onClickEdit={setEditFieldToSelect} onClickEdit={setEditFieldToSelect}
@ -235,22 +233,18 @@ const Item = ({
Item.defaultProps = { Item.defaultProps = {
componentUid: '', componentUid: '',
dynamicZoneComponents: [], dynamicZoneComponents: [],
isDraggingSibling: false,
setIsDraggingSibling: () => {},
type: 'string', type: 'string',
}; };
Item.propTypes = { Item.propTypes = {
componentUid: PropTypes.string, componentUid: PropTypes.string,
dynamicZoneComponents: PropTypes.array, dynamicZoneComponents: PropTypes.array,
isDraggingSibling: PropTypes.bool,
itemIndex: PropTypes.number.isRequired, itemIndex: PropTypes.number.isRequired,
moveItem: PropTypes.func.isRequired, moveItem: PropTypes.func.isRequired,
moveRow: PropTypes.func.isRequired, moveRow: PropTypes.func.isRequired,
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
removeField: PropTypes.func.isRequired, removeField: PropTypes.func.isRequired,
rowIndex: PropTypes.number.isRequired, rowIndex: PropTypes.number.isRequired,
setIsDraggingSibling: PropTypes.func,
size: PropTypes.number.isRequired, size: PropTypes.number.isRequired,
type: PropTypes.string, type: PropTypes.string,
}; };

View File

@ -1,4 +1,4 @@
import React, { memo, useCallback, useState } from 'react'; import React, { memo, useCallback } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { get } from 'lodash'; import { get } from 'lodash';
@ -19,7 +19,7 @@ const FieldsReorder = ({ className }) => {
onAddData, onAddData,
removeField, removeField,
} = useLayoutDnd(); } = useLayoutDnd();
const [isDraggingSibling, setIsDraggingSibling] = useState(false);
const getComponent = useCallback( const getComponent = useCallback(
attributeName => { attributeName => {
return get(attributes, [attributeName, 'component'], ''); return get(attributes, [attributeName, 'component'], '');
@ -64,7 +64,6 @@ const FieldsReorder = ({ className }) => {
<Item <Item
componentUid={getComponent(name)} componentUid={getComponent(name)}
dynamicZoneComponents={getDynamicZoneComponents(name)} dynamicZoneComponents={getDynamicZoneComponents(name)}
isDraggingSibling={isDraggingSibling}
itemIndex={index} itemIndex={index}
key={name} key={name}
moveRow={moveRow} moveRow={moveRow}
@ -72,7 +71,6 @@ const FieldsReorder = ({ className }) => {
name={name} name={name}
removeField={removeField} removeField={removeField}
rowIndex={rowIndex} rowIndex={rowIndex}
setIsDraggingSibling={setIsDraggingSibling}
size={size} size={size}
type={getType(name)} type={getType(name)}
/> />

View File

@ -9,19 +9,13 @@ import DraggedFieldWithPreview from '../DraggedFieldWithPreview';
import ItemTypes from '../../utils/ItemTypes'; import ItemTypes from '../../utils/ItemTypes';
const Item = ({ const Item = ({ index, move, name, removeItem }) => {
index,
isDraggingSibling,
move,
name,
removeItem,
setIsDraggingSibling,
}) => {
const { const {
goTo, goTo,
metadatas, metadatas,
selectedItemName, selectedItemName,
setEditFieldToSelect, setEditFieldToSelect,
setIsDraggingSibling,
} = useLayoutDnd(); } = useLayoutDnd();
const dragRef = useRef(null); const dragRef = useRef(null);
const dropRef = useRef(null); const dropRef = useRef(null);
@ -98,7 +92,6 @@ const Item = ({
return ( return (
<DraggedFieldWithPreview <DraggedFieldWithPreview
isDragging={isDragging} isDragging={isDragging}
isDraggingSibling={isDraggingSibling}
label={get(metadatas, [name, 'edit', 'label'], '')} label={get(metadatas, [name, 'edit', 'label'], '')}
name={name} name={name}
onClickEdit={() => setEditFieldToSelect(name)} onClickEdit={() => setEditFieldToSelect(name)}
@ -118,18 +111,14 @@ const Item = ({
}; };
Item.defaultProps = { Item.defaultProps = {
isDraggingSibling: false,
move: () => {}, move: () => {},
setIsDraggingSibling: () => {},
}; };
Item.propTypes = { Item.propTypes = {
index: PropTypes.number.isRequired, index: PropTypes.number.isRequired,
isDraggingSibling: PropTypes.bool,
move: PropTypes.func, move: PropTypes.func,
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
removeItem: PropTypes.func.isRequired, removeItem: PropTypes.func.isRequired,
setIsDraggingSibling: PropTypes.func,
}; };
export default Item; export default Item;

View File

@ -1,4 +1,4 @@
import React, { memo, useState } from 'react'; import React, { memo } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { useLayoutDnd } from '../../contexts/LayoutDnd'; import { useLayoutDnd } from '../../contexts/LayoutDnd';
@ -8,7 +8,6 @@ import Item from './Item';
const SortableList = ({ addItem, buttonData, moveItem, removeItem }) => { const SortableList = ({ addItem, buttonData, moveItem, removeItem }) => {
const { relationsLayout } = useLayoutDnd(); const { relationsLayout } = useLayoutDnd();
const [isDraggingSibling, setIsDraggingSibling] = useState(false);
return ( return (
<div className="col-4"> <div className="col-4">
@ -25,12 +24,10 @@ const SortableList = ({ addItem, buttonData, moveItem, removeItem }) => {
return ( return (
<Item <Item
index={index} index={index}
isDraggingSibling={isDraggingSibling}
key={relationName} key={relationName}
move={moveItem} move={moveItem}
name={relationName} name={relationName}
removeItem={removeItem} removeItem={removeItem}
setIsDraggingSibling={setIsDraggingSibling}
/> />
); );
})} })}

View File

@ -43,6 +43,7 @@ const EditSettingsView = ({
const { emitEvent } = useGlobalContext(); const { emitEvent } = useGlobalContext();
const [reducerState, dispatch] = useReducer(reducer, initialState); const [reducerState, dispatch] = useReducer(reducer, initialState);
const [isModalFormOpen, setIsModalFormOpen] = useState(false); const [isModalFormOpen, setIsModalFormOpen] = useState(false);
const [isDraggingSibling, setIsDraggingSibling] = useState(false);
const fieldsReorderClassName = type === 'content-types' ? 'col-8' : 'col-12'; const fieldsReorderClassName = type === 'content-types' ? 'col-8' : 'col-12';
const source = getQueryParameters(search, 'source'); const source = getQueryParameters(search, 'source');
@ -132,8 +133,6 @@ const EditSettingsView = ({
} }
); );
console.log({ data });
dispatch({ dispatch({
type: 'GET_DATA_SUCCEEDED', type: 'GET_DATA_SUCCEEDED',
data: data.contentType || data.component, data: data.contentType || data.component,
@ -309,8 +308,9 @@ const EditSettingsView = ({
<LayoutDndProvider <LayoutDndProvider
attributes={getAttributes} attributes={getAttributes}
buttonData={getEditRemainingFields()} buttonData={getEditRemainingFields()}
goTo={push}
componentLayouts={componentLayouts} componentLayouts={componentLayouts}
goTo={push}
isDraggingSibling={isDraggingSibling}
layout={getEditLayout()} layout={getEditLayout()}
metadatas={get(modifiedData, ['metadatas'], {})} metadatas={get(modifiedData, ['metadatas'], {})}
moveItem={moveItem} moveItem={moveItem}
@ -336,6 +336,7 @@ const EditSettingsView = ({
}); });
toggleModalForm(); toggleModalForm();
}} }}
setIsDraggingSibling={setIsDraggingSibling}
selectedItemName={metaToEdit} selectedItemName={metaToEdit}
> >
<SettingsViewWrapper <SettingsViewWrapper

View File

@ -4,20 +4,21 @@ import { useDrag, useDrop } from 'react-dnd';
import { getEmptyImage } from 'react-dnd-html5-backend'; import { getEmptyImage } from 'react-dnd-html5-backend';
import ItemTypes from '../../utils/ItemTypes'; import ItemTypes from '../../utils/ItemTypes';
import DraggedField from '../../components/DraggedField'; import DraggedField from '../../components/DraggedField';
import { useLayoutDnd } from '../../contexts/LayoutDnd';
const Label = ({ const Label = ({
count, count,
index, index,
isDraggingSibling,
label, label,
move, move,
name, name,
onClick, onClick,
onRemove, onRemove,
selectedItem, selectedItem,
setIsDraggingSibling,
}) => { }) => {
const ref = useRef(null); const ref = useRef(null);
const { setIsDraggingSibling } = useLayoutDnd();
const [, drop] = useDrop({ const [, drop] = useDrop({
accept: ItemTypes.FIELD, accept: ItemTypes.FIELD,
hover(item) { hover(item) {
@ -60,7 +61,6 @@ const Label = ({
count={count} count={count}
ref={ref} ref={ref}
isDragging={isDragging} isDragging={isDragging}
isDraggingSibling={isDraggingSibling}
label={label} label={label}
name={name} name={name}
onClick={onClick} onClick={onClick}
@ -72,24 +72,20 @@ const Label = ({
Label.defaultProps = { Label.defaultProps = {
index: 0, index: 0,
isDraggingSibling: false,
label: '', label: '',
move: () => {}, move: () => {},
selectedItem: '', selectedItem: '',
setIsDraggingSibling: () => {},
}; };
Label.propTypes = { Label.propTypes = {
count: PropTypes.number.isRequired, count: PropTypes.number.isRequired,
index: PropTypes.number, index: PropTypes.number,
isDraggingSibling: PropTypes.bool,
label: PropTypes.string, label: PropTypes.string,
move: PropTypes.func.isRequired, move: PropTypes.func.isRequired,
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
onClick: PropTypes.func.isRequired, onClick: PropTypes.func.isRequired,
onRemove: PropTypes.func.isRequired, onRemove: PropTypes.func.isRequired,
selectedItem: PropTypes.string, selectedItem: PropTypes.string,
setIsDraggingSibling: PropTypes.func,
}; };
export default Label; export default Label;

View File

@ -13,7 +13,7 @@ import { useDrop } from 'react-dnd';
import { DropdownItem } from 'reactstrap'; import { DropdownItem } from 'reactstrap';
import { Inputs as Input } from '@buffetjs/custom'; import { Inputs as Input } from '@buffetjs/custom';
import pluginId from '../../pluginId'; import pluginId from '../../pluginId';
import { LayoutDndProvider } from '../../contexts/LayoutDnd';
import ItemTypes from '../../utils/ItemTypes'; import ItemTypes from '../../utils/ItemTypes';
import getRequestUrl from '../../utils/getRequestUrl'; import getRequestUrl from '../../utils/getRequestUrl';
import PopupForm from '../../components/PopupForm'; import PopupForm from '../../components/PopupForm';
@ -207,7 +207,10 @@ const ListSettingsView = ({ deleteLayout, location: { search }, slug }) => {
); );
return ( return (
<> <LayoutDndProvider
isDraggingSibling={isDraggingSibling}
setIsDraggingSibling={setIsDraggingSibling}
>
<SettingsViewWrapper <SettingsViewWrapper
getListDisplayedFields={getListDisplayedFields} getListDisplayedFields={getListDisplayedFields}
inputs={forms} inputs={forms}
@ -321,7 +324,7 @@ const ListSettingsView = ({ deleteLayout, location: { search }, slug }) => {
subHeaderContent={labelToEdit} subHeaderContent={labelToEdit}
type={get(getAttributes, [labelToEdit, 'type'], 'text')} type={get(getAttributes, [labelToEdit, 'type'], 'text')}
/> />
</> </LayoutDndProvider>
); );
}; };

View File

@ -51,10 +51,16 @@ LayoutDndProvider.defaultProps = {
buttonData: [], buttonData: [],
goTo: () => {}, goTo: () => {},
layout: [], layout: [],
isDraggingSibling: true,
metadatas: {}, metadatas: {},
moveItem: () => {},
moveRow: () => {},
onAddData: () => {}, onAddData: () => {},
relationsLayout: [], relationsLayout: [],
removeField: () => {},
selectedItemName: null,
setEditFieldToSelect: () => {}, setEditFieldToSelect: () => {},
setIsDraggingSibling: () => {},
}; };
LayoutDndProvider.propTypes = { LayoutDndProvider.propTypes = {
@ -64,11 +70,11 @@ LayoutDndProvider.propTypes = {
goTo: PropTypes.func, goTo: PropTypes.func,
layout: PropTypes.array, layout: PropTypes.array,
metadatas: PropTypes.object, metadatas: PropTypes.object,
moveItem: PropTypes.func.isRequired, moveItem: PropTypes.func,
moveRow: PropTypes.func.isRequired, moveRow: PropTypes.func,
onAddData: PropTypes.func, onAddData: PropTypes.func,
relationsLayout: PropTypes.array, relationsLayout: PropTypes.array,
removeField: PropTypes.func.isRequired, removeField: PropTypes.func,
selectedItemName: PropTypes.string.isRequired, selectedItemName: PropTypes.string,
setEditFieldToSelect: PropTypes.func, setEditFieldToSelect: PropTypes.func,
}; };