debian-mirror-gitlab/app/assets/javascripts/blob/blob_line_permalink_updater.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

46 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import { getLocationHash } from '../lib/utils/url_utility';
2023-01-13 00:05:48 +05:30
import { getPageParamValue, getPageSearchString } from './utils';
2018-03-17 18:26:18 +05:30
2022-03-02 08:16:31 +05:30
const lineNumberRe = /^(L|LC)[0-9]+/;
2017-08-17 22:00:37 +05:30
2021-03-08 18:12:59 +05:30
const updateLineNumbersOnBlobPermalinks = (linksToUpdate) => {
2018-03-17 18:26:18 +05:30
const hash = getLocationHash();
2017-08-17 22:00:37 +05:30
if (hash && lineNumberRe.test(hash)) {
const hashUrlString = `#${hash}`;
2021-03-08 18:12:59 +05:30
[].concat(Array.prototype.slice.call(linksToUpdate)).forEach((permalinkButton) => {
2018-12-13 13:39:08 +05:30
const baseHref =
2022-07-23 23:45:48 +05:30
permalinkButton.dataset.originalHref ||
2018-12-13 13:39:08 +05:30
(() => {
const href = permalinkButton.getAttribute('href');
2022-07-23 23:45:48 +05:30
// eslint-disable-next-line no-param-reassign
permalinkButton.dataset.originalHref = href;
2018-12-13 13:39:08 +05:30
return href;
})();
2023-01-13 00:05:48 +05:30
const lineNum = parseInt(hash.split('L')[1], 10);
const page = getPageParamValue(lineNum);
const searchString = getPageSearchString(baseHref, page);
permalinkButton.setAttribute('href', `${baseHref}${searchString}${hashUrlString}`);
2017-08-17 22:00:37 +05:30
});
}
};
function BlobLinePermalinkUpdater(blobContentHolder, lineNumberSelector, elementsToUpdate) {
2022-11-25 23:54:43 +05:30
if (!blobContentHolder) return;
2017-08-17 22:00:37 +05:30
const updateBlameAndBlobPermalinkCb = () => {
// Wait for the hash to update from the LineHighlighter callback
setTimeout(() => {
updateLineNumbersOnBlobPermalinks(elementsToUpdate);
}, 0);
};
2021-03-08 18:12:59 +05:30
blobContentHolder.addEventListener('click', (e) => {
2017-08-17 22:00:37 +05:30
if (e.target.matches(lineNumberSelector)) {
updateBlameAndBlobPermalinkCb();
}
});
updateBlameAndBlobPermalinkCb();
}
export default BlobLinePermalinkUpdater;