githaven-fork/web_src/js/features/repo-code.js

195 lines
5.9 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
import {svg} from '../svg.js';
import {invertFileFolding} from './file-fold.js';
import {createTippy} from '../modules/tippy.js';
import {clippie} from 'clippie';
import {toAbsoluteUrl} from '../utils.js';
export const singleAnchorRegex = /^#(L|n)([1-9][0-9]*)$/;
export const rangeAnchorRegex = /^#(L[1-9][0-9]*)-(L[1-9][0-9]*)$/;
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) {
$linesEls.closest('tr').removeClass('active');
// add hashchange to permalink
const $refInNewIssue = $('a.ref-in-new-issue');
const $copyPermalink = $('a.copy-line-permalink');
const $viewGitBlame = $('a.view_git_blame');
const updateIssueHref = function (anchor) {
2024-03-24 12:14:03 +00:00
if (!$refInNewIssue.length) {
return;
}
const urlIssueNew = $refInNewIssue.attr('data-url-issue-new');
const urlParamBodyLink = $refInNewIssue.attr('data-url-param-body-link');
const issueContent = `${toAbsoluteUrl(urlParamBodyLink)}#${anchor}`; // the default content for issue body
$refInNewIssue.attr('href', `${urlIssueNew}?body=${encodeURIComponent(issueContent)}`);
};
const updateViewGitBlameFragment = function (anchor) {
2024-03-24 12:14:03 +00:00
if (!$viewGitBlame.length) return;
let href = $viewGitBlame.attr('href');
href = `${href.replace(/#L\d+$|#L\d+-L\d+$/, '')}`;
if (anchor.length !== 0) {
href = `${href}#${anchor}`;
}
$viewGitBlame.attr('href', href);
};
const updateCopyPermalinkUrl = function(anchor) {
2024-03-24 12:14:03 +00:00
if (!$copyPermalink.length) return;
let link = $copyPermalink.attr('data-url');
link = `${link.replace(/#L\d+$|#L\d+-L\d+$/, '')}#${anchor}`;
$copyPermalink.attr('data-url', link);
};
2024-03-24 12:14:03 +00:00
if ($selectionStartEls) {
let a = parseInt($selectionEndEl.attr('rel').slice(1));
let b = parseInt($selectionStartEls.attr('rel').slice(1));
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 () {
$(this).closest('tr').addClass('active');
});
changeHash(`#L${a}-L${b}`);
updateIssueHref(`L${a}-L${b}`);
updateViewGitBlameFragment(`L${a}-L${b}`);
updateCopyPermalinkUrl(`L${a}-L${b}`);
return;
}
}
2024-03-24 12:14:03 +00:00
$selectionEndEl.closest('tr').addClass('active');
changeHash(`#${$selectionEndEl.attr('rel')}`);
2024-03-24 12:14:03 +00:00
updateIssueHref($selectionEndEl.attr('rel'));
updateViewGitBlameFragment($selectionEndEl.attr('rel'));
updateCopyPermalinkUrl($selectionEndEl.attr('rel'));
}
function showLineButton() {
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');
const btn = document.createElement('button');
2024-03-24 12:14:03 +00:00
btn.classList.add('code-line-button', 'ui', 'basic', 'button');
btn.innerHTML = svg('octicon-kebab-horizontal');
td.prepend(btn);
// put a copy of the menu back into DOM for the next click
btn.closest('.code-view').append(menu.cloneNode(true));
createTippy(btn, {
trigger: 'click',
hideOnClick: true,
content: menu,
placement: 'right-start',
interactive: true,
onShow: (tippy) => {
tippy.popper.addEventListener('click', () => {
tippy.hide();
}, {once: true});
},
});
}
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');
});
}
2024-03-24 12:14:03 +00:00
selectRange($(linesEls), $(selectedEls), from ? $(from) : null);
if (window.getSelection) {
window.getSelection().removeAllRanges();
} else {
document.selection.empty();
}
2024-03-24 12:14:03 +00:00
showLineButton();
});
$(window).on('hashchange', () => {
let m = window.location.hash.match(rangeAnchorRegex);
2024-03-24 12:14:03 +00:00
const $linesEls = $(getLineEls());
let $first;
if (m) {
2024-03-24 12:14:03 +00:00
$first = $linesEls.filter(`[rel=${m[1]}]`);
if ($first.length) {
2024-03-24 12:14:03 +00:00
selectRange($linesEls, $first, $linesEls.filter(`[rel=${m[2]}]`));
// show code view menu marker (don't show in blame page)
2024-03-24 12:14:03 +00:00
if (!isBlame()) {
showLineButton();
}
$('html, body').scrollTop($first.offset().top - 200);
return;
}
}
m = window.location.hash.match(singleAnchorRegex);
if (m) {
2024-03-24 12:14:03 +00:00
$first = $linesEls.filter(`[rel=L${m[2]}]`);
if ($first.length) {
2024-03-24 12:14:03 +00:00
selectRange($linesEls, $first);
// show code view menu marker (don't show in blame page)
2024-03-24 12:14:03 +00:00
if (!isBlame()) {
showLineButton();
}
$('html, body').scrollTop($first.offset().top - 200);
}
}
}).trigger('hashchange');
}
$(document).on('click', '.fold-file', ({currentTarget}) => {
invertFileFolding(currentTarget.closest('.file-content'), currentTarget);
});
$(document).on('click', '.copy-line-permalink', async ({currentTarget}) => {
await clippie(toAbsoluteUrl(currentTarget.getAttribute('data-url')));
});
}