2017-12-07 07:30:53 -05:00
|
|
|
import { getLocationHash } from '../lib/utils/url_utility';
|
|
|
|
|
2022-01-18 19:12:04 -05:00
|
|
|
const lineNumberRe = /^(L|LC)[0-9]+/;
|
2017-02-22 20:27:06 -05:00
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
const updateLineNumbersOnBlobPermalinks = (linksToUpdate) => {
|
2017-12-07 07:30:53 -05:00
|
|
|
const hash = getLocationHash();
|
2017-02-22 20:27:06 -05:00
|
|
|
if (hash && lineNumberRe.test(hash)) {
|
|
|
|
const hashUrlString = `#${hash}`;
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
[].concat(Array.prototype.slice.call(linksToUpdate)).forEach((permalinkButton) => {
|
2018-10-10 02:09:55 -04:00
|
|
|
const baseHref =
|
2022-06-17 11:08:29 -04:00
|
|
|
permalinkButton.dataset.originalHref ||
|
2018-10-10 02:09:55 -04:00
|
|
|
(() => {
|
|
|
|
const href = permalinkButton.getAttribute('href');
|
2022-06-17 11:08:29 -04:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
permalinkButton.dataset.originalHref = href;
|
2018-10-10 02:09:55 -04:00
|
|
|
return href;
|
|
|
|
})();
|
2017-02-22 20:27:06 -05:00
|
|
|
permalinkButton.setAttribute('href', `${baseHref}${hashUrlString}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function BlobLinePermalinkUpdater(blobContentHolder, lineNumberSelector, elementsToUpdate) {
|
|
|
|
const updateBlameAndBlobPermalinkCb = () => {
|
|
|
|
// Wait for the hash to update from the LineHighlighter callback
|
|
|
|
setTimeout(() => {
|
|
|
|
updateLineNumbersOnBlobPermalinks(elementsToUpdate);
|
|
|
|
}, 0);
|
|
|
|
};
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
blobContentHolder.addEventListener('click', (e) => {
|
2017-02-22 20:27:06 -05:00
|
|
|
if (e.target.matches(lineNumberSelector)) {
|
|
|
|
updateBlameAndBlobPermalinkCb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
updateBlameAndBlobPermalinkCb();
|
|
|
|
}
|
|
|
|
|
|
|
|
export default BlobLinePermalinkUpdater;
|