@@ -30,6 +30,10 @@ The point of this code here is ensure that these objects stay in sync properly.
3030
3131import { List , Map } from "immutable" ;
3232import { close } from "@cocalc/util/misc" ;
33+ import {
34+ local_storage ,
35+ local_storage_delete ,
36+ } from "@cocalc/frontend/editor-local-storage" ;
3337import { ProjectActions } from "../project_actions" ;
3438import { ProjectStore } from "../project_store" ;
3539
@@ -38,6 +42,7 @@ type OpenFilesOrderType = List<string>;
3842type ClosedFilesType = List < string > ;
3943
4044const MAX_JUST_CLOSED_FILES = 50 ;
45+ const JUST_CLOSED_STORAGE_FILE = "__just_closed_files__" ;
4146
4247export class OpenFiles {
4348 private actions : ProjectActions ;
@@ -48,6 +53,7 @@ export class OpenFiles {
4853 const store = actions . get_store ( ) ;
4954 if ( store == null ) throw Error ( "store must be defined" ) ;
5055 this . store = store ;
56+ this . restoreClosedFiles ( ) ;
5157 }
5258
5359 public close ( ) : void {
@@ -64,10 +70,46 @@ export class OpenFiles {
6470 if ( open_files_order != null ) x . open_files_order = open_files_order ;
6571 if ( just_closed_files != null ) {
6672 x . just_closed_files = just_closed_files ;
73+ this . persistClosedFiles ( just_closed_files ) ;
6774 }
6875 this . actions . setState ( x ) ;
6976 }
7077
78+ private restoreClosedFiles ( ) : void {
79+ const current = this . store . get ( "just_closed_files" ) ;
80+ if ( current != null && current . size > 0 ) return ;
81+ const stored = local_storage (
82+ this . actions . project_id ,
83+ JUST_CLOSED_STORAGE_FILE ,
84+ "list" ,
85+ ) ;
86+ if ( ! Array . isArray ( stored ) || stored . length == 0 ) return ;
87+ const list = List ( stored . filter ( ( path ) => typeof path === "string" ) ) . slice (
88+ - MAX_JUST_CLOSED_FILES ,
89+ ) ;
90+ if ( list . size == 0 ) return ;
91+ this . actions . setState ( { just_closed_files : list } ) ;
92+ }
93+
94+ private persistClosedFiles ( just_closed_files : ClosedFilesType ) : void {
95+ const stored = just_closed_files . toArray ( ) ;
96+
97+ if ( stored . length == 0 ) {
98+ local_storage_delete (
99+ this . actions . project_id ,
100+ JUST_CLOSED_STORAGE_FILE ,
101+ "list" ,
102+ ) ;
103+ return ;
104+ }
105+ local_storage (
106+ this . actions . project_id ,
107+ JUST_CLOSED_STORAGE_FILE ,
108+ "list" ,
109+ stored ,
110+ ) ;
111+ }
112+
71113 public close_all ( ) : void {
72114 this . setState ( Map ( { } ) , List ( [ ] ) ) ;
73115 }
@@ -129,6 +171,10 @@ export class OpenFiles {
129171 }
130172 }
131173
174+ public set_closed_files ( just_closed_files : ClosedFilesType ) : void {
175+ this . setState ( undefined , undefined , just_closed_files ) ;
176+ }
177+
132178 public get ( path : string , key : string ) : any {
133179 return this . store . getIn ( [ "open_files" , path , key ] ) ;
134180 }
0 commit comments