-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_autosave_AutoSave.js.html
273 lines (233 loc) · 12.1 KB
/
export_autosave_AutoSave.js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: export/autosave/AutoSave.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: export/autosave/AutoSave.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*
* Copyright 2020 WICKLETS LLC
*
* This file is part of Wick Engine.
*
* Wick Engine is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Wick Engine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Wick Engine. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Utility class for autosaving projects.
*/
Wick.AutoSave = class {
/**
* The key used to store the list of autosaved projects.
* @type {string}
*/
static get AUTOSAVES_LIST_KEY () {
return 'autosaveList';
}
/**
* The prefix to use for keys to save project autosave data.
* @type {string}
*/
static get AUTOSAVE_DATA_PREFIX () {
return 'autosave_';
}
/**
* Saves a given project to localforage.
* @param {Wick.Project} project - the project to store in the AutoSave system.
*/
static save (project, callback) {
if(Wick.AutoSave.ENABLE_PERF_TIMERS) console.time('serialize step')
var autosaveData = this.generateAutosaveData(project);
if(Wick.AutoSave.ENABLE_PERF_TIMERS) console.timeEnd('serialize step')
if(Wick.AutoSave.ENABLE_PERF_TIMERS) console.time('localforage step')
this.addAutosaveToList(autosaveData, () => {
this.writeAutosaveData(autosaveData, () => {
if(Wick.AutoSave.ENABLE_PERF_TIMERS) console.timeEnd('localforage step')
callback();
})
});
}
/**
* Loads a given project from localforage.
* @param {string} uuid - the UUID of the project to load from the AutoSave system.
* @param {function} callback
*/
static load (uuid, callback) {
this.readAutosaveData(uuid, autosaveData => {
this.generateProjectFromAutosaveData(autosaveData, project => {
callback(project);
})
});
}
/**
* Deletes a project with a given UUID in the autosaves.
* @param {string} uuid - uuid of project ot delete.
* @param {function} callback
*/
static delete (uuid, callback) {
this.removeAutosaveFromList(uuid, () => {
this.deleteAutosaveData(uuid, () => {
callback();
});
});
}
/**
* Generates an object that is writable to localforage from a project.
* @param {Wick.Project} project - The project to generate data for.
*/
static generateAutosaveData (project) {
if(Wick.AutoSave.ENABLE_PERF_TIMERS) console.time('generate objects list')
var objects = Wick.ObjectCache.getActiveObjects(project);
if(Wick.AutoSave.ENABLE_PERF_TIMERS) console.timeEnd('generate objects list')
if(Wick.AutoSave.ENABLE_PERF_TIMERS) console.time('serialize objects list')
var projectData = project.serialize();
var objectsData = objects.map(object => {
return object.serialize();
});
if(Wick.AutoSave.ENABLE_PERF_TIMERS) console.timeEnd('serialize objects list')
var lastModified = projectData.metadata.lastModified;
return {
projectData: projectData,
objectsData: objectsData,
lastModified: lastModified,
};
}
/**
* Creates a project from data loaded from the autosave system
* @param {object} autosaveData - An autosave data object, use generateAutosaveData/readAutosaveData to get this object
*/
static generateProjectFromAutosaveData (autosaveData, callback) {
// Deserialize all objects in the project so they are added to the ObjectCache
autosaveData.objectsData.forEach(objectData => {
var object = Wick.Base.fromData(objectData);
});
// Deserialize the project itself
var project = Wick.Base.fromData(autosaveData.projectData);
// Load source files for assets from localforage
Wick.FileCache.loadFilesFromLocalforage(project, () => {
project.loadAssets(() => {
callback(project);
});
});
}
/**
* Adds autosaved project data to the list of autosaved projects.
* @param {Object} projectData -
*/
static addAutosaveToList (autosaveData, callback) {
this.getAutosavesList((list) => {
list.push({
uuid: autosaveData.projectData.uuid,
lastModified: autosaveData.lastModified,
});
this.updateAutosavesList(list, () => {
callback();
})
});
}
/**
* Removes autosaved project data to the list of autosaved projects.
* @param {string} uuid -
*/
static removeAutosaveFromList (uuid, callback) {
this.getAutosavesList((list) => {
list = list.filter(item => {
return item.uuid !== uuid;
})
this.updateAutosavesList(list, () => {
callback();
})
});
}
/**
* Get the list of autosaved projects currently in the AutoSave system.
* @param {function} callback - function to be passed object containing all autosaved projects.
*/
static getAutosavesList (callback) {
localforage.getItem(this.AUTOSAVES_LIST_KEY).then(result => {
var projectList = result || [];
// Sort by lastModified
projectList.sort((a,b) => {
return b.lastModified - a.lastModified;
});
callback(projectList);
});
}
/**
* Updates the list of autosaved projects currently in the AutoSave system.
* @param {Object} autosaveList - the list of projects
* @param {function} callback - called when saving is finished
*/
static updateAutosavesList (autosaveList, callback) {
localforage.setItem(this.AUTOSAVES_LIST_KEY, autosaveList).then(result => {
callback();
});
}
/**
* Save project data into the autosave system.
* @param {Object} autosaveData - Autosave data of a project, use generateAutosaveData to create this object
*/
static writeAutosaveData (autosaveData, callback) {
localforage.setItem(this.AUTOSAVE_DATA_PREFIX + autosaveData.projectData.uuid, autosaveData).then(() => {
callback();
});
}
/**
* Load project data from the autosave system.
* @param {string} uuid - the UUID of the project to load
*/
static readAutosaveData (uuid, callback) {
localforage.getItem(this.AUTOSAVE_DATA_PREFIX + uuid).then(result => {
if(!result) {
console.error('Could not load autosaveData for project: ' + uuid);
}
callback(result);
});
}
/**
* Deletes project data from the autosave system.
* @param {string} uuid - the UUID of the project to delete
*/
static deleteAutosaveData (uuid, callback) {
localforage.removeItem(this.AUTOSAVE_DATA_PREFIX + uuid).then(() => {
callback();
});
}
}
Wick.AutoSave.ENABLE_PERF_TIMERS = false;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BuiltinAssets.html">BuiltinAssets</a></li><li><a href="GlobalAPI.html">GlobalAPI</a></li><li><a href="paper.SelectionGUI.html">SelectionGUI</a></li><li><a href="paper-paper.Selection.html">Selection</a></li><li><a href="SelectionWidget.html">SelectionWidget</a></li><li><a href="Wick.AutoSave.html">AutoSave</a></li><li><a href="Wick.Base.html">Base</a></li><li><a href="Wick.Button.html">Button</a></li><li><a href="Wick.Clip.html">Clip</a></li><li><a href="Wick.Clipboard.html">Clipboard</a></li><li><a href="Wick.FileCache.html">FileCache</a></li><li><a href="Wick.Frame.html">Frame</a></li><li><a href="Wick.GUIElement.Project.html">Project</a></li><li><a href="Wick.GUIElement.Timeline.html">Timeline</a></li><li><a href="Wick.GUIElement-Wick.GUIElement.Breadcrumbs.html">Breadcrumbs</a></li><li><a href="Wick.GUIElement-Wick.GUIElement.Button.html">Button</a></li><li><a href="Wick.History.html">History</a></li><li><a href="Wick.HTMLExport.html">HTMLExport</a></li><li><a href="Wick.HTMLPreview.html">HTMLPreview</a></li><li><a href="Wick.ImageSequence.html">ImageSequence</a></li><li><a href="Wick.Layer.html">Layer</a></li><li><a href="Wick.Path.html">Path</a></li><li><a href="Wick.Project.html">Project</a></li><li><a href="Wick.Selection.html">Selection</a></li><li><a href="Wick.Tickable.html">Tickable</a></li><li><a href="Wick.Timeline.html">Timeline</a></li><li><a href="Wick.Tools-Wick.Tools.Brush.html">Brush</a></li><li><a href="Wick.Tools-Wick.Tools.Cursor.html">Cursor</a></li><li><a href="Wick.Tools-Wick.Tools.Ellipse.html">Ellipse</a></li><li><a href="Wick.Tools-Wick.Tools.Eraser.html">Eraser</a></li><li><a href="Wick.Tools-Wick.Tools.Eyedropper.html">Eyedropper</a></li><li><a href="Wick.Tools-Wick.Tools.FillBucket.html">FillBucket</a></li><li><a href="Wick.Tools-Wick.Tools.Interact.html">Interact</a></li><li><a href="Wick.Tools-Wick.Tools.Line.html">Line</a></li><li><a href="Wick.Tools-Wick.Tools.None.html">None</a></li><li><a href="Wick.Tools-Wick.Tools.Pan.html">Pan</a></li><li><a href="Wick.Tools-Wick.Tools.Pencil.html">Pencil</a></li><li><a href="Wick.Tools-Wick.Tools.Rectangle.html">Rectangle</a></li><li><a href="Wick.Tools-Wick.Tools.Text.html">Text</a></li><li><a href="Wick.Tools-Wick.Tools.Zoom.html">Zoom</a></li><li><a href="Wick.Transformation.html">Transformation</a></li><li><a href="Wick.Tween.html">Tween</a></li><li><a href="Wick.View-Wick.View.Clip.html">Clip</a></li><li><a href="Wick.View-Wick.View.Frame.html">Frame</a></li><li><a href="Wick.View-Wick.View.Path.html">Path</a></li><li><a href="Wick.View-Wick.View.Selection.html">Selection</a></li><li><a href="Wick.WickFile.html">WickFile</a></li><li><a href="Wick.WickObjectFile.html">WickObjectFile</a></li><li><a href="Wick.ZIPExport.html">ZIPExport</a></li><li><a href="WickObjectCache.html">WickObjectCache</a></li><li><a href="Wick-Wick.Asset.html">Asset</a></li><li><a href="Wick-Wick.AudioTrack.html">AudioTrack</a></li><li><a href="Wick-Wick.ClipAsset.html">ClipAsset</a></li><li><a href="Wick-Wick.Color.html">Color</a></li><li><a href="Wick-Wick.FileAsset.html">FileAsset</a></li><li><a href="Wick-Wick.FontAsset.html">FontAsset</a></li><li><a href="Wick-Wick.GIFAsset.html">GIFAsset</a></li><li><a href="Wick-Wick.GUIElement.html">GUIElement</a></li><li><a href="Wick-Wick.ImageAsset.html">ImageAsset</a></li><li><a href="Wick-Wick.SoundAsset.html">SoundAsset</a></li><li><a href="Wick-Wick.SVGAsset.html">SVGAsset</a></li><li><a href="Wick-Wick.Tool.html">Tool</a></li><li><a href="Wick-Wick.ToolSettings.html">ToolSettings</a></li><li><a href="Wick-Wick.View.html">View</a></li></ul><h3>Global</h3><ul><li><a href="global.html#type">type</a></li><li><a href="global.html#Wick">Wick</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Fri Apr 24 2020 15:26:12 GMT-0400 (Eastern Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>