Skip to content

Commit

Permalink
refactor(scripts): little refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mesteery authored and Julien00859 committed Apr 22, 2021
1 parent 7e45897 commit 3cab767
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
36 changes: 20 additions & 16 deletions bin/assets/scripts/loc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,47 @@ const selectedLocs = document.getElementsByClassName('selected');
handleHash();

window.addEventListener('hashchange', () => {
[...selectedLocs].forEach((e) => e.classList.remove('selected'));
clearSelections();
handleHash();
})

locs.forEach((loc, i) => {
loc.addEventListener('click', () => {
// add the line location to the URL without affecting the history and without triggering a hashchange
window.history.replaceState(undefined, undefined, `#L${i + 1}`);
// remove all selected lines
[...selectedLocs].forEach((e) => e.classList.remove('selected'));
clearSelections();
loc.nextElementSibling.classList.add('selected');
})
})

function clearSelections() {
[...selectedLocs].forEach((element) => element.classList.remove('selected'));
}

function handleHash() {
const hashMatch = location.hash.match(/^(?:#L(\d+)(?:-L(\d+))?)$/i);
const hashMatch = location.hash.match(/^#L(\d+)(?:-L(\d+))?$/i);
if (!hashMatch) {
return;
}
let start = parseInt(hashMatch[1], 10);
let end = hashMatch[2] ? parseInt(hashMatch[2], 10) : undefined;

let start = +hashMatch[1];
let end = hashMatch[2] ? +hashMatch[2] : undefined;
if (end && start > end) {
[start, end] = [end, start];
}
if (start <= 0 || start > locs.length || end && end <= 0 || end > locs.length) {
if (start <= 0 || start > locs.length || end && (end <= 0 || end > locs.length)) {
return;
}
if (!end) {
locs[start - 1].nextElementSibling.classList.add('selected');

locs[start - 1].nextElementSibling.classList.add('selected');

if (!end || start === end) {
return;
}
for (let i = start - 1; i <= locs.length && i < end; i++) {
const line = locs[i].nextElementSibling;
line.classList.add('selected');
if (i === start - 1) {
// scroll to the first line of the selection
line.scrollIntoView();
}

// scroll to the first line of the selection
locs[start - 1].nextElementSibling.scrollIntoView();
for (let i = start; i < end; i++) {
locs[i].nextElementSibling.classList.add('selected');
}
}
2 changes: 1 addition & 1 deletion bin/assets/scripts/newform.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ code.addEventListener('keydown', (event) => {
const { value, selectionStart, selectionEnd } = code;

// inserts tab at the position of the caret
code.value = value.slice(0, selectionStart) + '\t' + value.slice(selectionEnd);
code.value = `${value.slice(0, selectionStart)}\t${value.slice(selectionEnd)}`;

// puts caret after the newly inserted tab char
const caretPos = selectionStart + 1;
Expand Down

0 comments on commit 3cab767

Please sign in to comment.