-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
445 lines (411 loc) · 12 KB
/
index.html
File metadata and controls
445 lines (411 loc) · 12 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<html>
<head>
<script src="./d3.v7.min.js"></script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="https://repofiles.com" />
<meta name="twitter:title" content="repofiles.com" />
<meta name="twitter:description" content="View the file structure of a GitHub repo." />
<meta name="twitter:image" content="https://repofiles.com/social.jpg" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://repofiles.com" />
<meta property="og:title" content="repofiles.com" />
<meta property="og:description" content="View the file structure of a GitHub R=repo." />
<meta property="og:image" content="/social.jpg" />
<style>
body {
font-family: SFMono-Regular, monospace;
padding: 0px;
margin: 0px;
background-color: white;
}
#repo {
position: fixed;
top: 170px;
}
#menu {
position: fixed;
top: 0px;
left: 0px;
right: 0px;
height: 170px;
margin-bottom: 20px;
margin-left: 20px;
margin-left: auto;
margin-right: auto;
text-align: center;
width: 100%;
max-width: 700px;
}
h1 {
text-align: center;
font-size: 1.75em;
color: #47b7f8;
}
#status {
margin-top: 8px;
color: #444;
}
#repoInput {
text-align: left;
font-size: 1em;
font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
padding: 9px 7px;
margin: 0px 0;
width: 100%;
margin-left: auto;
margin-right: auto;
padding-right: 4em;
box-sizing: border-box;
border: 1px solid #bbb;
outline: none;
text-align: center;
background-color: #fff;
filter: drop-shadow( 0px 2px 2px rgba(150, 150, 150, 0.2));
}
#repoButton {
display: inline-block;
font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
padding: 9.5px 14px 9.5px;
font-size: 1em;
cursor: pointer;
text-align: center;
text-decoration: none;
outline: none;
background-color: #bbb;
border: 0px solid #444;
font-weight:600;
filter: drop-shadow( 0px 2px 2px rgba(150, 150, 150, 0.2));
margin-top: 0px !important;
position: absolute;
top: 0px;
right: 0px;
width: 4em;
}
div {
text-align: center;
color: #ddd;
}
svg {
text-align: center;
margin-left: auto;
margin-right: auto;
color: #ddd;
max-width: 100%;
height: auto;
display: block;
background: white;
cursor: pointer;
}
text {
font-family: SFMono-Regular,monospace;
font-size: 12px;
}
#inputs {
position: relative;
width: 90%;
max-width: 700px;
margin-left: auto;
margin-right: auto;
}
#made {
position: fixed;
bottom: 15px;
width: 500px;
left: 15px;
text-align: left;
color: black;
font-size: 0.8em;
}
#help {
position: fixed;
bottom: 15px;
width: 500px;
right: 15px;
text-align: right;
color: black;
font-size: 0.8em;
}
a {
color: #444;
}
</style>
</head>
<body>
<div id="menu">
<h1>repofiles</h1>
<div id=inputs>
<input id="repoInput" type="text" placeholder="enter a github repo url"></input>
<button id="repoButton">go</button>
</div>
<div id="status"></div>
</div>
<div id="repo"></div>
<div id="made">
Made by <a href="https://benjdd.com">Ben</a><br/>
Powered by <a href="https://d3js.org/">D3</a> <br/>
and <a href="https://docs.github.com/en/rest">GitHub</a><br/>
</div>
<div id="help">
Click to zoom<br/>
SHIFT-click to slow zoom<br/>
ALT-click to view file <br/>
Diameter → file size<br/>
</div>
</body>
<script>
let currentRepo = {};
function setStatus(text) {
document.getElementById('status').textContent = text;
}
async function fetchGitHub(url, after) {
const options = {
method: "GET",
headers: {
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28"
}
};
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
if ('message' in data) {
if (data.message && data.message.startsWith('API rate limit exceeded')) {
setStatus('GitHub API rate limit exceeded. Wait, or try from another device.', );
} else {
setStatus('An unknown error occurred. You can @BenjDicken on twitter.');
}
} else {
after(data);
}
} catch (error) {
console.error("Error fetching the latest commit:", error);
setStatus('An unknown error occurred. You can @BenjDicken on twitter.');
}
}
async function loadRepoTree(owner, repo, sha, defaultBranch) {
fetchGitHub(`https://api.github.com/repos/${owner}/${repo}/git/trees/${sha}?recursive=1`, (data) => {
currentRepo.tree = postProcessData(data.tree);
currentRepo.owner = owner;
currentRepo.repo = repo;
currentRepo.defaultBranch = defaultBranch;
draw(currentRepo.tree);
setStatus(`loaded file visualization for ${owner}/${repo}`);
});
}
async function loadRepoDefaultBranchAndTree(owner, repo, sha) {
fetchGitHub(`https://api.github.com/repos/${owner}/${repo}`, (data) => {
const defaultBranch = data['default_branch'];
loadRepoTree(owner, repo, sha, defaultBranch);
});
}
async function loadRepo(owner, repo) {
fetchGitHub(`https://api.github.com/repos/${owner}/${repo}/commits`, (data) => {
const latestCommit = data[0];
loadRepoDefaultBranchAndTree(owner, repo, latestCommit.sha);
});
}
function getElementWithName(climb, name) {
for (const e of climb.children) {
if (e.name == name) return e;
}
return undefined;
}
function postProcessData(data) {
const tree = {name: 'flare', children: []};
for (const row of data) {
const split = row.path.split('/');
let climb = tree;
for (let i = 0; i < split.length; i++) {
const component = split[i];
const element = getElementWithName(climb, component);
if (element == undefined) {
if (i == split.length - 1) {
const branch = {name: component, value: row.size, children: []};
climb.children.push(branch);
} else {
const branch = {name: component, children: []};
climb.children.push(branch);
climb = branch;
}
} else {
climb = element;
}
}
}
return tree;
}
function draw(data) {
let width = window.innerWidth;
let height = window.innerHeight - 170;
let wh = Math.min(width, height);
const svg = d3.select("#repo")
.append("svg")
.attr("viewBox", `-${width / 2} -${height / 2} ${width} ${height}`)
.attr("width", width)
.attr("height", height);
const color = d3.scaleLinear()
.domain([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
.range([
"#fff",
"#47b7f8",
"#40d763",
"#a18bf5",
"#fd812d",
"#f2b600",
"#ff7082",
"#40d763",
"#47b7f8",
"#a18bf5",
"#fd812d",
"#f2b600",
"#ff7082",
])
const pack = data => d3.pack()
.size([width, height])
.padding(2)
(d3.hierarchy(data)
.sum(d => d.value)
.sort((a, b) => b.value - a.value));
const root = pack(data);
function getGitHubFileURL(d) {
let url = `https://github.com/${currentRepo.owner}/${currentRepo.repo}/tree/${currentRepo.defaultBranch}`;
let path = '';
while (d.parent != null) {
path = '/' + d.data.name + path;
d = d.parent;
}
return encodeURI(url + path);
}
const ng = svg.append("g");
const node = ng
.selectAll("circle")
.data(root.descendants().slice(1))
.join("circle")
.attr("fill", d => d.children ? color(d.depth) : "#ee7")
.on("mouseover", function() { d3.select(this).style("filter", "brightness(110%)"); })
.on("mouseout", function() { d3.select(this).style("filter", "brightness(100%)"); })
.on("click", (event, d) => {
if (event.altKey) {
const url = getGitHubFileURL(d);
window.open(url, '_blank') .focus();
}
if(focus !== d) {
zoom(event, d);
event.stopPropagation();
}
});
function getFontSize(d, e) {
return (d.r * 1.5) / d.data.name.length + 'px'
}
const label = ng.append("g")
.style("font", "10px sans-serif")
.attr("pointer-events", "none")
.attr("text-anchor", "middle")
.selectAll("text")
.data(root.descendants())
.join("text")
.style('font-size', d => getFontSize(d, root))
.style("display", d => d.parent === root ? "inline" : "none")
.style("opacity", d => d.parent === root ? 0.75 : 0)
.style('font-weight', 'bold')
.style('dominant-baseline', 'middle')
.text(d => d.data.name);
svg.on("click", (event) => zoom(event, root));
let focus = root;
let view;
zoomTo([focus.x, focus.y, focus.r * 2]);
function zoomTo(v) {
const k = wh / v[2];
view = v;
label.attr("transform", d => `translate(${(d.x - v[0]) * k},${(d.y - v[1]) * k})`);
node.attr("transform", d => `translate(${(d.x - v[0]) * k},${(d.y - v[1]) * k})`);
node.attr("r", d => d.r * k);
}
function zoomToEfficient(v) {
const k = wh / v[2];
view = v;
const x = (-v[0] + width/2) * k;
const y = (-v[1] + height/2) * k;
ng.attr('transform', `translate(${x} ${y}) scale(${k})`)
}
function zoom(event, d) {
const focus0 = focus;
focus = d;
const transition = svg.transition()
.duration(event.shiftKey ? 5000 : 1000)
.tween("zoom", d => {
const i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2]);
return t => zoomToEfficient(i(t));
});
label
.filter(function(d) { return d.parent === focus || d === focus || this.style.display === "inline"; })
.transition(transition)
.style('font-size', d => getFontSize(d, focus))
.style("opacity", function(d) { if (d.parent === focus || (d === focus && !d.children)) return 0.75; return 0; })
.on("start", function(d) { if (d.parent === focus || (d === focus && !d.children)) this.style.display = "inline"; })
.on("end", function(d) { if (!(d.parent === focus || (d === focus && !d.children))) this.style.display = "none"; })
}
}
function getRepoInfoFromURL() {
const value = document.getElementById('repoInput').value;
try {
const params = new URLSearchParams(window.location.search);
const owner = params.get('owner');
const repo = params.get('repo');
if (owner && repo) {
fetchAndDisplay(owner, repo);
return true;
}
} catch (_) {
// do nothing on purpose here, just return false
}
return false;
}
function setURLParams(owner, repo) {
const toEditURL = new URL(window.location);
toEditURL.searchParams.set('owner', owner);
toEditURL.searchParams.set('repo', repo);
window.history.replaceState(null, null, toEditURL);
}
function fetchAndDisplay(owner, repo) {
setURLParams(owner, repo);
document.getElementById('repo').innerHTML = '';
setStatus(`loading ${owner}/${repo}...`);
loadRepo(owner, repo);
}
function go() {
const url = document.getElementById('repoInput').value;
const trimmedUrl = url.replace(/\/+$/, "");
const split = trimmedUrl.split('/')
const l = split.length;
repo = split[l-1];
owner = split[l-2];
fetchAndDisplay(owner, repo);
}
window.onresize = () => {
document.getElementById('repo').innerHTML = '';
draw(currentRepo.tree);
};
document.getElementById('repoButton').addEventListener('click', () => go());
document.getElementById("repoInput").addEventListener("keydown", function(event) {
if (event.key === "Enter") {
go();
}
});
if (!getRepoInfoFromURL()) {
// If no repo specified in URL,
// Initially just load the json file from the server for neovim
d3.json("/cache/neovim_neovim.json")
.then(function(data){
currentRepo = data;
draw(currentRepo.tree);
setStatus(`loaded file visualization for neovim/neovim`);
});
}
</script>
</html>