comments.js 2.5 KB

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