comments.js 2.5 KB

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