mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
196 lines
6.1 KiB
196 lines
6.1 KiB
3 years ago
|
import $ from 'jquery';
|
||
5 months ago
|
import {svg} from '../svg.ts';
|
||
|
import {invertFileFolding} from './file-fold.ts';
|
||
|
import {createTippy} from '../modules/tippy.ts';
|
||
2 years ago
|
import {clippie} from 'clippie';
|
||
5 months ago
|
import {toAbsoluteUrl} from '../utils.ts';
|
||
3 years ago
|
|
||
2 years ago
|
export const singleAnchorRegex = /^#(L|n)([1-9][0-9]*)$/;
|
||
|
export const rangeAnchorRegex = /^#(L[1-9][0-9]*)-(L[1-9][0-9]*)$/;
|
||
2 years ago
|
|
||
3 years ago
|
function changeHash(hash) {
|
||
|
if (window.history.pushState) {
|
||
|
window.history.pushState(null, null, hash);
|
||
|
} else {
|
||
|
window.location.hash = hash;
|
||
|
}
|
||
|
}
|
||
|
|
||
8 months ago
|
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) {
|
||
8 months ago
|
for (const el of $linesEls) {
|
||
|
el.closest('tr').classList.remove('active');
|
||
|
}
|
||
3 years ago
|
|
||
|
// add hashchange to permalink
|
||
8 months ago
|
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');
|
||
3 years ago
|
|
||
3 years ago
|
const updateIssueHref = function (anchor) {
|
||
8 months ago
|
if (!refInNewIssue) return;
|
||
|
const urlIssueNew = refInNewIssue.getAttribute('data-url-issue-new');
|
||
|
const urlParamBodyLink = refInNewIssue.getAttribute('data-url-param-body-link');
|
||
2 years ago
|
const issueContent = `${toAbsoluteUrl(urlParamBodyLink)}#${anchor}`; // the default content for issue body
|
||
8 months ago
|
refInNewIssue.setAttribute('href', `${urlIssueNew}?body=${encodeURIComponent(issueContent)}`);
|
||
3 years ago
|
};
|
||
|
|
||
3 years ago
|
const updateViewGitBlameFragment = function (anchor) {
|
||
8 months ago
|
if (!viewGitBlame) return;
|
||
|
let href = viewGitBlame.getAttribute('href');
|
||
3 years ago
|
href = `${href.replace(/#L\d+$|#L\d+-L\d+$/, '')}`;
|
||
|
if (anchor.length !== 0) {
|
||
|
href = `${href}#${anchor}`;
|
||
|
}
|
||
8 months ago
|
viewGitBlame.setAttribute('href', href);
|
||
3 years ago
|
};
|
||
|
|
||
8 months ago
|
const updateCopyPermalinkUrl = function (anchor) {
|
||
|
if (!copyPermalink) return;
|
||
|
let link = copyPermalink.getAttribute('data-url');
|
||
3 years ago
|
link = `${link.replace(/#L\d+$|#L\d+-L\d+$/, '')}#${anchor}`;
|
||
8 months ago
|
copyPermalink.setAttribute('data-url', link);
|
||
3 years ago
|
};
|
||
|
|
||
8 months ago
|
if ($selectionStartEls) {
|
||
8 months ago
|
let a = parseInt($selectionEndEl[0].getAttribute('rel').slice(1));
|
||
|
let b = parseInt($selectionStartEls[0].getAttribute('rel').slice(1));
|
||
3 years ago
|
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}]`);
|
||
|
}
|
||
8 months ago
|
$linesEls.filter(classes.join(',')).each(function () {
|
||
8 months ago
|
this.closest('tr').classList.add('active');
|
||
8 months ago
|
});
|
||
3 years ago
|
changeHash(`#L${a}-L${b}`);
|
||
|
|
||
|
updateIssueHref(`L${a}-L${b}`);
|
||
3 years ago
|
updateViewGitBlameFragment(`L${a}-L${b}`);
|
||
2 years ago
|
updateCopyPermalinkUrl(`L${a}-L${b}`);
|
||
3 years ago
|
return;
|
||
|
}
|
||
|
}
|
||
8 months ago
|
$selectionEndEl[0].closest('tr').classList.add('active');
|
||
8 months ago
|
changeHash(`#${$selectionEndEl[0].getAttribute('rel')}`);
|
||
3 years ago
|
|
||
8 months ago
|
updateIssueHref($selectionEndEl[0].getAttribute('rel'));
|
||
|
updateViewGitBlameFragment($selectionEndEl[0].getAttribute('rel'));
|
||
|
updateCopyPermalinkUrl($selectionEndEl[0].getAttribute('rel'));
|
||
3 years ago
|
}
|
||
|
|
||
|
function showLineButton() {
|
||
2 years ago
|
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
|
||
8 months ago
|
const tr = document.querySelector('.code-view tr.active');
|
||
|
const td = tr.querySelector('td.lines-num');
|
||
2 years ago
|
const btn = document.createElement('button');
|
||
8 months ago
|
btn.classList.add('code-line-button', 'ui', 'basic', 'button');
|
||
2 years ago
|
btn.innerHTML = svg('octicon-kebab-horizontal');
|
||
|
td.prepend(btn);
|
||
|
|
||
|
// put a copy of the menu back into DOM for the next click
|
||
2 years ago
|
btn.closest('.code-view').append(menu.cloneNode(true));
|
||
2 years ago
|
|
||
|
createTippy(btn, {
|
||
7 months ago
|
theme: 'menu',
|
||
2 years ago
|
trigger: 'click',
|
||
1 year ago
|
hideOnClick: true,
|
||
2 years ago
|
content: menu,
|
||
|
placement: 'right-start',
|
||
1 year ago
|
interactive: true,
|
||
1 year ago
|
onShow: (tippy) => {
|
||
|
tippy.popper.addEventListener('click', () => {
|
||
|
tippy.hide();
|
||
|
}, {once: true});
|
||
8 months ago
|
},
|
||
2 years ago
|
});
|
||
3 years ago
|
}
|
||
|
|
||
|
export function initRepoCodeView() {
|
||
|
if ($('.code-view .lines-num').length > 0) {
|
||
|
$(document).on('click', '.lines-num span', function (e) {
|
||
8 months ago
|
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');
|
||
|
});
|
||
3 years ago
|
}
|
||
8 months ago
|
selectRange($(linesEls), $(selectedEls), from ? $(from) : null);
|
||
3 years ago
|
|
||
|
if (window.getSelection) {
|
||
|
window.getSelection().removeAllRanges();
|
||
|
} else {
|
||
|
document.selection.empty();
|
||
|
}
|
||
|
|
||
8 months ago
|
showLineButton();
|
||
3 years ago
|
});
|
||
|
|
||
|
$(window).on('hashchange', () => {
|
||
5 months ago
|
let m = rangeAnchorRegex.exec(window.location.hash);
|
||
8 months ago
|
const $linesEls = $(getLineEls());
|
||
3 years ago
|
let $first;
|
||
|
if (m) {
|
||
8 months ago
|
$first = $linesEls.filter(`[rel=${m[1]}]`);
|
||
2 years ago
|
if ($first.length) {
|
||
8 months ago
|
selectRange($linesEls, $first, $linesEls.filter(`[rel=${m[2]}]`));
|
||
3 years ago
|
|
||
2 years ago
|
// show code view menu marker (don't show in blame page)
|
||
8 months ago
|
if (!isBlame()) {
|
||
2 years ago
|
showLineButton();
|
||
|
}
|
||
3 years ago
|
|
||
2 years ago
|
$('html, body').scrollTop($first.offset().top - 200);
|
||
|
return;
|
||
|
}
|
||
3 years ago
|
}
|
||
5 months ago
|
m = singleAnchorRegex.exec(window.location.hash);
|
||
3 years ago
|
if (m) {
|
||
8 months ago
|
$first = $linesEls.filter(`[rel=L${m[2]}]`);
|
||
2 years ago
|
if ($first.length) {
|
||
8 months ago
|
selectRange($linesEls, $first);
|
||
3 years ago
|
|
||
2 years ago
|
// show code view menu marker (don't show in blame page)
|
||
8 months ago
|
if (!isBlame()) {
|
||
2 years ago
|
showLineButton();
|
||
|
}
|
||
3 years ago
|
|
||
2 years ago
|
$('html, body').scrollTop($first.offset().top - 200);
|
||
|
}
|
||
3 years ago
|
}
|
||
|
}).trigger('hashchange');
|
||
|
}
|
||
|
$(document).on('click', '.fold-file', ({currentTarget}) => {
|
||
3 years ago
|
invertFileFolding(currentTarget.closest('.file-content'), currentTarget);
|
||
3 years ago
|
});
|
||
9 months ago
|
$(document).on('click', '.copy-line-permalink', async ({currentTarget}) => {
|
||
|
await clippie(toAbsoluteUrl(currentTarget.getAttribute('data-url')));
|
||
2 years ago
|
});
|
||
3 years ago
|
}
|