comments.js 2.6 KB

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