2022-01-28 21:00:11 +00:00
|
|
|
import $ from 'jquery';
|
2024-07-07 15:32:30 +00:00
|
|
|
import {svg} from '../svg.ts';
|
|
|
|
import {invertFileFolding} from './file-fold.ts';
|
|
|
|
import {createTippy} from '../modules/tippy.ts';
|
2023-04-02 09:25:36 +00:00
|
|
|
import {clippie} from 'clippie';
|
2024-07-07 15:32:30 +00:00
|
|
|
import {toAbsoluteUrl} from '../utils.ts';
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2022-11-11 10:22:36 +00:00
|
|
|
export const singleAnchorRegex = /^#(L|n)([1-9][0-9]*)$/;
|
|
|
|
export const rangeAnchorRegex = /^#(L[1-9][0-9]*)-(L[1-9][0-9]*)$/;
|
2022-11-04 19:33:50 +00:00
|
|
|
|
2021-10-16 17:28:04 +00:00
|
|
|
function changeHash(hash) {
|
|
|
|
if (window.history.pushState) {
|
|
|
|
window.history.pushState(null, null, hash);
|
|
|
|
} else {
|
|
|
|
window.location.hash = hash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-24 12:14:03 +00:00
|
|
|
function isBlame() {
|
|
|
|
return Boolean(document.querySelector('div.blame'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLineEls() {
|
|
|
|
return document.querySelectorAll(`.code-view td.lines-code${isBlame() ? '.blame-code' : ''}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectRange($linesEls, $selectionEndEl, $selectionStartEls) {
|
2024-03-29 17:08:54 +00:00
|
|
|
for (const el of $linesEls) {
|
|
|
|
el.closest('tr').classList.remove('active');
|
|
|
|
}
|
2021-10-16 17:28:04 +00:00
|
|
|
|
|
|
|
// add hashchange to permalink
|
2024-03-25 23:03:12 +00:00
|
|
|
const refInNewIssue = document.querySelector('a.ref-in-new-issue');
|
|
|
|
const copyPermalink = document.querySelector('a.copy-line-permalink');
|
|
|
|
const viewGitBlame = document.querySelector('a.view_git_blame');
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2022-02-12 05:00:24 +00:00
|
|
|
const updateIssueHref = function (anchor) {
|
2024-03-25 23:03:12 +00:00
|
|
|
if (!refInNewIssue) return;
|
|
|
|
const urlIssueNew = refInNewIssue.getAttribute('data-url-issue-new');
|
|
|
|
const urlParamBodyLink = refInNewIssue.getAttribute('data-url-param-body-link');
|
2023-02-07 16:08:44 +00:00
|
|
|
const issueContent = `${toAbsoluteUrl(urlParamBodyLink)}#${anchor}`; // the default content for issue body
|
2024-03-25 23:03:12 +00:00
|
|
|
refInNewIssue.setAttribute('href', `${urlIssueNew}?body=${encodeURIComponent(issueContent)}`);
|
2021-10-16 17:28:04 +00:00
|
|
|
};
|
|
|
|
|
2022-04-26 10:54:40 +00:00
|
|
|
const updateViewGitBlameFragment = function (anchor) {
|
2024-03-25 23:03:12 +00:00
|
|
|
if (!viewGitBlame) return;
|
|
|
|
let href = viewGitBlame.getAttribute('href');
|
2022-04-26 10:54:40 +00:00
|
|
|
href = `${href.replace(/#L\d+$|#L\d+-L\d+$/, '')}`;
|
|
|
|
if (anchor.length !== 0) {
|
|
|
|
href = `${href}#${anchor}`;
|
|
|
|
}
|
2024-03-25 23:03:12 +00:00
|
|
|
viewGitBlame.setAttribute('href', href);
|
2022-04-26 10:54:40 +00:00
|
|
|
};
|
|
|
|
|
2024-03-25 23:03:12 +00:00
|
|
|
const updateCopyPermalinkUrl = function (anchor) {
|
|
|
|
if (!copyPermalink) return;
|
|
|
|
let link = copyPermalink.getAttribute('data-url');
|
2021-10-16 17:28:04 +00:00
|
|
|
link = `${link.replace(/#L\d+$|#L\d+-L\d+$/, '')}#${anchor}`;
|
2024-03-25 23:03:12 +00:00
|
|
|
copyPermalink.setAttribute('data-url', link);
|
2021-10-16 17:28:04 +00:00
|
|
|
};
|
|
|
|
|
2024-03-24 12:14:03 +00:00
|
|
|
if ($selectionStartEls) {
|
2024-03-25 23:03:12 +00:00
|
|
|
let a = parseInt($selectionEndEl[0].getAttribute('rel').slice(1));
|
|
|
|
let b = parseInt($selectionStartEls[0].getAttribute('rel').slice(1));
|
2021-10-16 17:28:04 +00:00
|
|
|
let c;
|
|
|
|
if (a !== b) {
|
|
|
|
if (a > b) {
|
|
|
|
c = a;
|
|
|
|
a = b;
|
|
|
|
b = c;
|
|
|
|
}
|
|
|
|
const classes = [];
|
|
|
|
for (let i = a; i <= b; i++) {
|
|
|
|
classes.push(`[rel=L${i}]`);
|
|
|
|
}
|
2024-03-24 12:14:03 +00:00
|
|
|
$linesEls.filter(classes.join(',')).each(function () {
|
2024-03-29 17:08:54 +00:00
|
|
|
this.closest('tr').classList.add('active');
|
2024-03-24 12:14:03 +00:00
|
|
|
});
|
2021-10-16 17:28:04 +00:00
|
|
|
changeHash(`#L${a}-L${b}`);
|
|
|
|
|
|
|
|
updateIssueHref(`L${a}-L${b}`);
|
2022-04-26 10:54:40 +00:00
|
|
|
updateViewGitBlameFragment(`L${a}-L${b}`);
|
2022-08-09 12:37:34 +00:00
|
|
|
updateCopyPermalinkUrl(`L${a}-L${b}`);
|
2021-10-16 17:28:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2024-03-29 17:08:54 +00:00
|
|
|
$selectionEndEl[0].closest('tr').classList.add('active');
|
2024-03-25 23:03:12 +00:00
|
|
|
changeHash(`#${$selectionEndEl[0].getAttribute('rel')}`);
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2024-03-25 23:03:12 +00:00
|
|
|
updateIssueHref($selectionEndEl[0].getAttribute('rel'));
|
|
|
|
updateViewGitBlameFragment($selectionEndEl[0].getAttribute('rel'));
|
|
|
|
updateCopyPermalinkUrl($selectionEndEl[0].getAttribute('rel'));
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function showLineButton() {
|
2022-08-09 12:37:34 +00:00
|
|
|
const menu = document.querySelector('.code-line-menu');
|
|
|
|
if (!menu) return;
|
|
|
|
|
|
|
|
// remove all other line buttons
|
|
|
|
for (const el of document.querySelectorAll('.code-line-button')) {
|
|
|
|
el.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
// find active row and add button
|
2024-03-24 12:14:03 +00:00
|
|
|
const tr = document.querySelector('.code-view tr.active');
|
|
|
|
const td = tr.querySelector('td.lines-num');
|
2022-08-09 12:37:34 +00:00
|
|
|
const btn = document.createElement('button');
|
2024-03-24 12:14:03 +00:00
|
|
|
btn.classList.add('code-line-button', 'ui', 'basic', 'button');
|
2022-08-09 12:37:34 +00:00
|
|
|
btn.innerHTML = svg('octicon-kebab-horizontal');
|
|
|
|
td.prepend(btn);
|
|
|
|
|
|
|
|
// put a copy of the menu back into DOM for the next click
|
2023-05-09 02:35:49 +00:00
|
|
|
btn.closest('.code-view').append(menu.cloneNode(true));
|
2022-08-09 12:37:34 +00:00
|
|
|
|
|
|
|
createTippy(btn, {
|
2024-04-30 14:52:46 +00:00
|
|
|
theme: 'menu',
|
2022-08-09 12:37:34 +00:00
|
|
|
trigger: 'click',
|
2023-06-09 09:10:51 +00:00
|
|
|
hideOnClick: true,
|
2022-08-09 12:37:34 +00:00
|
|
|
content: menu,
|
|
|
|
placement: 'right-start',
|
2023-06-14 08:01:37 +00:00
|
|
|
interactive: true,
|
2023-06-09 09:10:51 +00:00
|
|
|
onShow: (tippy) => {
|
|
|
|
tippy.popper.addEventListener('click', () => {
|
|
|
|
tippy.hide();
|
|
|
|
}, {once: true});
|
2024-03-22 14:06:53 +00:00
|
|
|
},
|
2022-08-09 12:37:34 +00:00
|
|
|
});
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function initRepoCodeView() {
|
|
|
|
if ($('.code-view .lines-num').length > 0) {
|
|
|
|
$(document).on('click', '.lines-num span', function (e) {
|
2024-03-24 12:14:03 +00:00
|
|
|
const linesEls = getLineEls();
|
|
|
|
const selectedEls = Array.from(linesEls).filter((el) => {
|
|
|
|
return el.matches(`[rel=${this.getAttribute('id')}]`);
|
|
|
|
});
|
|
|
|
|
|
|
|
let from;
|
|
|
|
if (e.shiftKey) {
|
|
|
|
from = Array.from(linesEls).filter((el) => {
|
|
|
|
return el.closest('tr').classList.contains('active');
|
|
|
|
});
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|
2024-03-24 12:14:03 +00:00
|
|
|
selectRange($(linesEls), $(selectedEls), from ? $(from) : null);
|
2021-10-16 17:28:04 +00:00
|
|
|
|
|
|
|
if (window.getSelection) {
|
|
|
|
window.getSelection().removeAllRanges();
|
|
|
|
} else {
|
|
|
|
document.selection.empty();
|
|
|
|
}
|
|
|
|
|
2024-03-24 12:14:03 +00:00
|
|
|
showLineButton();
|
2021-10-16 17:28:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$(window).on('hashchange', () => {
|
2024-07-03 15:48:14 +00:00
|
|
|
let m = rangeAnchorRegex.exec(window.location.hash);
|
2024-03-24 12:14:03 +00:00
|
|
|
const $linesEls = $(getLineEls());
|
2021-10-16 17:28:04 +00:00
|
|
|
let $first;
|
|
|
|
if (m) {
|
2024-03-24 12:14:03 +00:00
|
|
|
$first = $linesEls.filter(`[rel=${m[1]}]`);
|
2022-11-11 10:22:36 +00:00
|
|
|
if ($first.length) {
|
2024-03-24 12:14:03 +00:00
|
|
|
selectRange($linesEls, $first, $linesEls.filter(`[rel=${m[2]}]`));
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2022-11-11 10:22:36 +00:00
|
|
|
// show code view menu marker (don't show in blame page)
|
2024-03-24 12:14:03 +00:00
|
|
|
if (!isBlame()) {
|
2022-11-11 10:22:36 +00:00
|
|
|
showLineButton();
|
|
|
|
}
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2022-11-11 10:22:36 +00:00
|
|
|
$('html, body').scrollTop($first.offset().top - 200);
|
|
|
|
return;
|
|
|
|
}
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|
2024-07-03 15:48:14 +00:00
|
|
|
m = singleAnchorRegex.exec(window.location.hash);
|
2021-10-16 17:28:04 +00:00
|
|
|
if (m) {
|
2024-03-24 12:14:03 +00:00
|
|
|
$first = $linesEls.filter(`[rel=L${m[2]}]`);
|
2022-11-11 10:22:36 +00:00
|
|
|
if ($first.length) {
|
2024-03-24 12:14:03 +00:00
|
|
|
selectRange($linesEls, $first);
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2022-11-11 10:22:36 +00:00
|
|
|
// show code view menu marker (don't show in blame page)
|
2024-03-24 12:14:03 +00:00
|
|
|
if (!isBlame()) {
|
2022-11-11 10:22:36 +00:00
|
|
|
showLineButton();
|
|
|
|
}
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2022-11-11 10:22:36 +00:00
|
|
|
$('html, body').scrollTop($first.offset().top - 200);
|
|
|
|
}
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|
|
|
|
}).trigger('hashchange');
|
|
|
|
}
|
|
|
|
$(document).on('click', '.fold-file', ({currentTarget}) => {
|
2022-05-07 18:28:10 +00:00
|
|
|
invertFileFolding(currentTarget.closest('.file-content'), currentTarget);
|
2021-10-16 17:28:04 +00:00
|
|
|
});
|
2024-02-16 21:41:23 +00:00
|
|
|
$(document).on('click', '.copy-line-permalink', async ({currentTarget}) => {
|
|
|
|
await clippie(toAbsoluteUrl(currentTarget.getAttribute('data-url')));
|
2022-08-09 12:37:34 +00:00
|
|
|
});
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|