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