diff --git a/js/topcodereditor.js b/js/topcodereditor.js index 385c45f..0d55aa5 100644 --- a/js/topcodereditor.js +++ b/js/topcodereditor.js @@ -490,6 +490,74 @@ return true; } + function _toggleBlock(editor, type, start_chars, end_chars) { + if (/editor-preview-active/.test(editor.codemirror.getWrapperElement().lastChild.className)) + return; + + end_chars = (typeof end_chars === 'undefined') ? start_chars : end_chars; + var cm = editor.codemirror; + var stat = getState(cm); + + var text; + var start = start_chars; + var end = end_chars; + + var startPoint = cm.getCursor('start'); + var endPoint = cm.getCursor('end'); + + if (stat[type]) { + text = cm.getLine(startPoint.line); + start = text.slice(0, startPoint.ch); + end = text.slice(startPoint.ch); + if (type == 'bold') { + start = start.replace(/(\*\*|__)(?![\s\S]*(\*\*|__))/, ''); + end = end.replace(/(\*\*|__)/, ''); + } else if (type == 'italic') { + start = start.replace(/(\*|_)(?![\s\S]*(\*|_))/, ''); + end = end.replace(/(\*|_)/, ''); + } else if (type == 'strikethrough') { + start = start.replace(/(\*\*|~~)(?![\s\S]*(\*\*|~~))/, ''); + end = end.replace(/(\*\*|~~)/, ''); + } + cm.replaceRange(start + end, { + line: startPoint.line, + ch: 0, + }, { + line: startPoint.line, + ch: 99999999999999, + }); + + if (type == 'bold' || type == 'strikethrough') { + startPoint.ch -= 2; + if (startPoint !== endPoint) { + endPoint.ch -= 2; + } + } else if (type == 'italic') { + startPoint.ch -= 1; + if (startPoint !== endPoint) { + endPoint.ch -= 1; + } + } + } else { + text = cm.getSelection(); + if (type == 'bold') { + text = text.split('**').join(''); + //text = text.split('__').join(''); + } else if (type == 'italic') { + text = text.split('*').join(''); + // text = text.split('_').join(''); + } else if (type == 'strikethrough') { + text = text.split('~~').join(''); + } + cm.replaceSelection(start + text + end); + + startPoint.ch += start_chars.length; + endPoint.ch = startPoint.ch + text.length; + } + + cm.setSelection(startPoint, endPoint); + cm.focus(); + } /** * Initialize editor on the page. * @@ -507,7 +575,21 @@ placeholder: '', element: $currentEditableTextarea[0], hintOptions: { hint: topcoderHandles }, - toolbar: ["bold", "italic", "strikethrough", "|", + toolbar: [{ name: "bold", + action: function format(editor) { + _toggleBlock(editor, 'bold', editor.options.blockStyles.bold); + }, + className: 'fa fa-bold', + title: 'Bold', + } + , { name: "italic", + action: function format(editor) { + _toggleBlock(editor, 'italic', editor.options.blockStyles.italic); + }, + className: 'fa fa-italic', + title: 'Italic', + } + , "strikethrough", "|", "heading-1", "heading-2", "heading-3", "|", "code", "quote", "|", "unordered-list", "ordered-list", "clean-block", "|", { name: "mentions", @@ -609,7 +691,14 @@ } else if(text.length > maxCommentLength) { $(editorContainer).addClass('error'); var count = text.length - maxCommentLength; - $(messageContainer).text('Comment is '+ count + ' characters too long'); + if($(frm).find('#Form_CommentID').length > 0) { + $(messageContainer).text('Comment is '+ count + ' characters too long'); + } else if($(frm).find('#Form_DiscussionID').length > 0) { + $(messageContainer).text('Discussion is '+ count + ' characters too long'); + } else { + $(messageContainer).text('Text is '+ count + ' characters too long'); + } + $(frm).find(':submit').attr('disabled', 'disabled'); $(frm).find('.Buttons a.Button:not(.Cancel)').addClass('Disabled'); }