2017-12-07 12:30:53 +00:00
|
|
|
import { getLocationHash } from '../lib/utils/url_utility';
|
|
|
|
|
2017-02-23 01:27:06 +00:00
|
|
|
const lineNumberRe = /^L[0-9]+/;
|
|
|
|
|
2018-10-10 06:09:55 +00:00
|
|
|
const updateLineNumbersOnBlobPermalinks = linksToUpdate => {
|
2017-12-07 12:30:53 +00:00
|
|
|
const hash = getLocationHash();
|
2017-02-23 01:27:06 +00:00
|
|
|
if (hash && lineNumberRe.test(hash)) {
|
|
|
|
const hashUrlString = `#${hash}`;
|
|
|
|
|
2018-10-10 06:09:55 +00:00
|
|
|
[].concat(Array.prototype.slice.call(linksToUpdate)).forEach(permalinkButton => {
|
|
|
|
const baseHref =
|
|
|
|
permalinkButton.getAttribute('data-original-href') ||
|
|
|
|
(() => {
|
|
|
|
const href = permalinkButton.getAttribute('href');
|
|
|
|
permalinkButton.setAttribute('data-original-href', href);
|
|
|
|
return href;
|
|
|
|
})();
|
2017-02-23 01:27:06 +00: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);
|
|
|
|
};
|
|
|
|
|
2018-10-10 06:09:55 +00:00
|
|
|
blobContentHolder.addEventListener('click', e => {
|
2017-02-23 01:27:06 +00:00
|
|
|
if (e.target.matches(lineNumberSelector)) {
|
|
|
|
updateBlameAndBlobPermalinkCb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
updateBlameAndBlobPermalinkCb();
|
|
|
|
}
|
|
|
|
|
|
|
|
export default BlobLinePermalinkUpdater;
|