-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.js
More file actions
185 lines (155 loc) · 4.05 KB
/
state.js
File metadata and controls
185 lines (155 loc) · 4.05 KB
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
import { syncfs } from "./file";
import { get as getOption } from "./options";
import { history, setDoScroll } from "./prompt";
import {
clear as clearWindow,
create as createWindow,
get as getWindow,
getUIState,
position as windowPosition
} from "./window";
import {
apply as applyStyle,
color,
restore as restoreStyle
} from "./style";
let autosaveFilename = "";
/**
* Read the UI state from the filesystem.
*/
function readUIState() {
try {
const state = FS.readFile(
autosaveFilename + "_uidata",
{ encoding: "utf8" }
);
return JSON.parse( state );
}
catch( e ) {
return null;
}
}
export const autosave = {
/**
* Delete the autosave files.
*/
remove: function() {
try {
FS.unlink( autosaveFilename );
}
catch( e ) {
// do nothing
}
try {
FS.unlink( autosaveFilename + "_uidata" );
}
catch( e ) {
// do nothing
}
syncfs( false );
},
/**
* Pass the autosave's filename to the engine that takes care of
* reloading the save.
*/
restore: function() {
try {
// Try to open the autosave file.
// If it doesn't exist, this throws an error.
FS.stat( autosaveFilename );
Module.ccall(
"hugojs_set_autosave_filename",
"null",
[ "string" ],
[ autosaveFilename ]
);
}
catch( e ) {
// autosave file doesn't exist, do nothing
}
},
save: function() {
if( !getOption( "autosave" ) ) {
return;
}
// trigger engine autosave
const engineSaveSucceeded = Module.ccall(
"haven_save_autosave",
"int",
[ "string" ],
[ autosaveFilename ]
);
// save UI state
if( engineSaveSucceeded ) {
FS.writeFile(
autosaveFilename + "_uidata",
JSON.stringify( getUIState() ),
{ encoding: "utf8" }
);
}
},
/**
* Remember the autosave's filename
* @param filename
*/
setName: function( filename ) {
autosaveFilename = filename;
}
};
/**
* Restore saved UI state.
*/
export function restoreUI() {
const savedState = readUIState();
let windowCount;
if( !savedState ) {
return;
}
// if windowing has been set off in options, restore only the main window
if( getOption( "windowing" ) ) {
windowCount = savedState.windowContents.length;
}
else {
windowCount = 1;
}
clearWindow();
for( let i = 0; i < windowCount; ++i ) {
createWindow(
i,
savedState.windowDimensions[ i ].left,
savedState.windowDimensions[ i ].top,
savedState.windowDimensions[ i ].right,
savedState.windowDimensions[ i ].bottom
);
}
color.restore( savedState.currentColors );
restoreStyle( savedState.font );
windowPosition.restore( savedState.position );
if( savedState.title ) {
document.title = savedState.title;
}
for( let i = 0; i < savedState.windowContents.length; ++i ) {
getWindow( i ).innerHTML = savedState.windowContents[ i ];
applyStyle( getWindow( i ), i );
}
applyStyle( document.body, 0 );
// TODO: only for Hugo!
// set the same style we had when saving
Module.ccall(
"hugojs_set_font",
"null",
[ "int" ],
[ savedState.font[ 0 ].original ]
);
Module.ccall(
"hugojs_set_colors",
"null",
[ "int", "int" ],
[ savedState.currentColors[ 0 ].text, savedState.currentColors[ 0 ].background ]
);
// restore command history
history.set( savedState.cmdHistory || [] );
// scroll to the bottom
window.scrollTo( 0, ( document.scrollingElement || document.body ).scrollHeight );
setDoScroll();
}