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