Skip to content

Commit

Permalink
fix: Optimizations, full split
Browse files Browse the repository at this point in the history
  • Loading branch information
yammesicka committed Mar 24, 2024
1 parent f276b16 commit 81221f4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 65 deletions.
45 changes: 0 additions & 45 deletions lms/static/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,50 +137,6 @@ function pullComments(fileId, callback) {
xhr.send('');
}

function updateOpenedSpans(currentSpans, line) {
/* Because we have each line wrapped in it's own span, we must close
* all the opened spans in this specific line and re-open them in the next
* line. This function help us to manage the state of open span tags.
*/
let isCatching = false;
let phrase = '';
for (let i = 0; i < line.length; i += 1) {
const c = line[i];
if (c === '>') {
isCatching = false;
phrase = `<${phrase}>`;
if (phrase === '</span>') {
currentSpans.pop();
} else if (phrase.startsWith('<span')) {
currentSpans.push(phrase);
}
phrase = '';
} else if (c === '<') {
isCatching = true;
} else if (isCatching) {
phrase += c;
}
}
}

function addLineSpansToPre(items) {
const openSpans = [];
Array.from(items).forEach((item) => {
const code = item.innerHTML.trim().split('\n');
const digits = code.length.toString().length;
item.innerHTML = code.map(
(line, i) => {
let lineContent = openSpans.join('') + line;
updateOpenedSpans(openSpans, line);
lineContent += '</span>'.repeat(openSpans.length);
const wrappedLine = `<div class="line-container" data-line="${i + 1}"><span class="line-number" style="width: ${digits}em">${i + 1}</span> <span data-line="${i + 1}" class="line">${lineContent}</span></div>`;
return wrappedLine;
},
).join('\n');
});
window.dispatchEvent(new Event('lines-numbered'));
}

class LineComment extends HTMLElement {
static observedAttributes = [
'data-line', 'avatar', 'name', 'date', 'editor', 'data-comment-id',
Expand Down Expand Up @@ -295,6 +251,5 @@ window.addEventListener('load', () => {
sessionStorage.setItem('allowedComment', codeElementData.allowedComment);
customElements.define('comment-line', LineComment);
configureMarkdownParser();
addLineSpansToPre(document.getElementsByTagName('code'));
pullComments(window.fileId, treatComments);
});
30 changes: 10 additions & 20 deletions lms/static/solution.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
function updateOpenedSpans(currentSpans, line) {
/* Because we have each line wrapped in it's own span, we must close
* all the opened spans in this specific line and re-open them in the next
* line. This function help us to manage the state of open span tags.
/* This function manages the state of open span tags by using regular expressions
* to find span tags and adjust the currentSpans array accordingly.
*/
let isCatching = false;
let phrase = '';
for (let i = 0; i < line.length; i += 1) {
const c = line[i];
if (c === '>') {
isCatching = false;
phrase = `<${phrase}>`;
if (phrase === '</span>') {
currentSpans.pop();
} else if (phrase.startsWith('<span')) {
currentSpans.push(phrase);
}
phrase = '';
} else if (c === '<') {
isCatching = true;
} else if (isCatching) {
phrase += c;
const spanRegex = /<span[^>]*>|<\/span>/g;
let match;

while ((match = spanRegex.exec(line)) !== null) {
if (match[0] === '</span>') {
currentSpans.pop();
} else {
currentSpans.push(match[0]);
}
}
}
Expand Down

0 comments on commit 81221f4

Please sign in to comment.