Skip to content

Commit f87079a

Browse files
authored
fix: Separate solution logic from comments.js (#378)
* fix: Separate solution logic from comments.js * fix: View now looks for solution.js * fix: Optimizations, full split
1 parent d092cd9 commit f87079a

File tree

3 files changed

+40
-47
lines changed

3 files changed

+40
-47
lines changed

lms/static/comments.js

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -137,50 +137,6 @@ function pullComments(fileId, callback) {
137137
xhr.send('');
138138
}
139139

140-
function updateOpenedSpans(currentSpans, line) {
141-
/* Because we have each line wrapped in it's own span, we must close
142-
* all the opened spans in this specific line and re-open them in the next
143-
* line. This function help us to manage the state of open span tags.
144-
*/
145-
let isCatching = false;
146-
let phrase = '';
147-
for (let i = 0; i < line.length; i += 1) {
148-
const c = line[i];
149-
if (c === '>') {
150-
isCatching = false;
151-
phrase = `<${phrase}>`;
152-
if (phrase === '</span>') {
153-
currentSpans.pop();
154-
} else if (phrase.startsWith('<span')) {
155-
currentSpans.push(phrase);
156-
}
157-
phrase = '';
158-
} else if (c === '<') {
159-
isCatching = true;
160-
} else if (isCatching) {
161-
phrase += c;
162-
}
163-
}
164-
}
165-
166-
function addLineSpansToPre(items) {
167-
const openSpans = [];
168-
Array.from(items).forEach((item) => {
169-
const code = item.innerHTML.trim().split('\n');
170-
const digits = code.length.toString().length;
171-
item.innerHTML = code.map(
172-
(line, i) => {
173-
let lineContent = openSpans.join('') + line;
174-
updateOpenedSpans(openSpans, line);
175-
lineContent += '</span>'.repeat(openSpans.length);
176-
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>`;
177-
return wrappedLine;
178-
},
179-
).join('\n');
180-
});
181-
window.dispatchEvent(new Event('lines-numbered'));
182-
}
183-
184140
class LineComment extends HTMLElement {
185141
static observedAttributes = [
186142
'data-line', 'avatar', 'name', 'date', 'editor', 'data-comment-id',
@@ -295,6 +251,5 @@ window.addEventListener('load', () => {
295251
sessionStorage.setItem('allowedComment', codeElementData.allowedComment);
296252
customElements.define('comment-line', LineComment);
297253
configureMarkdownParser();
298-
addLineSpansToPre(document.getElementsByTagName('code'));
299254
pullComments(window.fileId, treatComments);
300255
});

lms/static/solution.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function updateOpenedSpans(currentSpans, line) {
2+
/* This function manages the state of open span tags by using regular expressions
3+
* to find span tags and adjust the currentSpans array accordingly.
4+
*/
5+
const spanRegex = /<span[^>]*>|<\/span>/g;
6+
let match;
7+
8+
while ((match = spanRegex.exec(line)) !== null) {
9+
if (match[0] === '</span>') {
10+
currentSpans.pop();
11+
} else {
12+
currentSpans.push(match[0]);
13+
}
14+
}
15+
}
16+
17+
function addLineSpansToPre(items) {
18+
const openSpans = [];
19+
Array.from(items).forEach((item) => {
20+
const code = item.innerHTML.trim().split('\n');
21+
const digits = code.length.toString().length;
22+
item.innerHTML = code.map(
23+
(line, i) => {
24+
let lineContent = openSpans.join('') + line;
25+
updateOpenedSpans(openSpans, line);
26+
lineContent += '</span>'.repeat(openSpans.length);
27+
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>`;
28+
return wrappedLine;
29+
},
30+
).join('\n');
31+
});
32+
window.dispatchEvent(new Event('lines-numbered'));
33+
}
34+
35+
window.addEventListener('load', () => {
36+
addLineSpansToPre(document.getElementsByTagName('code'));
37+
});

lms/templates/view.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,11 @@ <h2>{{ _('Comments for this exercise') }}</h2>
146146
</div>
147147
</div>
148148
<script src="{{ url_for('static', filename='prism.js') }}"></script>
149-
<script src="{{ url_for('static', filename='purify.js') }}"></script>
150-
<script src="{{ url_for('static', filename='markdown.js') }}"></script>
149+
<script src="{{ url_for('static', filename='solution.js') }}"></script>
151150
{% if not shared_url %}
152151
{% include 'comment-template.html' %}
152+
<script src="{{ url_for('static', filename='purify.js') }}"></script>
153+
<script src="{{ url_for('static', filename='markdown.js') }}"></script>
153154
<script src="{{ url_for('static', filename='comments.js') }}"></script>
154155
{%- if is_manager or config.USERS_COMMENTS %}
155156
<script src="{{ url_for('static', filename='grader.js') }}"></script>

0 commit comments

Comments
 (0)