Skip to content

Commit 4a722cb

Browse files
authored
Merge pull request #25 from topcoder-platform/issues-535
Issues-535, Issues-568
2 parents 81701d7 + f995f29 commit 4a722cb

File tree

1 file changed

+91
-2
lines changed

1 file changed

+91
-2
lines changed

js/topcodereditor.js

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,74 @@
490490
return true;
491491
}
492492

493+
function _toggleBlock(editor, type, start_chars, end_chars) {
494+
if (/editor-preview-active/.test(editor.codemirror.getWrapperElement().lastChild.className))
495+
return;
496+
497+
end_chars = (typeof end_chars === 'undefined') ? start_chars : end_chars;
498+
var cm = editor.codemirror;
499+
var stat = getState(cm);
500+
501+
var text;
502+
var start = start_chars;
503+
var end = end_chars;
504+
505+
var startPoint = cm.getCursor('start');
506+
var endPoint = cm.getCursor('end');
507+
508+
if (stat[type]) {
509+
text = cm.getLine(startPoint.line);
510+
start = text.slice(0, startPoint.ch);
511+
end = text.slice(startPoint.ch);
512+
if (type == 'bold') {
513+
start = start.replace(/(\*\*|__)(?![\s\S]*(\*\*|__))/, '');
514+
end = end.replace(/(\*\*|__)/, '');
515+
} else if (type == 'italic') {
516+
start = start.replace(/(\*|_)(?![\s\S]*(\*|_))/, '');
517+
end = end.replace(/(\*|_)/, '');
518+
} else if (type == 'strikethrough') {
519+
start = start.replace(/(\*\*|~~)(?![\s\S]*(\*\*|~~))/, '');
520+
end = end.replace(/(\*\*|~~)/, '');
521+
}
522+
cm.replaceRange(start + end, {
523+
line: startPoint.line,
524+
ch: 0,
525+
}, {
526+
line: startPoint.line,
527+
ch: 99999999999999,
528+
});
529+
530+
if (type == 'bold' || type == 'strikethrough') {
531+
startPoint.ch -= 2;
532+
if (startPoint !== endPoint) {
533+
endPoint.ch -= 2;
534+
}
535+
} else if (type == 'italic') {
536+
startPoint.ch -= 1;
537+
if (startPoint !== endPoint) {
538+
endPoint.ch -= 1;
539+
}
540+
}
541+
} else {
542+
text = cm.getSelection();
543+
if (type == 'bold') {
544+
text = text.split('**').join('');
545+
//text = text.split('__').join('');
546+
} else if (type == 'italic') {
547+
text = text.split('*').join('');
548+
// text = text.split('_').join('');
549+
} else if (type == 'strikethrough') {
550+
text = text.split('~~').join('');
551+
}
552+
cm.replaceSelection(start + text + end);
553+
554+
startPoint.ch += start_chars.length;
555+
endPoint.ch = startPoint.ch + text.length;
556+
}
557+
558+
cm.setSelection(startPoint, endPoint);
559+
cm.focus();
560+
}
493561
/**
494562
* Initialize editor on the page.
495563
*
@@ -507,7 +575,21 @@
507575
placeholder: '',
508576
element: $currentEditableTextarea[0],
509577
hintOptions: { hint: topcoderHandles },
510-
toolbar: ["bold", "italic", "strikethrough", "|",
578+
toolbar: [{ name: "bold",
579+
action: function format(editor) {
580+
_toggleBlock(editor, 'bold', editor.options.blockStyles.bold);
581+
},
582+
className: 'fa fa-bold',
583+
title: 'Bold',
584+
}
585+
, { name: "italic",
586+
action: function format(editor) {
587+
_toggleBlock(editor, 'italic', editor.options.blockStyles.italic);
588+
},
589+
className: 'fa fa-italic',
590+
title: 'Italic',
591+
}
592+
, "strikethrough", "|",
511593
"heading-1", "heading-2", "heading-3", "|", "code", "quote", "|", "unordered-list",
512594
"ordered-list", "clean-block", "|", {
513595
name: "mentions",
@@ -609,7 +691,14 @@
609691
} else if(text.length > maxCommentLength) {
610692
$(editorContainer).addClass('error');
611693
var count = text.length - maxCommentLength;
612-
$(messageContainer).text('Comment is '+ count + ' characters too long');
694+
if($(frm).find('#Form_CommentID').length > 0) {
695+
$(messageContainer).text('Comment is '+ count + ' characters too long');
696+
} else if($(frm).find('#Form_DiscussionID').length > 0) {
697+
$(messageContainer).text('Discussion is '+ count + ' characters too long');
698+
} else {
699+
$(messageContainer).text('Text is '+ count + ' characters too long');
700+
}
701+
613702
$(frm).find(':submit').attr('disabled', 'disabled');
614703
$(frm).find('.Buttons a.Button:not(.Cancel)').addClass('Disabled');
615704
}

0 commit comments

Comments
 (0)