1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
/*
* comment-toolbar.js
* - Adds a toolbar above the commenting area containing most of 8Chan's formatting options
* - Press Esc to close quick-reply window when it's in focus
*
* Usage:
* $config['additional_javascript'][] = 'js/jquery.min.js';
* $config['additional_javascript'][] = 'js/comment-toolbar.js';
*/
if (active_page == 'thread' || active_page == 'index') {
var formatText = (function($){
"use strict";
var self = {};
self.rules = {
spoiler: {
text: 'Spoiler',
key: 's',
multiline: false,
exclusiveline: false,
prefix:'**',
suffix:'**'
},
italics: {
text: 'Italics',
key: 'i',
multiline: false,
exclusiveline: false,
prefix: "''",
suffix: "''"
},
bold: {
text: 'Bold',
key: 'b',
multiline: false,
exclusiveline: false,
prefix: "'''",
suffix: "'''"
},
underline: {
text: 'Underline',
key: 'u',
multiline: false,
exclusiveline: false,
prefix:'__',
suffix:'__'
},
code: {
text: 'Code',
key: 'f',
multiline: true,
exclusiveline: false,
prefix: '[code]',
suffix: '[/code]'
},
strike: {
text: 'Strike',
key: 'd',
multiline:false,
exclusiveline:false,
prefix:'~~',
suffix:'~~'
},
heading: {
text: 'Heading',
key: 'r',
multiline:false,
exclusiveline:true,
prefix:'==',
suffix:'=='
}
};
self.toolbar_wrap = function(node) {
var parent = $(node).parents('form[name="post"]');
self.wrap(parent.find('#body')[0],'textarea[name="body"]', parent.find('.format-text > select')[0].value, false);
};
self.wrap = function(ref, target, option, expandedwrap) {
// clean and validate arguments
if (ref == null) return;
var settings = {multiline: false, exclusiveline: false, prefix:'', suffix: null};
$.extend(settings,JSON.parse(localStorage.formatText_rules)[option]);
// resolve targets into array of proper node elements
// yea, this is overly verbose, oh well.
var res = [];
if (target instanceof Array) {
for (var indexa in target) {
if (target.hasOwnProperty(indexa)) {
if (typeof target[indexa] == 'string') {
var nodes = $(target[indexa]);
for (var indexb in nodes) {
if (indexa.hasOwnProperty(indexb)) res.push(nodes[indexb]);
}
} else {
res.push(target[indexa]);
}
}
}
} else {
if (typeof target == 'string') {
var nodes = $(target);
for (var index in nodes) {
if (nodes.hasOwnProperty(index)) res.push(nodes[index]);
}
} else {
res.push(target);
}
}
target = res;
//record scroll top to restore it later.
var scrollTop = ref.scrollTop;
//We will restore the selection later, so record the current selection
var selectionStart = ref.selectionStart;
var selectionEnd = ref.selectionEnd;
var text = ref.value;
var before = text.substring(0, selectionStart);
var selected = text.substring(selectionStart, selectionEnd);
var after = text.substring(selectionEnd);
var whiteSpace = [" ","\t"];
var breakSpace = ["\r","\n"];
var cursor;
// handles multiline selections on formatting that doesn't support spanning over multiple lines
if (!settings.multiline) selected = selected.replace(/(\r|\n|\r\n)/g,settings.suffix +"$1"+ settings.prefix);
// handles formatting that requires it to be on it's own line OR if the user wishes to expand the wrap to the nearest linebreak
if (settings.exclusiveline || expandedwrap) {
// buffer the begining of the selection until a linebreak
cursor = before.length -1;
while (cursor >= 0 && breakSpace.indexOf(before.charAt(cursor)) == -1) {
cursor--;
}
selected = before.substring(cursor +1) + selected;
before = before.substring(0, cursor +1);
// buffer the end of the selection until a linebreak
cursor = 0;
while (cursor < after.length && breakSpace.indexOf(after.charAt(cursor)) == -1) {
cursor++;
}
selected += after.substring(0, cursor);
after = after.substring(cursor);
}
// set values
var res = before + settings.prefix + selected + settings.suffix + after;
$(target).val(res);
// restore the selection area and scroll of the reference
ref.selectionEnd = before.length + settings.prefix.length + selected.length;
if (selectionStart === selectionEnd) {
ref.selectionStart = ref.selectionEnd;
} else {
ref.selectionStart = before.length + settings.prefix.length;
}
ref.scrollTop = scrollTop;
};
self.build_toolbars = function(){
if (localStorage.formatText_toolbar == 'true'){
// remove existing toolbars
if ($('.format-text').length > 0) $('.format-text').remove();
// Place toolbar above each textarea input
var name, options = '', rules = JSON.parse(localStorage.formatText_rules);
for (var index in rules) {
if (!rules.hasOwnProperty(index)) continue;
name = rules[index].text;
//add hint if key exists
if (rules[index].key) {
name += ' (CTRL + '+ rules[index].key.toUpperCase() +')';
}
options += '<option value="'+ index +'">'+ name +'</option>';
}
$('[name="body"]').before('<div class="format-text"><a href="javascript:;" onclick="formatText.toolbar_wrap(this);">Wrap</a><select>'+ options +'</select></div>');
$('body').append('<style>#quick-reply .format-text>a{width:15%;display:inline-block;text-align:center;}#quick-reply .format-text>select{width:85%;};</style>');
}
};
self.add_rule = function(rule, index){
if (rule === undefined) rule = {
text: 'New Rule',
key: '',
multiline:false,
exclusiveline:false,
prefix:'',
suffix:''
}
// generate an id for the rule
if (index === undefined) {
var rules = JSON.parse(localStorage.formatText_rules);
while (rules[index] || index === undefined) {
index = ''
index +='abcdefghijklmnopqrstuvwxyz'.substr(Math.floor(Math.random()*26),1);
index +='abcdefghijklmnopqrstuvwxyz'.substr(Math.floor(Math.random()*26),1);
index +='abcdefghijklmnopqrstuvwxyz'.substr(Math.floor(Math.random()*26),1);
}
}
if (window.Options && Options.get_tab('formatting')){
var html = $('<div class="format_rule" name="'+ index +'"></div>').html('\
<input type="text" name="text" class="format_option" size="10" value=\"'+ rule.text.replace(/"/g, '"') +'\">\
<input type="checkbox" name="multiline" class="format_option" '+ (rule.multiline ? 'checked' : '') +'>\
<input type="checkbox" name="exclusiveline" class="format_option" '+ (rule.exclusiveline ? 'checked' : '') +'>\
<input type="text" name="prefix" class="format_option" size="8" value=\"'+ (rule.prefix ? rule.prefix.replace(/"/g, '"') : '') +'\">\
<input type="text" name="suffix" class="format_option" size="8" value=\"'+ (rule.suffix ? rule.suffix.replace(/"/g, '"') : '') +'\">\
<input type="text" name="key" class="format_option" size="2" maxlength="1" value=\"'+ rule.key +'\">\
<input type="button" value="X" onclick="if(confirm(\'Do you wish to remove the '+ rule.text +' formatting rule?\'))$(this).parent().remove();">\
');
if ($('.format_rule').length > 0) {
$('.format_rule').last().after(html);
} else {
Options.extend_tab('formatting', html);
}
}
};
self.save_rules = function(){
var rule, newrules = {}, rules = $('.format_rule');
for (var index=0;rules[index];index++) {
rule = $(rules[index]);
newrules[rule.attr('name')] = {
text: rule.find('[name="text"]').val(),
key: rule.find('[name="key"]').val(),
prefix: rule.find('[name="prefix"]').val(),
suffix: rule.find('[name="suffix"]').val(),
multiline: rule.find('[name="multiline"]').is(':checked'),
exclusiveline: rule.find('[name="exclusiveline"]').is(':checked')
};
}
localStorage.formatText_rules = JSON.stringify(newrules);
self.build_toolbars();
};
self.reset_rules = function(to_default) {
$('.format_rule').remove();
var rules;
if (to_default) rules = self.rules;
else rules = JSON.parse(localStorage.formatText_rules);
for (var index in rules){
if (!rules.hasOwnProperty(index)) continue;
self.add_rule(rules[index], index);
}
};
// setup default rules for customizing
if (!localStorage.formatText_rules) localStorage.formatText_rules = JSON.stringify(self.rules);
// setup code to be ran when page is ready (work around for main.js compilation).
$(document).ready(function(){
// Add settings to Options panel general tab
if (window.Options && Options.get_tab('general')) {
var s1 = '#formatText_keybinds>input', s2 = '#formatText_toolbar>input', e = 'change';
Options.extend_tab('general', '\
<fieldset>\
<legend>Formatting Options</legend>\
<label id="formatText_keybinds"><input type="checkbox">' + _('Enable formatting keybinds') + '</label>\
<label id="formatText_toolbar"><input type="checkbox">' + _('Show formatting toolbar') + '</label>\
</fieldset>\
');
} else {
var s1 = '#formatText_keybinds', s2 = '#formatText_toolbar', e = 'click';
$('hr:first').before('<div id="formatText_keybinds" style="text-align:right"><a class="unimportant" href="javascript:void(0)">'+ _('Enable formatting keybinds') +'</a></div>');
$('hr:first').before('<div id="formatText_toolbar" style="text-align:right"><a class="unimportant" href="javascript:void(0)">'+ _('Show formatting toolbar') +'</a></div>');
}
// add the tab for customizing the format settings
if (window.Options && !Options.get_tab('formatting')) {
Options.add_tab('formatting', 'angle-right', 'Customize Formatting');
Options.extend_tab('formatting', '\
<style>\
.format_option{\
margin-right:5px;\
overflow:initial;\
font-size:15px;\
}\
.format_option[type="text"]{\
text-align:center;\
padding-bottom: 2px;\
padding-top: 2px;\
}\
.format_option:last-child{\
margin-right:0;\
}\
fieldset{\
margin-top:5px;\
}\
</style>\
');
// Data control row
Options.extend_tab('formatting', '\
<button onclick="formatText.add_rule();">Add Rule</button>\
<button onclick="formatText.save_rules();">Save Rules</button>\
<button onclick="formatText.reset_rules(false);">Revert</button>\
<button onclick="formatText.reset_rules(true);">Reset to Default</button>\
');
// Descriptor row
Options.extend_tab('formatting', '\
<span class="format_option" style="margin-left:25px;">Name</span>\
<span class="format_option" style="margin-left:45px;" title="Multi-line: Allow formatted area to contain linebreaks.">ML</span>\
<span class="format_option" style="margin-left:0px;" title="Exclusive-line: Require formatted area to start after and end before a linebreak.">EL</span>\
<span class="format_option" style="margin-left:25px;" title="Text injected at the start of a format area.">Prefix</span>\
<span class="format_option" style="margin-left:60px;" title="Text injected at the end of a format area.">Suffix</span>\
<span class="format_option" style="margin-left:40px;" title="Optional keybind value to allow keyboard shortcut access.">Key</span>\
');
// Rule rows
var rules = JSON.parse(localStorage.formatText_rules);
for (var index in rules){
if (!rules.hasOwnProperty(index)) continue;
self.add_rule(rules[index], index);
}
}
// setting for enabling formatting keybinds
$(s1).on(e, function(e) {
console.log('Keybind');
if (!localStorage.formatText_keybinds || localStorage.formatText_keybinds == 'false') {
localStorage.formatText_keybinds = 'true';
if (window.Options && Options.get_tab('general')) e.target.checked = true;
} else {
localStorage.formatText_keybinds = 'false';
if (window.Options && Options.get_tab('general')) e.target.checked = false;
}
});
// setting for toolbar injection
$(s2).on(e, function(e) {
console.log('Toolbar');
if (!localStorage.formatText_toolbar || localStorage.formatText_toolbar == 'false') {
localStorage.formatText_toolbar = 'true';
if (window.Options && Options.get_tab('general')) e.target.checked = true;
formatText.build_toolbars();
} else {
localStorage.formatText_toolbar = 'false';
if (window.Options && Options.get_tab('general')) e.target.checked = false;
$('.format-text').remove();
}
});
// make sure the tab settings are switch properly at loadup
if (window.Options && Options.get_tab('general')) {
if (localStorage.formatText_keybinds == 'true') $(s1)[0].checked = true;
else $(s1)[0].checked = false;
if (localStorage.formatText_toolbar == 'true') $(s2)[0].checked = true;
else $(s2)[0].checked = false;
}
// Initial toolbar injection
formatText.build_toolbars();
//attach listener to <body> so it also works on quick-reply box
$('body').on('keydown', '[name="body"]', function(e) {
if (!localStorage.formatText_keybinds || localStorage.formatText_keybinds == 'false') return;
var key = String.fromCharCode(e.which).toLowerCase();
var rules = JSON.parse(localStorage.formatText_rules);
for (var index in rules) {
if (!rules.hasOwnProperty(index)) continue;
if (key === rules[index].key && e.ctrlKey) {
e.preventDefault();
if (e.shiftKey) {
formatText.wrap(e.target, 'textarea[name="body"]', index, true);
} else {
formatText.wrap(e.target, 'textarea[name="body"]', index, false);
}
}
}
});
// Signal that comment-toolbar loading has completed.
$(document).trigger('formatText');
});
return self;
})(jQuery);
}
|