2019-09-17 20:57:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
2021-02-04 03:12:39 +01:00
|
|
|
* or more contributor license agreements. Licensed under the Elastic License
|
|
|
|
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
|
|
* 2.0.
|
2019-09-17 20:57:53 +02:00
|
|
|
*/
|
|
|
|
|
2020-09-29 19:00:29 +02:00
|
|
|
import './drag_drop.scss';
|
2021-02-01 11:54:16 +01:00
|
|
|
import React, { useContext, useEffect, memo } from 'react';
|
2019-09-17 20:57:53 +02:00
|
|
|
import classNames from 'classnames';
|
2020-11-03 21:18:44 +01:00
|
|
|
import { keys, EuiScreenReaderOnly } from '@elastic/eui';
|
2021-02-05 18:17:03 +01:00
|
|
|
import useShallowCompareEffect from 'react-use/lib/useShallowCompareEffect';
|
2021-02-01 11:54:16 +01:00
|
|
|
import {
|
|
|
|
DragDropIdentifier,
|
2021-02-05 18:17:03 +01:00
|
|
|
DropIdentifier,
|
2021-02-01 11:54:16 +01:00
|
|
|
DragContext,
|
|
|
|
DragContextState,
|
2021-02-05 18:17:03 +01:00
|
|
|
nextValidDropTarget,
|
2021-02-01 11:54:16 +01:00
|
|
|
ReorderContext,
|
|
|
|
ReorderState,
|
2021-02-05 18:17:03 +01:00
|
|
|
DropHandler,
|
2021-02-21 10:02:04 +01:00
|
|
|
announce,
|
2021-02-01 11:54:16 +01:00
|
|
|
} from './providers';
|
2019-10-16 05:01:43 +02:00
|
|
|
import { trackUiEvent } from '../lens_ui_telemetry';
|
2021-02-05 18:17:03 +01:00
|
|
|
import { DropType } from '../types';
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2020-11-03 21:18:44 +01:00
|
|
|
export type DroppableEvent = React.DragEvent<HTMLElement>;
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
/**
|
2020-03-03 17:48:54 +01:00
|
|
|
* The base props to the DragDrop component.
|
2019-09-17 20:57:53 +02:00
|
|
|
*/
|
2020-03-03 17:48:54 +01:00
|
|
|
interface BaseProps {
|
2019-09-17 20:57:53 +02:00
|
|
|
/**
|
|
|
|
* The CSS class(es) for the root element.
|
|
|
|
*/
|
|
|
|
className?: string;
|
2021-02-01 11:54:16 +01:00
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
/**
|
|
|
|
* The event handler that fires when an item
|
|
|
|
* is dropped onto this DragDrop component.
|
|
|
|
*/
|
|
|
|
onDrop?: DropHandler;
|
|
|
|
/**
|
2021-02-01 11:54:16 +01:00
|
|
|
* The value associated with this item.
|
2019-09-17 20:57:53 +02:00
|
|
|
*/
|
2021-02-01 11:54:16 +01:00
|
|
|
value: DragDropIdentifier;
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-01 17:58:23 +02:00
|
|
|
* Optional comparison function to check whether a value is the dragged one
|
2019-09-17 20:57:53 +02:00
|
|
|
*/
|
2020-10-01 17:58:23 +02:00
|
|
|
isValueEqual?: (value1: unknown, value2: unknown) => boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The React element which will be passed the draggable handlers
|
|
|
|
*/
|
|
|
|
children: React.ReactElement;
|
2021-02-01 11:54:16 +01:00
|
|
|
/**
|
|
|
|
* Indicates whether or not this component is draggable.
|
|
|
|
*/
|
|
|
|
draggable?: boolean;
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2020-09-04 16:11:41 +02:00
|
|
|
/**
|
|
|
|
* Additional class names to apply when another element is over the drop target
|
|
|
|
*/
|
2021-02-05 18:17:03 +01:00
|
|
|
getAdditionalClassesOnEnter?: (dropType?: DropType) => string | undefined;
|
|
|
|
/**
|
|
|
|
* Additional class names to apply when another element is droppable for a currently dragged item
|
|
|
|
*/
|
|
|
|
getAdditionalClassesOnDroppable?: (dropType?: DropType) => string | undefined;
|
2020-09-04 16:11:41 +02:00
|
|
|
|
2020-03-03 17:48:54 +01:00
|
|
|
/**
|
|
|
|
* The optional test subject associated with this DOM element.
|
|
|
|
*/
|
2021-02-01 11:54:16 +01:00
|
|
|
dataTestSubj?: string;
|
2020-10-01 17:58:23 +02:00
|
|
|
|
2020-11-03 21:18:44 +01:00
|
|
|
/**
|
|
|
|
* items belonging to the same group that can be reordered
|
|
|
|
*/
|
2021-02-05 18:17:03 +01:00
|
|
|
reorderableGroup?: Array<{ id: string }>;
|
2020-11-03 21:18:44 +01:00
|
|
|
|
2020-10-01 17:58:23 +02:00
|
|
|
/**
|
|
|
|
* Indicates to the user whether the currently dragged item
|
|
|
|
* will be moved or copied
|
|
|
|
*/
|
2021-02-05 18:17:03 +01:00
|
|
|
dragType?: 'copy' | 'move';
|
2020-10-01 17:58:23 +02:00
|
|
|
|
|
|
|
/**
|
2021-02-05 18:17:03 +01:00
|
|
|
* Indicates the type of a drop - when undefined, the currently dragged item
|
|
|
|
* cannot be dropped onto this component.
|
2020-10-01 17:58:23 +02:00
|
|
|
*/
|
2021-02-05 18:17:03 +01:00
|
|
|
dropType?: DropType;
|
2021-02-01 11:54:16 +01:00
|
|
|
/**
|
2021-02-05 18:17:03 +01:00
|
|
|
* Order for keyboard dragging. This takes an array of numbers which will be used to order hierarchically
|
2021-02-01 11:54:16 +01:00
|
|
|
*/
|
2021-02-05 18:17:03 +01:00
|
|
|
order: number[];
|
2020-03-03 17:48:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The props for a draggable instance of that component.
|
|
|
|
*/
|
2021-02-01 11:54:16 +01:00
|
|
|
interface DragInnerProps extends BaseProps {
|
|
|
|
setKeyboardMode: DragContextState['setKeyboardMode'];
|
|
|
|
setDragging: DragContextState['setDragging'];
|
|
|
|
setActiveDropTarget: DragContextState['setActiveDropTarget'];
|
|
|
|
setA11yMessage: DragContextState['setA11yMessage'];
|
2021-02-21 10:02:04 +01:00
|
|
|
activeDraggingProps?: {
|
|
|
|
keyboardMode: DragContextState['keyboardMode'];
|
|
|
|
activeDropTarget: DragContextState['activeDropTarget'];
|
|
|
|
dropTargetsByOrder: DragContextState['dropTargetsByOrder'];
|
|
|
|
};
|
2021-02-01 11:54:16 +01:00
|
|
|
onDragStart?: (
|
|
|
|
target?:
|
|
|
|
| DroppableEvent['currentTarget']
|
|
|
|
| React.KeyboardEvent<HTMLButtonElement>['currentTarget']
|
|
|
|
) => void;
|
|
|
|
onDragEnd?: () => void;
|
|
|
|
extraKeyboardHandler?: (e: React.KeyboardEvent<HTMLButtonElement>) => void;
|
2021-02-05 18:17:03 +01:00
|
|
|
ariaDescribedBy?: string;
|
2020-03-03 17:48:54 +01:00
|
|
|
}
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2020-03-03 17:48:54 +01:00
|
|
|
/**
|
|
|
|
* The props for a non-draggable instance of that component.
|
|
|
|
*/
|
2021-02-10 15:25:47 +01:00
|
|
|
interface DropInnerProps extends BaseProps {
|
|
|
|
dragging: DragContextState['dragging'];
|
2021-02-21 10:02:04 +01:00
|
|
|
keyboardMode: DragContextState['keyboardMode'];
|
2021-02-10 15:25:47 +01:00
|
|
|
setKeyboardMode: DragContextState['setKeyboardMode'];
|
|
|
|
setDragging: DragContextState['setDragging'];
|
|
|
|
setActiveDropTarget: DragContextState['setActiveDropTarget'];
|
|
|
|
setA11yMessage: DragContextState['setA11yMessage'];
|
|
|
|
registerDropTarget: DragContextState['registerDropTarget'];
|
|
|
|
isActiveDropTarget: boolean;
|
2021-02-01 11:54:16 +01:00
|
|
|
isNotDroppable: boolean;
|
|
|
|
}
|
2020-03-03 17:48:54 +01:00
|
|
|
|
2021-02-01 11:54:16 +01:00
|
|
|
const lnsLayerPanelDimensionMargin = 8;
|
2020-11-03 21:18:44 +01:00
|
|
|
|
2021-02-01 11:54:16 +01:00
|
|
|
export const DragDrop = (props: BaseProps) => {
|
|
|
|
const {
|
|
|
|
dragging,
|
|
|
|
setDragging,
|
|
|
|
keyboardMode,
|
2021-02-21 10:02:04 +01:00
|
|
|
registerDropTarget,
|
|
|
|
dropTargetsByOrder,
|
2021-02-01 11:54:16 +01:00
|
|
|
setKeyboardMode,
|
|
|
|
activeDropTarget,
|
|
|
|
setActiveDropTarget,
|
|
|
|
setA11yMessage,
|
|
|
|
} = useContext(DragContext);
|
|
|
|
|
2021-02-05 18:17:03 +01:00
|
|
|
const { value, draggable, dropType, reorderableGroup } = props;
|
2021-02-01 11:54:16 +01:00
|
|
|
const isDragging = !!(draggable && value.id === dragging?.id);
|
|
|
|
|
2021-02-21 10:02:04 +01:00
|
|
|
const activeDraggingProps = isDragging
|
|
|
|
? {
|
|
|
|
keyboardMode,
|
|
|
|
activeDropTarget,
|
|
|
|
dropTargetsByOrder,
|
|
|
|
}
|
|
|
|
: undefined;
|
|
|
|
|
2021-02-10 15:25:47 +01:00
|
|
|
if (draggable && !dropType) {
|
|
|
|
const dragProps = {
|
|
|
|
...props,
|
2021-02-21 10:02:04 +01:00
|
|
|
activeDraggingProps,
|
2021-02-10 15:25:47 +01:00
|
|
|
setKeyboardMode,
|
|
|
|
setDragging,
|
|
|
|
setActiveDropTarget,
|
|
|
|
setA11yMessage,
|
|
|
|
};
|
|
|
|
if (reorderableGroup && reorderableGroup.length > 1) {
|
2021-02-21 10:02:04 +01:00
|
|
|
return <ReorderableDrag {...dragProps} reorderableGroup={reorderableGroup} />;
|
2021-02-10 15:25:47 +01:00
|
|
|
} else {
|
2021-02-21 10:02:04 +01:00
|
|
|
return <DragInner {...dragProps} />;
|
2021-02-10 15:25:47 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-01 11:54:16 +01:00
|
|
|
|
2021-02-21 10:02:04 +01:00
|
|
|
const isActiveDropTarget = Boolean(activeDropTarget?.id === value.id);
|
2021-02-01 11:54:16 +01:00
|
|
|
const dropProps = {
|
|
|
|
...props,
|
2021-02-15 12:48:06 +01:00
|
|
|
keyboardMode,
|
2021-02-01 11:54:16 +01:00
|
|
|
setKeyboardMode,
|
|
|
|
dragging,
|
|
|
|
setDragging,
|
2021-02-10 15:25:47 +01:00
|
|
|
isActiveDropTarget,
|
2021-02-01 11:54:16 +01:00
|
|
|
setActiveDropTarget,
|
2021-02-05 18:17:03 +01:00
|
|
|
registerDropTarget,
|
2021-02-01 11:54:16 +01:00
|
|
|
setA11yMessage,
|
|
|
|
isNotDroppable:
|
|
|
|
// If the configuration has provided a droppable flag, but this particular item is not
|
|
|
|
// droppable, then it should be less prominent. Ignores items that are both
|
|
|
|
// draggable and drop targets
|
2021-02-05 18:17:03 +01:00
|
|
|
!!(!dropType && dragging && value.id !== dragging.id),
|
2021-02-01 11:54:16 +01:00
|
|
|
};
|
|
|
|
if (
|
|
|
|
reorderableGroup &&
|
|
|
|
reorderableGroup.length > 1 &&
|
2021-02-05 18:17:03 +01:00
|
|
|
reorderableGroup?.some((i) => i.id === dragging?.id)
|
2021-02-01 11:54:16 +01:00
|
|
|
) {
|
2021-02-05 18:17:03 +01:00
|
|
|
return <ReorderableDrop {...dropProps} reorderableGroup={reorderableGroup} />;
|
2021-02-01 11:54:16 +01:00
|
|
|
}
|
|
|
|
return <DropInner {...dropProps} />;
|
|
|
|
};
|
|
|
|
|
2021-03-02 11:08:18 +01:00
|
|
|
const removeSelectionBeforeDragging = () => {
|
|
|
|
const selection = window.getSelection();
|
|
|
|
if (selection) {
|
|
|
|
selection.removeAllRanges();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-05 18:17:03 +01:00
|
|
|
const DragInner = memo(function DragInner({
|
2021-02-01 11:54:16 +01:00
|
|
|
dataTestSubj,
|
|
|
|
className,
|
|
|
|
value,
|
|
|
|
children,
|
|
|
|
setDragging,
|
|
|
|
setKeyboardMode,
|
|
|
|
setActiveDropTarget,
|
2021-02-05 18:17:03 +01:00
|
|
|
order,
|
2021-02-21 10:02:04 +01:00
|
|
|
activeDraggingProps,
|
2021-02-01 11:54:16 +01:00
|
|
|
dragType,
|
|
|
|
onDragStart,
|
|
|
|
onDragEnd,
|
|
|
|
extraKeyboardHandler,
|
2021-02-05 18:17:03 +01:00
|
|
|
ariaDescribedBy,
|
|
|
|
setA11yMessage,
|
2021-02-01 11:54:16 +01:00
|
|
|
}: DragInnerProps) {
|
2021-02-21 10:02:04 +01:00
|
|
|
const keyboardMode = activeDraggingProps?.keyboardMode;
|
|
|
|
const activeDropTarget = activeDraggingProps?.activeDropTarget;
|
|
|
|
const dropTargetsByOrder = activeDraggingProps?.dropTargetsByOrder;
|
|
|
|
|
2021-02-15 12:48:06 +01:00
|
|
|
const dragStart = (
|
|
|
|
e: DroppableEvent | React.KeyboardEvent<HTMLButtonElement>,
|
|
|
|
keyboardModeOn?: boolean
|
|
|
|
) => {
|
2021-02-01 11:54:16 +01:00
|
|
|
// Setting stopPropgagation causes Chrome failures, so
|
|
|
|
// we are manually checking if we've already handled this
|
|
|
|
// in a nested child, and doing nothing if so...
|
|
|
|
if (e && 'dataTransfer' in e && e.dataTransfer.getData('text')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only can reach the dragStart method if the element is draggable,
|
|
|
|
// so we know we have DraggableProps if we reach this code.
|
|
|
|
if (e && 'dataTransfer' in e) {
|
2021-02-05 18:17:03 +01:00
|
|
|
e.dataTransfer.setData('text', value.humanData.label);
|
2021-02-01 11:54:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chrome causes issues if you try to render from within a
|
|
|
|
// dragStart event, so we drop a setTimeout to avoid that.
|
|
|
|
|
|
|
|
const currentTarget = e?.currentTarget;
|
2021-02-15 12:48:06 +01:00
|
|
|
|
2021-02-01 11:54:16 +01:00
|
|
|
setTimeout(() => {
|
2021-02-15 12:48:06 +01:00
|
|
|
setDragging({
|
|
|
|
...value,
|
|
|
|
ghost: keyboardModeOn
|
|
|
|
? {
|
|
|
|
children,
|
|
|
|
style: { width: currentTarget.offsetWidth, height: currentTarget.offsetHeight },
|
|
|
|
}
|
|
|
|
: undefined,
|
|
|
|
});
|
2021-02-05 18:17:03 +01:00
|
|
|
setA11yMessage(announce.lifted(value.humanData));
|
2021-02-15 12:48:06 +01:00
|
|
|
if (keyboardModeOn) {
|
|
|
|
setKeyboardMode(true);
|
|
|
|
}
|
2021-02-01 11:54:16 +01:00
|
|
|
if (onDragStart) {
|
|
|
|
onDragStart(currentTarget);
|
2020-10-01 17:58:23 +02:00
|
|
|
}
|
2021-02-01 11:54:16 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const dragEnd = (e?: DroppableEvent) => {
|
|
|
|
e?.stopPropagation();
|
|
|
|
setDragging(undefined);
|
|
|
|
setActiveDropTarget(undefined);
|
|
|
|
setKeyboardMode(false);
|
2021-02-17 16:48:26 +01:00
|
|
|
setA11yMessage(announce.cancelled(value.humanData));
|
2021-02-01 11:54:16 +01:00
|
|
|
if (onDragEnd) {
|
|
|
|
onDragEnd();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const dropToActiveDropTarget = () => {
|
2021-02-21 10:02:04 +01:00
|
|
|
if (activeDropTarget) {
|
2021-02-01 11:54:16 +01:00
|
|
|
trackUiEvent('drop_total');
|
2021-02-21 10:02:04 +01:00
|
|
|
const { dropType, humanData, onDrop: onTargetDrop } = activeDropTarget;
|
2021-02-05 18:17:03 +01:00
|
|
|
setTimeout(() => setA11yMessage(announce.dropped(value.humanData, humanData, dropType)));
|
|
|
|
onTargetDrop(value, dropType);
|
2021-02-01 11:54:16 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-05 18:17:03 +01:00
|
|
|
const setNextTarget = (reversed = false) => {
|
|
|
|
if (!order) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextTarget = nextValidDropTarget(
|
2021-02-21 10:02:04 +01:00
|
|
|
dropTargetsByOrder,
|
2021-02-05 18:17:03 +01:00
|
|
|
activeDropTarget,
|
|
|
|
[order.join(',')],
|
|
|
|
(el) => el?.dropType !== 'reorder',
|
|
|
|
reversed
|
|
|
|
);
|
|
|
|
|
|
|
|
setActiveDropTarget(nextTarget);
|
|
|
|
setA11yMessage(
|
|
|
|
nextTarget
|
|
|
|
? announce.selectedTarget(value.humanData, nextTarget?.humanData, nextTarget?.dropType)
|
|
|
|
: announce.noTarget()
|
|
|
|
);
|
|
|
|
};
|
2021-02-15 12:48:06 +01:00
|
|
|
const shouldShowGhostImageInstead =
|
|
|
|
dragType === 'move' &&
|
|
|
|
keyboardMode &&
|
2021-02-21 10:02:04 +01:00
|
|
|
activeDropTarget &&
|
|
|
|
activeDropTarget.dropType !== 'reorder';
|
2021-02-01 11:54:16 +01:00
|
|
|
return (
|
2021-02-15 12:48:06 +01:00
|
|
|
<div
|
|
|
|
className={classNames(className, {
|
|
|
|
'lnsDragDrop-isHidden-noFocus': shouldShowGhostImageInstead,
|
|
|
|
})}
|
|
|
|
data-test-subj={`lnsDragDrop_draggable-${value.humanData.label}`}
|
|
|
|
>
|
2021-02-05 18:17:03 +01:00
|
|
|
<EuiScreenReaderOnly showOnFocus>
|
|
|
|
<button
|
|
|
|
aria-label={value.humanData.label}
|
|
|
|
aria-describedby={ariaDescribedBy || `lnsDragDrop-keyboardInstructions`}
|
|
|
|
className="lnsDragDrop__keyboardHandler"
|
|
|
|
data-test-subj="lnsDragDrop-keyboardHandler"
|
|
|
|
onBlur={() => {
|
2021-02-21 10:02:04 +01:00
|
|
|
if (activeDraggingProps) {
|
2021-02-01 11:54:16 +01:00
|
|
|
dragEnd();
|
2021-02-05 18:17:03 +01:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
onKeyDown={(e: React.KeyboardEvent<HTMLButtonElement>) => {
|
|
|
|
const { key } = e;
|
|
|
|
if (key === keys.ENTER || key === keys.SPACE) {
|
|
|
|
if (activeDropTarget) {
|
|
|
|
dropToActiveDropTarget();
|
|
|
|
}
|
2021-02-15 12:48:06 +01:00
|
|
|
|
2021-02-21 10:02:04 +01:00
|
|
|
if (activeDraggingProps) {
|
2021-02-01 11:54:16 +01:00
|
|
|
dragEnd();
|
2021-02-05 18:17:03 +01:00
|
|
|
} else {
|
2021-02-15 12:48:06 +01:00
|
|
|
dragStart(e, true);
|
2021-02-01 11:54:16 +01:00
|
|
|
}
|
2021-02-05 18:17:03 +01:00
|
|
|
} else if (key === keys.ESCAPE) {
|
2021-02-21 10:02:04 +01:00
|
|
|
if (activeDraggingProps) {
|
2021-02-15 12:48:06 +01:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2021-02-05 18:17:03 +01:00
|
|
|
dragEnd();
|
2021-02-01 11:54:16 +01:00
|
|
|
}
|
2021-02-05 18:17:03 +01:00
|
|
|
}
|
|
|
|
if (extraKeyboardHandler) {
|
|
|
|
extraKeyboardHandler(e);
|
|
|
|
}
|
|
|
|
if (keyboardMode && (keys.ARROW_LEFT === key || keys.ARROW_RIGHT === key)) {
|
|
|
|
setNextTarget(!!(keys.ARROW_LEFT === key));
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</EuiScreenReaderOnly>
|
2021-02-01 11:54:16 +01:00
|
|
|
|
|
|
|
{React.cloneElement(children, {
|
|
|
|
'data-test-subj': dataTestSubj || 'lnsDragDrop',
|
|
|
|
className: classNames(children.props.className, 'lnsDragDrop', 'lnsDragDrop-isDraggable', {
|
2021-02-15 12:48:06 +01:00
|
|
|
'lnsDragDrop-isHidden':
|
2021-02-21 10:02:04 +01:00
|
|
|
(activeDraggingProps && dragType === 'move' && !keyboardMode) ||
|
|
|
|
shouldShowGhostImageInstead,
|
2021-02-01 11:54:16 +01:00
|
|
|
}),
|
|
|
|
draggable: true,
|
|
|
|
onDragEnd: dragEnd,
|
|
|
|
onDragStart: dragStart,
|
2021-03-02 11:08:18 +01:00
|
|
|
onMouseDown: removeSelectionBeforeDragging,
|
2021-02-01 11:54:16 +01:00
|
|
|
})}
|
|
|
|
</div>
|
2020-05-29 09:34:55 +02:00
|
|
|
);
|
2021-02-01 11:54:16 +01:00
|
|
|
});
|
2020-05-29 09:34:55 +02:00
|
|
|
|
2021-02-01 11:54:16 +01:00
|
|
|
const DropInner = memo(function DropInner(props: DropInnerProps) {
|
2020-05-29 09:34:55 +02:00
|
|
|
const {
|
2021-02-01 11:54:16 +01:00
|
|
|
dataTestSubj,
|
2020-05-29 09:34:55 +02:00
|
|
|
className,
|
|
|
|
onDrop,
|
|
|
|
value,
|
|
|
|
children,
|
|
|
|
draggable,
|
|
|
|
dragging,
|
2020-09-04 16:11:41 +02:00
|
|
|
isNotDroppable,
|
2021-02-05 18:17:03 +01:00
|
|
|
dropType,
|
2021-02-10 15:25:47 +01:00
|
|
|
order,
|
2021-02-01 11:54:16 +01:00
|
|
|
getAdditionalClassesOnEnter,
|
2021-02-05 18:17:03 +01:00
|
|
|
getAdditionalClassesOnDroppable,
|
2021-02-10 15:25:47 +01:00
|
|
|
isActiveDropTarget,
|
|
|
|
registerDropTarget,
|
|
|
|
setActiveDropTarget,
|
2021-02-21 10:02:04 +01:00
|
|
|
keyboardMode,
|
2021-02-05 18:17:03 +01:00
|
|
|
setKeyboardMode,
|
|
|
|
setDragging,
|
|
|
|
setA11yMessage,
|
2020-05-29 09:34:55 +02:00
|
|
|
} = props;
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2021-02-05 18:17:03 +01:00
|
|
|
useShallowCompareEffect(() => {
|
2021-02-21 10:02:04 +01:00
|
|
|
if (dropType && onDrop && keyboardMode) {
|
2021-02-05 18:17:03 +01:00
|
|
|
registerDropTarget(order, { ...value, onDrop, dropType });
|
|
|
|
return () => {
|
|
|
|
registerDropTarget(order, undefined);
|
|
|
|
};
|
|
|
|
}
|
2021-02-21 10:02:04 +01:00
|
|
|
}, [order, value, registerDropTarget, dropType, keyboardMode]);
|
2021-02-05 18:17:03 +01:00
|
|
|
|
|
|
|
const classesOnEnter = getAdditionalClassesOnEnter?.(dropType);
|
|
|
|
const classesOnDroppable = getAdditionalClassesOnDroppable?.(dropType);
|
|
|
|
|
2020-09-04 16:11:41 +02:00
|
|
|
const classes = classNames(
|
|
|
|
'lnsDragDrop',
|
|
|
|
{
|
2020-10-01 17:58:23 +02:00
|
|
|
'lnsDragDrop-isDraggable': draggable,
|
|
|
|
'lnsDragDrop-isDroppable': !draggable,
|
2021-02-05 18:17:03 +01:00
|
|
|
'lnsDragDrop-isDropTarget': dropType && dropType !== 'reorder',
|
2021-02-10 15:25:47 +01:00
|
|
|
'lnsDragDrop-isActiveDropTarget': dropType && isActiveDropTarget && dropType !== 'reorder',
|
|
|
|
'lnsDragDrop-isNotDroppable': isNotDroppable,
|
2020-09-04 16:11:41 +02:00
|
|
|
},
|
2021-02-10 15:25:47 +01:00
|
|
|
classesOnEnter && { [classesOnEnter]: isActiveDropTarget },
|
2021-02-05 18:17:03 +01:00
|
|
|
classesOnDroppable && { [classesOnDroppable]: dropType }
|
2021-02-01 11:54:16 +01:00
|
|
|
);
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
const dragOver = (e: DroppableEvent) => {
|
2021-02-05 18:17:03 +01:00
|
|
|
if (!dropType) {
|
2019-09-17 20:57:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// An optimization to prevent a bunch of React churn.
|
2021-02-10 15:25:47 +01:00
|
|
|
if (!isActiveDropTarget && dragging && onDrop) {
|
2021-02-05 18:17:03 +01:00
|
|
|
setActiveDropTarget({ ...value, dropType, onDrop });
|
|
|
|
setA11yMessage(announce.selectedTarget(dragging.humanData, value.humanData, dropType));
|
2019-09-17 20:57:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const dragLeave = () => {
|
2021-02-01 11:54:16 +01:00
|
|
|
setActiveDropTarget(undefined);
|
2019-09-17 20:57:53 +02:00
|
|
|
};
|
|
|
|
|
2021-02-01 11:54:16 +01:00
|
|
|
const drop = (e: DroppableEvent | React.KeyboardEvent<HTMLButtonElement>) => {
|
2019-09-17 20:57:53 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2021-02-05 18:17:03 +01:00
|
|
|
if (onDrop && dropType && dragging) {
|
2019-10-16 05:01:43 +02:00
|
|
|
trackUiEvent('drop_total');
|
2021-02-05 18:17:03 +01:00
|
|
|
onDrop(dragging, dropType);
|
|
|
|
setTimeout(() =>
|
|
|
|
setA11yMessage(announce.dropped(dragging.humanData, value.humanData, dropType))
|
|
|
|
);
|
2019-09-17 20:57:53 +02:00
|
|
|
}
|
2021-02-01 11:54:16 +01:00
|
|
|
setDragging(undefined);
|
2021-02-05 18:17:03 +01:00
|
|
|
setActiveDropTarget(undefined);
|
2021-02-01 11:54:16 +01:00
|
|
|
setKeyboardMode(false);
|
2019-09-17 20:57:53 +02:00
|
|
|
};
|
2021-02-15 12:48:06 +01:00
|
|
|
|
|
|
|
const ghost =
|
|
|
|
isActiveDropTarget && dropType !== 'reorder' && dragging?.ghost ? dragging.ghost : undefined;
|
|
|
|
|
2021-02-01 11:54:16 +01:00
|
|
|
return (
|
2021-02-25 09:42:50 +01:00
|
|
|
<>
|
2021-02-01 11:54:16 +01:00
|
|
|
{React.cloneElement(children, {
|
|
|
|
'data-test-subj': dataTestSubj || 'lnsDragDrop',
|
|
|
|
className: classNames(children.props.className, classes, className),
|
|
|
|
onDragOver: dragOver,
|
|
|
|
onDragLeave: dragLeave,
|
|
|
|
onDrop: drop,
|
|
|
|
draggable,
|
|
|
|
})}
|
2021-02-15 12:48:06 +01:00
|
|
|
{ghost
|
|
|
|
? React.cloneElement(ghost.children, {
|
|
|
|
className: classNames(ghost.children.props.className, 'lnsDragDrop_ghost'),
|
|
|
|
style: ghost.style,
|
|
|
|
})
|
|
|
|
: null}
|
2021-02-25 09:42:50 +01:00
|
|
|
</>
|
2021-02-01 11:54:16 +01:00
|
|
|
);
|
|
|
|
});
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2021-02-01 11:54:16 +01:00
|
|
|
const ReorderableDrag = memo(function ReorderableDrag(
|
2021-02-05 18:17:03 +01:00
|
|
|
props: DragInnerProps & { reorderableGroup: Array<{ id: string }>; dragging?: DragDropIdentifier }
|
2021-02-01 11:54:16 +01:00
|
|
|
) {
|
|
|
|
const {
|
|
|
|
reorderState: { isReorderOn, reorderedItems, direction },
|
|
|
|
setReorderState,
|
|
|
|
} = useContext(ReorderContext);
|
2020-11-03 21:18:44 +01:00
|
|
|
|
2021-02-01 11:54:16 +01:00
|
|
|
const {
|
|
|
|
value,
|
|
|
|
setActiveDropTarget,
|
2021-02-21 10:02:04 +01:00
|
|
|
activeDraggingProps,
|
2021-02-01 11:54:16 +01:00
|
|
|
reorderableGroup,
|
|
|
|
setA11yMessage,
|
|
|
|
} = props;
|
2020-11-03 21:18:44 +01:00
|
|
|
|
2021-02-21 10:02:04 +01:00
|
|
|
const keyboardMode = activeDraggingProps?.keyboardMode;
|
|
|
|
const activeDropTarget = activeDraggingProps?.activeDropTarget;
|
|
|
|
const dropTargetsByOrder = activeDraggingProps?.dropTargetsByOrder;
|
|
|
|
const isDragging = !!activeDraggingProps;
|
|
|
|
|
2021-02-01 11:54:16 +01:00
|
|
|
const isFocusInGroup = keyboardMode
|
|
|
|
? isDragging &&
|
2021-02-21 10:02:04 +01:00
|
|
|
(!activeDropTarget || reorderableGroup.some((i) => i.id === activeDropTarget?.id))
|
2021-02-01 11:54:16 +01:00
|
|
|
: isDragging;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
isReorderOn: isFocusInGroup,
|
|
|
|
}));
|
|
|
|
}, [setReorderState, isFocusInGroup]);
|
|
|
|
|
|
|
|
const onReorderableDragStart = (
|
|
|
|
currentTarget?:
|
|
|
|
| DroppableEvent['currentTarget']
|
|
|
|
| React.KeyboardEvent<HTMLButtonElement>['currentTarget']
|
|
|
|
) => {
|
|
|
|
if (currentTarget) {
|
|
|
|
const height = currentTarget.offsetHeight + lnsLayerPanelDimensionMargin;
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
draggingHeight: height,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
};
|
2020-11-03 21:18:44 +01:00
|
|
|
|
2021-02-01 11:54:16 +01:00
|
|
|
const onReorderableDragEnd = () => {
|
|
|
|
resetReorderState();
|
2020-11-03 21:18:44 +01:00
|
|
|
};
|
|
|
|
|
2021-02-01 11:54:16 +01:00
|
|
|
const resetReorderState = () =>
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
reorderedItems: [],
|
|
|
|
}));
|
|
|
|
|
|
|
|
const extraKeyboardHandler = (e: React.KeyboardEvent<HTMLButtonElement>) => {
|
|
|
|
if (isReorderOn && keyboardMode) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
let activeDropTargetIndex = reorderableGroup.findIndex((i) => i.id === value.id);
|
2021-02-21 10:02:04 +01:00
|
|
|
if (activeDropTarget) {
|
|
|
|
const index = reorderableGroup.findIndex((i) => i.id === activeDropTarget?.id);
|
2021-02-01 11:54:16 +01:00
|
|
|
if (index !== -1) activeDropTargetIndex = index;
|
|
|
|
}
|
2021-02-05 18:17:03 +01:00
|
|
|
if (e.key === keys.ARROW_LEFT || e.key === keys.ARROW_RIGHT) {
|
|
|
|
resetReorderState();
|
|
|
|
setActiveDropTarget(undefined);
|
|
|
|
} else if (keys.ARROW_DOWN === e.key) {
|
2021-02-01 11:54:16 +01:00
|
|
|
if (activeDropTargetIndex < reorderableGroup.length - 1) {
|
2021-02-05 18:17:03 +01:00
|
|
|
const nextTarget = nextValidDropTarget(
|
2021-02-21 10:02:04 +01:00
|
|
|
dropTargetsByOrder,
|
2021-02-05 18:17:03 +01:00
|
|
|
activeDropTarget,
|
|
|
|
[props.order.join(',')],
|
|
|
|
(el) => el?.dropType === 'reorder'
|
2021-02-01 11:54:16 +01:00
|
|
|
);
|
2021-02-05 18:17:03 +01:00
|
|
|
onReorderableDragOver(nextTarget);
|
2021-02-01 11:54:16 +01:00
|
|
|
}
|
|
|
|
} else if (keys.ARROW_UP === e.key) {
|
|
|
|
if (activeDropTargetIndex > 0) {
|
2021-02-05 18:17:03 +01:00
|
|
|
const nextTarget = nextValidDropTarget(
|
2021-02-21 10:02:04 +01:00
|
|
|
dropTargetsByOrder,
|
2021-02-05 18:17:03 +01:00
|
|
|
activeDropTarget,
|
|
|
|
[props.order.join(',')],
|
|
|
|
(el) => el?.dropType === 'reorder',
|
|
|
|
true
|
2021-02-01 11:54:16 +01:00
|
|
|
);
|
2021-02-05 18:17:03 +01:00
|
|
|
onReorderableDragOver(nextTarget);
|
2021-02-01 11:54:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-11-03 21:18:44 +01:00
|
|
|
|
2021-02-05 18:17:03 +01:00
|
|
|
const onReorderableDragOver = (target?: DropIdentifier) => {
|
|
|
|
if (!target) {
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
reorderedItems: [],
|
|
|
|
}));
|
|
|
|
setA11yMessage(announce.selectedTarget(value.humanData, value.humanData, 'reorder'));
|
2021-02-01 11:54:16 +01:00
|
|
|
setActiveDropTarget(target);
|
2021-02-05 18:17:03 +01:00
|
|
|
return;
|
2021-02-01 11:54:16 +01:00
|
|
|
}
|
2021-02-05 18:17:03 +01:00
|
|
|
const droppingIndex = reorderableGroup.findIndex((i) => i.id === target.id);
|
2021-02-01 11:54:16 +01:00
|
|
|
const draggingIndex = reorderableGroup.findIndex((i) => i.id === value?.id);
|
|
|
|
if (draggingIndex === -1) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-05 18:17:03 +01:00
|
|
|
setActiveDropTarget(target);
|
2021-02-01 11:54:16 +01:00
|
|
|
|
2021-02-05 18:17:03 +01:00
|
|
|
setA11yMessage(announce.selectedTarget(value.humanData, target.humanData, 'reorder'));
|
2021-02-01 11:54:16 +01:00
|
|
|
|
|
|
|
setReorderState((s: ReorderState) =>
|
|
|
|
draggingIndex < droppingIndex
|
|
|
|
? {
|
|
|
|
...s,
|
|
|
|
reorderedItems: reorderableGroup.slice(draggingIndex + 1, droppingIndex + 1),
|
|
|
|
direction: '-',
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
...s,
|
|
|
|
reorderedItems: reorderableGroup.slice(droppingIndex, draggingIndex),
|
|
|
|
direction: '+',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const areItemsReordered = isDragging && keyboardMode && reorderedItems.length;
|
2020-11-03 21:18:44 +01:00
|
|
|
|
|
|
|
return (
|
2020-11-11 12:36:04 +01:00
|
|
|
<div
|
2021-02-01 11:54:16 +01:00
|
|
|
data-test-subj="lnsDragDrop-reorderableDrag"
|
|
|
|
className={
|
|
|
|
isDragging
|
|
|
|
? 'lnsDragDrop-reorderable lnsDragDrop-translatableDrag'
|
|
|
|
: 'lnsDragDrop-reorderable'
|
|
|
|
}
|
|
|
|
style={
|
|
|
|
areItemsReordered
|
|
|
|
? {
|
|
|
|
transform: `translateY(${direction === '+' ? '-' : '+'}${reorderedItems.reduce(
|
2021-02-05 18:17:03 +01:00
|
|
|
(acc, cur) => acc + Number(cur.height || 0) + lnsLayerPanelDimensionMargin,
|
2021-02-01 11:54:16 +01:00
|
|
|
0
|
|
|
|
)}px)`,
|
2020-11-03 21:18:44 +01:00
|
|
|
}
|
2021-02-01 11:54:16 +01:00
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<DragInner
|
|
|
|
{...props}
|
2021-02-05 18:17:03 +01:00
|
|
|
ariaDescribedBy="lnsDragDrop-keyboardInstructionsWithReorder"
|
2021-02-01 11:54:16 +01:00
|
|
|
extraKeyboardHandler={extraKeyboardHandler}
|
|
|
|
onDragStart={onReorderableDragStart}
|
|
|
|
onDragEnd={onReorderableDragEnd}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const ReorderableDrop = memo(function ReorderableDrop(
|
2021-02-05 18:17:03 +01:00
|
|
|
props: DropInnerProps & { reorderableGroup: Array<{ id: string }> }
|
2021-02-01 11:54:16 +01:00
|
|
|
) {
|
|
|
|
const {
|
|
|
|
onDrop,
|
|
|
|
value,
|
|
|
|
dragging,
|
|
|
|
setDragging,
|
|
|
|
setKeyboardMode,
|
2021-02-10 15:25:47 +01:00
|
|
|
isActiveDropTarget,
|
2021-02-01 11:54:16 +01:00
|
|
|
setActiveDropTarget,
|
|
|
|
reorderableGroup,
|
|
|
|
setA11yMessage,
|
2021-02-05 18:17:03 +01:00
|
|
|
dropType,
|
2021-02-01 11:54:16 +01:00
|
|
|
} = props;
|
|
|
|
|
|
|
|
const currentIndex = reorderableGroup.findIndex((i) => i.id === value.id);
|
|
|
|
|
|
|
|
const {
|
|
|
|
reorderState: { isReorderOn, reorderedItems, draggingHeight, direction },
|
|
|
|
setReorderState,
|
|
|
|
} = useContext(ReorderContext);
|
|
|
|
|
|
|
|
const heightRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
const isReordered =
|
|
|
|
isReorderOn && reorderedItems.some((el) => el.id === value.id) && reorderedItems.length;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isReordered && heightRef.current?.clientHeight) {
|
|
|
|
setReorderState((s) => ({
|
|
|
|
...s,
|
|
|
|
reorderedItems: s.reorderedItems.map((el) =>
|
|
|
|
el.id === value.id
|
|
|
|
? {
|
|
|
|
...el,
|
|
|
|
height: heightRef.current?.clientHeight,
|
2020-11-03 21:18:44 +01:00
|
|
|
}
|
2021-02-01 11:54:16 +01:00
|
|
|
: el
|
|
|
|
),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}, [isReordered, setReorderState, value.id]);
|
|
|
|
|
|
|
|
const onReorderableDragOver = (e: DroppableEvent) => {
|
2021-02-05 18:17:03 +01:00
|
|
|
if (!dropType) {
|
2021-02-01 11:54:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// An optimization to prevent a bunch of React churn.
|
2021-02-10 15:25:47 +01:00
|
|
|
if (!isActiveDropTarget && dropType && onDrop) {
|
2021-02-05 18:17:03 +01:00
|
|
|
setActiveDropTarget({ ...value, dropType, onDrop });
|
2021-02-01 11:54:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const draggingIndex = reorderableGroup.findIndex((i) => i.id === dragging?.id);
|
|
|
|
|
|
|
|
if (!dragging || draggingIndex === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const droppingIndex = currentIndex;
|
|
|
|
if (draggingIndex === droppingIndex) {
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
reorderedItems: [],
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
setReorderState((s: ReorderState) =>
|
|
|
|
draggingIndex < droppingIndex
|
|
|
|
? {
|
2020-11-03 21:18:44 +01:00
|
|
|
...s,
|
2021-02-01 11:54:16 +01:00
|
|
|
reorderedItems: reorderableGroup.slice(draggingIndex + 1, droppingIndex + 1),
|
|
|
|
direction: '-',
|
2020-11-03 21:18:44 +01:00
|
|
|
}
|
2021-02-01 11:54:16 +01:00
|
|
|
: {
|
|
|
|
...s,
|
|
|
|
reorderedItems: reorderableGroup.slice(droppingIndex, draggingIndex),
|
|
|
|
direction: '+',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onReorderableDrop = (e: DroppableEvent) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
setActiveDropTarget(undefined);
|
|
|
|
setDragging(undefined);
|
|
|
|
setKeyboardMode(false);
|
|
|
|
|
2021-02-05 18:17:03 +01:00
|
|
|
if (onDrop && dropType && dragging) {
|
2021-02-01 11:54:16 +01:00
|
|
|
trackUiEvent('drop_total');
|
2021-02-05 18:17:03 +01:00
|
|
|
onDrop(dragging, 'reorder');
|
2021-02-01 11:54:16 +01:00
|
|
|
// setTimeout ensures it will run after dragEnd messaging
|
|
|
|
setTimeout(() =>
|
2021-02-05 18:17:03 +01:00
|
|
|
setA11yMessage(announce.dropped(dragging.humanData, value.humanData, 'reorder'))
|
2021-02-01 11:54:16 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
2020-11-03 21:18:44 +01:00
|
|
|
<div
|
2021-02-01 11:54:16 +01:00
|
|
|
style={
|
|
|
|
reorderedItems.some((i) => i.id === value.id)
|
|
|
|
? {
|
|
|
|
transform: `translateY(${direction}${draggingHeight}px)`,
|
|
|
|
}
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
ref={heightRef}
|
|
|
|
data-test-subj="lnsDragDrop-translatableDrop"
|
|
|
|
className="lnsDragDrop-translatableDrop lnsDragDrop-reorderable"
|
|
|
|
>
|
|
|
|
<DropInner {...props} />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div
|
|
|
|
data-test-subj="lnsDragDrop-reorderableDropLayer"
|
2020-11-03 21:18:44 +01:00
|
|
|
className={classNames('lnsDragDrop', {
|
2021-02-05 18:17:03 +01:00
|
|
|
['lnsDragDrop__reorderableDrop']: dragging && dropType,
|
2020-11-03 21:18:44 +01:00
|
|
|
})}
|
2021-02-01 11:54:16 +01:00
|
|
|
onDrop={onReorderableDrop}
|
|
|
|
onDragOver={onReorderableDragOver}
|
2020-11-03 21:18:44 +01:00
|
|
|
onDragLeave={() => {
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
reorderedItems: [],
|
|
|
|
}));
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2021-02-01 11:54:16 +01:00
|
|
|
});
|