Skip to content

Commit 0af0136

Browse files
authored
Merge pull request #1024 from rgantzos/main
New feature: `comment-newlines`
2 parents 0d18e11 + 170479d commit 0af0136

File tree

4 files changed

+91
-4
lines changed

4 files changed

+91
-4
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"title": "Comment Newlines",
3+
"description": "Allows you to view and use multiple lines in comments, rather than just one row of text.",
4+
"credits": [
5+
{ "username": "rgantzos", "url": "https://scratch.mit.edu/users/rgantzos/" }
6+
],
7+
"type": ["Website"],
8+
"tags": ["New"],
9+
"dynamic": true,
10+
"scripts": [{ "file": "script.js", "runOn": "/users/*", "module": true }],
11+
"styles": [{ "file": "style.css", "runOn": "/*" }]
12+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
export default async function ({ feature, console, className }) {
2+
const FORMATTED_CLASS = className("formatted newline comment");
3+
4+
// 2.0 comments
5+
feature.page.waitForElements(
6+
"ul.comments .comment .info",
7+
function (comment) {
8+
if (comment.querySelector(".content." + FORMATTED_CLASS)) return;
9+
const content = comment.querySelector(".content");
10+
11+
const clonedContent = content.cloneNode(true);
12+
clonedContent.classList.add(FORMATTED_CLASS);
13+
comment.insertBefore(clonedContent, content);
14+
feature.self.hideOnDisable(clonedContent);
15+
trimCommentText(clonedContent);
16+
}
17+
);
18+
19+
function trimCommentText(el) {
20+
let first = el.firstChild;
21+
while (
22+
first &&
23+
first.nodeType === Node.TEXT_NODE &&
24+
!first.textContent.trim()
25+
) {
26+
el.removeChild(first);
27+
first = el.firstChild;
28+
}
29+
30+
if (first) {
31+
if (first.nodeType === Node.ELEMENT_NODE && first.tagName === "A") {
32+
let afterA = first.nextSibling;
33+
if (afterA && afterA.nodeType === Node.TEXT_NODE) {
34+
afterA.textContent = afterA.textContent.replace(/^\s+/, " "); // ensure 1 space
35+
} else if (!afterA || afterA.nodeType !== Node.TEXT_NODE) {
36+
first.after(document.createTextNode(" "));
37+
}
38+
} else if (first.nodeType === Node.TEXT_NODE) {
39+
first.textContent = first.textContent.replace(/^\s+/, "");
40+
}
41+
}
42+
43+
let last = el.lastChild;
44+
while (
45+
last &&
46+
last.nodeType === Node.TEXT_NODE &&
47+
!last.textContent.trim()
48+
) {
49+
el.removeChild(last);
50+
last = el.lastChild;
51+
}
52+
if (last && last.nodeType === Node.TEXT_NODE) {
53+
last.textContent = last.textContent.replace(/\s+$/, "");
54+
}
55+
}
56+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ul.comments .comment .info .content:not(.ste-formatted-newline-comment) {
2+
display: none !important;
3+
}
4+
5+
.ste-formatted-newline-comment {
6+
white-space: pre-wrap !important;
7+
}
8+
9+
.flex-row.comments-list .comment .comment-content, .comment .comment-body .comment-content {
10+
white-space: pre-wrap !important;
11+
}

features/features.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
[
2+
{
3+
"version": 2,
4+
"id": "comment-newlines",
5+
"versionAdded": "v5.0.0"
6+
},
27
{
38
"version": 2,
49
"id": "recent-followers-and-following",
@@ -22,7 +27,7 @@
2227
{
2328
"version": 2,
2429
"id": "copy-paste-lists",
25-
"versionAdded": "v4.2.0"
30+
"versionAdded": "v5.0.0"
2631
},
2732
{
2833
"version": 2,
@@ -87,7 +92,8 @@
8792
{
8893
"version": 2,
8994
"id": "align-to-center",
90-
"versionAdded": "v4.0.0"
95+
"versionAdded": "v4.0.0",
96+
"versionUpdated": "v5.0.0"
9197
},
9298
{
9399
"version": 2,
@@ -243,7 +249,8 @@
243249
{
244250
"version": 2,
245251
"id": "follow-on-projects",
246-
"versionAdded": "v3.5.0"
252+
"versionAdded": "v3.5.0",
253+
"versionUpdated": "v5.0.0"
247254
},
248255
{
249256
"version": 2,
@@ -323,7 +330,8 @@
323330
{
324331
"version": 2,
325332
"id": "localized-explore",
326-
"versionAdded": "v3.2.0"
333+
"versionAdded": "v3.2.0",
334+
"versionUpdated": "v5.0.0"
327335
},
328336
{
329337
"version": 2,

0 commit comments

Comments
 (0)