comments.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const commentFormIsOpen = new ReactiveVar(false);
  2. BlazeComponent.extendComponent({
  3. onDestroyed() {
  4. commentFormIsOpen.set(false);
  5. },
  6. commentFormIsOpen() {
  7. return commentFormIsOpen.get();
  8. },
  9. getInput() {
  10. return this.$('.js-new-comment-input');
  11. },
  12. events() {
  13. return [
  14. {
  15. 'submit .js-new-comment-form'(evt) {
  16. const input = this.getInput();
  17. const text = input.val().trim();
  18. const card = this.currentData();
  19. let boardId = card.boardId;
  20. let cardId = card._id;
  21. if (card.isLinkedCard()) {
  22. boardId = Cards.findOne(card.linkedId).boardId;
  23. cardId = card.linkedId;
  24. }
  25. if (text) {
  26. CardComments.insert({
  27. text,
  28. boardId,
  29. cardId,
  30. });
  31. resetCommentInput(input);
  32. // With Richer editor is in use, and comment is submitted,
  33. // clear comment form with JQuery. Id #summernote is defined
  34. // at client/components/main/editor.jade where it previously was
  35. // id=id, now it is id="summernote".
  36. if (Meteor.settings.public.RICHER_CARD_COMMENT_EDITOR === 'true') {
  37. $('#summernote').summernote('code', '');
  38. }
  39. Tracker.flush();
  40. autosize.update(input);
  41. input.trigger('submitted');
  42. }
  43. evt.preventDefault();
  44. },
  45. // Pressing Ctrl+Enter should submit the form
  46. 'keydown form textarea'(evt) {
  47. if (evt.keyCode === 13 && (evt.metaKey || evt.ctrlKey)) {
  48. this.find('button[type=submit]').click();
  49. }
  50. },
  51. },
  52. ];
  53. },
  54. }).register('commentForm');
  55. // XXX This should be a static method of the `commentForm` component
  56. function resetCommentInput(input) {
  57. input.val(''); // without manually trigger, input event won't be fired
  58. input.blur();
  59. commentFormIsOpen.set(false);
  60. }
  61. // XXX This should handled a `onUpdated` callback of the `commentForm` component
  62. // but since this callback doesn't exists, and `onRendered` is not called if the
  63. // data is not destroyed and recreated, we simulate the desired callback using
  64. // Tracker.autorun to register the component dependencies, and re-run when these
  65. // dependencies are invalidated. A better component API would remove this hack.
  66. Tracker.autorun(() => {
  67. Session.get('currentCard');
  68. Tracker.afterFlush(() => {
  69. autosize.update($('.js-new-comment-input'));
  70. });
  71. });
  72. EscapeActions.register(
  73. 'inlinedForm',
  74. () => {
  75. const draftKey = {
  76. fieldName: 'cardComment',
  77. docId: Session.get('currentCard'),
  78. };
  79. const commentInput = $('.js-new-comment-input');
  80. const draft = commentInput.val().trim();
  81. if (draft) {
  82. UnsavedEdits.set(draftKey, draft);
  83. } else {
  84. UnsavedEdits.reset(draftKey);
  85. }
  86. resetCommentInput(commentInput);
  87. },
  88. () => {
  89. return commentFormIsOpen.get();
  90. },
  91. {
  92. noClickEscapeOn: '.js-new-comment',
  93. },
  94. );