session.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. Code Studio — recent projects and per project workspace state
  3. Two kinds of memory:
  4. * the list of recently opened project folders, kept in the module
  5. database and shown under File > Open Recent
  6. * which tabs were open in a project and in what order, kept in
  7. <project>/.metadata/.codestudio.json so reopening the folder
  8. restores the workspace (see backend/session.agi)
  9. */
  10. /* ═══════════════════════════════════════════════════════════════════
  11. Recently opened projects
  12. ═══════════════════════════════════════════════════════════════════ */
  13. var CS_RECENT_KEY = "recentFolders";
  14. var CS_RECENT_LIMIT = 8;
  15. var csRecentFolders = []; //[{path, name, time}], newest first
  16. function loadRecentFolders(callback){
  17. getStorage(CS_RECENT_KEY, function(data){
  18. if (typeof data === "string"){
  19. try {
  20. var stored = JSON.parse(data);
  21. if (Array.isArray(stored)) csRecentFolders = stored;
  22. } catch(e){ /* corrupted list — start over */ }
  23. }
  24. if (callback) callback();
  25. });
  26. }
  27. function getRecentFolders(){ return csRecentFolders; }
  28. function pushRecentFolder(path, name){
  29. if (!path) return;
  30. csRecentFolders = csRecentFolders.filter(function(entry){ return entry.path != path; });
  31. csRecentFolders.unshift({ path: path, name: name || path.split("/").filter(Boolean).pop(), time: Date.now() });
  32. if (csRecentFolders.length > CS_RECENT_LIMIT) csRecentFolders = csRecentFolders.slice(0, CS_RECENT_LIMIT);
  33. setStorage(CS_RECENT_KEY, JSON.stringify(csRecentFolders));
  34. }
  35. //The File menu addresses entries by index so paths never end up inside markup
  36. function openRecentFolder(index){
  37. var entry = csRecentFolders[index];
  38. if (!entry) return;
  39. openProjectFolder([{ filename: entry.name, filepath: entry.path }]);
  40. }
  41. function clearRecentFolders(){
  42. csRecentFolders = [];
  43. setStorage(CS_RECENT_KEY, JSON.stringify(csRecentFolders));
  44. setStatusMessage("checkmark", "Recent project list cleared");
  45. }
  46. //"user:/Code/…/my-website" — keeps the menu narrow for deep paths
  47. function shortenVirtualPath(path){
  48. if (!path) return "";
  49. if (path.length <= 34) return path;
  50. var parts = path.split("/").filter(Boolean);
  51. if (parts.length <= 2) return path;
  52. return parts[0] + "/…/" + parts[parts.length - 1];
  53. }
  54. /* ═══════════════════════════════════════════════════════════════════
  55. Per project workspace state
  56. ═══════════════════════════════════════════════════════════════════ */
  57. var csSessionSaveTimer = null;
  58. var csProjectSessionRestoring = false;
  59. function projectSessionCall(payload, callback){
  60. $.ajax({
  61. url: "../system/ajgi/interface?script=Code Studio/backend/session.agi",
  62. method: "POST",
  63. dataType: "json",
  64. data: payload,
  65. success: function(response){ if (callback) callback(response); },
  66. error: function(){ if (callback) callback({ success: false, error: "workspace state backend unreachable" }); }
  67. });
  68. }
  69. //Tabs of files that live inside the open project, in the order they appear
  70. function projectSessionSnapshot(){
  71. if (!currentProjectFolder) return null;
  72. var root = currentProjectFolder;
  73. if (!root.endsWith("/")) root += "/";
  74. var files = [];
  75. editors.forEach(function(entry){
  76. entry.tabs.forEach(function(tab){
  77. if (!tab.filepath || tab.error || tab.virtual) return;
  78. if (tab.filepath.indexOf(root) !== 0) return;
  79. if (files.indexOf(tab.filepath) === -1) files.push(tab.filepath);
  80. });
  81. });
  82. var focused = getFocusedTabInfo();
  83. var active = (focused && focused.filepath && focused.filepath.indexOf(root) === 0) ?
  84. focused.filepath : null;
  85. return { version: 1, folder: currentProjectFolder, files: files, active: active };
  86. }
  87. //Coalesce the bursts of tab changes that opening or closing files produces
  88. function scheduleProjectSessionSave(){
  89. if (!currentProjectFolder || csProjectSessionRestoring) return;
  90. if (csSessionSaveTimer) clearTimeout(csSessionSaveTimer);
  91. csSessionSaveTimer = setTimeout(saveProjectSession, 800);
  92. }
  93. function saveProjectSession(){
  94. var snapshot = projectSessionSnapshot();
  95. if (snapshot == null) return;
  96. projectSessionCall({
  97. opr: "save",
  98. folder: currentProjectFolder,
  99. data: JSON.stringify(snapshot)
  100. }, function(response){
  101. if (!response.success) csLog("warn", "Could not save the workspace state: " + response.error);
  102. });
  103. }
  104. function restoreProjectSession(folderpath){
  105. projectSessionCall({ opr: "load", folder: folderpath }, function(response){
  106. if (!response.success || !response.session) return;
  107. var session = response.session;
  108. if (!session.files || session.files.length == 0) return;
  109. //Opening files must not immediately write the state back out
  110. csProjectSessionRestoring = true;
  111. session.files.forEach(function(filepath){ openFile(filepath, true); });
  112. //Give the reads time to land, then focus the tab that was active
  113. setTimeout(function(){
  114. if (session.active) focusFileTab(session.active);
  115. csProjectSessionRestoring = false;
  116. }, 600 + session.files.length * 120);
  117. csLog("info", "Restored " + session.files.length + " tab(s) from the last session");
  118. });
  119. }
  120. function forgetProjectSession(){
  121. if (!currentProjectFolder) return;
  122. projectSessionCall({ opr: "clear", folder: currentProjectFolder }, function(response){
  123. if (response.success) setStatusMessage("checkmark", "Saved tabs of this project forgotten");
  124. });
  125. }
  126. //Focus the tab holding a given file, in whichever editor group has it
  127. function focusFileTab(filepath){
  128. for (var i = 0; i < editors.length; i++){
  129. var entry = editors[i];
  130. for (var j = 0; j < entry.tabs.length; j++){
  131. if (entry.tabs[j].filepath == filepath){
  132. focusedEditor = entry.uuid;
  133. changeTab(entry, entry.tabs[j].tabUUID);
  134. return true;
  135. }
  136. }
  137. }
  138. return false;
  139. }