Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add select range in prism-line-highlight.js #3759

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 33 additions & 3 deletions plugins/line-highlight/prism-line-highlight.js
Expand Up @@ -231,18 +231,48 @@

var start = parseInt(pre.getAttribute('data-start') || '1');

var rangeStart = null;

// iterate all line number spans
$$('.line-numbers-rows > span', pre).forEach(function (lineSpan, i) {
var lineNumber = i + start;
lineSpan.onclick = function () {
var hash = id + '.' + lineNumber;
lineSpan.onmousedown = function () {
// Set the start of the range
rangeStart = lineNumber;
};

lineSpan.onmouseup = function () {
var rangeEnd = lineNumber;
var currentHashLines = location.hash.substr(1);
if (currentHashLines){
currentHashLines = currentHashLines.split('.');
currentHashLines = currentHashLines[1].split(",");
}
else currentHashLines = [];

// Determine the range of lines
for (var num = Math.min(rangeStart, rangeEnd); num <= Math.max(rangeStart, rangeEnd); num++) {
var hashValue = num.toString();
var hashIndex = currentHashLines.indexOf(hashValue);

if (hashIndex === -1) {
// Line number not in hash, add it
currentHashLines.push(hashValue);
} else {
// Line number already in hash, remove it
currentHashLines.splice(hashIndex, 1);
}
}


// this will prevent scrolling since the span is obviously in view
scrollIntoView = false;
location.hash = hash;
location.hash = id + "." + currentHashLines.join(',');
setTimeout(function () {
scrollIntoView = true;
}, 1);

rangeStart = null; // Reset the range start
};
});
}
Expand Down