1
+ // The initial SF app object
1
2
// Create the initial formatter element
2
- var formatter ;
3
+ var sf = {
4
+ formatter : null
5
+ } ;
3
6
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 ;
8
9
if ( document . body . children [ 0 ] . id == "pagewrapper" ) {
9
- version = 2 ;
10
+ sf . version = 2 ;
10
11
}
11
12
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 = [
13
18
{
14
19
"name" : "bold" ,
15
20
"tag" : "b" ,
@@ -50,7 +55,7 @@ var tags = [
50
55
"name" : "link" ,
51
56
"tag" : "link" ,
52
57
"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]" ] ,
54
59
"formatter" : function ( part1 , part2 ) {
55
60
return "<a href='" + part1 + "' target='_newtab'>" + part2 + "</a>" ;
56
61
}
@@ -70,12 +75,11 @@ var tags = [
70
75
"src" : "https://raw.githubusercontent.com/Remix-Design/RemixIcon/master/icons/Development/bug-line.svg" ,
71
76
"ignore" : true
72
77
}
73
- ]
78
+ ] ;
74
79
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 ( ) {
79
83
var textareaFinder = "[name=compose-comment],[name=content]" ;
80
84
81
85
// Helpful first textarea message
@@ -87,33 +91,38 @@ setTimeout(function() {
87
91
return ;
88
92
}
89
93
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
95
99
continue ;
96
100
}
97
101
98
102
var icon = document . createElement ( "img" ) ;
99
- icon . src = tags [ t ] . src ;
103
+ icon . src = sf . tags [ t ] . src ;
100
104
101
105
// Help icon
102
- if ( tags [ t ] . help ) {
106
+ if ( sf . tags [ t ] . help ) {
103
107
icon . style . float = "right" ;
104
108
icon . onclick = function ( ) {
105
109
// 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
+ }
110
119
}
111
120
112
- formatter . appendChild ( icon ) ;
121
+ sf . formatter . appendChild ( icon ) ;
113
122
continue ;
114
123
}
115
124
116
- icon . fillers = tags [ t ] . fillers ;
125
+ icon . fillers = sf . tags [ t ] . fillers ;
117
126
118
127
// This may look janky, but with Chrome extensions,
119
128
// Everything is jank. Basically I have to set custom
@@ -149,78 +158,94 @@ setTimeout(function() {
149
158
textarea . focus ( ) ;
150
159
}
151
160
152
- formatter . appendChild ( icon ) ;
161
+ sf . formatter . appendChild ( icon ) ;
153
162
}
154
163
155
164
// Move formatter if user clicks on textarea.
156
165
document . body . onclick = function ( event ) {
166
+ // Note: duplicate of "textareaFinder"
157
167
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
+ }
160
174
}
161
175
}
162
176
163
177
// Initial background formatting loop.
164
178
setInterval ( function ( ) {
165
- format ( ) ;
179
+ sf . format ( ) ;
166
180
} , 300 ) ;
167
- } , 1000 ) ;
181
+ }
168
182
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 ( ) {
171
191
// Quit if we already formatted those comments.
172
192
// Checks for last vs new length.
173
193
var comments = document . querySelectorAll ( ".content, .emoji-text" ) ;
174
- if ( oldComments == comments . length ) {
194
+ if ( sf . oldComments == comments . length ) {
175
195
return ;
176
196
}
177
197
178
- oldComments = comments . length ;
198
+ sf . oldComments = comments . length ;
179
199
180
200
for ( var c = 0 ; c < comments . length ; c ++ ) {
181
201
comments [ c ] . style . whiteSpace = "pre-line" ;
182
202
if ( comments [ c ] . className == "emoji-text" ) {
183
203
comments [ c ] . style . marginLeft = "3px" ;
184
204
}
185
205
186
- comments [ c ] . innerHTML = parse ( comments [ c ] . innerHTML ) ;
206
+ comments [ c ] . innerHTML = sf . parse ( comments [ c ] . innerHTML ) ;
187
207
}
188
208
}
189
209
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 ) {
192
217
// Note that the new scratchformat standard is [],
193
218
// and the () is outdated, and a bit harder to type.
194
219
// But, we will detect both for historical reasons
195
220
var startBracket = "[\\(|\\[]" ;
196
221
var endBracket = "[\\)|\\]]" ;
197
222
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 ) {
200
225
continue ;
201
226
}
202
227
203
228
// First part of tag
204
229
var regex = "" ;
205
230
regex += startBracket ;
206
- regex += tags [ t ] . tag ;
231
+ regex += sf . tags [ t ] . tag ;
207
232
regex += "[=]*([^\\]\\[\\)\\(]*)" ;
208
233
regex += endBracket ;
209
234
210
235
// If just 1 tag (Ex [br])
211
- if ( tags [ t ] . fillers . length > 1 ) {
236
+ if ( sf . tags [ t ] . fillers . length > 1 ) {
212
237
// Lazy matching (?)
213
238
regex += "(.*?)" ;
214
239
215
240
// Second part of tag
216
241
regex += startBracket ;
217
242
regex += "\/" ;
218
- regex += tags [ t ] . tag ;
243
+ regex += sf . tags [ t ] . tag ;
219
244
regex += endBracket ;
220
245
}
221
- console . log ( regex ) ;
246
+
222
247
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" ) ) ;
224
249
}
225
250
226
251
// Format trailing breaklines and spaces
0 commit comments