Skip to content

Commit 4f2642d

Browse files
committed
Issues-483, Issues-484, Issues-535: Added validation of comment length on client side
1 parent 5826a03 commit 4f2642d

File tree

4 files changed

+144
-84
lines changed

4 files changed

+144
-84
lines changed

TopcoderEditorPlugin.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ private function beforeRender($sender){
8383
$sender->addJsFile('topcodereditor.mathjax.js', 'plugins/TopcoderEditor', ['defer'=>'defer']);
8484
$c = Gdn::controller();
8585

86+
$sender->Form->addHidden('MaxCommentLength', c('Vanilla.Comment.MaxLength', 10000));
87+
8688
// Set formats
8789
$c->addDefinition('defaultInputFormat', c('Garden.InputFormatter'));
8890
$c->addDefinition('defaultMobileInputFormat', c('Garden.MobileInputFormatter'));
@@ -115,11 +117,13 @@ private function beforeRender($sender){
115117
// Get max file uploads, to be used for max drops at once.
116118
$c->addDefinition('maxFileUploads', ini_get('max_file_uploads'));
117119

120+
// Max Comment Length
121+
$c->addDefinition('maxCommentLength', c('Vanilla.Comment.MaxLength', 10000));
122+
118123
// Set editor definitions
119124
$c->addDefinition('editorVersion', $this->pluginInfo['Version']);
120125
$c->addDefinition('editorInputFormat', ucfirst(self::FORMAT_NAME));
121126
$c->addDefinition('editorPluginAssets', $this->AssetPath);
122-
123127
$additionalDefinitions = [];
124128
$this->EventArguments['definitions'] = &$additionalDefinitions;
125129
$this->fireEvent('GetJSDefinitions');

js/easymde.min.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19359,26 +19359,27 @@ EasyMDE.prototype.createStatusbar = function (status) {
1935919359

1936019360
// Ensure the defaultValue is a function
1936119361
if (typeof item.defaultValue === 'function') {
19362-
item.defaultValue(el);
19362+
item.defaultValue(el, this.codemirror);
1936319363
}
1936419364

1936519365

1936619366
// Ensure the onUpdate is a function
1936719367
if (typeof item.onUpdate === 'function') {
1936819368
// Create a closure around the span of the current action, then execute the onUpdate handler
19369-
this.codemirror.on('update', (function (el, item) {
19369+
// FIX: issues with status bar if there are several editors on a page
19370+
this.codemirror.on('update', (function (el, item, cm) {
1937019371
return function () {
19371-
item.onUpdate(el);
19372+
item.onUpdate(el,cm);
1937219373
};
19373-
}(el, item)));
19374+
}(el, item, this.codemirror)));
1937419375
}
1937519376
if (typeof item.onActivity === 'function') {
1937619377
// Create a closure around the span of the current action, then execute the onActivity handler
19377-
this.codemirror.on('cursorActivity', (function (el, item) {
19378+
this.codemirror.on('cursorActivity', (function (el, item, cm) {
1937819379
return function () {
19379-
item.onActivity(el);
19380+
item.onActivity(el, cm);
1938019381
};
19381-
}(el, item)));
19382+
}(el, item, this.codemirror)));
1938219383
}
1938319384

1938419385

js/topcodereditor.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/**
1010
* Determine editor settings
1111
*/
12+
var maxCommentLength = (gdn.definition('maxCommentLength'));
1213
var canUpload = (gdn.definition('canUpload', false)) ? 1 : 0;
1314
var maxUploadSize = gdn.definition('maxUploadSize');
1415
var allowedImageExtensions = gdn.definition('allowedImageExtensions');
@@ -525,7 +526,40 @@
525526
fileTooLarge: 'Uploading #image_name# was failed. The file is too big (#image_size#).\n' +
526527
'Maximum file size is #image_max_size#.',
527528
importError: 'Uploading #image_name# was failed. Something went wrong when uploading the file.',
528-
}
529+
},
530+
status: [{
531+
className: 'message',
532+
defaultValue: function(el) {
533+
el.innerHTML = '';
534+
},
535+
onUpdate: function(el) {
536+
},
537+
}
538+
, 'upload-image', {
539+
className: 'countOfRemainingChars',
540+
defaultValue: function(el, cm) {
541+
var countOfRemainingChars = maxCommentLength;
542+
var text = cm.getValue();
543+
if(text != null && text.length > 0) {
544+
countOfRemainingChars = maxCommentLength - text.length;
545+
if(countOfRemainingChars < 0) {
546+
countOfRemainingChars = 0;
547+
}
548+
}
549+
el.innerHTML = countOfRemainingChars +" character remaining";
550+
},
551+
onUpdate: function(el, cm) {
552+
var countOfRemainingChars = maxCommentLength;
553+
var text = cm.getValue();
554+
if(text != null && text.length > 0) {
555+
countOfRemainingChars = maxCommentLength - text.length;
556+
if(countOfRemainingChars < 0) {
557+
countOfRemainingChars = 0;
558+
}
559+
}
560+
el.innerHTML = countOfRemainingChars +" character remaining";
561+
},
562+
}],
529563
});
530564

531565
// forceSync = true, need to clear form after async requests
@@ -534,6 +568,24 @@
534568
});
535569

536570
editor.codemirror.on('change', function (cm, event) {
571+
var frm = $(cm.getInputField()).closest('form').first();
572+
var editorContainer = $(frm).find('.EasyMDEContainer');
573+
var messageContainer = $(frm).find('.editor-statusbar .message');
574+
575+
var text = cm.getValue();
576+
if(text.length > 0 && text.length <= maxCommentLength) {
577+
$(editorContainer).removeClass('error');
578+
$(messageContainer).text('');
579+
$(frm).find(':submit').removeAttr("disabled");
580+
$(frm).find('.Buttons a.Button').removeClass('Disabled');
581+
} else if(text.length > maxCommentLength) {
582+
$(editorContainer).addClass('error');
583+
var count = text.length - maxCommentLength;
584+
$(messageContainer).text('Comment is '+ count + ' characters too long');
585+
$(frm).find(':submit').attr('disabled', 'disabled');
586+
$(frm).find('.Buttons a.Button:not(.Cancel)').addClass('Disabled');
587+
}
588+
537589
// Key events don't work properly on Android Chrome
538590
if (!cm.state.completionActive /*Enables keyboard navigation in autocomplete list*/) {
539591
if (event.origin == '+input' && event.text && event.text.length > 0 && event.text[0] === '@') {

0 commit comments

Comments
 (0)