From 9265ebea43fe1aa6036a9106f115e5bb1420d301 Mon Sep 17 00:00:00 2001 From: Markerov Date: Thu, 30 Oct 2014 20:26:27 +0800 Subject: initial commit --- js/comment-toolbar.js | 260 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 260 insertions(+) create mode 100644 js/comment-toolbar.js (limited to 'js/comment-toolbar.js') diff --git a/js/comment-toolbar.js b/js/comment-toolbar.js new file mode 100644 index 00000000..7ae11a73 --- /dev/null +++ b/js/comment-toolbar.js @@ -0,0 +1,260 @@ +/* + * 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') { + $(document).ready(function () { + 'use strict'; + var formats = { + bold: { + displayText: 'B', + altText: 'bold', + styleCSS: 'font-weight: bold;', + options: { + prefix: "'''", + suffix: "'''" + }, + edit: function (box, options) { + wrapSelection(box, options); + }, + shortcutKey: 'b' + }, + italics: { + displayText: 'i', + altText: 'italics', + styleCSS: 'font-style: italic;', + options: { + prefix: "''", + suffix: "''" + }, + edit: function (box, options) { + wrapSelection(box, options); + }, + shortcutKey: 'i' + }, + under: { + displayText: 'U', + altText: 'underline', + styleCSS: 'text-decoration: underline;', + options: { + prefix: '__', + suffix: '__' + }, + edit: function (box, options) { + wrapSelection(box, options); + }, + shortcutKey: 'u' + }, + spoiler: { + displayText: 'spoiler', + altText: 'mark as spoiler', + styleCSS: '', + options: { + prefix: '[spoiler]', + suffix: '[/spoiler]' + }, + edit: function (box, options) { + wrapSelection(box, options); + }, + shortcutKey: 's' + }, + code: { + displayText: 'code', + altText: "code formatting", + styleCSS: 'font-family: "Courier New", Courier, monospace;', + options: { + prefix: '[code]', + suffix: '[/code]', + multiline: true + }, + edit: function (box, options) { + wrapSelection(box, options); + }, + shortcutKey: 'd' + }, + strike: { + displayText: 'strike', + altText: 'strikethrough', + styleCSS: 'text-decoration: line-through;', + options: { + prefix: '~~', + suffix: '~~' + }, + edit: function (box, options) { + wrapSelection(box, options); + } + }, + heading: { + displayText: 'heading', + altText: 'redtext', + styleCSS: 'color: #AF0A0F; font-weight: bold;', + options: { + prefix: '==', + suffix: '==', + exclusiveLine: true + }, + edit: function (box, options) { + wrapSelection(box, options); + } + } + }; + + var key, name, altText, ele; + var strBuilder = []; + var subStr = ''; + var styleRules = ''; + + //not exactly mine + var wrapSelection = function (box, options) { + if (box == null) { + return; + } + var prefix = options.prefix; + var suffix = options.suffix; + var multiline = options.multiline || false; + var exclusiveLine = options.exclusiveLine || false; + + //record scroll top to restore it later. + var scrollTop = box.scrollTop; + var selectionStart = box.selectionStart; + var selectionEnd = box.selectionEnd; + var text = box.value; + var beforeSelection = text.substring(0, selectionStart); + var selectedText = text.substring(selectionStart, selectionEnd); + var afterSelection = text.substring(selectionEnd); + + var breakSpace = ["\r","\n"]; + var trailingSpace = ""; + var cursor = selectedText.length - 1; + + //remove trailing space + while (cursor > 0 && selectedText[cursor] === " ") { + trailingSpace += " "; + cursor--; + } + selectedText = selectedText.substring(0, cursor + 1); + + if (!multiline) + selectedText = selectedText.replace(/(\r|\n|\r\n)/g, suffix +"$1"+ prefix); + + if (exclusiveLine) { + // buffer the begining of the selection until a linebreak + cursor = beforeSelection.length -1; + while (cursor >= 0 && breakSpace.indexOf(beforeSelection.charAt(cursor)) == -1) { + cursor--; + } + selectedText = beforeSelection.substring(cursor +1) + selectedText; + beforeSelection = beforeSelection.substring(0, cursor +1); + + // buffer the end of the selection until a linebreak + cursor = 0; + while (cursor < afterSelection.length && breakSpace.indexOf(afterSelection.charAt(cursor)) == -1) { + cursor++; + } + selectedText += afterSelection.substring(0, cursor); + afterSelection = afterSelection.substring(cursor); + } + + box.value = beforeSelection + prefix + selectedText + suffix + trailingSpace + afterSelection; + + box.selectionEnd = beforeSelection.length + prefix.length + selectedText.length; + if (selectionStart === selectionEnd) { + box.selectionStart = box.selectionEnd; + } else { + box.selectionStart = beforeSelection.length + prefix.length; + } + box.scrollTop = scrollTop; + }; + + /* Generate the HTML for the toolbar + */ + for (ele in formats) { + if (formats.hasOwnProperty(ele) && formats[ele].displayText != null) { + name = formats[ele].displayText; + altText = formats[ele].altText || ''; + key = formats[ele].shortcutKey; + + //add tooltip text + if (altText) { + if (key) { + altText += ' (ctrl+'+ key +')'; + } + altText = 'title="'+ altText +'"'; + } + + subStr = ''+ name +''; + strBuilder.push(subStr); + } else { + continue; + } + } + + $( 'textarea[name="body"]' ).before( '
' ); + $( '.tf-toolbar' ).html( strBuilder.join(' | ') ); + + /* Sets the CSS style + */ + styleRules = '\n/* generated by 8chan Formatting Tools */'+ + '\n.tf-toolbar {padding: 0px 5px 1px 5px;}'+ + '\n.tf-toolbar :link {text-decoration: none;}'; + for (ele in formats) { + if (formats.hasOwnProperty(ele) && formats[ele].styleCSS) { + styleRules += ' \n#tf-' + ele + ' {' + formats[ele].styleCSS + '}'; + } + } + //add CSS rule to user's custom CSS if it exist + if ($( '.user-css' ).length !== 0) { + $( '.user-css' ).append( styleRules ); + } else { + $( 'body' ).append( '' ); + } + + /* Attach event listeners + */ + $( 'body' ).on( 'keydown', 'textarea[name="body"]', {formats: formats}, function (e) { + //shortcuts + if (e.ctrlKey) { + var ch = String.fromCharCode(e.which).toLowerCase(); + var box = e.target; + var formats = e.data.formats; + for (var ele in formats) { + if (formats.hasOwnProperty(ele) && (ch === formats[ele].shortcutKey)) { + formats[ele].edit(box, formats[ele].options); + e.preventDefault(); + } + } + } + }); + $( 'body' ).on( 'keydown', '#quick-reply textarea[name="body"]', {formats: formats}, function (e) { + //close quick reply when esc is prssed + if (e.which === 27) { + $( '.close-btn' ).trigger( 'click' ); + } + }); + $( 'body' ).on( 'click', '.tf-toolbar a[id]', {formats: formats}, function (e) { + //toolbar buttons + var formats = e.data.formats; + var box = $(e.target).parent().next()[0]; + + for (var ele in formats) { + if (formats.hasOwnProperty(ele) && (e.target.id === 'tf-' + ele)) { + formats[ele].edit(box, formats[ele].options); + } + } + }); + // $( 'body' ).on( 'keydown', function (e) { + // if (e.which === 67 && + // e.target.nodeName !== 'INPUT' && //The C, the whole C, and nothing but the C + // e.target.nodeName !== 'TEXTAREA' && + // !(e.ctrlKey || e.altKey || e.shiftKey)) { + // document.location.href = '//'+ document.location.host +'/'+ board_name +'/catalog.html'; + // } + // }); + + }); +} -- cgit v1.3 From 86ddb4ecbbca36739da1993497376fb309c6b79f Mon Sep 17 00:00:00 2001 From: ForklessAnon Date: Fri, 31 Oct 2014 21:53:02 -0500 Subject: Update formatting toolbar to include user definable settings and customized options. --- js/comment-toolbar.js | 516 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 310 insertions(+), 206 deletions(-) (limited to 'js/comment-toolbar.js') diff --git a/js/comment-toolbar.js b/js/comment-toolbar.js index 7ae11a73..2c8b9ca8 100644 --- a/js/comment-toolbar.js +++ b/js/comment-toolbar.js @@ -8,253 +8,357 @@ * $config['additional_javascript'][] = 'js/comment-toolbar.js'; */ if (active_page == 'thread' || active_page == 'index') { - $(document).ready(function () { - 'use strict'; - var formats = { - bold: { - displayText: 'B', - altText: 'bold', - styleCSS: 'font-weight: bold;', - options: { - prefix: "'''", - suffix: "'''" - }, - edit: function (box, options) { - wrapSelection(box, options); - }, - shortcutKey: 'b' + var formatText = (function($){ + "use strict"; + var self = {}; + self.rules = { + spoiler: { + text: 'Spoiler', + key: 's', + multiline: false, + exclusiveline: false, + prefix:'**', + suffix:'**' }, italics: { - displayText: 'i', - altText: 'italics', - styleCSS: 'font-style: italic;', - options: { - prefix: "''", - suffix: "''" - }, - edit: function (box, options) { - wrapSelection(box, options); - }, - shortcutKey: 'i' + text: 'Italics', + key: 'i', + multiline: false, + exclusiveline: false, + prefix: "''", + suffix: "''" }, - under: { - displayText: 'U', - altText: 'underline', - styleCSS: 'text-decoration: underline;', - options: { - prefix: '__', - suffix: '__' - }, - edit: function (box, options) { - wrapSelection(box, options); - }, - shortcutKey: 'u' + bold: { + text: 'Bold', + key: 'b', + multiline: false, + exclusiveline: false, + prefix: "'''", + suffix: "'''" }, - spoiler: { - displayText: 'spoiler', - altText: 'mark as spoiler', - styleCSS: '', - options: { - prefix: '[spoiler]', - suffix: '[/spoiler]' - }, - edit: function (box, options) { - wrapSelection(box, options); - }, - shortcutKey: 's' + underline: { + text: 'Underline', + key: 'u', + multiline: false, + exclusiveline: false, + prefix:'__', + suffix:'__' }, code: { - displayText: 'code', - altText: "code formatting", - styleCSS: 'font-family: "Courier New", Courier, monospace;', - options: { - prefix: '[code]', - suffix: '[/code]', - multiline: true - }, - edit: function (box, options) { - wrapSelection(box, options); - }, - shortcutKey: 'd' + text: 'Code', + key: 'f', + multiline: true, + exclusiveline: false, + prefix: '[code]', + suffix: '[/code]' }, strike: { - displayText: 'strike', - altText: 'strikethrough', - styleCSS: 'text-decoration: line-through;', - options: { - prefix: '~~', - suffix: '~~' - }, - edit: function (box, options) { - wrapSelection(box, options); - } + text: 'Strike', + key: 'd', + multiline:false, + exclusiveline:false, + prefix:'~~', + suffix:'~~' }, heading: { - displayText: 'heading', - altText: 'redtext', - styleCSS: 'color: #AF0A0F; font-weight: bold;', - options: { - prefix: '==', - suffix: '==', - exclusiveLine: true - }, - edit: function (box, options) { - wrapSelection(box, options); - } + text: 'Heading', + key: 'r', + multiline:false, + exclusiveline:true, + prefix:'==', + suffix:'==' } }; - - var key, name, altText, ele; - var strBuilder = []; - var subStr = ''; - var styleRules = ''; - - //not exactly mine - var wrapSelection = function (box, options) { - if (box == null) { - return; + + self.toolbar_wrap = function(node) { + if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; + 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) { + if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; + // 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); + } } - var prefix = options.prefix; - var suffix = options.suffix; - var multiline = options.multiline || false; - var exclusiveLine = options.exclusiveLine || false; - + target = res; //record scroll top to restore it later. - var scrollTop = box.scrollTop; - var selectionStart = box.selectionStart; - var selectionEnd = box.selectionEnd; - var text = box.value; - var beforeSelection = text.substring(0, selectionStart); - var selectedText = text.substring(selectionStart, selectionEnd); - var afterSelection = text.substring(selectionEnd); + var scrollTop = ref.scrollTop; - var breakSpace = ["\r","\n"]; - var trailingSpace = ""; - var cursor = selectedText.length - 1; + //We will restore the selection later, so record the current selection + var selectionStart = ref.selectionStart; + var selectionEnd = ref.selectionEnd; - //remove trailing space - while (cursor > 0 && selectedText[cursor] === " ") { - trailingSpace += " "; - cursor--; - } - selectedText = selectedText.substring(0, cursor + 1); - - if (!multiline) - selectedText = selectedText.replace(/(\r|\n|\r\n)/g, suffix +"$1"+ prefix); - - if (exclusiveLine) { + 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 = beforeSelection.length -1; - while (cursor >= 0 && breakSpace.indexOf(beforeSelection.charAt(cursor)) == -1) { + cursor = before.length -1; + while (cursor >= 0 && breakSpace.indexOf(before.charAt(cursor)) == -1) { cursor--; } - selectedText = beforeSelection.substring(cursor +1) + selectedText; - beforeSelection = beforeSelection.substring(0, cursor +1); + 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 < afterSelection.length && breakSpace.indexOf(afterSelection.charAt(cursor)) == -1) { + while (cursor < after.length && breakSpace.indexOf(after.charAt(cursor)) == -1) { cursor++; } - selectedText += afterSelection.substring(0, cursor); - afterSelection = afterSelection.substring(cursor); + selected += after.substring(0, cursor); + after = after.substring(cursor); } - - box.value = beforeSelection + prefix + selectedText + suffix + trailingSpace + afterSelection; - - box.selectionEnd = beforeSelection.length + prefix.length + selectedText.length; + + // 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) { - box.selectionStart = box.selectionEnd; + ref.selectionStart = ref.selectionEnd; } else { - box.selectionStart = beforeSelection.length + prefix.length; + ref.selectionStart = before.length + settings.prefix.length; } - box.scrollTop = scrollTop; + ref.scrollTop = scrollTop; }; + + self.build_toolbars = function(){ + if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; + 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; - /* Generate the HTML for the toolbar - */ - for (ele in formats) { - if (formats.hasOwnProperty(ele) && formats[ele].displayText != null) { - name = formats[ele].displayText; - altText = formats[ele].altText || ''; - key = formats[ele].shortcutKey; - - //add tooltip text - if (altText) { - if (key) { - altText += ' (ctrl+'+ key +')'; + //add hint if key exists + if (rules[index].key) { + name += ' (CTRL + '+ rules[index].key.toUpperCase() +')'; } - altText = 'title="'+ altText +'"'; + options += ''; } - - subStr = ''+ name +''; - strBuilder.push(subStr); - } else { - continue; + $('[name="body"]').before('
Wrap
'); + $('body').append(''); } - } - - $( 'textarea[name="body"]' ).before( '
' ); - $( '.tf-toolbar' ).html( strBuilder.join(' | ') ); - - /* Sets the CSS style - */ - styleRules = '\n/* generated by 8chan Formatting Tools */'+ - '\n.tf-toolbar {padding: 0px 5px 1px 5px;}'+ - '\n.tf-toolbar :link {text-decoration: none;}'; - for (ele in formats) { - if (formats.hasOwnProperty(ele) && formats[ele].styleCSS) { - styleRules += ' \n#tf-' + ele + ' {' + formats[ele].styleCSS + '}'; - } + }; + + self.add_rule = function(rule, index){ + if (rule === undefined) rule = { + text: 'New Rule', + key: '', + multiline:false, + exclusiveline:false, + prefix:'', + suffix:'' } - //add CSS rule to user's custom CSS if it exist - if ($( '.user-css' ).length !== 0) { - $( '.user-css' ).append( styleRules ); - } else { - $( 'body' ).append( '' ); + + // 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); + } } - - /* Attach event listeners - */ - $( 'body' ).on( 'keydown', 'textarea[name="body"]', {formats: formats}, function (e) { - //shortcuts - if (e.ctrlKey) { - var ch = String.fromCharCode(e.which).toLowerCase(); - var box = e.target; - var formats = e.data.formats; - for (var ele in formats) { - if (formats.hasOwnProperty(ele) && (ch === formats[ele].shortcutKey)) { - formats[ele].edit(box, formats[ele].options); - e.preventDefault(); - } + if (window.Options && Options.get_tab('formatting')){ + var html = $('
').html('\ + \ + \ + \ + \ + \ + \ + \ + '); + + if ($('.format_rule').length > 0) { + $('.format_rule').last().after(html); + } else { + Options.extend_tab('formatting', html); } } - }); - $( 'body' ).on( 'keydown', '#quick-reply textarea[name="body"]', {formats: formats}, function (e) { - //close quick reply when esc is prssed - if (e.which === 27) { - $( '.close-btn' ).trigger( 'click' ); + }; + + self.save_rules = function(){ + var newrules = {}, rules = $('.format_rule'); + for (var index=0;rules[index];index++) { + console.log(rules[index]); + newrules[$(rules[index]).attr('name')] = { + text: $(rules[index]).find('[name="text"]').val(), + key: $(rules[index]).find('[name="key"]').val(), + prefix: $(rules[index]).find('[name="prefix"]').val(), + suffix: $(rules[index]).find('[name="suffix"]').val(), + multiline: $(rules[index]).find('[name="multiline"]').is(':checked'), + exclusiveline: $(rules[index]).find('[name="exclusiveline"]').is(':checked') + }; + } + console.log(newrules); + localStorage.formatText_rules = JSON.stringify(newrules); + self.build_toolbars(); + }; + + self.reset_rules = function() { + localStorage.formatText_rules = JSON.stringify(self.rules); + }; + + // store default rules for customizing + if (!localStorage.formatText_rules) self.reset_rules(); + + // Add settings to Options panel general tab + if (window.Options && Options.get_tab('general')) { + var s1 = '#formatText_enable>input', s2 = '#formatText_toolbar>input', e = 'change'; + Options.extend_tab('general', ''); + Options.extend_tab('general', ''); + } else { + var s1 = '#formatText_enable', s2 = '#formatText_toolbar', e = 'click'; + $('hr:first').before(''); + $('hr:first').before(''); + } + + // setting for enableing text formatting + $(s1).on(e, function(e) { + if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') { + localStorage.formatText_enable = 'true'; + if (window.Options && Options.get_tab('general')) e.target.checked = true; + } else { + localStorage.formatText_enable = 'false'; + if (window.Options && Options.get_tab('general')) e.target.checked = false; } }); - $( 'body' ).on( 'click', '.tf-toolbar a[id]', {formats: formats}, function (e) { - //toolbar buttons - var formats = e.data.formats; - var box = $(e.target).parent().next()[0]; - - for (var ele in formats) { - if (formats.hasOwnProperty(ele) && (e.target.id === 'tf-' + ele)) { - formats[ele].edit(box, formats[ele].options); - } + + // setting for toolbar injection + $(s2).on(e, function(e) { + 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(); } }); - // $( 'body' ).on( 'keydown', function (e) { - // if (e.which === 67 && - // e.target.nodeName !== 'INPUT' && //The C, the whole C, and nothing but the C - // e.target.nodeName !== 'TEXTAREA' && - // !(e.ctrlKey || e.altKey || e.shiftKey)) { - // document.location.href = '//'+ document.location.host +'/'+ board_name +'/catalog.html'; - // } - // }); + + // make sure the tab settings are switch properly at loadup + if (window.Options && Options.get_tab('general')) { + if (localStorage.formatText_enable == 'true') $(s1)[0].checked = true; + else $(s1)[0].checked = false; + if (localStorage.formatText_toolbar == 'true') $(s2)[0].checked = true; + else $(s2)[0].checked = false; + } + + // 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', '\ + \ + '); + + // Data control row + Options.extend_tab('formatting', '\ + \ + \ + \ + '); + + // Descriptor row + Options.extend_tab('formatting', '\ + Name\ + ML\ + EL\ + Prefix\ + Suffix\ + Key\ + '); + + // 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); + } + } + + return self; + })(jQuery); + + // run initial toolbar injection + formatText.build_toolbars(); + //attach listeners to so it also works on quick-reply box + $('body').on('keydown', '#body, #quick-reply #body', function(e) { + if (!localStorage.formatText_enable || localStorage.formatText_enable == '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); + } + } + } }); + $(document).trigger('formatText'); } -- cgit v1.3 From 69a6631742c2c2b464c5beca289619dca5760e45 Mon Sep 17 00:00:00 2001 From: ForklessAnon Date: Sat, 1 Nov 2014 16:49:34 -0500 Subject: Added option to disable/ignore keybinds. --- js/comment-toolbar.js | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'js/comment-toolbar.js') diff --git a/js/comment-toolbar.js b/js/comment-toolbar.js index 2c8b9ca8..74654868 100644 --- a/js/comment-toolbar.js +++ b/js/comment-toolbar.js @@ -226,7 +226,6 @@ if (active_page == 'thread' || active_page == 'index') { self.save_rules = function(){ var newrules = {}, rules = $('.format_rule'); for (var index=0;rules[index];index++) { - console.log(rules[index]); newrules[$(rules[index]).attr('name')] = { text: $(rules[index]).find('[name="text"]').val(), key: $(rules[index]).find('[name="key"]').val(), @@ -236,7 +235,6 @@ if (active_page == 'thread' || active_page == 'index') { exclusiveline: $(rules[index]).find('[name="exclusiveline"]').is(':checked') }; } - console.log(newrules); localStorage.formatText_rules = JSON.stringify(newrules); self.build_toolbars(); }; @@ -250,12 +248,19 @@ if (active_page == 'thread' || active_page == 'index') { // Add settings to Options panel general tab if (window.Options && Options.get_tab('general')) { - var s1 = '#formatText_enable>input', s2 = '#formatText_toolbar>input', e = 'change'; - Options.extend_tab('general', ''); - Options.extend_tab('general', ''); + var s1 = '#formatText_enable>input', s2 = '#formatText_keybinds>input', s3 = '#formatText_toolbar>input', e = 'change'; + Options.extend_tab('general', '\ +
\ + Formatting Options\ + \ + \ + \ +
\ + '); } else { - var s1 = '#formatText_enable', s2 = '#formatText_toolbar', e = 'click'; + var s1 = '#formatText_enable', s2 = '#formatText_keybinds', s3 = '#formatText_toolbar', e = 'click'; $('hr:first').before(''); + $('hr:first').before(''); $('hr:first').before(''); } @@ -270,8 +275,19 @@ if (active_page == 'thread' || active_page == 'index') { } }); - // setting for toolbar injection + // setting for enableing formatting keybinds $(s2).on(e, function(e) { + 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 + $(s3).on(e, function(e) { if (!localStorage.formatText_toolbar || localStorage.formatText_toolbar == 'false') { localStorage.formatText_toolbar = 'true'; if (window.Options && Options.get_tab('general')) e.target.checked = true; @@ -287,8 +303,10 @@ if (active_page == 'thread' || active_page == 'index') { if (window.Options && Options.get_tab('general')) { if (localStorage.formatText_enable == 'true') $(s1)[0].checked = true; else $(s1)[0].checked = false; - if (localStorage.formatText_toolbar == 'true') $(s2)[0].checked = true; + if (localStorage.formatText_keybinds == 'true') $(s2)[0].checked = true; else $(s2)[0].checked = false; + if (localStorage.formatText_toolbar == 'true') $(s2)[0].checked = true; + else $(s3)[0].checked = false; } // add the tab for customizing the format settings @@ -309,6 +327,9 @@ if (active_page == 'thread' || active_page == 'index') { .format_option:last-child{\ margin-right:0;\ }\ + fieldset{\ + margin-top:5px;\ + }\ \ '); @@ -346,6 +367,7 @@ if (active_page == 'thread' || active_page == 'index') { //attach listeners to so it also works on quick-reply box $('body').on('keydown', '#body, #quick-reply #body', function(e) { if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; + 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) { @@ -361,4 +383,4 @@ if (active_page == 'thread' || active_page == 'index') { } }); $(document).trigger('formatText'); -} +} \ No newline at end of file -- cgit v1.3 From 1b0f5fd24cf363d88460b0483f5b3d26c763b8ed Mon Sep 17 00:00:00 2001 From: Fredrick Brennan Date: Sun, 2 Nov 2014 07:08:01 +0800 Subject: Revert "Comment toolbar update and Thread stats addition" --- js/comment-toolbar.js | 538 +++++++++++++++++++------------------------------- stylesheets/style.css | 1 - 2 files changed, 206 insertions(+), 333 deletions(-) (limited to 'js/comment-toolbar.js') diff --git a/js/comment-toolbar.js b/js/comment-toolbar.js index 74654868..7ae11a73 100644 --- a/js/comment-toolbar.js +++ b/js/comment-toolbar.js @@ -8,379 +8,253 @@ * $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:'**' + $(document).ready(function () { + 'use strict'; + var formats = { + bold: { + displayText: 'B', + altText: 'bold', + styleCSS: 'font-weight: bold;', + options: { + prefix: "'''", + suffix: "'''" + }, + edit: function (box, options) { + wrapSelection(box, options); + }, + shortcutKey: 'b' }, italics: { - text: 'Italics', - key: 'i', - multiline: false, - exclusiveline: false, - prefix: "''", - suffix: "''" + displayText: 'i', + altText: 'italics', + styleCSS: 'font-style: italic;', + options: { + prefix: "''", + suffix: "''" + }, + edit: function (box, options) { + wrapSelection(box, options); + }, + shortcutKey: 'i' }, - bold: { - text: 'Bold', - key: 'b', - multiline: false, - exclusiveline: false, - prefix: "'''", - suffix: "'''" + under: { + displayText: 'U', + altText: 'underline', + styleCSS: 'text-decoration: underline;', + options: { + prefix: '__', + suffix: '__' + }, + edit: function (box, options) { + wrapSelection(box, options); + }, + shortcutKey: 'u' }, - underline: { - text: 'Underline', - key: 'u', - multiline: false, - exclusiveline: false, - prefix:'__', - suffix:'__' + spoiler: { + displayText: 'spoiler', + altText: 'mark as spoiler', + styleCSS: '', + options: { + prefix: '[spoiler]', + suffix: '[/spoiler]' + }, + edit: function (box, options) { + wrapSelection(box, options); + }, + shortcutKey: 's' }, code: { - text: 'Code', - key: 'f', - multiline: true, - exclusiveline: false, - prefix: '[code]', - suffix: '[/code]' + displayText: 'code', + altText: "code formatting", + styleCSS: 'font-family: "Courier New", Courier, monospace;', + options: { + prefix: '[code]', + suffix: '[/code]', + multiline: true + }, + edit: function (box, options) { + wrapSelection(box, options); + }, + shortcutKey: 'd' }, strike: { - text: 'Strike', - key: 'd', - multiline:false, - exclusiveline:false, - prefix:'~~', - suffix:'~~' + displayText: 'strike', + altText: 'strikethrough', + styleCSS: 'text-decoration: line-through;', + options: { + prefix: '~~', + suffix: '~~' + }, + edit: function (box, options) { + wrapSelection(box, options); + } }, heading: { - text: 'Heading', - key: 'r', - multiline:false, - exclusiveline:true, - prefix:'==', - suffix:'==' + displayText: 'heading', + altText: 'redtext', + styleCSS: 'color: #AF0A0F; font-weight: bold;', + options: { + prefix: '==', + suffix: '==', + exclusiveLine: true + }, + edit: function (box, options) { + wrapSelection(box, options); + } } }; - - self.toolbar_wrap = function(node) { - if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; - 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) { - if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; - // 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); - } + + var key, name, altText, ele; + var strBuilder = []; + var subStr = ''; + var styleRules = ''; + + //not exactly mine + var wrapSelection = function (box, options) { + if (box == null) { + return; } - target = res; - //record scroll top to restore it later. - var scrollTop = ref.scrollTop; + var prefix = options.prefix; + var suffix = options.suffix; + var multiline = options.multiline || false; + var exclusiveLine = options.exclusiveLine || false; - //We will restore the selection later, so record the current selection - var selectionStart = ref.selectionStart; - var selectionEnd = ref.selectionEnd; + //record scroll top to restore it later. + var scrollTop = box.scrollTop; + var selectionStart = box.selectionStart; + var selectionEnd = box.selectionEnd; + var text = box.value; + var beforeSelection = text.substring(0, selectionStart); + var selectedText = text.substring(selectionStart, selectionEnd); + var afterSelection = text.substring(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) { + var trailingSpace = ""; + var cursor = selectedText.length - 1; + + //remove trailing space + while (cursor > 0 && selectedText[cursor] === " ") { + trailingSpace += " "; + cursor--; + } + selectedText = selectedText.substring(0, cursor + 1); + + if (!multiline) + selectedText = selectedText.replace(/(\r|\n|\r\n)/g, suffix +"$1"+ prefix); + + if (exclusiveLine) { // buffer the begining of the selection until a linebreak - cursor = before.length -1; - while (cursor >= 0 && breakSpace.indexOf(before.charAt(cursor)) == -1) { + cursor = beforeSelection.length -1; + while (cursor >= 0 && breakSpace.indexOf(beforeSelection.charAt(cursor)) == -1) { cursor--; } - selected = before.substring(cursor +1) + selected; - before = before.substring(0, cursor +1); + selectedText = beforeSelection.substring(cursor +1) + selectedText; + beforeSelection = beforeSelection.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) { + while (cursor < afterSelection.length && breakSpace.indexOf(afterSelection.charAt(cursor)) == -1) { cursor++; } - selected += after.substring(0, cursor); - after = after.substring(cursor); + selectedText += afterSelection.substring(0, cursor); + afterSelection = afterSelection.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; + + box.value = beforeSelection + prefix + selectedText + suffix + trailingSpace + afterSelection; + + box.selectionEnd = beforeSelection.length + prefix.length + selectedText.length; if (selectionStart === selectionEnd) { - ref.selectionStart = ref.selectionEnd; + box.selectionStart = box.selectionEnd; } else { - ref.selectionStart = before.length + settings.prefix.length; + box.selectionStart = beforeSelection.length + prefix.length; } - ref.scrollTop = scrollTop; + box.scrollTop = scrollTop; }; - - self.build_toolbars = function(){ - if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; - 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() +')'; + /* Generate the HTML for the toolbar + */ + for (ele in formats) { + if (formats.hasOwnProperty(ele) && formats[ele].displayText != null) { + name = formats[ele].displayText; + altText = formats[ele].altText || ''; + key = formats[ele].shortcutKey; + + //add tooltip text + if (altText) { + if (key) { + altText += ' (ctrl+'+ key +')'; } - options += ''; - } - $('[name="body"]').before('
Wrap
'); - $('body').append(''); - } - }; - - 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); + altText = 'title="'+ altText +'"'; } + + subStr = ''+ name +''; + strBuilder.push(subStr); + } else { + continue; } - if (window.Options && Options.get_tab('formatting')){ - var html = $('
').html('\ - \ - \ - \ - \ - \ - \ - \ - '); - - if ($('.format_rule').length > 0) { - $('.format_rule').last().after(html); - } else { - Options.extend_tab('formatting', html); + } + + $( 'textarea[name="body"]' ).before( '
' ); + $( '.tf-toolbar' ).html( strBuilder.join(' | ') ); + + /* Sets the CSS style + */ + styleRules = '\n/* generated by 8chan Formatting Tools */'+ + '\n.tf-toolbar {padding: 0px 5px 1px 5px;}'+ + '\n.tf-toolbar :link {text-decoration: none;}'; + for (ele in formats) { + if (formats.hasOwnProperty(ele) && formats[ele].styleCSS) { + styleRules += ' \n#tf-' + ele + ' {' + formats[ele].styleCSS + '}'; } } - }; - - self.save_rules = function(){ - var newrules = {}, rules = $('.format_rule'); - for (var index=0;rules[index];index++) { - newrules[$(rules[index]).attr('name')] = { - text: $(rules[index]).find('[name="text"]').val(), - key: $(rules[index]).find('[name="key"]').val(), - prefix: $(rules[index]).find('[name="prefix"]').val(), - suffix: $(rules[index]).find('[name="suffix"]').val(), - multiline: $(rules[index]).find('[name="multiline"]').is(':checked'), - exclusiveline: $(rules[index]).find('[name="exclusiveline"]').is(':checked') - }; - } - localStorage.formatText_rules = JSON.stringify(newrules); - self.build_toolbars(); - }; - - self.reset_rules = function() { - localStorage.formatText_rules = JSON.stringify(self.rules); - }; - - // store default rules for customizing - if (!localStorage.formatText_rules) self.reset_rules(); - - // Add settings to Options panel general tab - if (window.Options && Options.get_tab('general')) { - var s1 = '#formatText_enable>input', s2 = '#formatText_keybinds>input', s3 = '#formatText_toolbar>input', e = 'change'; - Options.extend_tab('general', '\ -
\ - Formatting Options\ - \ - \ - \ -
\ - '); - } else { - var s1 = '#formatText_enable', s2 = '#formatText_keybinds', s3 = '#formatText_toolbar', e = 'click'; - $('hr:first').before(''); - $('hr:first').before(''); - $('hr:first').before(''); - } - - // setting for enableing text formatting - $(s1).on(e, function(e) { - if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') { - localStorage.formatText_enable = 'true'; - if (window.Options && Options.get_tab('general')) e.target.checked = true; + //add CSS rule to user's custom CSS if it exist + if ($( '.user-css' ).length !== 0) { + $( '.user-css' ).append( styleRules ); } else { - localStorage.formatText_enable = 'false'; - if (window.Options && Options.get_tab('general')) e.target.checked = false; + $( 'body' ).append( '' ); } - }); - - // setting for enableing formatting keybinds - $(s2).on(e, function(e) { - 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; + + /* Attach event listeners + */ + $( 'body' ).on( 'keydown', 'textarea[name="body"]', {formats: formats}, function (e) { + //shortcuts + if (e.ctrlKey) { + var ch = String.fromCharCode(e.which).toLowerCase(); + var box = e.target; + var formats = e.data.formats; + for (var ele in formats) { + if (formats.hasOwnProperty(ele) && (ch === formats[ele].shortcutKey)) { + formats[ele].edit(box, formats[ele].options); + e.preventDefault(); + } + } } }); - - // setting for toolbar injection - $(s3).on(e, function(e) { - 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(); + $( 'body' ).on( 'keydown', '#quick-reply textarea[name="body"]', {formats: formats}, function (e) { + //close quick reply when esc is prssed + if (e.which === 27) { + $( '.close-btn' ).trigger( 'click' ); } }); - - // make sure the tab settings are switch properly at loadup - if (window.Options && Options.get_tab('general')) { - if (localStorage.formatText_enable == 'true') $(s1)[0].checked = true; - else $(s1)[0].checked = false; - if (localStorage.formatText_keybinds == 'true') $(s2)[0].checked = true; - else $(s2)[0].checked = false; - if (localStorage.formatText_toolbar == 'true') $(s2)[0].checked = true; - else $(s3)[0].checked = false; - } - - // 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', '\ - \ - '); - - // Data control row - Options.extend_tab('formatting', '\ - \ - \ - \ - '); - - // Descriptor row - Options.extend_tab('formatting', '\ - Name\ - ML\ - EL\ - Prefix\ - Suffix\ - Key\ - '); - - // 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); - } - } - - return self; - })(jQuery); - - // run initial toolbar injection - formatText.build_toolbars(); - - //attach listeners to so it also works on quick-reply box - $('body').on('keydown', '#body, #quick-reply #body', function(e) { - if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; - 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); + $( 'body' ).on( 'click', '.tf-toolbar a[id]', {formats: formats}, function (e) { + //toolbar buttons + var formats = e.data.formats; + var box = $(e.target).parent().next()[0]; + + for (var ele in formats) { + if (formats.hasOwnProperty(ele) && (e.target.id === 'tf-' + ele)) { + formats[ele].edit(box, formats[ele].options); } } - } + }); + // $( 'body' ).on( 'keydown', function (e) { + // if (e.which === 67 && + // e.target.nodeName !== 'INPUT' && //The C, the whole C, and nothing but the C + // e.target.nodeName !== 'TEXTAREA' && + // !(e.ctrlKey || e.altKey || e.shiftKey)) { + // document.location.href = '//'+ document.location.host +'/'+ board_name +'/catalog.html'; + // } + // }); + }); - $(document).trigger('formatText'); -} \ No newline at end of file +} diff --git a/stylesheets/style.css b/stylesheets/style.css index ec07fe0a..0e56f442 100644 --- a/stylesheets/style.css +++ b/stylesheets/style.css @@ -896,7 +896,6 @@ div.thread:hover { right: 0px; text-align: left; font-size: 12px; - overflow-y: auto; } .options_tab h2 { -- cgit v1.3 From 1663efcf9d0dd85a49526d21e761bf3ee4a675d8 Mon Sep 17 00:00:00 2001 From: Forkless Date: Sat, 1 Nov 2014 22:28:16 -0500 Subject: Fix for update to comment toolbar (should work now) --- js/comment-toolbar.js | 549 ++++++++++++++++++++++++++++++------------------- js/options/user-css.js | 2 +- js/options/user-js.js | 2 +- 3 files changed, 344 insertions(+), 209 deletions(-) (limited to 'js/comment-toolbar.js') diff --git a/js/comment-toolbar.js b/js/comment-toolbar.js index 7ae11a73..be58c1f6 100644 --- a/js/comment-toolbar.js +++ b/js/comment-toolbar.js @@ -8,253 +8,388 @@ * $config['additional_javascript'][] = 'js/comment-toolbar.js'; */ if (active_page == 'thread' || active_page == 'index') { - $(document).ready(function () { - 'use strict'; - var formats = { - bold: { - displayText: 'B', - altText: 'bold', - styleCSS: 'font-weight: bold;', - options: { - prefix: "'''", - suffix: "'''" - }, - edit: function (box, options) { - wrapSelection(box, options); - }, - shortcutKey: 'b' + var formatText = (function($){ + "use strict"; + var self = {}; + self.rules = { + spoiler: { + text: 'Spoiler', + key: 's', + multiline: false, + exclusiveline: false, + prefix:'**', + suffix:'**' }, italics: { - displayText: 'i', - altText: 'italics', - styleCSS: 'font-style: italic;', - options: { - prefix: "''", - suffix: "''" - }, - edit: function (box, options) { - wrapSelection(box, options); - }, - shortcutKey: 'i' - }, - under: { - displayText: 'U', - altText: 'underline', - styleCSS: 'text-decoration: underline;', - options: { - prefix: '__', - suffix: '__' - }, - edit: function (box, options) { - wrapSelection(box, options); - }, - shortcutKey: 'u' + text: 'Italics', + key: 'i', + multiline: false, + exclusiveline: false, + prefix: "''", + suffix: "''" + }, + bold: { + text: 'Bold', + key: 'b', + multiline: false, + exclusiveline: false, + prefix: "'''", + suffix: "'''" }, - spoiler: { - displayText: 'spoiler', - altText: 'mark as spoiler', - styleCSS: '', - options: { - prefix: '[spoiler]', - suffix: '[/spoiler]' - }, - edit: function (box, options) { - wrapSelection(box, options); - }, - shortcutKey: 's' + underline: { + text: 'Underline', + key: 'u', + multiline: false, + exclusiveline: false, + prefix:'__', + suffix:'__' }, code: { - displayText: 'code', - altText: "code formatting", - styleCSS: 'font-family: "Courier New", Courier, monospace;', - options: { - prefix: '[code]', - suffix: '[/code]', - multiline: true - }, - edit: function (box, options) { - wrapSelection(box, options); - }, - shortcutKey: 'd' + text: 'Code', + key: 'f', + multiline: true, + exclusiveline: false, + prefix: '[code]', + suffix: '[/code]' }, strike: { - displayText: 'strike', - altText: 'strikethrough', - styleCSS: 'text-decoration: line-through;', - options: { - prefix: '~~', - suffix: '~~' - }, - edit: function (box, options) { - wrapSelection(box, options); - } + text: 'Strike', + key: 'd', + multiline:false, + exclusiveline:false, + prefix:'~~', + suffix:'~~' }, heading: { - displayText: 'heading', - altText: 'redtext', - styleCSS: 'color: #AF0A0F; font-weight: bold;', - options: { - prefix: '==', - suffix: '==', - exclusiveLine: true - }, - edit: function (box, options) { - wrapSelection(box, options); - } + text: 'Heading', + key: 'r', + multiline:false, + exclusiveline:true, + prefix:'==', + suffix:'==' } }; - - var key, name, altText, ele; - var strBuilder = []; - var subStr = ''; - var styleRules = ''; - - //not exactly mine - var wrapSelection = function (box, options) { - if (box == null) { - return; + + self.toolbar_wrap = function(node) { + if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; + 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) { + if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; + // 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); + } } - var prefix = options.prefix; - var suffix = options.suffix; - var multiline = options.multiline || false; - var exclusiveLine = options.exclusiveLine || false; - + target = res; //record scroll top to restore it later. - var scrollTop = box.scrollTop; - var selectionStart = box.selectionStart; - var selectionEnd = box.selectionEnd; - var text = box.value; - var beforeSelection = text.substring(0, selectionStart); - var selectedText = text.substring(selectionStart, selectionEnd); - var afterSelection = text.substring(selectionEnd); + var scrollTop = ref.scrollTop; - var breakSpace = ["\r","\n"]; - var trailingSpace = ""; - var cursor = selectedText.length - 1; + //We will restore the selection later, so record the current selection + var selectionStart = ref.selectionStart; + var selectionEnd = ref.selectionEnd; - //remove trailing space - while (cursor > 0 && selectedText[cursor] === " ") { - trailingSpace += " "; - cursor--; - } - selectedText = selectedText.substring(0, cursor + 1); - - if (!multiline) - selectedText = selectedText.replace(/(\r|\n|\r\n)/g, suffix +"$1"+ prefix); - - if (exclusiveLine) { + 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 = beforeSelection.length -1; - while (cursor >= 0 && breakSpace.indexOf(beforeSelection.charAt(cursor)) == -1) { + cursor = before.length -1; + while (cursor >= 0 && breakSpace.indexOf(before.charAt(cursor)) == -1) { cursor--; } - selectedText = beforeSelection.substring(cursor +1) + selectedText; - beforeSelection = beforeSelection.substring(0, cursor +1); + 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 < afterSelection.length && breakSpace.indexOf(afterSelection.charAt(cursor)) == -1) { + while (cursor < after.length && breakSpace.indexOf(after.charAt(cursor)) == -1) { cursor++; } - selectedText += afterSelection.substring(0, cursor); - afterSelection = afterSelection.substring(cursor); + selected += after.substring(0, cursor); + after = after.substring(cursor); } - - box.value = beforeSelection + prefix + selectedText + suffix + trailingSpace + afterSelection; - - box.selectionEnd = beforeSelection.length + prefix.length + selectedText.length; + + // 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) { - box.selectionStart = box.selectionEnd; + ref.selectionStart = ref.selectionEnd; } else { - box.selectionStart = beforeSelection.length + prefix.length; + ref.selectionStart = before.length + settings.prefix.length; } - box.scrollTop = scrollTop; + ref.scrollTop = scrollTop; }; + + self.build_toolbars = function(){ + if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; + 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; - /* Generate the HTML for the toolbar - */ - for (ele in formats) { - if (formats.hasOwnProperty(ele) && formats[ele].displayText != null) { - name = formats[ele].displayText; - altText = formats[ele].altText || ''; - key = formats[ele].shortcutKey; - - //add tooltip text - if (altText) { - if (key) { - altText += ' (ctrl+'+ key +')'; + //add hint if key exists + if (rules[index].key) { + name += ' (CTRL + '+ rules[index].key.toUpperCase() +')'; } - altText = 'title="'+ altText +'"'; + options += ''; } - - subStr = ''+ name +''; - strBuilder.push(subStr); - } else { - continue; + $('[name="body"]').before('
Wrap
'); + $('body').append(''); } - } - - $( 'textarea[name="body"]' ).before( '
' ); - $( '.tf-toolbar' ).html( strBuilder.join(' | ') ); - - /* Sets the CSS style - */ - styleRules = '\n/* generated by 8chan Formatting Tools */'+ - '\n.tf-toolbar {padding: 0px 5px 1px 5px;}'+ - '\n.tf-toolbar :link {text-decoration: none;}'; - for (ele in formats) { - if (formats.hasOwnProperty(ele) && formats[ele].styleCSS) { - styleRules += ' \n#tf-' + ele + ' {' + formats[ele].styleCSS + '}'; - } + }; + + self.add_rule = function(rule, index){ + if (rule === undefined) rule = { + text: 'New Rule', + key: '', + multiline:false, + exclusiveline:false, + prefix:'', + suffix:'' } - //add CSS rule to user's custom CSS if it exist - if ($( '.user-css' ).length !== 0) { - $( '.user-css' ).append( styleRules ); - } else { - $( 'body' ).append( '' ); + + // 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); + } } - - /* Attach event listeners - */ - $( 'body' ).on( 'keydown', 'textarea[name="body"]', {formats: formats}, function (e) { - //shortcuts - if (e.ctrlKey) { - var ch = String.fromCharCode(e.which).toLowerCase(); - var box = e.target; - var formats = e.data.formats; - for (var ele in formats) { - if (formats.hasOwnProperty(ele) && (ch === formats[ele].shortcutKey)) { - formats[ele].edit(box, formats[ele].options); - e.preventDefault(); - } + if (window.Options && Options.get_tab('formatting')){ + var html = $('
').html('\ + \ + \ + \ + \ + \ + \ + \ + '); + + 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); + + // Add settings to Options panel general tab + if (window.Options && Options.get_tab('general')) { + var s1 = '#formatText_enable>input', s2 = '#formatText_keybinds>input', s3 = '#formatText_toolbar>input', e = 'change'; + Options.extend_tab('general', '\ +
\ + Formatting Options\ + \ + \ + \ +
\ + '); + } else { + var s1 = '#formatText_enable', s2 = '#formatText_keybinds', s3 = '#formatText_toolbar', e = 'click'; + $('hr:first').before(''); + $('hr:first').before(''); + $('hr:first').before(''); + } + + // setting for enableing text formatting + $(s1).on(e, function(e) { + if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') { + localStorage.formatText_enable = 'true'; + if (window.Options && Options.get_tab('general')) e.target.checked = true; + } else { + localStorage.formatText_enable = 'false'; + if (window.Options && Options.get_tab('general')) e.target.checked = false; + } }); - $( 'body' ).on( 'keydown', '#quick-reply textarea[name="body"]', {formats: formats}, function (e) { - //close quick reply when esc is prssed - if (e.which === 27) { - $( '.close-btn' ).trigger( 'click' ); + + // setting for enableing formatting keybinds + $(s2).on(e, function(e) { + 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; } }); - $( 'body' ).on( 'click', '.tf-toolbar a[id]', {formats: formats}, function (e) { - //toolbar buttons - var formats = e.data.formats; - var box = $(e.target).parent().next()[0]; - - for (var ele in formats) { - if (formats.hasOwnProperty(ele) && (e.target.id === 'tf-' + ele)) { - formats[ele].edit(box, formats[ele].options); - } + + // setting for toolbar injection + $(s3).on(e, function(e) { + 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(); } }); - // $( 'body' ).on( 'keydown', function (e) { - // if (e.which === 67 && - // e.target.nodeName !== 'INPUT' && //The C, the whole C, and nothing but the C - // e.target.nodeName !== 'TEXTAREA' && - // !(e.ctrlKey || e.altKey || e.shiftKey)) { - // document.location.href = '//'+ document.location.host +'/'+ board_name +'/catalog.html'; - // } - // }); + + // make sure the tab settings are switch properly at loadup + if (window.Options && Options.get_tab('general')) { + if (localStorage.formatText_enable == 'true') $(s1)[0].checked = true; + else $(s1)[0].checked = false; + if (localStorage.formatText_keybinds == 'true') $(s2)[0].checked = true; + else $(s2)[0].checked = false; + if (localStorage.formatText_toolbar == 'true') $(s2)[0].checked = true; + else $(s3)[0].checked = false; + } + + // 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', '\ + \ + '); + + // Data control row + Options.extend_tab('formatting', '\ + \ + \ + \ + \ + '); + + // Descriptor row + Options.extend_tab('formatting', '\ + Name\ + ML\ + EL\ + Prefix\ + Suffix\ + Key\ + '); + + // 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); + } + } + + return self; + })(jQuery); + + // run initial toolbar injection + formatText.build_toolbars(); + //attach listeners to so it also works on quick-reply box + $('body').on('keydown', '#body, #quick-reply #body', function(e) { + if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; + 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); + } + } + } }); -} + $(document).trigger('formatText'); +} \ No newline at end of file diff --git a/js/options/user-css.js b/js/options/user-css.js index 840bcd1f..fed91ab3 100644 --- a/js/options/user-css.js +++ b/js/options/user-css.js @@ -17,7 +17,7 @@ var textarea = $("").css({ "font-size": 12, position: "absolute", top: 35, bottom: 35, - width: "calc(100% - 12px)", margin: 0, padding: 0, border: "1px solid black", + width: "calc(100% - 20px)", margin: 0, padding: "4px", border: "1px solid black", left: 5, right: 5 }).appendTo(tab.content); var submit = $("").css({ diff --git a/js/options/user-js.js b/js/options/user-js.js index 0b7d140f..0ba9b1d0 100644 --- a/js/options/user-js.js +++ b/js/options/user-js.js @@ -17,7 +17,7 @@ var textarea = $("").css({ "font-size": 12, position: "absolute", top: 35, bottom: 35, - width: "calc(100% - 12px)", margin: 0, padding: 0, border: "1px solid black", + width: "calc(100% - 20px)", margin: 0, padding: "4px", border: "1px solid black", left: 5, right: 5 }).appendTo(tab.content); var submit = $("").css({ -- cgit v1.3 From f6b4b2ac182fa1e155eff5697d8f5352de3385ab Mon Sep 17 00:00:00 2001 From: Forkless Date: Thu, 6 Nov 2014 06:29:59 -0600 Subject: Removed redundant setting. Bugfix. --- js/comment-toolbar.js | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) (limited to 'js/comment-toolbar.js') diff --git a/js/comment-toolbar.js b/js/comment-toolbar.js index be58c1f6..ab9e3eb1 100644 --- a/js/comment-toolbar.js +++ b/js/comment-toolbar.js @@ -27,7 +27,7 @@ if (active_page == 'thread' || active_page == 'index') { exclusiveline: false, prefix: "''", suffix: "''" - }, + }, bold: { text: 'Bold', key: 'b', @@ -71,13 +71,11 @@ if (active_page == 'thread' || active_page == 'index') { }; self.toolbar_wrap = function(node) { - if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; 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) { - if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; // clean and validate arguments if (ref == null) return; var settings = {multiline: false, exclusiveline: false, prefix:'', suffix: null}; @@ -162,7 +160,6 @@ if (active_page == 'thread' || active_page == 'index') { }; self.build_toolbars = function(){ - if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; if (localStorage.formatText_toolbar == 'true'){ // remove existing toolbars if ($('.format-text').length > 0) $('.format-text').remove(); @@ -256,35 +253,23 @@ if (active_page == 'thread' || active_page == 'index') { // Add settings to Options panel general tab if (window.Options && Options.get_tab('general')) { - var s1 = '#formatText_enable>input', s2 = '#formatText_keybinds>input', s3 = '#formatText_toolbar>input', e = 'change'; + var s1 = '#formatText_keybinds>input', s2 = '#formatText_toolbar>input', e = 'change'; Options.extend_tab('general', '\
\ Formatting Options\ - \ \ \
\ '); } else { - var s1 = '#formatText_enable', s2 = '#formatText_keybinds', s3 = '#formatText_toolbar', e = 'click'; - $('hr:first').before(''); + var s1 = '#formatText_keybinds', s2 = '#formatText_toolbar', e = 'click'; $('hr:first').before(''); $('hr:first').before(''); } - // setting for enableing text formatting - $(s1).on(e, function(e) { - if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') { - localStorage.formatText_enable = 'true'; - if (window.Options && Options.get_tab('general')) e.target.checked = true; - } else { - localStorage.formatText_enable = 'false'; - if (window.Options && Options.get_tab('general')) e.target.checked = false; - } - }); // setting for enableing formatting keybinds - $(s2).on(e, function(e) { + $(s1).on(e, function(e) { if (!localStorage.formatText_keybinds || localStorage.formatText_keybinds == 'false') { localStorage.formatText_keybinds = 'true'; if (window.Options && Options.get_tab('general')) e.target.checked = true; @@ -295,7 +280,7 @@ if (active_page == 'thread' || active_page == 'index') { }); // setting for toolbar injection - $(s3).on(e, function(e) { + $(s2).on(e, function(e) { if (!localStorage.formatText_toolbar || localStorage.formatText_toolbar == 'false') { localStorage.formatText_toolbar = 'true'; if (window.Options && Options.get_tab('general')) e.target.checked = true; @@ -309,12 +294,10 @@ if (active_page == 'thread' || active_page == 'index') { // make sure the tab settings are switch properly at loadup if (window.Options && Options.get_tab('general')) { - if (localStorage.formatText_enable == 'true') $(s1)[0].checked = true; + if (localStorage.formatText_keybinds == 'true') $(s1)[0].checked = true; else $(s1)[0].checked = false; - if (localStorage.formatText_keybinds == 'true') $(s2)[0].checked = true; - else $(s2)[0].checked = false; if (localStorage.formatText_toolbar == 'true') $(s2)[0].checked = true; - else $(s3)[0].checked = false; + else $(s2)[0].checked = false; } // add the tab for customizing the format settings @@ -375,7 +358,6 @@ if (active_page == 'thread' || active_page == 'index') { //attach listeners to so it also works on quick-reply box $('body').on('keydown', '#body, #quick-reply #body', function(e) { - if (!localStorage.formatText_enable || localStorage.formatText_enable == 'false') return; if (!localStorage.formatText_keybinds || localStorage.formatText_keybinds == 'false') return; var key = String.fromCharCode(e.which).toLowerCase(); var rules = JSON.parse(localStorage.formatText_rules); -- cgit v1.3 From 81daf934fb4d1740583220694083b292bc697206 Mon Sep 17 00:00:00 2001 From: Forkless Date: Sat, 8 Nov 2014 14:25:54 -0600 Subject: Fix for the js being shitty inside the compiled main.js. --- js/comment-toolbar.js | 118 ++++++++++++++++++++++++++------------------------ 1 file changed, 62 insertions(+), 56 deletions(-) (limited to 'js/comment-toolbar.js') diff --git a/js/comment-toolbar.js b/js/comment-toolbar.js index ab9e3eb1..80993cb0 100644 --- a/js/comment-toolbar.js +++ b/js/comment-toolbar.js @@ -257,8 +257,8 @@ if (active_page == 'thread' || active_page == 'index') { Options.extend_tab('general', '\
\ Formatting Options\ - \ - \ + \ + \
\ '); } else { @@ -267,39 +267,6 @@ if (active_page == 'thread' || active_page == 'index') { $('hr:first').before(''); } - - // setting for enableing formatting keybinds - $(s1).on(e, function(e) { - 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) { - 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; - } - // add the tab for customizing the format settings if (window.Options && !Options.get_tab('formatting')) { Options.add_tab('formatting', 'angle-right', 'Customize Formatting'); @@ -350,28 +317,67 @@ if (active_page == 'thread' || active_page == 'index') { } } - return self; - })(jQuery); - - // run initial toolbar injection - formatText.build_toolbars(); - - //attach listeners to so it also works on quick-reply box - $('body').on('keydown', '#body, #quick-reply #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); + // setup code to be ran when page is ready (work around for main.js compilation). + $(document).ready(function(){ + // 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 { - formatText.wrap(e.target, 'textarea[name="body"]', index, false); + 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; } - } - }); - $(document).trigger('formatText'); + + // Initial toolbar injection + formatText.build_toolbars(); + + //attach listener to 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); } \ No newline at end of file -- cgit v1.3 From 4e39262223eb255904fcd86532058e810e244b79 Mon Sep 17 00:00:00 2001 From: Forkless Date: Sat, 8 Nov 2014 22:17:33 -0600 Subject: Moved the Options tab stuff to be setup after the document is ready. --- js/comment-toolbar.js | 130 +++++++++++++++++++++++++------------------------- 1 file changed, 65 insertions(+), 65 deletions(-) (limited to 'js/comment-toolbar.js') diff --git a/js/comment-toolbar.js b/js/comment-toolbar.js index 80993cb0..65bdc529 100644 --- a/js/comment-toolbar.js +++ b/js/comment-toolbar.js @@ -251,74 +251,74 @@ if (active_page == 'thread' || active_page == 'index') { // setup default rules for customizing if (!localStorage.formatText_rules) localStorage.formatText_rules = JSON.stringify(self.rules); - // 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', '\ -
\ - Formatting Options\ - \ - \ -
\ - '); - } else { - var s1 = '#formatText_keybinds', s2 = '#formatText_toolbar', e = 'click'; - $('hr:first').before(''); - $('hr:first').before(''); - } - - // 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', '\ - \ - '); - - // Data control row - Options.extend_tab('formatting', '\ - \ - \ - \ - \ - '); - - // Descriptor row - Options.extend_tab('formatting', '\ - Name\ - ML\ - EL\ - Prefix\ - Suffix\ - Key\ - '); + // 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', '\ +
\ + Formatting Options\ + \ + \ +
\ + '); + } else { + var s1 = '#formatText_keybinds', s2 = '#formatText_toolbar', e = 'click'; + $('hr:first').before(''); + $('hr:first').before(''); + } - // 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); + // 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', '\ + \ + '); + + // Data control row + Options.extend_tab('formatting', '\ + \ + \ + \ + \ + '); + + // Descriptor row + Options.extend_tab('formatting', '\ + Name\ + ML\ + EL\ + Prefix\ + Suffix\ + Key\ + '); + + // 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); + } } - } - // setup code to be ran when page is ready (work around for main.js compilation). - $(document).ready(function(){ // setting for enabling formatting keybinds $(s1).on(e, function(e) { console.log('Keybind'); -- cgit v1.3 From cac428b30ccc2815264b15797e6397d6942b7adc Mon Sep 17 00:00:00 2001 From: Fredrick Brennan Date: Fri, 20 Mar 2015 16:09:21 +0800 Subject: Add some missing i18n tags --- js/comment-toolbar.js | 26 +++++++++++++------------- js/fix-report-delete-submit.js | 8 ++++---- js/inline.js | 4 ++-- js/post-filter.js | 40 ++++++++++++++++++++-------------------- js/thread-watcher.js | 12 ++++++------ 5 files changed, 45 insertions(+), 45 deletions(-) (limited to 'js/comment-toolbar.js') diff --git a/js/comment-toolbar.js b/js/comment-toolbar.js index 65bdc529..51aa9808 100644 --- a/js/comment-toolbar.js +++ b/js/comment-toolbar.js @@ -13,7 +13,7 @@ if (active_page == 'thread' || active_page == 'index') { var self = {}; self.rules = { spoiler: { - text: 'Spoiler', + text: _('Spoiler'), key: 's', multiline: false, exclusiveline: false, @@ -21,7 +21,7 @@ if (active_page == 'thread' || active_page == 'index') { suffix:'**' }, italics: { - text: 'Italics', + text: _('Italics'), key: 'i', multiline: false, exclusiveline: false, @@ -29,7 +29,7 @@ if (active_page == 'thread' || active_page == 'index') { suffix: "''" }, bold: { - text: 'Bold', + text: _('Bold'), key: 'b', multiline: false, exclusiveline: false, @@ -37,7 +37,7 @@ if (active_page == 'thread' || active_page == 'index') { suffix: "'''" }, underline: { - text: 'Underline', + text: _('Underline'), key: 'u', multiline: false, exclusiveline: false, @@ -45,7 +45,7 @@ if (active_page == 'thread' || active_page == 'index') { suffix:'__' }, code: { - text: 'Code', + text: _('Code'), key: 'f', multiline: true, exclusiveline: false, @@ -53,7 +53,7 @@ if (active_page == 'thread' || active_page == 'index') { suffix: '[/code]' }, strike: { - text: 'Strike', + text: _('Strike'), key: 'd', multiline:false, exclusiveline:false, @@ -61,7 +61,7 @@ if (active_page == 'thread' || active_page == 'index') { suffix:'~~' }, heading: { - text: 'Heading', + text: _('Heading'), key: 'r', multiline:false, exclusiveline:true, @@ -271,7 +271,7 @@ if (active_page == 'thread' || active_page == 'index') { // add the tab for customizing the format settings if (window.Options && !Options.get_tab('formatting')) { - Options.add_tab('formatting', 'angle-right', 'Customize Formatting'); + Options.add_tab('formatting', 'angle-right', _('Customize Formatting')); Options.extend_tab('formatting', '\