Skip to content

Commit

Permalink
Basic state retention
Browse files Browse the repository at this point in the history
  • Loading branch information
brianpeiris committed Dec 24, 2019
1 parent e77fcc3 commit f226d61
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 16 additions & 0 deletions js/Sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export default class Sketch {
this.uuid = uuid || Math.generateUUID();
this.name = name || "Example Sketch";
this.files = files || [new File()];
this.initialized = false;
this.state = {};
}
getCode() {
let code = "";
Expand All @@ -15,6 +17,20 @@ export default class Sketch {
}
return code;
}
initialState(obj) {
if (this.initialized) return;
for (const key in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, key)) continue;
this.state[key] = obj[key];
}
}
toJSON() {
return {
uuid: this.uuid,
name: this.name,
files: this.files
};
}
static fromJSON(obj) {
return new Sketch(
obj.name,
Expand Down
4 changes: 3 additions & 1 deletion js/SketchController.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default class SketchController {
}

_readCode() {
localStorage.setItem("sketches_v1", JSON.stringify([this._sketch]));
localStorage.setItem("sketches_v1", JSON.stringify([this._sketch.toJSON()]));
this._domTextAreas.forEach((domTextArea, i) => {
domTextArea.value = this._sketch.files[i].contents;
});
Expand All @@ -117,6 +117,8 @@ export default class SketchController {
if (_sketchLoop) {
this._sketchLoop = _sketchLoop;
}

this._sketch.initialized = true;
}

spinNumberAndKeepSelection(domTextArea, file, direction, amount) {
Expand Down

0 comments on commit f226d61

Please sign in to comment.