comments.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. input.trigger('submitted');
  38. }
  39. evt.preventDefault();
  40. },
  41. // Pressing Ctrl+Enter should submit the form
  42. 'keydown form textarea'(evt) {
  43. if (evt.keyCode === 13 && (evt.metaKey || evt.ctrlKey)) {
  44. this.find('button[type=submit]').click();
  45. }
  46. },
  47. },
  48. ];
  49. },
  50. }).register('commentForm');
  51. // XXX This should be a static method of the `commentForm` component
  52. function resetCommentInput(input) {
  53. input.val(''); // without manually trigger, input event won't be fired
  54. input.blur();
  55. commentFormIsOpen.set(false);
  56. }
  57. // XXX This should handled a `onUpdated` callback of the `commentForm` component
  58. // but since this callback doesn't exists, and `onRendered` is not called if the
  59. // data is not destroyed and recreated, we simulate the desired callback using
  60. // Tracker.autorun to register the component dependencies, and re-run when these
  61. // dependencies are invalidated. A better component API would remove this hack.
  62. Tracker.autorun(() => {
  63. Session.get('currentCard');
  64. Tracker.afterFlush(() => {
  65. autosize.update($('.js-new-comment-input'));
  66. });
  67. });
  68. EscapeActions.register(
  69. 'inlinedForm',
  70. () => {
  71. const draftKey = {
  72. fieldName: 'cardComment',
  73. docId: Session.get('currentCard'),
  74. };
  75. const commentInput = $('.js-new-comment-input');
  76. const draft = commentInput.val().trim();
  77. if (draft) {
  78. UnsavedEdits.set(draftKey, draft);
  79. } else {
  80. UnsavedEdits.reset(draftKey);
  81. }
  82. resetCommentInput(commentInput);
  83. },
  84. () => {
  85. return commentFormIsOpen.get();
  86. },
  87. {
  88. noClickEscapeOn: '.js-new-comment',
  89. },
  90. );