comments.js 2.3 KB

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