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

Commit

Permalink
fix: clone element actions if required
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Sep 10, 2018
1 parent 479a713 commit 1594e53
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/model/element/element.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ElementAction } from '../element-action';
import { ElementContent } from './element-content';
import { ElementProperty } from './element-property';
import * as _ from 'lodash';
Expand Down Expand Up @@ -233,11 +234,48 @@ export class Element {
public clone(opts?: { withState: boolean }): Element {
const withState = Boolean(opts && opts.withState);

const clonedActions: Map<string, ElementAction> = this.properties
.filter(prop => {
const patternProperty = prop.getPatternProperty();
return (
patternProperty &&
patternProperty.getType() === Types.PatternPropertyType.EventHandler
);
})
.reduce((clones, prop) => {
const id = prop.getValue() as string;

if (!id) {
return clones;
}

const elementAction = this.project.getElementActionById(id);

if (!elementAction) {
return clones;
}

clones.set(prop.getPatternPropertyId(), elementAction.clone());
return clones;
}, new Map());

const clonedContents = this.contentIds
.map(contentId => this.project.getElementContentById(contentId))
.filter((content): content is ElementContent => typeof content !== 'undefined')
.map(content => content.clone({ withState }));

const propertyValues: [string, Types.ElementPropertyValue][] = [
...this.propertyValues.entries()
].map(([id, value]) => {
const clonedAction = clonedActions.get(id);

if (clonedAction) {
return [id, clonedAction.getId()] as [string, string];
}

return [id, value] as [string, Types.ElementPropertyValue];
});

const clone = new Element(
{
dragged: false,
Expand All @@ -251,7 +289,7 @@ export class Element {
forcedOpen: withState ? this.forcedOpen : false,
patternId: this.patternId,
placeholderHighlighted: withState ? this.placeholderHighlighted : false,
propertyValues: [...this.propertyValues.entries()],
propertyValues,
role: this.role,
selected: withState ? this.selected : false
},
Expand All @@ -265,6 +303,10 @@ export class Element {
this.project.addElementContent(clonedContent);
});

[...clonedActions.values()].forEach(clonedAction => {
this.project.addElementAction(clonedAction);
});

this.project.addElement(clone);
return clone;
}
Expand Down

0 comments on commit 1594e53

Please sign in to comment.