From 81221f4dfbb22d8b0a75915855c87ef99f3ceeaa Mon Sep 17 00:00:00 2001 From: Yam Mesicka Date: Mon, 25 Mar 2024 00:40:21 +0200 Subject: [PATCH] fix: Optimizations, full split --- lms/static/comments.js | 45 ------------------------------------------ lms/static/solution.js | 30 ++++++++++------------------ 2 files changed, 10 insertions(+), 65 deletions(-) diff --git a/lms/static/comments.js b/lms/static/comments.js index 4300a496..cd4dae77 100644 --- a/lms/static/comments.js +++ b/lms/static/comments.js @@ -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 === '') { - currentSpans.pop(); - } else if (phrase.startsWith(' { - 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 += ''.repeat(openSpans.length); - const wrappedLine = `
${i + 1} ${lineContent}
`; - 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', @@ -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); }); diff --git a/lms/static/solution.js b/lms/static/solution.js index 5fed22fd..4809ba66 100644 --- a/lms/static/solution.js +++ b/lms/static/solution.js @@ -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 === '') { - currentSpans.pop(); - } else if (phrase.startsWith(']*>|<\/span>/g; + let match; + + while ((match = spanRegex.exec(line)) !== null) { + if (match[0] === '') { + currentSpans.pop(); + } else { + currentSpans.push(match[0]); } } }