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

Commit

Permalink
fix: dedupe pages
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Sep 10, 2018
1 parent 009b7e9 commit bc2a509
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
49 changes: 25 additions & 24 deletions src/model/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export class Project {

@Mobx.observable private name: string;

@Mobx.observable private pages: Page[] = [];
@Mobx.observable private internalPages: Map<string, Page> = new Map();

@Mobx.observable private pageList: string[] = [];

@Mobx.observable private path;

Expand Down Expand Up @@ -143,13 +145,23 @@ export class Project {
return Types.ItemType.None;
}

@Mobx.computed
private get pages(): Page[] {
return this.pageList
.map(id => this.internalPages.get(id))
.filter((page): page is Page => typeof page !== 'undefined');
}

public constructor(init: ProjectProperties) {
this.name = init.name;
this.id = init.id ? init.id : uuid.v4();
this.pages = init.pages ? init.pages : [];
this.path = init.path;
this.userStore = init.userStore;

init.pages.forEach(page => {
this.addPage(page);
});

init.patternLibraries.forEach(patternLibrary => {
this.addPatternLibrary(patternLibrary);
});
Expand Down Expand Up @@ -288,7 +300,11 @@ export class Project {

@Mobx.action
public addPage(page: Page): void {
this.pages.push(page);
if (!this.pageList.includes(page.getId())) {
this.pageList.push(page.getId());
}

this.internalPages.set(page.getId(), page);
page.setProject(this);
}

Expand Down Expand Up @@ -552,16 +568,16 @@ export class Project {
}

@Mobx.action
public removePage(page: Page): boolean {
const index = this.pages.indexOf(page);
public removePage(page: Page): void {
this.internalPages.delete(page.getId());

const index = this.pageList.indexOf(page.getId());

if (index === -1) {
return false;
return;
}

this.pages.splice(index, 1);

return true;
this.pageList.splice(index, 1);
}

@Mobx.action
Expand Down Expand Up @@ -627,11 +643,6 @@ export class Project {
this.name = name;
}

@Mobx.action
public setPages(pages: Page[]): void {
this.pages = pages;
}

@Mobx.action
public setPath(path: string): void {
this.path = path;
Expand Down Expand Up @@ -882,16 +893,6 @@ export class Project {
pageChanges.removed.forEach(change => this.removePage(change.before));
pageChanges.added.forEach(change => this.addPage(change.after));
pageChanges.changed.forEach(change => change.before.update(change.after));

// elements: this.getElements().map(e => e.toJSON()),
// elementActions: this.getElementActions().map(e => e.toJSON()),
// elementContents: this.getElementContents().map(e => e.toJSON()),
// id: this.id,
// name: this.name,
// pages: this.pages.map(p => p.toJSON()),
// path: this.path,
// patternLibraries: this.getPatternLibraries().map(p => p.toJSON()),
// userStore: this.userStore.toJSON()
}

public startBatch(): void {
Expand Down
7 changes: 4 additions & 3 deletions src/renderer/create-edit-message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ export function createEditMessageHandler({
project: contextProject
});

const clonedElement = sourceElement.clone();
project.endBatch();

const clonedElement = sourceElement.clone();

project.importElement(clonedElement);

switch (message.payload.targetType) {
Expand Down Expand Up @@ -117,10 +118,10 @@ export function createEditMessageHandler({
: store.getProject();

const sourcePage = Model.Page.from(message.payload.page, { project: contextProject });
const clonedPage = sourcePage.clone();

project.endBatch();

const clonedPage = sourcePage.clone();

project.importPage(clonedPage);
store.commit();

Expand Down

0 comments on commit bc2a509

Please sign in to comment.