2020-04-29 20:09:37 -04:00
|
|
|
/* eslint-disable func-names, no-underscore-dangle, no-param-reassign, consistent-return */
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2020-12-30 04:10:10 -05:00
|
|
|
import { scrollToElement } from '~/lib/utils/common_utils';
|
2018-03-09 15:18:59 -05:00
|
|
|
|
2016-07-26 23:32:10 -04:00
|
|
|
// LineHighlighter
|
|
|
|
//
|
|
|
|
// Handles single- and multi-line selection and highlight for blob views.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// ### Example Markup
|
|
|
|
//
|
|
|
|
// <div id="blob-content-holder">
|
|
|
|
// <div class="file-content">
|
|
|
|
// <div class="line-numbers">
|
|
|
|
// <a href="#L1" id="L1" data-line-number="1">1</a>
|
|
|
|
// <a href="#L2" id="L2" data-line-number="2">2</a>
|
|
|
|
// <a href="#L3" id="L3" data-line-number="3">3</a>
|
|
|
|
// <a href="#L4" id="L4" data-line-number="4">4</a>
|
|
|
|
// <a href="#L5" id="L5" data-line-number="5">5</a>
|
|
|
|
// </div>
|
|
|
|
// <pre class="code highlight">
|
|
|
|
// <code>
|
|
|
|
// <span id="LC1" class="line">...</span>
|
|
|
|
// <span id="LC2" class="line">...</span>
|
|
|
|
// <span id="LC3" class="line">...</span>
|
|
|
|
// <span id="LC4" class="line">...</span>
|
|
|
|
// <span id="LC5" class="line">...</span>
|
|
|
|
// </code>
|
|
|
|
// </pre>
|
|
|
|
// </div>
|
|
|
|
// </div>
|
|
|
|
//
|
2017-04-13 12:49:54 -04:00
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
const LineHighlighter = function (options = {}) {
|
2017-09-22 09:42:05 -04:00
|
|
|
options.highlightLineClass = options.highlightLineClass || 'hll';
|
|
|
|
options.fileHolderSelector = options.fileHolderSelector || '.file-holder';
|
|
|
|
options.scrollFileHolder = options.scrollFileHolder || false;
|
2018-06-15 11:58:27 -04:00
|
|
|
options.hash = options.hash || window.location.hash;
|
2022-03-21 11:08:37 -04:00
|
|
|
options.scrollBehavior = options.scrollBehavior || 'smooth';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-09-22 09:42:05 -04:00
|
|
|
this.options = options;
|
|
|
|
this._hash = options.hash;
|
|
|
|
this.highlightLineClass = options.highlightLineClass;
|
|
|
|
this.setHash = this.setHash.bind(this);
|
|
|
|
this.highlightLine = this.highlightLine.bind(this);
|
|
|
|
this.clickHandler = this.clickHandler.bind(this);
|
|
|
|
this.highlightHash = this.highlightHash.bind(this);
|
|
|
|
|
|
|
|
this.bindEvents();
|
|
|
|
this.highlightHash();
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
LineHighlighter.prototype.bindEvents = function () {
|
2017-09-22 09:42:05 -04:00
|
|
|
const $fileHolder = $(this.options.fileHolderSelector);
|
|
|
|
|
|
|
|
$fileHolder.on('click', 'a[data-line-number]', this.clickHandler);
|
|
|
|
$fileHolder.on('highlight:line', this.highlightHash);
|
2020-12-23 16:10:24 -05:00
|
|
|
window.addEventListener('hashchange', (e) => this.highlightHash(e.target.location.hash));
|
2017-09-22 09:42:05 -04:00
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
LineHighlighter.prototype.highlightHash = function (newHash) {
|
2017-10-05 05:54:46 -04:00
|
|
|
let range;
|
2017-10-05 09:14:33 -04:00
|
|
|
if (newHash && typeof newHash === 'string') this._hash = newHash;
|
2017-10-05 05:54:46 -04:00
|
|
|
|
|
|
|
this.clearHighlight();
|
2017-09-22 09:42:05 -04:00
|
|
|
|
|
|
|
if (this._hash !== '') {
|
|
|
|
range = this.hashToRange(this._hash);
|
|
|
|
if (range[0]) {
|
|
|
|
this.highlightRange(range);
|
|
|
|
const lineSelector = `#L${range[0]}`;
|
2020-12-30 04:10:10 -05:00
|
|
|
|
|
|
|
scrollToElement(lineSelector, {
|
2017-09-22 09:42:05 -04:00
|
|
|
// Scroll to the first highlighted line on initial load
|
2020-12-30 04:10:10 -05:00
|
|
|
// Add an offset of -100 for some context
|
|
|
|
offset: -100,
|
2022-03-21 11:08:37 -04:00
|
|
|
behavior: this.options.scrollBehavior,
|
2020-12-30 04:10:10 -05:00
|
|
|
});
|
2017-09-22 09:42:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
LineHighlighter.prototype.clickHandler = function (event) {
|
2019-11-14 22:06:34 -05:00
|
|
|
let range;
|
2017-09-22 09:42:05 -04:00
|
|
|
event.preventDefault();
|
|
|
|
this.clearHighlight();
|
2020-12-23 07:10:26 -05:00
|
|
|
const lineNumber = $(event.target).closest('a').data('lineNumber');
|
2019-11-14 22:06:34 -05:00
|
|
|
const current = this.hashToRange(this._hash);
|
2017-09-22 09:42:05 -04:00
|
|
|
if (!(current[0] && event.shiftKey)) {
|
|
|
|
// If there's no current selection, or there is but Shift wasn't held,
|
|
|
|
// treat this like a single-line selection.
|
|
|
|
this.setHash(lineNumber);
|
|
|
|
return this.highlightLine(lineNumber);
|
|
|
|
} else if (event.shiftKey) {
|
|
|
|
if (lineNumber < current[0]) {
|
|
|
|
range = [lineNumber, current[0]];
|
|
|
|
} else {
|
|
|
|
range = [current[0], lineNumber];
|
|
|
|
}
|
|
|
|
this.setHash(range[0], range[1]);
|
|
|
|
return this.highlightRange(range);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
LineHighlighter.prototype.clearHighlight = function () {
|
2019-09-24 08:06:20 -04:00
|
|
|
return $(`.${this.highlightLineClass}`).removeClass(this.highlightLineClass);
|
2017-09-22 09:42:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Convert a URL hash String into line numbers
|
|
|
|
//
|
|
|
|
// hash - Hash String
|
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
//
|
|
|
|
// hashToRange('#L5') # => [5, null]
|
|
|
|
// hashToRange('#L5-15') # => [5, 15]
|
|
|
|
// hashToRange('#foo') # => [null, null]
|
|
|
|
//
|
|
|
|
// Returns an Array
|
2020-12-23 07:10:26 -05:00
|
|
|
LineHighlighter.prototype.hashToRange = function (hash) {
|
2021-07-08 05:09:33 -04:00
|
|
|
// ?L(\d+)(?:-L?(\d+))?$/)
|
|
|
|
const matches = hash.match(/^#?L(\d+)(?:-L?(\d+))?$/);
|
2017-09-22 09:42:05 -04:00
|
|
|
if (matches && matches.length) {
|
2019-11-14 22:06:34 -05:00
|
|
|
const first = parseInt(matches[1], 10);
|
|
|
|
const last = matches[2] ? parseInt(matches[2], 10) : null;
|
2017-09-22 09:42:05 -04:00
|
|
|
return [first, last];
|
|
|
|
}
|
2020-04-29 20:09:37 -04:00
|
|
|
return [null, null];
|
2017-09-22 09:42:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Highlight a single line
|
|
|
|
//
|
|
|
|
// lineNumber - Line number to highlight
|
2020-12-23 07:10:26 -05:00
|
|
|
LineHighlighter.prototype.highlightLine = function (lineNumber) {
|
2019-09-24 08:06:20 -04:00
|
|
|
return $(`#LC${lineNumber}`).addClass(this.highlightLineClass);
|
2017-09-22 09:42:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Highlight all lines within a range
|
|
|
|
//
|
|
|
|
// range - Array containing the starting and ending line numbers
|
2020-12-23 07:10:26 -05:00
|
|
|
LineHighlighter.prototype.highlightRange = function (range) {
|
2017-09-22 09:42:05 -04:00
|
|
|
if (range[1]) {
|
2018-06-21 10:50:34 -04:00
|
|
|
const results = [];
|
|
|
|
const ref = range[0] <= range[1] ? range : range.reverse();
|
2018-06-14 02:18:05 -04:00
|
|
|
|
2018-06-21 10:50:34 -04:00
|
|
|
for (let lineNumber = range[0]; lineNumber <= ref[1]; lineNumber += 1) {
|
2017-09-22 09:42:05 -04:00
|
|
|
results.push(this.highlightLine(lineNumber));
|
|
|
|
}
|
2018-06-21 10:50:34 -04:00
|
|
|
|
2017-09-22 09:42:05 -04:00
|
|
|
return results;
|
|
|
|
}
|
2020-04-29 20:09:37 -04:00
|
|
|
return this.highlightLine(range[0]);
|
2017-09-22 09:42:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Set the URL hash string
|
2020-12-23 07:10:26 -05:00
|
|
|
LineHighlighter.prototype.setHash = function (firstLineNumber, lastLineNumber) {
|
2019-11-14 22:06:34 -05:00
|
|
|
let hash;
|
2017-09-22 09:42:05 -04:00
|
|
|
if (lastLineNumber) {
|
2019-09-24 08:06:20 -04:00
|
|
|
hash = `#L${firstLineNumber}-${lastLineNumber}`;
|
2017-09-22 09:42:05 -04:00
|
|
|
} else {
|
2019-09-24 08:06:20 -04:00
|
|
|
hash = `#L${firstLineNumber}`;
|
2017-09-22 09:42:05 -04:00
|
|
|
}
|
|
|
|
this._hash = hash;
|
|
|
|
return this.__setLocationHash__(hash);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Make the actual hash change in the browser
|
|
|
|
//
|
|
|
|
// This method is stubbed in tests.
|
2020-12-23 07:10:26 -05:00
|
|
|
LineHighlighter.prototype.__setLocationHash__ = function (value) {
|
2018-10-10 03:13:34 -04:00
|
|
|
return window.history.pushState(
|
|
|
|
{
|
|
|
|
url: value,
|
|
|
|
// We're using pushState instead of assigning location.hash directly to
|
|
|
|
// prevent the page from scrolling on the hashchange event
|
|
|
|
},
|
|
|
|
document.title,
|
|
|
|
value,
|
|
|
|
);
|
2017-09-22 09:42:05 -04:00
|
|
|
};
|
|
|
|
|
2017-12-15 07:57:08 -05:00
|
|
|
export default LineHighlighter;
|