comments.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. 'click .js-new-comment:not(.focus)'() {
  15. commentFormIsOpen.set(true);
  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.isImportedCard()) {
  24. boardId = Cards.findOne(card.importedId).boardId;
  25. cardId = card.importedId;
  26. }
  27. if (text) {
  28. CardComments.insert({
  29. text,
  30. boardId,
  31. cardId,
  32. });
  33. resetCommentInput(input);
  34. Tracker.flush();
  35. autosize.update(input);
  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. }).register('commentForm');
  48. // XXX This should be a static method of the `commentForm` component
  49. function resetCommentInput(input) {
  50. input.val('');
  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('inlinedForm',
  66. () => {
  67. const draftKey = {
  68. fieldName: 'cardComment',
  69. docId: Session.get('currentCard'),
  70. };
  71. const commentInput = $('.js-new-comment-input');
  72. const draft = commentInput.val().trim();
  73. if (draft) {
  74. UnsavedEdits.set(draftKey, draft);
  75. } else {
  76. UnsavedEdits.reset(draftKey);
  77. }
  78. resetCommentInput(commentInput);
  79. },
  80. () => { return commentFormIsOpen.get(); }, {
  81. noClickEscapeOn: '.js-new-comment',
  82. }
  83. );