-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.js
More file actions
182 lines (145 loc) · 5.06 KB
/
logic.js
File metadata and controls
182 lines (145 loc) · 5.06 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
var params = new URLSearchParams(document.location.search);
const user = "crazy1112345"; // github link info
const repo = "RMC14Paperwork";
const branch = "main";
var dir; // Scrapped directory json file
var textbox; // <p> with contenteditable
var tree; // TreeView
var file_name = "document.txt";
const elements = { // Tags
"[bold]": "<strong>[bold]",
"[italic]": "<em>[italic]",
"[bolditalic]": "<strong><em>[bolditalic]",
"[/bold]": "[/bold]</strong>",
"[/italic]": "[/italic]</em>",
"[/bolditalic]": "[/bolditalic]</em></strong>",
"[head]": "<h3>[head]",
"[head=1]": "<h3>[head=1]",
"[head=2]": "<h3>[head=2]",
"[head=3]": "<h3>[head=3]",
"[/head]": "[/head]</h3>",
"[/color]": "[/color]</span>",
}
document.addEventListener('DOMContentLoaded', function()
{
textbox = document.getElementById("text");
textbox.addEventListener("focusout", rerender_text)
tree = document.getElementById("tree");
fetch_directory(`https://api.github.com/repos/${user}/${repo}/git/trees/${branch}?recursive=1`);
if (params.has('file'))
{
file_name = params.get('file').slice(params.get('file').lastIndexOf('/')+1)
fetch_file(`https://api.github.com/repos/${user}/${repo}/contents/${params.get('file')}`)
}
}, false);
// Fetches json file with every file in specified repo
async function fetch_directory(url)
{
const response = await fetch(url);
dir = await response.json();
var tree_links = {"": tree}
for (const i of dir.tree)
{
// Gets array with directory path and file name.
const path = [i.path.split('/').slice(0, -1).join('/'), i.path.split('/').slice(-1)[0]];
if (path[1].startsWith('Placeholder'))
continue;
const tree_item = document.createElement("li");
const tree_item_span = document.createElement("span");
tree_item_span.innerText = path[1];
if (i.type == "tree") // Directory
{
tree_item_span.classList.add("caret");
tree_item_span.addEventListener("click", function() {
this.parentElement.querySelector(".nested").classList.toggle("active");
this.classList.toggle("caret-down");
});
}
else // File
{
tree_item_span.addEventListener("click", function() {
params.set('file', i.path);
history.pushState('', '', '?' + params.toString());
file_name = i.path.split('/').at(-1);
fetch_file(i.url);
});
}
tree_item.appendChild(tree_item_span);
const tree_item_ul = document.createElement("ul");
tree_item_ul.classList.add("nested");
tree_item.appendChild(tree_item_ul);
if (i.type == "tree")
{
if (path[0] === "")
tree_links[path[1]] = tree_item_ul;
else
tree_links[path.join('/')] = tree_item_ul;
}
tree_links[path[0]].appendChild(tree_item);
}
}
// Fetches file json with b64 encoded text
async function fetch_file(url)
{
const response = await fetch(url);
const file = await response.json();
console.log(url);
text.innerHTML = parse_text(decode_text(file.content));
}
// Transforms SS14 papercode tags into html tags
function parse_text(file_content)
{
var content = "";
const tokens = `${file_content}`.split(/(\[\/?[a-z=0-9#]+\])/i).filter(Boolean); // stolen from yagwog, thanks <3
for (const i in tokens)
{
if (tokens[i] in elements)
content += elements[tokens[i]];
else if (tokens[i].startsWith("[color="))
content += `<span style="color: ${tokens[i].split('=')[1].slice(0, -1)};">${tokens[i]}`
else
content += tokens[i];
}
content += "<br/>"
return content;
}
function rerender_text()
{
textbox.innerHTML = parse_text(textbox.textContent);
}
// Decodes b64 to UTF-8
function decode_text(encoded)
{
// atob only can decode into ASCII, github encodes text into UTF-8.
// Taken from https://stackoverflow.com/a/64752311
const text = atob(encoded);
const length = text.length;
const bytes = new Uint8Array(length);
for (let i = 0; i < length; i++) {
bytes[i] = text.charCodeAt(i);
}
const decoder = new TextDecoder(); // default is utf-8
return decoder.decode(bytes);
}
// Adds/Removes autolabel
function toggle_label()
{
if (textbox.textContent[0] == '#')
textbox.innerHTML = textbox.innerHTML.slice(textbox.innerHTML.indexOf('\n')+1);
else
textbox.innerHTML = `# ${file_name.replace('.txt', '')}\n` + textbox.innerHTML
}
function copy()
{
navigator.clipboard.writeText(textbox.textContent);
console.log("Copied the text!");
}
function save()
{
// TODO, https://developer.mozilla.org/en-US/docs/Web/API/Window/showSaveFilePicker#browser_compatibility
const blob = new Blob([textbox.textContent], {type:'text/plain'});
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = file_name;
a.click();
}