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

Commit

Permalink
fix: update pattern / element properties correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Sep 10, 2018
1 parent be491b7 commit 1a37530
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 397 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
"common-dir": "2.0.2",
"commondir": "1.0.1",
"deep-assign": "2.0.0",
"deep-diff": "1.0.1",
"electron-is-dev": "0.3.0",
"electron-log": "2.2.14",
"electron-store": "2.0.0",
Expand Down
44 changes: 0 additions & 44 deletions src/container/pattern-list/pattern-folder-container.tsx

This file was deleted.

33 changes: 13 additions & 20 deletions src/container/pattern-list/pattern-list-container.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Search, Space, SpaceSize } from '../../components';
import * as Components from '../../components';
import { ElementDragImage } from '../element-drag-image';
import * as MobxReact from 'mobx-react';
import { PatternFolderContainer } from './pattern-folder-container';
// import { PatternFolderContainer } from './pattern-folder-container';
import { PatternItemContainer } from './pattern-item-container';
import * as React from 'react';
import { ViewStore } from '../../store';
Expand All @@ -23,29 +23,22 @@ export class PatternListContainer extends React.Component {

return (
<div onDragStart={e => this.handleDragStart(e)}>
<Space sizeBottom={SpaceSize.XXS}>
<Search
<Components.Space sizeBottom={Components.SpaceSize.XXS}>
<Components.Search
placeholder="Search Library"
onChange={e => store.setPatternSearchTerm(e.target.value)}
value={store.getPatternSearchTerm()}
/>
</Space>
{store.getPatternLibraries().map(library => {
const patternRoot = library.getRoot();
const matches = library.query(store.getPatternSearchTerm());

return (
<PatternFolderContainer
isRoot
folder={patternRoot}
matches={matches}
key={library.getId()}
render={pattern => (
</Components.Space>
{store.getPatternLibraries().map(library => (
<Components.PatternFolderView key={library.getId()} name={library.getName()}>
{library
.getPatterns()
.map(pattern => (
<PatternItemContainer key={pattern.getId()} pattern={pattern} />
)}
/>
);
})}
))}
</Components.PatternFolderView>
))}

<ElementDragImage
element={store.getDraggedElement()}
Expand Down
9 changes: 3 additions & 6 deletions src/container/property-list/property-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { partition } from 'lodash';
import * as Model from '../../model';
import { PropertyListItem } from './property-list-item';
import * as React from 'react';
import { Space, SpaceSize } from '../../components/space';
import { ViewStore } from '../../store';
import * as Types from '../../types';

Expand All @@ -28,11 +27,9 @@ export class PropertyListContainer extends React.Component {
{regularProps.map(elementProperty => (
<PropertyListItem key={elementProperty.getId()} property={elementProperty} />
))}
<Space sizeTop={SpaceSize.L}>
{eventHandlerProps.map(elementProperty => (
<PropertyListItem key={elementProperty.getId()} property={elementProperty} />
))}
</Space>
{eventHandlerProps.map(elementProperty => (
<PropertyListItem key={elementProperty.getId()} property={elementProperty} />
))}
</div>
);
}
Expand Down
12 changes: 7 additions & 5 deletions src/model/element/element-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,21 @@ export class ElementContent {
}

@Mobx.action
public clone(): ElementContent {
public clone(opts?: { withState: boolean }): ElementContent {
const withState = Boolean(opts && opts.withState);

const clonedElements = this.elementIds
.map(elementId => this.project.getElementById(elementId))
.filter((e): e is Element => typeof e !== 'undefined')
.map(e => e.clone());
.map(e => e.clone({ withState }));

const clone = new ElementContent(
{
elementIds: clonedElements.map(e => e.getId()),
forcedOpen: false,
highlighted: false,
forcedOpen: withState ? this.forcedOpen : false,
highlighted: withState ? this.highlighted : false,
id: uuid.v4(),
open: false,
open: withState ? this.open : false,
slotId: this.slotId
},
{
Expand Down
15 changes: 15 additions & 0 deletions src/model/element/element-property/element-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ export class ElementProperty {
);
}

public static fromPatternProperty(
patternProperty: AnyPatternProperty,
context: ElementPropertyContext
): ElementProperty {
return new ElementProperty(
{
id: uuid.v4(),
patternPropertyId: patternProperty.getId(),
setDefault: patternProperty.getRequired(),
value: patternProperty.getDefaultValue()
},
context
);
}

public clone(): ElementProperty {
const cloneElementAction = (): string | undefined => {
const patternProperty = this.getPatternProperty();
Expand Down
62 changes: 48 additions & 14 deletions src/model/element/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class Element {

private readonly project: Project;

@Mobx.observable private properties: ElementProperty[];
@Mobx.observable private properties: Map<string, ElementProperty> = new Map();

private role: Types.ElementRole;

Expand Down Expand Up @@ -122,7 +122,7 @@ export class Element {
});
};

this.properties = getProperties();
getProperties().forEach(prop => this.properties.set(prop.getId(), prop));
}

public static from(serialized: Types.SerializedElement, context: ElementContext): Element {
Expand Down Expand Up @@ -171,28 +171,35 @@ export class Element {
}

@Mobx.action
public clone(): Element {
public addProperty(property: ElementProperty): void {
this.properties.set(property.getId(), property);
}

@Mobx.action
public clone(opts?: { withState: boolean }): Element {
const withState = Boolean(opts && opts.withState);

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

const clone = new Element(
{
dragged: false,
focused: false,
highlighted: false,
focused: withState ? this.focused : false,
highlighted: withState ? this.highlighted : false,
id: uuid.v4(),
containerId: undefined,
containerId: withState ? this.containerId : undefined,
contentIds: clonedContents.map(content => content.getId()),
name: this.name,
open: false,
forcedOpen: false,
open: withState ? this.open : false,
forcedOpen: withState ? this.forcedOpen : false,
patternId: this.patternId,
placeholderHighlighted: false,
properties: this.properties.map(propertyValue => propertyValue.clone()),
placeholderHighlighted: withState ? this.placeholderHighlighted : false,
properties: this.getProperties().map(propertyValue => propertyValue.clone()),
role: this.role,
selected: false
selected: withState ? this.selected : false
},
{
project: this.project
Expand Down Expand Up @@ -388,6 +395,10 @@ export class Element {
return this.project.getElementById(containerParentId);
}

public hasPattern(pattern: Pattern): boolean {
return this.patternId === pattern.getId();
}

public getPattern(): Pattern | undefined {
return this.project.getPatternById(this.patternId);
}
Expand All @@ -396,8 +407,31 @@ export class Element {
return this.placeholderHighlighted;
}

@Mobx.action
public getProperties(): ElementProperty[] {
return this.properties;
const pattern = this.getPattern();
const elementProperties = [...this.properties.values()];

if (pattern) {
return pattern.getProperties().map(patternProperty => {
const elementProperty = elementProperties.find(
e => e.getPatternPropertyId() === patternProperty.getId()
);

if (elementProperty) {
return elementProperty;
}

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

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

return elementProperties;
}

public getRole(): Types.ElementRole {
Expand Down Expand Up @@ -556,7 +590,7 @@ export class Element {
forcedOpen: this.forcedOpen,
patternId: this.patternId,
placeholderHighlighted: this.placeholderHighlighted,
properties: this.properties.map(elementProperty => elementProperty.toJSON()),
properties: this.getProperties().map(elementProperty => elementProperty.toJSON()),
role: serializeRole(this.role),
selected: this.selected
};
Expand Down
1 change: 0 additions & 1 deletion src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from './edit-history';
export * from './element';
export * from './element-action';
export * from './page';
export * from './pattern-folder';
export * from './pattern-library';
export * from './pattern-property';
export * from './pattern';
Expand Down
Loading

0 comments on commit 1a37530

Please sign in to comment.