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

Commit

Permalink
fix: derive new element props from pattern props
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Sep 10, 2018
1 parent 82a058e commit ed9d171
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/model/element/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as _ from 'lodash';
import * as Mobx from 'mobx';
import { Page } from '../page';
import { Pattern } from '../pattern';
import { AnyPatternProperty } from '../pattern-property';
import { Project } from '../project';
import * as Types from '../../types';
import * as uuid from 'uuid';
Expand Down Expand Up @@ -73,6 +74,25 @@ export class Element {
return this.project.getPages().find(page => page.hasElement(this));
}

@Mobx.computed
private get pattern(): Pattern | undefined {
return this.project.getPatternById(this.patternId);
}

@Mobx.computed
private get flattenedProperties(): ElementProperty[] {
return [...this.properties.values()];
}

@Mobx.computed
private get patternProperties(): AnyPatternProperty[] {
if (!this.pattern) {
return [];
}

return this.pattern.getProperties();
}

public constructor(init: ElementInit, context: ElementContext) {
this.dragged = init.dragged;
this.focused = init.focused;
Expand Down Expand Up @@ -413,7 +433,22 @@ export class Element {

@Mobx.action
public getProperties(): ElementProperty[] {
return [...this.properties.values()];
return this.patternProperties.map(patternProperty => {
const elementProperty = this.flattenedProperties.find(
e => e.getPatternPropertyId() === patternProperty.getId()
);

if (elementProperty) {
return elementProperty;
}

const newElementProperty = ElementProperty.fromPatternProperty(patternProperty, {
project: this.project
});

this.addProperty(newElementProperty);
return newElementProperty;
});
}

public getRole(): Types.ElementRole {
Expand Down

0 comments on commit ed9d171

Please sign in to comment.