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

Commit

Permalink
feat(store): save projects and page refs
Browse files Browse the repository at this point in the history
  • Loading branch information
TheReincarnator committed Dec 13, 2017
1 parent 58f9912 commit 20bb970
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/component/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function createMenu(store: Store): void {
accelerator: 'CmdOrCtrl+S',
role: 'save',
click: () => {
store.savePage();
store.save();
}
},
{
Expand Down
21 changes: 17 additions & 4 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export class Store {
this.projects.push(project);
}

public closePage(): void {
this.currentPage = undefined;
}

public getCurrentPage(): Page | undefined {
return this.currentPage;
}
Expand Down Expand Up @@ -114,12 +118,21 @@ export class Store {
(this.projects as IObservableArray<Project>).remove(project);
}

public savePage(): void {
if (!this.currentPage) {
throw new Error('Cannot save page: No page open');
public save(): void {
if (this.currentPage) {
const pagePath: string = PathUtils.join(
this.getPagesPath(),
`page-${this.currentPage.getId()}.yaml`
);
Persister.saveYaml(pagePath, this.currentPage.toJsonObject());
}

this.currentPage.save();
const projectsJsonObject: JsonObject = { projects: [] };
this.projects.forEach(project => {
(projectsJsonObject.projects as JsonArray).push(project.toJsonObject());
});
const projectsPath = PathUtils.join(this.getPagesPath(), 'projects.yaml');
Persister.saveYaml(projectsPath, projectsJsonObject);
}

public searchPatterns(term: string): Pattern[] {
Expand Down
11 changes: 1 addition & 10 deletions src/store/page/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { JsonObject, Persister } from '../json';
import { JsonObject } from '../json';
import { PageElement } from './page_element';
import * as PathUtils from 'path';
import { Store } from '..';

export class Page {
private name: string;
private id: string;
private root?: PageElement;
private store: Store;

public constructor(id: string, store: Store) {
this.id = id;
this.store = store;
this.name = 'New Page';
}

Expand All @@ -35,12 +32,6 @@ export class Page {
return this.root as PageElement;
}

public save(): void {
const pagePath: string = PathUtils.join(this.store.getPagesPath(), `page-${this.id}.yaml`);
const jsonObject: JsonObject = this.toJsonObject();
Persister.saveYaml(pagePath, jsonObject);
}

public toJsonObject(): JsonObject {
return { name: this.name, root: this.root ? this.root.toJsonObject() : undefined };
}
Expand Down
13 changes: 13 additions & 0 deletions src/store/project/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,17 @@ export class Project {
public getPagesInternal(): MobX.IObservableArray<PageRef> {
return this.pages as MobX.IObservableArray<PageRef>;
}

public toJsonObject(): JsonObject {
const pagesJsonObject: JsonObject[] = [];
this.pages.forEach(pageRef => {
pagesJsonObject.push(pageRef.toJsonObject());
});

return {
id: this.id,
name: this.name,
pages: pagesJsonObject
};
}
}
7 changes: 7 additions & 0 deletions src/store/project/page_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ export class PageRef {
project.getPagesInternal().push(this);
}
}

public toJsonObject(): JsonObject {
return {
id: this.id,
name: this.name
};
}
}

0 comments on commit 20bb970

Please sign in to comment.