mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Adding drag-n-drop behavior
Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
parent
9b07ba47cc
commit
a3958b6a64
@ -158,6 +158,20 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
|
|||||||
hotbar.items[index] = null;
|
hotbar.items[index] = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
swapItems(from: number, to: number): void {
|
||||||
|
const { items } = this.getActive();
|
||||||
|
|
||||||
|
if (from < 0 || to < 0 || from >= items.length || to >= items.length || isNaN(from) || isNaN(to)) {
|
||||||
|
throw new Error("Invalid 'from' or 'to' arguments");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (from != to) {
|
||||||
|
const source = items.splice(from, 1);
|
||||||
|
|
||||||
|
items.splice(to, 0, source[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switchToPrevious() {
|
switchToPrevious() {
|
||||||
const hotbarStore = HotbarStore.getInstance();
|
const hotbarStore = HotbarStore.getInstance();
|
||||||
let index = hotbarStore.activeHotbarIndex - 1;
|
let index = hotbarStore.activeHotbarIndex - 1;
|
||||||
|
|||||||
@ -6,9 +6,9 @@ import { observer } from "mobx-react";
|
|||||||
import { HotbarIcon } from "./hotbar-icon";
|
import { HotbarIcon } from "./hotbar-icon";
|
||||||
import { cssNames, IClassName } from "../../utils";
|
import { cssNames, IClassName } from "../../utils";
|
||||||
import { catalogEntityRegistry } from "../../api/catalog-entity-registry";
|
import { catalogEntityRegistry } from "../../api/catalog-entity-registry";
|
||||||
import { HotbarItem, HotbarStore } from "../../../common/hotbar-store";
|
import { defaultHotbarCells, HotbarItem, HotbarStore } from "../../../common/hotbar-store";
|
||||||
import { CatalogEntity, catalogEntityRunContext } from "../../api/catalog-entity";
|
import { CatalogEntity, catalogEntityRunContext } from "../../api/catalog-entity";
|
||||||
import { DragDropContext, Draggable, DraggableProvided, Droppable, DroppableProvided, DropResult } from "react-beautiful-dnd";
|
import { DragDropContext, Draggable, Droppable, DropResult } from "react-beautiful-dnd";
|
||||||
import { HotbarSelector } from "./hotbar-selector";
|
import { HotbarSelector } from "./hotbar-selector";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@ -35,8 +35,17 @@ export class HotbarMenu extends React.Component<Props> {
|
|||||||
return item ? catalogEntityRegistry.items.find((entity) => entity.metadata.uid === item.entity.uid) : null;
|
return item ? catalogEntityRegistry.items.find((entity) => entity.metadata.uid === item.entity.uid) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
onDragEnd() {
|
onDragEnd(result: DropResult) {
|
||||||
console.log("drag end")
|
const { source, destination } = result;
|
||||||
|
|
||||||
|
if (!destination) { // Dropped outside of the list
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const from = parseInt(source.droppableId);
|
||||||
|
const to = parseInt(destination.droppableId);
|
||||||
|
|
||||||
|
HotbarStore.getInstance().swapItems(from, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
renderGrid() {
|
renderGrid() {
|
||||||
@ -44,33 +53,46 @@ export class HotbarMenu extends React.Component<Props> {
|
|||||||
const entity = this.getEntity(item);
|
const entity = this.getEntity(item);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Droppable droppableId={`droppable-${index}`} key={index} isCombineEnabled>
|
<Droppable droppableId={`${index}`} key={index}>
|
||||||
{(provided, snapshot) => (
|
{(provided, snapshot) => (
|
||||||
<HotbarCell
|
<HotbarCell
|
||||||
index={index}
|
index={index}
|
||||||
ref={provided.innerRef}
|
key={entity ? entity.getId() : `cell${index}`}
|
||||||
style={{ backgroundColor: snapshot.isDraggingOver ? 'blue' : 'grey' }}
|
innerRef={provided.innerRef}
|
||||||
|
className={cssNames({ isDraggingOver: snapshot.isDraggingOver })}
|
||||||
{...provided.droppableProps}
|
{...provided.droppableProps}
|
||||||
>
|
>
|
||||||
{entity && (
|
{entity && (
|
||||||
<Draggable draggableId={`draggable-${index}`} index={0}>
|
<Draggable draggableId={item.entity.uid} key={item.entity.uid} index={0}>
|
||||||
{(provided, snapshot) => (
|
{(provided, snapshot) => {
|
||||||
<div
|
const style = {
|
||||||
ref={provided.innerRef}
|
zIndex: defaultHotbarCells - index,
|
||||||
{...provided.draggableProps}
|
position: "absolute",
|
||||||
{...provided.dragHandleProps}
|
...provided.draggableProps.style,
|
||||||
>
|
} as React.CSSProperties;
|
||||||
<HotbarIcon
|
|
||||||
key={index}
|
return (
|
||||||
index={index}
|
<div
|
||||||
entity={entity}
|
key={item.entity.uid}
|
||||||
isActive={this.isActive(entity)}
|
ref={provided.innerRef}
|
||||||
onClick={() => entity.onRun(catalogEntityRunContext)}
|
{...provided.draggableProps}
|
||||||
/>
|
{...provided.dragHandleProps}
|
||||||
</div>
|
style={style}
|
||||||
)}
|
>
|
||||||
|
<HotbarIcon
|
||||||
|
key={index}
|
||||||
|
index={index}
|
||||||
|
entity={entity}
|
||||||
|
isActive={this.isActive(entity)}
|
||||||
|
onClick={() => entity.onRun(catalogEntityRunContext)}
|
||||||
|
className={cssNames({ isDragging: snapshot.isDragging })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}}
|
||||||
</Draggable>
|
</Draggable>
|
||||||
)}
|
)}
|
||||||
|
{provided.placeholder}
|
||||||
</HotbarCell>
|
</HotbarCell>
|
||||||
)}
|
)}
|
||||||
</Droppable>
|
</Droppable>
|
||||||
@ -99,21 +121,29 @@ export class HotbarMenu extends React.Component<Props> {
|
|||||||
interface HotbarCellProps extends HTMLAttributes<HTMLDivElement> {
|
interface HotbarCellProps extends HTMLAttributes<HTMLDivElement> {
|
||||||
children?: ReactNode;
|
children?: ReactNode;
|
||||||
index: number;
|
index: number;
|
||||||
ref?: any;
|
innerRef?: React.LegacyRef<HTMLDivElement>;
|
||||||
}
|
}
|
||||||
|
|
||||||
function HotbarCell(props: HotbarCellProps) {
|
function HotbarCell({ innerRef, children, className, ...rest }: HotbarCellProps) {
|
||||||
const [animating, setAnimating] = useState(false);
|
const [animating, setAnimating] = useState(false);
|
||||||
const onAnimationEnd = () => { setAnimating(false); };
|
const onAnimationEnd = () => { setAnimating(false); };
|
||||||
const onClick = () => { setAnimating(true); };
|
const onClick = () => {
|
||||||
|
if (className.includes("isDraggingOver")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setAnimating(true);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={cssNames("HotbarCell", { animating })}
|
className={cssNames("HotbarCell", { animating }, className)}
|
||||||
onAnimationEnd={onAnimationEnd}
|
onAnimationEnd={onAnimationEnd}
|
||||||
onClick={onClick}
|
onClick={onClick}
|
||||||
|
ref={innerRef}
|
||||||
|
{...rest}
|
||||||
>
|
>
|
||||||
{props.children}
|
{children}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,9 +11,8 @@ interface Props {
|
|||||||
hotbar: Hotbar;
|
hotbar: Hotbar;
|
||||||
}
|
}
|
||||||
|
|
||||||
const store = HotbarStore.getInstance();
|
|
||||||
|
|
||||||
export function HotbarSelector({ hotbar }: Props) {
|
export function HotbarSelector({ hotbar }: Props) {
|
||||||
|
const store = HotbarStore.getInstance();
|
||||||
const activeIndexDisplay = store.activeHotbarIndex + 1;
|
const activeIndexDisplay = store.activeHotbarIndex + 1;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user