forked from codeitcodes/codeit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.js
288 lines (172 loc) · 5.51 KB
/
files.js
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// create a file
function createFile(dir, sha, name, content, lang,
caretPos, scrollPos, eclipsed) {
return {
dir,
sha,
name,
content,
lang,
caretPos,
scrollPos,
eclipsed
}
}
// selected file
function changeSelectedFile(dir, sha, name, content, lang,
caretPos, scrollPos, eclipsed) {
selectedFile = createFile(dir, sha, name, content, lang,
caretPos, scrollPos, eclipsed);
updateSelectedFileLS();
}
function saveSelectedFileContent() {
selectedFile.content = encodeUnicode(cd.textContent);
updateSelectedFileLS();
}
function saveSelectedFileCaretPos() {
const codeSel = cd.getSelection();
selectedFile.caretPos = [codeSel.start, codeSel.end];
updateSelectedFileLS();
}
function saveSelectedFileScrollPos() {
selectedFile.scrollPos = [cd.scrollLeft, cd.scrollTop];
updateSelectedFileLS();
}
function saveSelectedFileLang() {
selectedFile.lang = cd.lang;
updateSelectedFileLS();
}
// modified files
function addSelectedFileToModFiles() {
modifiedFiles[selectedFile.sha] = selectedFile;
updateModFilesLS();
}
function updateModFileContent(sha, content) {
modifiedFiles[sha].content = content;
updateModFilesLS();
}
function updateModFileCaretPos(sha, caretPos) {
modifiedFiles[sha].caretPos = caretPos;
updateModFilesLS();
}
function updateModFileScrollPos(sha, scrollPos) {
modifiedFiles[sha].scrollPos = scrollPos;
updateModFilesLS();
}
// when Git file is eclipsed (not updated) in browser private cache,
// store the updated file under old sha as key
// and store the updated file under new sha as key
// in modifiedFiles object for 1 minute after commit
function onFileEclipsedInCache(oldSha, newSha, newFile) {
// if file element under old sha exists in HTML,
// update it to the new sha
const fileEl = fileWrapper.querySelector('.file[sha="' + oldSha + '"]');
if (fileEl) setAttr(fileEl, 'sha', newSha);
// search for old eclipsed file
// in modifiedFiles
const oldEclipsedFile = modifiedFiles[oldSha];
// if old eclipsed file
// is modified
if (oldEclipsedFile) {
// update old eclipsed file sha
// to point to new file sha
updateOldEclipsedFile(oldEclipsedFile, newSha);
}
// if old eclipsed file
// is selected
if (selectedFile.sha === oldSha) {
// update old eclipsed file sha
// to point to new file sha
updateOldEclipsedFile(selectedFile, newSha);
updateSelectedFileLS();
// if old eclipsed file
// is modified
if (oldEclipsedFile) {
// update old file content
// to selected file content
oldEclipsedFile.content = selectedFile.content;
}
}
function updateOldEclipsedFile(oldEclipsedFile, newSha) {
// update old eclipsed file sha to point to new file sha
oldEclipsedFile.sha = newSha;
// set old file to eclipsed mode
oldEclipsedFile.eclipsed = true;
}
// if old eclipsed file is modified
if (oldEclipsedFile) {
// store the modified file under new sha as key
modifiedFiles[newSha] = oldEclipsedFile;
} else if (selectedFile.sha === newSha) {
// if old eclipsed file is selected
// store the selected file under new sha as key
modifiedFiles[newSha] = selectedFile;
}
// update modified files in local storage
updateModFilesLS();
// set 1 minute timeout to remove eclipsed files
window.setTimeout(() => {
// if old eclipsed file exists
if (modifiedFiles[oldSha]) {
// delete the old eclipsed file
// from modifiedFiles
deleteModFile(oldSha);
}
// if not edited eclipsed file under new sha as key
// while in timeout (file is still eclipsed)
if (modifiedFiles[newSha] &&
modifiedFiles[newSha].eclipsed) {
// remove the eclipsed file under new sha as key
// from modifiedFiles
deleteModFile(newSha);
}
}, 65 * 1000); // 65s
}
function setTimeoutForEclipsedFiles() {
const eclipsedFiles = Object.entries(modifiedFiles).filter(file => file[1].eclipsed);
// run on all eclipsed files
eclipsedFiles.forEach(file => {
// set 1 minute timeout to remove eclipsed file
window.setTimeout(() => {
// if not edited eclipsed file
// while in timeout (file is still eclipsed)
if (modifiedFiles[file[0]] &&
modifiedFiles[file[0]].eclipsed) {
// remove the eclipsed file
// from modifiedFiles
deleteModFile(file[0]);
}
}, 65 * 1000); // 65s
});
}
function deleteModFile(fileSha) {
delete modifiedFiles[fileSha];
updateModFilesLS();
}
// follow breadcrumb trail of file versions until
// reached latest version
function getLatestVersion(item) {
function followTrail(crumb) {
if (modifiedFiles[crumb]) {
// if version sha matches its key
// (it dosen't point to another version)
if (modifiedFiles[crumb].sha === crumb) {
// reached the most recent version
return modifiedFiles[crumb];
} else {
// version sha points to another version,
// follow the trail
return followTrail(modifiedFiles[crumb].sha);
}
} else {
return item;
}
}
// if item is in modifiedFiles object
if (modifiedFiles[item.sha]) {
// get latest version
return followTrail(item.sha);
} else {
return item;
}
}