-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
287 lines (252 loc) · 8.28 KB
/
main.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
console.log("Hello from vcstool repos shortcut extension");
const SSH_PATTERN = /^git@([\w.-]+):([\w.-]+)\/([\w.-]+)\.git$/;
const COMMIT_HASH_PATTERN = /^[0-9a-f]{40}$/;
const CODE_FILE_CLASS = "Box-sc-g0xbh4-0 react-code-file-contents";
const CODE_LINES_CLASS = "react-code-lines";
const FILE_LINE_CLASS = "react-file-line";
const TEXTAREA_ID = "read-only-cursor-text-area";
const BUTTON_CLASS = "open-repo-button";
let storedRepositories = {};
// Convert SSH URL to HTTP URL
const convertSshToHttp = (sshUrl) => {
const match = sshUrl.match(SSH_PATTERN);
if (match) {
const [_, domain, username, repository] = match;
return `https://${domain}/${username}/${repository}.git`;
} else {
console.log("Invalid SSH URL format:", sshUrl);
return null;
}
};
// Function to get the value of a field in a line of text
const getFieldValue = (text, prefix) => {
if (text.startsWith(prefix)) {
return text.slice(prefix.length).split("#")[0].trim();
}
return null;
};
// Function to apply version logic to repository object
const applyVersionLogic = (repo) => {
if (repo.type && repo.type.includes("git") && repo.url) {
let baseUrl = repo.url;
if (baseUrl.endsWith(".git")) {
baseUrl = baseUrl.slice(0, -4);
}
if (repo.version) {
if (COMMIT_HASH_PATTERN.test(repo.version)) {
repo.url = baseUrl + "/blob/" + repo.version;
} else {
repo.url = baseUrl + "/tree/" + repo.version;
}
} else {
repo.url = baseUrl;
}
}
};
const parseRepositoryData = (repositories, codeLines) => {
if (!codeLines) {
console.error("No code lines found");
return null;
}
let currentBlock = [];
const processBlock = (blockLines) => {
if (blockLines.length === 0) return;
const repoKeyLine = blockLines[0].innerText.trim();
const repoName = repoKeyLine.split(":")[0].trim();
if (repositories[repoName]) return;
const repo = { name: repoName };
console.debug("Processing repository:", repoName);
blockLines.slice(1).forEach((line) => {
const text = line.innerText.trim();
if (!text || text.startsWith("#")) return;
let value;
if ((value = getFieldValue(text, "type:"))) {
repo.type = value;
} else if ((value = getFieldValue(text, "url:"))) {
if (!value.startsWith("https://") && value.startsWith("git@")) {
value = convertSshToHttp(value);
if (!value) return;
}
repo.url = value;
} else if ((value = getFieldValue(text, "version:"))) {
repo.version = value;
}
});
applyVersionLogic(repo);
repo.key = blockLines[0].id;
if (repo.url && repo.type && repo.version && repo.key) {
repositories[repoName] = repo;
}
};
Array.from(codeLines).forEach((codeLine) => {
const lineText = codeLine.innerText.trim();
if (/^[\w.\-\/_]+:\s*(#.*)?$/.test(lineText)) {
if (codeLine.id === "LC1" && lineText === "repositories:") return;
if (currentBlock.length > 0) processBlock(currentBlock);
currentBlock = [codeLine];
} else {
if (currentBlock.length > 0) currentBlock.push(codeLine);
}
});
if (currentBlock.length > 0) processBlock(currentBlock);
return Object.keys(repositories).length > 0 ? repositories : null;
};
// Function to get the position of the text area
const getTextareaRect = () => {
const readOnlyTextArea = document.getElementById(TEXTAREA_ID);
if (!readOnlyTextArea) {
console.error("Textarea not found");
return null;
}
return readOnlyTextArea.getBoundingClientRect();
};
const createRepoButton = (repo, top, left) => {
const link = document.createElement("a");
link.href = repo.url;
link.style.position = "absolute";
link.style.zIndex = "999";
link.style.top = `${top}px`;
link.style.left = `${left}px`;
const button = document.createElement("button");
button.className = BUTTON_CLASS;
button.innerHTML = "Open";
link.appendChild(button);
document.body.appendChild(link);
};
const displayRepoButtons = (repositories, codeLinesElement) => {
if (!repositories) {
console.error("No repository data available");
return;
}
const textareaRect = getTextareaRect();
if (!textareaRect) {
console.error("Failed to get textarea rect");
return;
}
Object.values(repositories).forEach((repo) => {
const { name, key, url, type, version } = repo;
if (name === "repositories" && key === "LC1") return; // Skip the repositories: line
if (!url || !type) {
console.warn(
`Incomplete repository data for key: ${key}, name: ${name}, url: ${url}, type: ${type}, version: ${version}`
);
return;
}
if (!type.includes("git")) {
console.warn("Repository type is not git");
return;
}
const codeLineElement = codeLinesElement.querySelector(`#${key}`);
if (!codeLineElement) {
console.error(`Code line element not found for key: ${key}`);
return;
}
const rect = codeLineElement.getBoundingClientRect();
createRepoButton(repo, rect.top + window.scrollY, textareaRect.left - 50);
});
};
const removeRepoButtons = () => {
const buttons = document.querySelectorAll(`.${BUTTON_CLASS}`);
buttons.forEach((button) => button.remove());
};
const getElementByClass = (className) => {
const element = document.getElementsByClassName(className)[0];
if (!element) {
console.error(`Element with class ${className} not found`);
return null;
}
return element;
};
const updateRepoButtons = (codeLinesElement) => {
const codeLines = codeLinesElement.getElementsByClassName(FILE_LINE_CLASS);
parseRepositoryData(storedRepositories, codeLines);
removeRepoButtons();
displayRepoButtons(storedRepositories, codeLinesElement);
};
const registerEventListeners = (codeLinesElement) => {
window.addEventListener("resize", () => {
updateRepoButtons(codeLinesElement);
});
let scrollDebounce;
window.addEventListener("scroll", () => {
clearTimeout(scrollDebounce);
scrollDebounce = setTimeout(() => {
updateRepoButtons(codeLinesElement);
}, 100);
});
};
const init = () => {
try {
const codeFileContentsElement = getElementByClass(CODE_FILE_CLASS);
if (!codeFileContentsElement) return;
const codeLinesElement =
codeFileContentsElement.getElementsByClassName(CODE_LINES_CLASS)[0];
if (!codeLinesElement) {
console.error("Code lines element not found");
return;
}
updateRepoButtons(codeLinesElement);
registerEventListeners(codeLinesElement);
} catch (error) {
console.error("An error occurred:", error);
}
};
const findFilenameElement = () => {
const fileNameElement = document.getElementById("file-name-id");
const wideFileNameElement = document.getElementById("file-name-id-wide");
if (!fileNameElement && !wideFileNameElement) {
return null;
}
return fileNameElement || wideFileNameElement;
};
const getCurrentFilename = () => {
const element = findFilenameElement();
return element ? element.textContent || "" : "";
};
const isReposFilename = (filename) => filename.includes(".repos");
let filenameDebounce;
const handleFilenameChange = (newFilename) => {
if (!newFilename) {
removeRepoButtons();
previousFilename = "";
return;
}
const previouslyRepos = isReposFilename(previousFilename);
const currentlyRepos = isReposFilename(newFilename);
if (
currentlyRepos &&
(!previousFilename || !previouslyRepos || previousFilename !== newFilename)
) {
storedRepositories = {};
removeRepoButtons();
clearTimeout(filenameDebounce);
filenameDebounce = setTimeout(() => {
init();
}, 500);
console.debug("Filename changed to include .repos");
} else if (previouslyRepos && !currentlyRepos) {
console.debug("Filename changed to exclude .repos");
removeRepoButtons();
storedRepositories = {};
}
previousFilename = newFilename;
};
let previousFilename = "";
const observeFilenameChanges = () => {
const observer = new MutationObserver(() => {
const updatedFilename = getCurrentFilename();
handleFilenameChange(updatedFilename);
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
// Initial check
const initialFilename = getCurrentFilename();
if (isReposFilename(initialFilename)) {
removeRepoButtons();
init();
}
previousFilename = initialFilename;
};
observeFilenameChanges();