Skip to content

Issues-535, Issues-568 #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 91 additions & 2 deletions js/topcodereditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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",
Expand Down Expand Up @@ -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');
}
Expand Down