This commit is contained in:
puck 2024-03-15 23:03:22 +00:00
parent 6a24594030
commit 30de2412c2

78
script.js Normal file
View file

@ -0,0 +1,78 @@
(async () => {
const data = await (await fetch('https://git.lix.systems/puck/testfudgethingy/raw/branch/main/database.json')).json();
document.querySelector('.code-view').addEventListener('click', (e) => {
let clicky = e.target;
let isLine = false;
if (clicky.classList.contains('code-inner')) {
isLine = true;
} else if (!clicky.parentNode.classList.contains('code-inner')) {
return;
}
// TODO: non-ASCII support
let index = 0;
if (isLine) { // mehhhh
index = ((e.clientX - clicky.getBoundingClientRect().x) / clicky.getBoundingClientRect().width) * clicky.textContent.length;
} else {
for (let node of Array.from(clicky.parentNode.children)) {
if (node !== clicky) {
index += node.textContent.length;
} else {
break;
}
}
index += ((e.clientX - clicky.getBoundingClientRect().x) / clicky.getBoundingClientRect().width) * clicky.textContent.length;
}
index = Math.floor(index);
let line = parseInt((isLine ? clicky : clicky.parentNode).parentNode.attributes.rel.nodeValue.substring(1), 10);
let file = window.config.pageData.branchDropdownDataList[0].treePath;
console.log(`clicked line ${line} char ${index}`);
let symbol;
for (let doc of data.documents) {
if (doc.relativePath !== file) {
continue;
}
console.log('found document');
for (let occurrence of doc.occurrences) {
let startLine = occurrence.range[0] + 1;
let startChar = occurrence.range[1];
let endLine = (occurrence.range.length === 3 ? occurrence.range[0] : occurrence.range[2]) + 1;
let endChar = occurrence.range.length === 3 ? occurrence.range[2] : occurrence.range[3];
if (startLine > line || endLine < line)
continue;
if (startChar > index || endChar < index)
continue;
symbol = occurrence.symbol;
}
}
if (!symbol) return;
for (let doc of data.documents) {
for (let occurrence of doc.occurrences) {
if ((occurrence.symbolRoles & 1) !== 1)
continue;
if (occurrence.symbol !== symbol)
continue;
let data = window.config.pageData.branchDropdownDataList[0];
window.location = `${data.repoLink}/src/${data.branchNameSubURL}/${doc.relativePath}#L${occurrence.range[0] + 1}`;
}
}
}, { capture: true });
})();