Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
feat(component): elements can now be dragged to a specific position
Browse files Browse the repository at this point in the history
  • Loading branch information
Lasse Küchler committed Dec 13, 2017
1 parent f55f3a6 commit 8e136a2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/component/container/elementWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as React from 'react';
export interface ElementWrapperState {
open?: boolean;
highlight?: boolean;
highlightPlaceholder?: boolean;
}

export interface ElementWrapperProps {
Expand All @@ -12,6 +13,7 @@ export interface ElementWrapperProps {
title: string;
handleClick?: React.MouseEventHandler<HTMLElement>;
handleDragDrop?: React.DragEventHandler<HTMLElement>;
handleDragDropForChild?: React.DragEventHandler<HTMLElement>;
}

export class ElementWrapper extends React.Component<ElementWrapperProps, ElementWrapperState> {
Expand All @@ -27,6 +29,9 @@ export class ElementWrapper extends React.Component<ElementWrapperProps, Element
this.handleDragEnter = this.handleDragEnter.bind(this);
this.handleDragLeave = this.handleDragLeave.bind(this);
this.handleDragDrop = this.handleDragDrop.bind(this);
this.handleDragEnterForChild = this.handleDragEnterForChild.bind(this);
this.handleDragLeaveForChild = this.handleDragLeaveForChild.bind(this);
this.handleDragDropForChild = this.handleDragDropForChild.bind(this);
}

private handleIconClick(): void {
Expand Down Expand Up @@ -54,6 +59,28 @@ export class ElementWrapper extends React.Component<ElementWrapperProps, Element
this.props.handleDragDrop && this.props.handleDragDrop(e);
}

private handleDragEnterForChild(e: React.DragEvent<HTMLElement>): void {
console.log('handleDragEnterForChild');
this.setState({
highlightPlaceholder: true
});
}

private handleDragLeaveForChild(e: React.DragEvent<HTMLElement>): void {
console.log('handleDragLeaveForChild');
this.setState({
highlightPlaceholder: false
});
}

private handleDragDropForChild(e: React.DragEvent<HTMLElement>): void {
console.log('handleDragDropForChild');
this.setState({
highlightPlaceholder: false
});
this.props.handleDragDropForChild && this.props.handleDragDropForChild(e);
}

public render(): JSX.Element {
const { active, children, handleClick, title } = this.props;
return (
Expand All @@ -62,11 +89,15 @@ export class ElementWrapper extends React.Component<ElementWrapperProps, Element
open={!this.state.open}
active={active}
highlight={this.state.highlight}
highlightPlaceholder={this.state.highlightPlaceholder}
handleClick={handleClick}
handleIconClick={this.handleIconClick}
handleDragEnter={this.handleDragEnter}
handleDragLeave={this.handleDragLeave}
handleDragDrop={this.handleDragDrop}
handleDragEnterForChild={this.handleDragEnterForChild}
handleDragLeaveForChild={this.handleDragLeaveForChild}
handleDragDropForChild={this.handleDragDropForChild}
>
{children}
</Element>
Expand Down
18 changes: 18 additions & 0 deletions src/component/container/element_list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class ElementList extends React.Component<ElementListProps> {
key={key}
handleClick={item.onClick}
active={item.active}
handleDragDropForChild={item.handleDragDropForChild}
handleDragDrop={item.handleDragDrop}
>
{item.children &&
Expand Down Expand Up @@ -86,6 +87,23 @@ export class ElementList extends React.Component<ElementListProps> {
label: key,
value: patternPath.replace(/^.*\//, ''),
onClick: updatePageElement,
handleDragDropForChild: (e: React.DragEvent<HTMLElement>) => {
console.log('handleDragDropForChild', element);
const transfePatternPath = e.dataTransfer.getData('patternPath');
const parentElement = element.getParent();
if (!parentElement) {
return;
}
console.log('parent', parentElement);
parentElement.getChildren().map((child: PageElement) => {
console.log('children', child);
});
console.log('index:', parentElement.getChildren().indexOf(element));
parentElement.addChild(
new PageElement(this.props.store.getPattern(transfePatternPath)),
parentElement.getChildren().indexOf(element)
);
},
handleDragDrop: (e: React.DragEvent<HTMLElement>) => {
const transfePatternPath = e.dataTransfer.getData('patternPath');
element.addChild(new PageElement(this.props.store.getPattern(transfePatternPath)));
Expand Down

0 comments on commit 8e136a2

Please sign in to comment.