comments.js 2.7 KB

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