|
1 | 1 | (function($) {
|
2 | 2 | var editor;
|
| 3 | + var allEditors = []; |
3 | 4 | $.fn.setAsEditor = function(selector) {
|
4 | 5 | selector = selector || '.BodyBox,.js-bodybox';
|
5 | 6 |
|
|
9 | 10 | /**
|
10 | 11 | * Determine editor settings
|
11 | 12 | */
|
| 13 | + var maxCommentLength = (gdn.definition('maxCommentLength')); |
12 | 14 | var canUpload = (gdn.definition('canUpload', false)) ? 1 : 0;
|
13 | 15 | var maxUploadSize = gdn.definition('maxUploadSize');
|
14 | 16 | var allowedImageExtensions = gdn.definition('allowedImageExtensions');
|
|
525 | 527 | fileTooLarge: 'Uploading #image_name# was failed. The file is too big (#image_size#).\n' +
|
526 | 528 | 'Maximum file size is #image_max_size#.',
|
527 | 529 | 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 | + }], |
529 | 564 | });
|
530 | 565 |
|
531 | 566 | // forceSync = true, need to clear form after async requests
|
532 | 567 | $currentEditableTextarea.closest('form').on('complete', function (frm, btn) {
|
533 |
| - editor.codemirror.setValue(''); |
| 568 | + var mainEditor = allEditors[0]; |
| 569 | + mainEditor.codemirror.setValue(''); |
534 | 570 | });
|
535 | 571 |
|
536 | 572 | 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 | + |
537 | 591 | // Key events don't work properly on Android Chrome
|
538 | 592 | if (!cm.state.completionActive /*Enables keyboard navigation in autocomplete list*/) {
|
539 | 593 | if (event.origin == '+input' && event.text && event.text.length > 0 && event.text[0] === '@') {
|
|
563 | 617 | }
|
564 | 618 | }
|
565 | 619 | });
|
| 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 | + } |
566 | 625 | }
|
567 | 626 | }; //editorInit
|
568 | 627 |
|
|
594 | 653 | $(postForm).find('#Form_CategoryID').val(categoryID);
|
595 | 654 | }
|
596 | 655 | var uploads = element.attr("uploads");
|
597 |
| - editor.enableUploadImages(uploads === "1"); |
| 656 | + var mainEditor = allEditors[0]; |
| 657 | + mainEditor.enableUploadImages(uploads === "1"); |
598 | 658 | });
|
599 | 659 |
|
600 | 660 | // Preview mode
|
|
612 | 672 | });
|
613 | 673 |
|
614 | 674 | // 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'); |
618 | 679 | });
|
619 | 680 | }(jQuery));
|
0 commit comments