Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions features/comment-newlines/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"title": "Comment Newlines",
"description": "Allows you to view and use multiple lines in comments, rather than just one row of text.",
"credits": [
{ "username": "rgantzos", "url": "https://scratch.mit.edu/users/rgantzos/" }
],
"type": ["Website"],
"tags": ["New"],
"dynamic": true,
"scripts": [{ "file": "script.js", "runOn": "/users/*", "module": true }],
"styles": [{ "file": "style.css", "runOn": "/*" }]
}
56 changes: 56 additions & 0 deletions features/comment-newlines/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export default async function ({ feature, console, className }) {
const FORMATTED_CLASS = className("formatted newline comment");

// 2.0 comments
feature.page.waitForElements(
"ul.comments .comment .info",
function (comment) {
if (comment.querySelector(".content." + FORMATTED_CLASS)) return;
const content = comment.querySelector(".content");

const clonedContent = content.cloneNode(true);
clonedContent.classList.add(FORMATTED_CLASS);
comment.insertBefore(clonedContent, content);
feature.self.hideOnDisable(clonedContent);
trimCommentText(clonedContent);
}
);

function trimCommentText(el) {
let first = el.firstChild;
while (
first &&
first.nodeType === Node.TEXT_NODE &&
!first.textContent.trim()
) {
el.removeChild(first);
first = el.firstChild;
}

if (first) {
if (first.nodeType === Node.ELEMENT_NODE && first.tagName === "A") {
let afterA = first.nextSibling;
if (afterA && afterA.nodeType === Node.TEXT_NODE) {
afterA.textContent = afterA.textContent.replace(/^\s+/, " "); // ensure 1 space
} else if (!afterA || afterA.nodeType !== Node.TEXT_NODE) {
first.after(document.createTextNode(" "));
}
} else if (first.nodeType === Node.TEXT_NODE) {
first.textContent = first.textContent.replace(/^\s+/, "");
}
}

let last = el.lastChild;
while (
last &&
last.nodeType === Node.TEXT_NODE &&
!last.textContent.trim()
) {
el.removeChild(last);
last = el.lastChild;
}
if (last && last.nodeType === Node.TEXT_NODE) {
last.textContent = last.textContent.replace(/\s+$/, "");
}
}
}
11 changes: 11 additions & 0 deletions features/comment-newlines/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ul.comments .comment .info .content:not(.ste-formatted-newline-comment) {
display: none !important;
}

.ste-formatted-newline-comment {
white-space: pre-wrap !important;
}

.flex-row.comments-list .comment .comment-content, .comment .comment-body .comment-content {
white-space: pre-wrap !important;
}
16 changes: 12 additions & 4 deletions features/features.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[
{
"version": 2,
"id": "comment-newlines",
"versionAdded": "v5.0.0"
},
{
"version": 2,
"id": "recent-followers-and-following",
Expand All @@ -22,7 +27,7 @@
{
"version": 2,
"id": "copy-paste-lists",
"versionAdded": "v4.2.0"
"versionAdded": "v5.0.0"
},
{
"version": 2,
Expand Down Expand Up @@ -87,7 +92,8 @@
{
"version": 2,
"id": "align-to-center",
"versionAdded": "v4.0.0"
"versionAdded": "v4.0.0",
"versionUpdated": "v5.0.0"
},
{
"version": 2,
Expand Down Expand Up @@ -243,7 +249,8 @@
{
"version": 2,
"id": "follow-on-projects",
"versionAdded": "v3.5.0"
"versionAdded": "v3.5.0",
"versionUpdated": "v5.0.0"
},
{
"version": 2,
Expand Down Expand Up @@ -323,7 +330,8 @@
{
"version": 2,
"id": "localized-explore",
"versionAdded": "v3.2.0"
"versionAdded": "v3.2.0",
"versionUpdated": "v5.0.0"
},
{
"version": 2,
Expand Down