Skip to content

Commit 6a15096

Browse files
authored
Merge pull request #20 from topcoder-platform/develop
v1.5
2 parents fecabfb + b1dc5a7 commit 6a15096

File tree

4 files changed

+158
-89
lines changed

4 files changed

+158
-89
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: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(function($) {
22
var editor;
3+
var allEditors = [];
34
$.fn.setAsEditor = function(selector) {
45
selector = selector || '.BodyBox,.js-bodybox';
56

@@ -9,6 +10,7 @@
910
/**
1011
* Determine editor settings
1112
*/
13+
var maxCommentLength = (gdn.definition('maxCommentLength'));
1214
var canUpload = (gdn.definition('canUpload', false)) ? 1 : 0;
1315
var maxUploadSize = gdn.definition('maxUploadSize');
1416
var allowedImageExtensions = gdn.definition('allowedImageExtensions');
@@ -525,15 +527,67 @@
525527
fileTooLarge: 'Uploading #image_name# was failed. The file is too big (#image_size#).\n' +
526528
'Maximum file size is #image_max_size#.',
527529
importError: 'Uploading #image_name# was failed. Something went wrong when uploading the file.',
528-
}
530+
},
531+
status: [{
532+
className: 'message',
533+
defaultValue: function(el) {
534+
el.innerHTML = '';
535+
},
536+
onUpdate: function(el) {
537+
},
538+
}
539+
, 'upload-image', {
540+
className: 'countOfRemainingChars',
541+
defaultValue: function(el, cm) {
542+
var countOfRemainingChars = maxCommentLength;
543+
var text = cm.getValue();
544+
if(text != null && text.length > 0) {
545+
countOfRemainingChars = maxCommentLength - text.length;
546+
if(countOfRemainingChars < 0) {
547+
countOfRemainingChars = 0;
548+
}
549+
}
550+
el.innerHTML = countOfRemainingChars +" character remaining";
551+
},
552+
onUpdate: function(el, cm) {
553+
var countOfRemainingChars = maxCommentLength;
554+
var text = cm.getValue();
555+
if(text != null && text.length > 0) {
556+
countOfRemainingChars = maxCommentLength - text.length;
557+
if(countOfRemainingChars < 0) {
558+
countOfRemainingChars = 0;
559+
}
560+
}
561+
el.innerHTML = countOfRemainingChars +" character remaining";
562+
},
563+
}],
529564
});
530565

531566
// forceSync = true, need to clear form after async requests
532567
$currentEditableTextarea.closest('form').on('complete', function (frm, btn) {
533-
editor.codemirror.setValue('');
568+
var mainEditor = allEditors[0];
569+
mainEditor.codemirror.setValue('');
534570
});
535571

536572
editor.codemirror.on('change', function (cm, event) {
573+
var frm = $(cm.getInputField()).closest('form').first();
574+
var editorContainer = $(frm).find('.EasyMDEContainer');
575+
var messageContainer = $(frm).find('.editor-statusbar .message');
576+
577+
var text = cm.getValue();
578+
if(text.length > 0 && text.length <= maxCommentLength) {
579+
$(editorContainer).removeClass('error');
580+
$(messageContainer).text('');
581+
$(frm).find(':submit').removeAttr("disabled");
582+
$(frm).find('.Buttons a.Button').removeClass('Disabled');
583+
} else if(text.length > maxCommentLength) {
584+
$(editorContainer).addClass('error');
585+
var count = text.length - maxCommentLength;
586+
$(messageContainer).text('Comment is '+ count + ' characters too long');
587+
$(frm).find(':submit').attr('disabled', 'disabled');
588+
$(frm).find('.Buttons a.Button:not(.Cancel)').addClass('Disabled');
589+
}
590+
537591
// Key events don't work properly on Android Chrome
538592
if (!cm.state.completionActive /*Enables keyboard navigation in autocomplete list*/) {
539593
if (event.origin == '+input' && event.text && event.text.length > 0 && event.text[0] === '@') {
@@ -563,6 +617,11 @@
563617
}
564618
}
565619
});
620+
// We have only one main editor at a page which should used for quote/replyto
621+
// FIX: https://github.com/topcoder-platform/forums/issues/540
622+
if(allEditors.length == 0) {
623+
allEditors.push(editor);
624+
}
566625
}
567626
}; //editorInit
568627

@@ -594,7 +653,8 @@
594653
$(postForm).find('#Form_CategoryID').val(categoryID);
595654
}
596655
var uploads = element.attr("uploads");
597-
editor.enableUploadImages(uploads === "1");
656+
var mainEditor = allEditors[0];
657+
mainEditor.enableUploadImages(uploads === "1");
598658
});
599659

600660
// Preview mode
@@ -612,8 +672,9 @@
612672
});
613673

614674
// Comment with quotes
615-
$(document).on('ApplyQuoteText',function(ev, quoteText) {
616-
var text = editor.value();
617-
editor.value(quoteText + '\n' + text + '\n');
675+
$(document).on('ApplyQuoteText',function(ev, quoteText, ed) {
676+
var mainEditor = allEditors[0];
677+
var text = mainEditor.value();
678+
mainEditor.value(quoteText + '\n' + text + '\n');
618679
});
619680
}(jQuery));

0 commit comments

Comments
 (0)