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

Commit 711802b

Browse files
committed
Code clenaup, safer, and more 'perfect'
1 parent 8723014 commit 711802b

File tree

2 files changed

+71
-45
lines changed

2 files changed

+71
-45
lines changed

javascript.js

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1+
// The initial SF app object
12
// Create the initial formatter element
2-
var formatter;
3+
var sf = {
4+
formatter: null
5+
};
36

4-
// Everything is customizable here.
5-
// (local images don't work yet, but who cares.
6-
// this is the internet.)
7-
var version = 3;
7+
// Use a simple DOM element to check Scratch 3/2 UI.
8+
sf.version = 3;
89
if (document.body.children[0].id == "pagewrapper") {
9-
version = 2;
10+
sf.version = 2;
1011
}
1112

12-
var tags = [
13+
// Create the SF "tags"
14+
// Everything is customizable here.
15+
// (local images don't work yet, but who cares.
16+
// this is the internet.)
17+
sf.tags = [
1318
{
1419
"name": "bold",
1520
"tag": "b",
@@ -50,7 +55,7 @@ var tags = [
5055
"name": "link",
5156
"tag": "link",
5257
"src": "https://raw.githubusercontent.com/Remix-Design/RemixIcon/master/icons/Editor/link.svg",
53-
"fillers": ["[link=https://example.com]", "[/link]"],
58+
"fillers": ["[link=", "]Link[/link]"],
5459
"formatter": function(part1, part2) {
5560
return "<a href='" + part1 + "' target='_newtab'>" + part2 + "</a>";
5661
}
@@ -70,12 +75,11 @@ var tags = [
7075
"src": "https://raw.githubusercontent.com/Remix-Design/RemixIcon/master/icons/Development/bug-line.svg",
7176
"ignore": true
7277
}
73-
]
78+
];
7479

75-
// Firstly, initialize the formatter, and its icons
76-
// This is a 1 second timeout for page load, since I am
77-
// too lazy to figure out real page load times
78-
setTimeout(function() {
80+
// Firstly, initialize the formatter, and its icons.
81+
// This is executed on the next block
82+
sf.init = function() {
7983
var textareaFinder = "[name=compose-comment],[name=content]";
8084

8185
// Helpful first textarea message
@@ -87,33 +91,38 @@ setTimeout(function() {
8791
return;
8892
}
8993

90-
formatter = document.createElement("div");
91-
formatter.id = "formatter";
92-
for (var t = 0; t < tags.length; t++) {
93-
if (tags[t].dontshow) {
94-
// Basically, skip to next part
94+
sf.formatter = document.createElement("div");
95+
sf.formatter.id = "formatter";
96+
for (var t = 0; t < sf.tags.length; t++) {
97+
if (sf.tags[t].dontshow) {
98+
// Skip to next part int this loop
9599
continue;
96100
}
97101

98102
var icon = document.createElement("img");
99-
icon.src = tags[t].src;
103+
icon.src = sf.tags[t].src;
100104

101105
// Help icon
102-
if (tags[t].help) {
106+
if (sf.tags[t].help) {
103107
icon.style.float = "right";
104108
icon.onclick = function() {
105109
// Popup message HTML got a bit out of hand here
106-
smod.dialogText("ScratchFormat Help", `
107-
<a href="https://github.com/ScratchFormat/ScratchFormat2/issues" style="color: #12b1e4;">Report issues at our Github</a>
108-
If you do not own a Github account, simply comment on my profile <a href="https://scratch.mit.edu/users/pufflegamerz/" style="color: #12b1e4;">@pufflegamerz</a>
109-
`, version);
110+
if (sf.version == 2) {
111+
smod.dialogText(
112+
"ScratchFormat Help",
113+
`<a href="https://github.com/ScratchFormat/ScratchFormat2/issues" style="color: #12b1e4;">Report issues at our Github</a> If you do not own a Github account, simply comment on my profile <a href="https://scratch.mit.edu/users/pufflegamerz/" style="color: #12b1e4;">@pufflegamerz</a>`,
114+
sf.version
115+
);
116+
} else {
117+
prompt("Go here to report bugs:", "https://github.com/ScratchFormat/ScratchFormat2/issues");
118+
}
110119
}
111120

112-
formatter.appendChild(icon);
121+
sf.formatter.appendChild(icon);
113122
continue;
114123
}
115124

116-
icon.fillers = tags[t].fillers;
125+
icon.fillers = sf.tags[t].fillers;
117126

118127
// This may look janky, but with Chrome extensions,
119128
// Everything is jank. Basically I have to set custom
@@ -149,78 +158,94 @@ setTimeout(function() {
149158
textarea.focus();
150159
}
151160

152-
formatter.appendChild(icon);
161+
sf.formatter.appendChild(icon);
153162
}
154163

155164
// Move formatter if user clicks on textarea.
156165
document.body.onclick = function(event) {
166+
// Note: duplicate of "textareaFinder"
157167
if (event.target.name == "content" || event.target.name == "compose-comment") {
158-
event.target.parentElement.prepend(formatter);
159-
formatter.style.width = event.target.offsetWidth + "px";
168+
// Check if it already has formatter.
169+
// A somewhat messy solution, but it is fine.
170+
if (event.target.parentElement.children[0].id !== "formatter") {
171+
event.target.parentElement.prepend(sf.formatter);
172+
sf.formatter.style.width = event.target.offsetWidth + "px";
173+
}
160174
}
161175
}
162176

163177
// Initial background formatting loop.
164178
setInterval(function() {
165-
format();
179+
sf.format();
166180
}, 300);
167-
}, 1000);
181+
}
168182

169-
var oldComments = 0;
170-
function format() {
183+
// This is a 1 second timeout for page load, since I am
184+
// too lazy to figure out real page load times
185+
setTimeout(sf.init, 1000);
186+
187+
// Function to format comments that are not already
188+
// formatted
189+
sf.oldComments = 0;
190+
sf.format = function() {
171191
// Quit if we already formatted those comments.
172192
// Checks for last vs new length.
173193
var comments = document.querySelectorAll(".content, .emoji-text");
174-
if (oldComments == comments.length) {
194+
if (sf.oldComments == comments.length) {
175195
return;
176196
}
177197

178-
oldComments = comments.length;
198+
sf.oldComments = comments.length;
179199

180200
for (var c = 0; c < comments.length; c++) {
181201
comments[c].style.whiteSpace = "pre-line";
182202
if (comments[c].className == "emoji-text") {
183203
comments[c].style.marginLeft = "3px";
184204
}
185205

186-
comments[c].innerHTML = parse(comments[c].innerHTML);
206+
comments[c].innerHTML = sf.parse(comments[c].innerHTML);
187207
}
188208
}
189209

190-
// Custom regex parser. Easy to maintain.
191-
function parse(text) {
210+
// Custom regex SFML* parser. It parses differently than HTML. Instead
211+
// Of replacing [b] with <b>, it it replaces both tags with
212+
// text between them. Therefore, "[b][b]Hello[/b][/b]" will not work.
213+
// It doesn't really matter though, and won't be changed unless it
214+
// is able to cause significant issues in the future.
215+
// *ScratchFormat Markup Language (basically BBCode)
216+
sf.parse = function(text) {
192217
// Note that the new scratchformat standard is [],
193218
// and the () is outdated, and a bit harder to type.
194219
// But, we will detect both for historical reasons
195220
var startBracket = "[\\(|\\[]";
196221
var endBracket = "[\\)|\\]]";
197222

198-
for (var t = 0; t < tags.length; t++) {
199-
if (tags[t].ignore) {
223+
for (var t = 0; t < sf.tags.length; t++) {
224+
if (sf.tags[t].ignore) {
200225
continue;
201226
}
202227

203228
// First part of tag
204229
var regex = "";
205230
regex += startBracket;
206-
regex += tags[t].tag;
231+
regex += sf.tags[t].tag;
207232
regex += "[=]*([^\\]\\[\\)\\(]*)";
208233
regex += endBracket;
209234

210235
// If just 1 tag (Ex [br])
211-
if (tags[t].fillers.length > 1) {
236+
if (sf.tags[t].fillers.length > 1) {
212237
// Lazy matching (?)
213238
regex += "(.*?)";
214239

215240
// Second part of tag
216241
regex += startBracket;
217242
regex += "\/";
218-
regex += tags[t].tag;
243+
regex += sf.tags[t].tag;
219244
regex += endBracket;
220245
}
221-
console.log(regex);
246+
222247
regex = new RegExp(regex, "gm");
223-
text = text.replace(regex, tags[t].formatter("$1", "$2"));
248+
text = text.replace(regex, sf.tags[t].formatter("$1", "$2"));
224249
}
225250

226251
// Format trailing breaklines and spaces

smod.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var smod = {
3030
// This is bad, bad, bad. If anybody can find the native
3131
// way to create popups in Scratch 3.0 React.js, let me know.
3232
dialog3: function(title, text) {
33+
console.log("asd");
3334
var html = `"<div class="ReactModalPortal"><div class="modal-overlay modal-overlay"><div aria-label="Report Comment" class="modal-content mod-report modal-sizes modal-content mod-report" role="dialog" tabindex="-1"><div class="modal-content-close" onclick="this.parentElement.parentElement.parentElement.outerHTML = null;"><img alt="close-icon" class="modal-content-close-img" draggable="true" src="/svgs/modal/close-x.svg"></div><div><div class="report-modal-header" style="background-color: #395c79; box-shadow: inset 0 -1px 0 0 #001fff;"><div class="report-content-label"><span>` + title + `</span></div></div><div class="report-modal-content" style="padding-bottom: 50px;"><div><div class="instructions"><span>` + text +`</span></div></div></div></div></div></div>"</div>`;
3435
document.body.innerHTML += html;
3536
},

0 commit comments

Comments
 (0)