Skip to content

Commit aa55ff7

Browse files
committed
Remaining replies
1 parent ef8e501 commit aa55ff7

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

features/features.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
[
2+
{
3+
"version": 2,
4+
"id": "remaining-replies",
5+
"versionAdded": "v4.0.0"
6+
},
27
{
38
"version": 2,
49
"id": "video-recorder",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"title": "Remaining Replies",
3+
"description": "Shows how many more replies are allowed in a thread of studio comments.",
4+
"credits": [
5+
{
6+
"url": "https://scratch.mit.edu/users/rgantzos/",
7+
"username": "rgantzos"
8+
}
9+
],
10+
"type": ["Website"],
11+
"dynamic": true,
12+
"scripts": [{ "file": "script.js", "runOn": "/studios/*" }]
13+
}
14+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
export default async function ({ feature, console }) {
2+
window.feature = feature
3+
4+
ScratchTools.waitForElements(".flex-row.comment", function (comment) {
5+
let data = feature.redux
6+
.getState()
7+
.comments.comments.find(
8+
(c) => c.id.toString() === comment.id.split("-")[1]
9+
);
10+
11+
if (data) {
12+
let replyCount =
13+
feature.redux.getState().comments.replies[data.id]?.length || 0;
14+
let repliesLeft = 25 - replyCount;
15+
16+
updateReply(data.id, repliesLeft);
17+
} else {
18+
let parent = findParent(Number(comment.id.split("-")[1]));
19+
20+
if (parent) {
21+
let replyCount =
22+
feature.redux.getState().comments.replies[parent]?.length || 0;
23+
let repliesLeft = 25 - replyCount;
24+
updateReply(parent, repliesLeft);
25+
} else {
26+
console.log("nope")
27+
}
28+
}
29+
});
30+
31+
function findParent(replyId) {
32+
let replies = feature.redux.getState().comments.replies;
33+
let keys = Object.keys(replies);
34+
35+
let key = keys.find((k) => replies[k].find((r) => r.id === replyId));
36+
37+
return key ? Number(key) : null;
38+
}
39+
40+
function updateReply(commentId, count) {
41+
let div = document.querySelector(`.comment#comments-${commentId}`);
42+
if (!div) return;
43+
44+
let reply = div.querySelector(".comment-reply span");
45+
46+
if (reply.querySelector(".ste-reply-count")) {
47+
reply.querySelector(
48+
".ste-reply-count"
49+
).textContent = ` (${count.toString()} left)`;
50+
} else {
51+
let span = document.createElement("span");
52+
span.className = "ste-reply-count";
53+
feature.self.hideOnDisable(span)
54+
span.textContent = ` (${count.toString()} left)`;
55+
reply.appendChild(span);
56+
}
57+
58+
let data = feature.redux
59+
.getState()
60+
.comments.comments.find((c) => c.id.toString() === commentId.toString());
61+
62+
let replies = feature.redux.getState().comments.replies[commentId.toString()];
63+
64+
if (data && replies) {
65+
for (var i in replies) {
66+
updateReply(replies[i].id, count);
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)