Skip to content
This repository was archived by the owner on Oct 28, 2022. It is now read-only.

Commit 06c71a5

Browse files
committed
Fix Projec comments bug, and parser bugs
1 parent 880462b commit 06c71a5

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

javascript.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,12 @@ var tags = [
6262
]
6363

6464
// Firstly, initialize the formatter, and its icons
65-
window.onload = function() {
66-
document.getElementsByName("content")[0].placeholder = "Click here to activate ScratchFormat";
65+
// This is a 1 second timeout for page load, since I am
66+
// too lazy to figure out real page load times
67+
setTimeout(function() {
68+
var textareaFinder = "[name=compose-comment],[name=content]";
69+
70+
document.querySelectorAll(textareaFinder)[0].placeholder = "Click here to activate ScratchFormat";
6771

6872
formatter = document.createElement("div");
6973
formatter.id = "formatter";
@@ -111,22 +115,23 @@ window.onload = function() {
111115

112116
// Move formatter if user clicks on textarea.
113117
document.body.onclick = function(event) {
114-
if (event.target.name == "content") {
118+
if (event.target.name == "content" || event.target.name == "compose-comment") {
115119
event.target.parentElement.prepend(formatter);
120+
formatter.style.width = event.target.style.width;
116121
}
117122
}
118123

119124
// Initial background formatting loop.
120125
setInterval(function() {
121126
format();
122127
}, 300);
123-
}
128+
}, 1000);
124129

125130
var oldComments = 0;
126131
function format() {
127132
// Quit if we already formatted those comments.
128133
// Checks for last vs new length.
129-
var comments = document.getElementsByClassName("content");
134+
var comments = document.querySelectorAll(".content, .emoji-text");
130135
if (oldComments == comments.length) {
131136
return;
132137
}
@@ -135,12 +140,16 @@ function format() {
135140

136141
for (var c = 0; c < comments.length; c++) {
137142
comments[c].style.whiteSpace = "pre";
143+
comments[c].style.marginLeft = "5px";
138144
comments[c].innerHTML = parse(comments[c].innerHTML);
139145
}
140146
}
141147

142148
// Custom regex parser. Easy to maintain.
143149
function parse(text) {
150+
// Note that the new scratchformat standard is [],
151+
// and the () is outdated, and a bit harder to type.
152+
// But, we will detect both for historical reasons
144153
var startBracket = "[\\(|\\[]";
145154
var endBracket = "[\\)|\\]]";
146155

@@ -155,7 +164,8 @@ function parse(text) {
155164

156165
// If just 1 tag (Ex [br])
157166
if (tags[t].fillers.length > 1) {
158-
regex += "(.*)";
167+
// Regex statement to parse anything but (), []
168+
regex += "([^\\)\\]\\[\\(]*)";
159169

160170
// Second part of tag
161171
regex += startBracket;
@@ -164,6 +174,7 @@ function parse(text) {
164174
regex += endBracket;
165175
}
166176

177+
console.log(regex);
167178
regex = new RegExp(regex, "gm");
168179
text = text.replace(regex, tags[t].formatter("$2", "$3"));
169180
}

style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#formatter {
33
border: 1px solid rgb(169, 169, 169);
44
box-shadow: inset 0 1px 1px #ccc;
5+
background-color: white;
56
border-radius: 3px;
67
margin-bottom: 10px;
78
width: 500px;

0 commit comments

Comments
 (0)