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

Commit

Permalink
feat(component): functionality to add patterns to element list with d…
Browse files Browse the repository at this point in the history
…rag and drop
  • Loading branch information
Lasse Küchler committed Dec 13, 2017
1 parent bdc70ae commit a2e3feb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/component/container/elementWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@ import * as React from 'react';

export interface ElementWrapperState {
open?: boolean;
highlight?: boolean;
}

export interface ElementWrapperProps {
active?: boolean;
open?: boolean;
title: string;
handleClick?: React.MouseEventHandler<HTMLElement>;
handleDragDrop?: React.DragEventHandler<HTMLElement>;
}

export class ElementWrapper extends React.Component<ElementWrapperProps, ElementWrapperState> {
public constructor(props: ElementWrapperProps) {
super(props);

this.state = {
open: this.props.open
open: this.props.open,
highlight: false
};

this.handleIconClick = this.handleIconClick.bind(this);
this.handleDragEnter = this.handleDragEnter.bind(this);
this.handleDragLeave = this.handleDragLeave.bind(this);
this.handleDragDrop = this.handleDragDrop.bind(this);
}

private handleIconClick(): void {
Expand All @@ -29,15 +35,38 @@ export class ElementWrapper extends React.Component<ElementWrapperProps, Element
});
}

private handleDragEnter(e: React.DragEvent<HTMLElement>): void {
this.setState({
highlight: true
});
}

private handleDragLeave(e: React.DragEvent<HTMLElement>): void {
this.setState({
highlight: false
});
}

private handleDragDrop(e: React.DragEvent<HTMLElement>): void {
this.setState({
highlight: false
});
this.props.handleDragDrop && this.props.handleDragDrop(e);
}

public render(): JSX.Element {
const { active, children, handleClick, title } = this.props;
return (
<Element
title={title}
open={!this.state.open}
active={active}
highlight={this.state.highlight}
handleClick={handleClick}
handleIconClick={this.handleIconClick}
handleDragEnter={this.handleDragEnter}
handleDragLeave={this.handleDragLeave}
handleDragDrop={this.handleDragDrop}
>
{children}
</Element>
Expand Down
5 changes: 5 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}
handleDragDrop={item.handleDragDrop}
>
{item.children &&
item.children.length > 0 &&
Expand Down Expand Up @@ -85,6 +86,10 @@ export class ElementList extends React.Component<ElementListProps> {
label: key,
value: patternPath.replace(/^.*\//, ''),
onClick: updatePageElement,
handleDragDrop: (e: React.DragEvent<HTMLElement>) => {
const transfePatternPath = e.dataTransfer.getData('patternPath');
element.addChild(new PageElement(this.props.store.getPattern(transfePatternPath)));
},
children: items,
active: element === selectedElement
};
Expand Down
10 changes: 10 additions & 0 deletions src/component/container/pattern_list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class PatternList extends React.Component<PatternListProps> {

this.handleSearchInputChange = this.handleSearchInputChange.bind(this);
this.handlePatternClick = this.handlePatternClick.bind(this);
this.handleDragStart = this.handleDragStart.bind(this);
}

public render(): JSX.Element {
Expand Down Expand Up @@ -53,6 +54,10 @@ export class PatternList extends React.Component<PatternListProps> {
folder.getPatterns().forEach((pattern: Pattern) => {
result.push({
value: pattern.getName(),
draggable: true,
handleDragStart: (e: React.DragEvent<HTMLElement>) => {
this.handleDragStart(e, pattern);
},
onClick: () => {
this.handlePatternClick(pattern);
}
Expand All @@ -73,4 +78,9 @@ export class PatternList extends React.Component<PatternListProps> {
selectedElement.addChild(new PageElement(pattern));
}
}
@action
protected handleDragStart(e: React.DragEvent<HTMLElement>, pattern: Pattern): void {
const data = pattern.getRelativePath();
e.dataTransfer.setData('patternPath', data);
}
}

0 comments on commit a2e3feb

Please sign in to comment.