2017-12-07 07:30:53 -05:00
|
|
|
import { getLocationHash } from '../lib/utils/url_utility';
|
|
|
|
|
2017-02-22 20:27:06 -05:00
|
|
|
const lineNumberRe = /^L[0-9]+/;
|
|
|
|
|
2018-10-10 02:09:55 -04: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}`;
|
|
|
|
|
2018-10-10 02:09:55 -04: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-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);
|
|
|
|
};
|
|
|
|
|
2018-10-10 02:09:55 -04:00
|
|
|
blobContentHolder.addEventListener('click', e => {
|
2017-02-22 20:27:06 -05:00
|
|
|
if (e.target.matches(lineNumberSelector)) {
|
|
|
|
updateBlameAndBlobPermalinkCb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
updateBlameAndBlobPermalinkCb();
|
|
|
|
}
|
|
|
|
|
|
|
|
export default BlobLinePermalinkUpdater;
|