Skip to content

Commit

Permalink
Saveable refactor (eclipsesource#22)
Browse files Browse the repository at this point in the history
Provide preliminary API to allow overriding of save and load behavior
  • Loading branch information
Ahmetanakol authored and edgarmueller committed Aug 28, 2018
1 parent b04842e commit 5737a22
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 65 deletions.
41 changes: 41 additions & 0 deletions src/browser/ResourceSaveable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Disposable, Event, MaybePromise, Resource } from '@theia/core/lib/common';
import { Saveable } from "@theia/core/lib/browser";

export class ResourceSaveable implements Saveable {
autoSave;
dirty: boolean = false;
onDirtyChanged: Event<void> = Object.assign((listener: (e) => any) => {
let result: Disposable;
result = {
dispose: () => {}
};

return result;
}, {
maxListeners: 30
}
);

constructor(private resource: Resource, private getData: () => any) {}

save(): MaybePromise<void> {
return this.onSave(this.getData()).then(this.doSave)
}

doSave = (content: string): MaybePromise<void> => {
if ( this.resource.saveContents !== undefined ) {
return this.resource.saveContents(content, { encoding: 'UTF-8' })
.then(() => {
this.dirty = false;
});
} else {
console.warn('resource cannot save');
return undefined;
}
}

onSave(data: any): Promise<string> {
const content = JSON.stringify(data, null, 2);
return Promise.resolve(content);
}
}
45 changes: 0 additions & 45 deletions src/browser/TreeEditorSaveable.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/browser/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './theia-tree-editor-widget';
export * from './tree-editor-utils';

export * from './ResourceSaveable';
49 changes: 30 additions & 19 deletions src/browser/theia-tree-editor-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Resource } from '@theia/core/lib/common';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { TreeEditorSaveable } from './TreeEditorSaveable';
import { inject, injectable } from 'inversify';
import { Actions } from '@jsonforms/core';
import { withProps } from 'recompose';
Expand All @@ -15,7 +14,19 @@ export interface TreeEditorWidgetOptions {
store: any;
EditorComponent: React.Component;
fileName: string;
saveable: Saveable;
onResourceLoad: any;
}
const defaultResourceParser = (content: string): Promise<any> => {
let parsedContent;
try {
parsedContent = JSON.parse(content);
} catch (err) {
console.warn('Invalid content', err);
parsedContent = {};
}
return parsedContent;
};

let widgetCounter = 0;
@injectable()
Expand All @@ -32,28 +43,28 @@ export class TreeEditorWidget extends BaseWidget implements SaveableSource {
this.resource = this.options.resource;
this.store = this.options.store;
this.addClass('tree-class');
this.saveable = new TreeEditorSaveable(this.resource, this.store);
this.saveable = this.options.saveable;
this.title.closable = true;
this.title.label = this.options.fileName;
this.title.caption = this.title.label;
this.resource.readContents().then(content => {
let parsedContent;
try {
parsedContent = JSON.parse(content);
} catch (err) {
console.warn('Invalid content', err);
parsedContent = {};
}
Promise.resolve(this.store).then(initializedStore => {
initializedStore.dispatch(Actions.update('', () => parsedContent));
const Editor = withProps({'saveable': this.saveable})(this.options.EditorComponent);
ReactDOM.render(
<Provider store={initializedStore}>
<Editor/>
</Provider>,
this.node);
this.resource.readContents()
.then(content => {
if(this.options.onResourceLoad === undefined) {
return defaultResourceParser(content);
}
return this.options.onResourceLoad(content);
})
.then(parsedContent => {
Promise.resolve(this.store).then(initializedStore => {
initializedStore.dispatch(Actions.update('', () => parsedContent));
const Editor = withProps({'saveable': this.saveable})(this.options.EditorComponent);
ReactDOM.render(
<Provider store={initializedStore}>
<Editor/>
</Provider>,
this.node);
});
});
});
}

onActivateRequest(msg: Message): void {
Expand Down

0 comments on commit 5737a22

Please sign in to comment.