storage.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * @fileoverview Loading and saving blocks with localStorage and cloud storage.
  3. * @author q.neutron@gmail.com (Quynh Neutron)
  4. */
  5. 'use strict';
  6. // Create a namespace.
  7. var BlocklyStorage = BlocklyStorage || {};
  8. var Blockscad = Blockscad || {};
  9. Blockscad.Auth = Blockscad.Auth || {};
  10. var Blockly = Blockly || {};
  11. Blockly.Xml = Blockly.Xml || {};
  12. BlocklyStorage.autosaveBlocks = function(xml_text) {
  13. if ('localStorage' in window) {
  14. // in standalone, the "url" scheme doesn't work. Have to name the
  15. // items in localStorage directly.
  16. localStorage.xml = xml_text;
  17. localStorage.proj_name = $('#project-name').val();
  18. }
  19. };
  20. BlocklyStorage.standaloneRestoreBlocks = function() {
  21. if (localStorage.xml) {
  22. var xml = Blockly.Xml.textToDom(localStorage.xml);
  23. Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xml);
  24. var project_name = localStorage.proj_name;
  25. if (project_name != "undefined") {
  26. $('#project-name').val(project_name);
  27. }
  28. else $('#project-name').val('Untitled');
  29. }
  30. }
  31. /**
  32. * Backup code blocks to localStorage.
  33. * @private
  34. */
  35. BlocklyStorage.backupBlocks_ = function() {
  36. // console.log("in backupBlocks");
  37. if ('localStorage' in window) {
  38. // clear out old stuff from localStorage
  39. localStorage.clear();
  40. var xml = Blockly.Xml.workspaceToDom(Blockscad.workspace);
  41. // Gets the current URL, not including the hash.
  42. var url = window.location.href.split('#')[0];
  43. url = url.split('?lang')[0];
  44. var url2 = url + "proj_name";
  45. var url3 = url + "current_project";
  46. var url4 = url + "current_project_key";
  47. var url5 = url + "needToSave";
  48. var url6 = url + "pLang";
  49. // do I have any stl files (converted to CSG commands) I want to save?
  50. // TO-DO: don't do this unless you find it in the xml you're saving.
  51. var blocks = Blockscad.workspace.getAllBlocks();
  52. for (var i = 0; i < blocks.length; i++){
  53. if (blocks[i].type == 'stl_import') {
  54. var csg_key = blocks[i].getField('STL_CONTENTS').getText();
  55. if (csg_key.length > 0) {
  56. var url_csg = url + csg_key;
  57. var url_csg_center = url + csg_key + 'center';
  58. // can I zip up the text?
  59. // only save this if it isn't too big.
  60. if (Blockscad.csg_commands[csg_key].length < 3000000) {
  61. var compressed_data = Base64.toBase64(RawDeflate.deflate(Base64.utob(Blockscad.csg_commands[csg_key])));
  62. window.localStorage.setItem(url_csg, compressed_data);
  63. window.localStorage.setItem(url_csg_center, Blockscad.csg_center[csg_key]);
  64. }
  65. }
  66. }
  67. }
  68. window.localStorage.setItem(url, Blockly.Xml.domToText(xml));
  69. window.localStorage.setItem(url2, $('#project-name').val());
  70. window.localStorage.setItem(url3, Blockscad.Auth.currentProject);
  71. window.localStorage.setItem(url4, Blockscad.Auth.currentProjectKey);
  72. window.localStorage.setItem(url5, Blockscad.needToSave);
  73. if (Blockscad.pLang)
  74. window.localStorage.setItem(url6, Blockscad.pLang);
  75. else
  76. localStorage.removeItem(url6);
  77. }
  78. };
  79. /**
  80. * Bind the localStorage backup function to the unload event.
  81. */
  82. BlocklyStorage.backupOnUnload = function() {
  83. //console.log("in backupOnUnload");
  84. window.addEventListener('unload', BlocklyStorage.backupBlocks_, false);
  85. };
  86. /**
  87. * Restore code blocks from localStorage.
  88. */
  89. BlocklyStorage.restoreBlocks = function() {
  90. var url = window.location.href.split('#')[0];
  91. url = url.split('?lang')[0];
  92. // console.log("url for restoring blocks is:",url);
  93. var url2 = url + "proj_name";
  94. var url3 = url + "current_project";
  95. var url4 = url + "current_project_key";
  96. var url5 = url + "needToSave";
  97. // console.log(window.localStorage);
  98. if ('localStorage' in window && window.localStorage[url]) {
  99. var xml = Blockly.Xml.textToDom(window.localStorage[url]);
  100. Blockly.Xml.domToWorkspace(xml, Blockscad.workspace);
  101. var blocks = Blockscad.workspace.getAllBlocks();
  102. for (var i = 0; i < blocks.length; i++){
  103. if (blocks[i].type == 'stl_import') {
  104. var csg_key = blocks[i].getField('STL_CONTENTS').getText();
  105. var csg_filename = blocks[i].getField('STL_FILENAME').getText();
  106. var browse_button = blocks[i].getField('STL_BUTTON');
  107. if (csg_key.length > 0) {
  108. var url_csg = url + csg_key;
  109. var url_csg_center = url + csg_key + 'center';
  110. var csg_contents = window.localStorage[url_csg];
  111. var csg_center = window.localStorage[url_csg_center];
  112. if (csg_contents && csg_contents.length>0) {
  113. browse_button.setVisible(false);
  114. // inflate and assign to my variable.
  115. //console.log("csg contents compressed is:",csg_contents);
  116. console.log("csg contents compressed length is:",csg_contents.length);
  117. var decompressed = Base64.btou(RawDeflate.inflate(Base64.fromBase64(csg_contents)));
  118. console.log("decompressed lengeth is:",decompressed.length);
  119. Blockscad.csg_commands[csg_key] = decompressed;
  120. // I need to populate the filename->key variable too.
  121. Blockscad.csg_filename[csg_key] = csg_filename + ":::";
  122. Blockscad.csg_center[csg_key] = csg_center;
  123. blocks[i].render();
  124. }
  125. else {
  126. // The xml thinks there should be stl data here, but I can't find it. Clear the block.
  127. console.log("couldn't find the stl in localStorage");
  128. // set a warning message in the render pane
  129. $( '#error-message' ).html("Warning: Re-load your .STL files");
  130. blocks[i].getField('STL_CONTENTS').setText('');
  131. blocks[i].getField('STL_FILENAME').setText('');
  132. var cText = blocks[i].getCommentText();
  133. if (!cText.match(/^RELOAD/)) cText = 'RELOAD: ' + cText;
  134. blocks[i].setCommentText(cText);
  135. browse_button.setText('Reload');
  136. blocks[i].backlight();
  137. // if block is in a collapsed parent, highlight collapsed parent too
  138. var others = blocks[i].collapsedParents();
  139. if (others)
  140. for (var j=0; j < others.length; j++)
  141. others[j].backlight();
  142. if (!blocks[i].isCollapsed())
  143. browse_button.setVisible(true);
  144. // Bubble up to re-collapse top collapsed block
  145. var parent = blocks[i];
  146. var collapsedParent = null;
  147. while (parent) {
  148. if (parent.isCollapsed()) {
  149. collapsedParent = parent;
  150. }
  151. parent = parent.getSurroundParent();
  152. }
  153. if (collapsedParent) {
  154. collapsedParent.setCollapsed(true,true);
  155. }
  156. blocks[i].render();
  157. // If my block had a button field for browsing, I'd make it visible now.
  158. // Then I'd make the filename field invisible.
  159. }
  160. }
  161. }
  162. }
  163. var project_name = window.localStorage[url2];
  164. if (project_name != "undefined") {
  165. $('#project-name').val(project_name);
  166. }
  167. else $('#project-name').val('Untitled');
  168. var current_project = window.localStorage[url3];
  169. if (current_project != "undefined") {
  170. Blockscad.Auth.currentProject = current_project;
  171. }
  172. else Blockscad.Auth.currentProject = '';
  173. var current_project_key = window.localStorage[url4];
  174. if (current_project_key != "undefined") {
  175. Blockscad.Auth.currentProjectKey = current_project_key;
  176. }
  177. var needToSave = Number(window.localStorage[url5]);
  178. if (needToSave != "undefined" && (needToSave == 1 || needToSave == 0)) {
  179. setTimeout(function() {
  180. Blockscad.needToSave = needToSave;
  181. // console.log("loading from storage. setting needToSave to:", needToSave);
  182. }, 300);
  183. }
  184. }
  185. }
  186. Blockscad.getLangFromLS = function() {
  187. var url = window.location.href.split('#')[0];
  188. url = url.split('?lang')[0];
  189. var url6 = url + "pLang";
  190. return window.localStorage[url6];
  191. }