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

Commit

Permalink
feat: init new element on pattern doubleclick
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed May 24, 2018
1 parent ce202c5 commit d3077f7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 33 deletions.
8 changes: 7 additions & 1 deletion src/components/pattern-list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface PatternListItemProps {
draggable?: boolean;
icon?: string;
onClick?: React.MouseEventHandler<HTMLElement>;
onDoubleClick?: React.MouseEventHandler<HTMLElement>;
onDragStart?: React.DragEventHandler<HTMLElement>;
}

Expand Down Expand Up @@ -91,7 +92,12 @@ export const PatternFolderView: React.SFC<PatternFolderViewProps> = props => (
export const PatternListItem: React.StatelessComponent<PatternListItemProps> = props => {
const { draggable, onDragStart, icon, onClick } = props;
return (
<StyledPatternListItem onDragStart={onDragStart} draggable={draggable} onClick={onClick}>
<StyledPatternListItem
onDoubleClick={props.onDoubleClick}
onDragStart={onDragStart}
draggable={draggable}
onClick={onClick}
>
{icon ? (
<StyledImg {...{ [PatternAnchor.icon]: 'true' }} src={icon} />
) : (
Expand Down
15 changes: 15 additions & 0 deletions src/container/pattern-list/pattern-item-container.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { PatternAnchor, PatternListItem } from '../../components';
import { Pattern } from '../../model';
import * as React from 'react';
import { ViewStore } from '../../store';

export interface PatternItemContainerContainerProps {
pattern: Pattern;
}

export class PatternItemContainer extends React.Component<PatternItemContainerContainerProps> {
private handleDoubleClick(e: React.MouseEvent<HTMLElement>): void {
const store = ViewStore.getInstance();
const element = store.addNewElement({ pattern: this.props.pattern });
const selectedElement = store.getSelectedElement();
const page = store.getCurrentPage();

const targetElement = selectedElement ? selectedElement : page ? page.getRoot() : null;

if (element && targetElement) {
store.insertAfterElement({ element, targetElement });
}
}

private handleDragStart(e: React.DragEvent<HTMLElement>): void {
e.dataTransfer.dropEffect = 'copy';

Expand All @@ -25,6 +39,7 @@ export class PatternItemContainer extends React.Component<PatternItemContainerCo
<PatternListItem
key={props.pattern.getId()}
draggable
onDoubleClick={e => this.handleDoubleClick(e)}
onDragStart={e => this.handleDragStart(e)}
>
{props.pattern.getName()}
Expand Down
84 changes: 52 additions & 32 deletions src/store/view-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,55 @@ export class ViewStore {
return this.undoBuffer.length > 0;
}

public insertAfterElement(init: {
element: Model.Element;
targetElement: Model.Element;
}): Model.Element | undefined {
if (init.targetElement.isRoot()) {
return this.insertInsideElement(init);
}

const container = init.targetElement.getContainer();

if (!container) {
return;
}

this.execute(
Model.ElementLocationCommand.addChild({
index: init.targetElement.getIndex() + 1,
contentId: container.getId(),
childId: init.element.getId()
})
);

this.setSelectedElement(init.element);
return init.element;
}

public insertInsideElement(init: {
element: Model.Element;
targetElement: Model.Element;
}): Model.Element | undefined {
const contents = init.targetElement.getContentBySlotType(Types.SlotType.Children);

if (!contents) {
return;
}

this.execute(
Model.ElementLocationCommand.addChild({
contentId: contents.getId(),
childId: init.element.getId(),
index: contents.getElements().length
})
);

this.setSelectedElement(init.element);

return init.element;
}

public isElementHighlightedById(id: string): boolean {
const highlightedElement = this.getHighlightedElement();

Expand Down Expand Up @@ -552,27 +601,12 @@ export class ViewStore {

public pasteAfterElement(targetElement: Model.Element): Model.Element | undefined {
const clipboardElement = this.getClipboardItem(ClipBoardType.Element);
const container = targetElement.getContainer();

if (!clipboardElement || !container) {
if (!clipboardElement) {
return;
}

if (targetElement.isRoot()) {
return this.pasteInsideElement(targetElement);
}

this.execute(
Model.ElementLocationCommand.addChild({
index: targetElement.getIndex() + 1,
contentId: container.getId(),
childId: clipboardElement.getId()
})
);

this.setSelectedElement(clipboardElement);

return clipboardElement;
return this.insertAfterElement({ element: clipboardElement, targetElement });
}

public pasteAfterElementById(id: string): Model.Element | undefined {
Expand Down Expand Up @@ -602,21 +636,7 @@ export class ViewStore {
return;
}

const contents = element.getContentBySlotType(Types.SlotType.Children);

if (!contents) {
return;
}

this.execute(
Model.ElementLocationCommand.addChild({
contentId: contents.getId(),
childId: clipboardElement.getId(),
index: contents.getElements().length - 1
})
);

this.setSelectedElement(clipboardElement);
this.insertInsideElement({ element: clipboardElement, targetElement: element });

return clipboardElement;
}
Expand Down

0 comments on commit d3077f7

Please sign in to comment.