Skip to content

Commit 2e67007

Browse files
committed
add support for other panel to highlight
1 parent 53a6561 commit 2e67007

2 files changed

Lines changed: 29 additions & 21 deletions

File tree

elixir/web.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,6 @@ def generate_source_page(ctx: RequestContext, q: Query,
796796
'title_path': title_path,
797797
'path': path,
798798
'breadcrumb_urls': breadcrumb_urls,
799-
'breadcrumb_links': breadcrumb_links,
800799
'diff_mode_available': True,
801800

802801
**template_ctx,

static/script.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,19 @@ function setupSidebarSwitch() {
113113
});
114114
}
115115

116-
// Parse and validate line identifier in format L${number}
117-
function parseLineId(lineId) {
118-
if (lineId[0] != "L") {
119-
return;
116+
function getPrefix(lineId) {
117+
if (lineId[0] == "L") {
118+
return "L";
119+
} else if (lineId.startsWith("OL")) {
120+
return "OL";
121+
} else {
122+
return "";
120123
}
124+
}
121125

122-
let lineIdNum = parseInt(lineId.substring(1));
126+
// Parse and validate line identifier in format L${number}
127+
function parseLineId(lineId) {
128+
const lineIdNum = parseInt(lineId.substring(getPrefix(lineId).length));
123129
console.assert(!isNaN(lineIdNum), "Invalid line id");
124130

125131
let lineElement = document.getElementById(lineId);
@@ -151,16 +157,16 @@ function parseLineRangeAnchor(hashStr) {
151157
firstLine = lineTmp;
152158
}
153159

154-
return [firstLine, lastLine];
160+
return [firstLine, lastLine, getPrefix(hash[0])];
155161
}
156162

157163
// Highlights line number elements from firstLine to lastLine
158-
function highlightFromTo(firstLine, lastLine) {
159-
const firstLineElement = document.getElementById(`L${ firstLine }`);
160-
const lastLineElement = document.getElementById(`L${ lastLine }`);
164+
function highlightFromTo(firstLine, lastLine, prefix='L') {
165+
const firstLineElement = document.getElementById(`${ prefix }${ firstLine }`);
166+
const lastLineElement = document.getElementById(`${ prefix }${ lastLine }`);
161167

162-
const firstCodeLine = document.getElementById(`codeline-${ firstLine }`);
163-
const lastCodeLine = document.getElementById(`codeline-${ lastLine }`);
168+
const firstCodeLine = firstLineElement.parentNode;
169+
const lastCodeLine = lastLineElement.parentNode;
164170

165171
addClassToRangeOfElements(firstLineElement.parentNode, lastLineElement.parentNode, "line-highlight");
166172
addClassToRangeOfElements(firstCodeLine, lastCodeLine, "line-highlight");
@@ -192,20 +198,23 @@ function setupLineRangeHandlers() {
192198
return;
193199
}
194200

195-
let rangeStartLine, rangeEndLine;
201+
let rangeStartLine, rangeEndLine, rangePrefix;
196202

197203
const parseFromHash = () => {
198204
const highlightedRange = parseLineRangeAnchor(window.location.hash);
199205
// Set range start/end to elements from hash
200206
if (highlightedRange !== undefined) {
201207
rangeStartLine = highlightedRange[0];
202208
rangeEndLine = highlightedRange[1];
203-
highlightFromTo(rangeStartLine, rangeEndLine);
209+
rangePrefix = highlightedRange[2];
210+
highlightFromTo(rangeStartLine, rangeEndLine, rangePrefix);
204211
const wrapper = document.querySelector('.wrapper');
205-
const offsetTop = document.getElementById(`L${rangeStartLine}`).offsetTop;
212+
const offsetTop = document.getElementById(`${rangePrefix}${rangeStartLine}`).offsetTop;
206213
wrapper.scrollTop = offsetTop < 100 ? 200 : offsetTop + 100;
207214
} else if (location.hash !== "" && location.hash[1] === "L") {
208215
rangeStartLine = parseLineId(location.hash.substring(1));
216+
} else if (location.hash !== "" && location.hash.startsWith("OL")) {
217+
rangeStartLine = parseLineId(location.hash.substring(2));
209218
}
210219
}
211220

@@ -216,7 +225,6 @@ function setupLineRangeHandlers() {
216225

217226
parseFromHash();
218227

219-
console.log(lxrcode);
220228
lxrcode.forEach(el => el.addEventListener("click", ev => {
221229
if (ev.ctrlKey || ev.metaKey) {
222230
return;
@@ -232,16 +240,17 @@ function setupLineRangeHandlers() {
232240

233241
// Handler is set on the element that contains all line numbers, check if the
234242
// event is directed at an actual line number element
235-
if (typeof(el.id) !== "string" || el.id[0] !== "L" || el.tagName !== "A") {
243+
if (typeof(el.id) !== "string" || !(el.id[0] == "L" || el.id.startsWith("OL")) || el.tagName !== "A") {
236244
return;
237245
}
238246

239247
clearRangeHighlight();
240248

241-
if (rangeStartLine === undefined || !ev.shiftKey) {
249+
if (rangeStartLine === undefined || !ev.shiftKey || rangePrefix != getPrefix(el.id)) {
242250
rangeStartLine = parseLineId(el.id);
251+
rangePrefix = getPrefix(el.id);
243252
rangeEndLine = undefined;
244-
highlightFromTo(rangeStartLine, rangeStartLine);
253+
highlightFromTo(rangeStartLine, rangeStartLine, rangePrefix);
245254
window.location.hash = el.id;
246255
} else if (ev.shiftKey) {
247256
if (rangeEndLine === undefined) {
@@ -277,8 +286,8 @@ function setupLineRangeHandlers() {
277286
}
278287
}
279288

280-
highlightFromTo(rangeStartLine, rangeEndLine);
281-
window.location.hash = `L${rangeStartLine}-L${rangeEndLine}`;
289+
highlightFromTo(rangeStartLine, rangeEndLine, rangePrefix);
290+
window.location.hash = `${rangePrefix}${rangeStartLine}-${rangePrefix}${rangeEndLine}`;
282291
}
283292
}));
284293
}

0 commit comments

Comments
 (0)