editor.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. let dropdownMenuIsOpened = false;
  2. Template.editor.onRendered(() => {
  3. const $textarea = this.$('textarea');
  4. autosize($textarea);
  5. $textarea.textcomplete([
  6. // Emojies
  7. {
  8. match: /\B:([\-+\w]*)$/,
  9. search(term, callback) {
  10. callback($.map(Emoji.values, (emoji) => {
  11. return emoji.indexOf(term) === 0 ? emoji : null;
  12. }));
  13. },
  14. template(value) {
  15. const imgSrc = Emoji.baseImagePath + value;
  16. const image = `<img src="${imgSrc}.png" />`;
  17. return image + value;
  18. },
  19. replace(value) {
  20. return `:${value}:`;
  21. },
  22. index: 1,
  23. },
  24. // User mentions
  25. {
  26. match: /\B@(\w*)$/,
  27. search(term, callback) {
  28. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  29. callback($.map(currentBoard.members, (member) => {
  30. const username = Users.findOne(member.userId).username;
  31. return username.indexOf(term) === 0 ? username : null;
  32. }));
  33. },
  34. template(value) {
  35. return value;
  36. },
  37. replace(username) {
  38. return `@${username} `;
  39. },
  40. index: 1,
  41. },
  42. ]);
  43. // Since commit d474017 jquery-textComplete automatically closes a potential
  44. // opened dropdown menu when the user press Escape. This behavior conflicts
  45. // with our EscapeActions system, but it's too complicated and hacky to
  46. // monkey-pach textComplete to disable it -- I tried. Instead we listen to
  47. // 'open' and 'hide' events, and create a ghost escapeAction when the dropdown
  48. // is opened (and rely on textComplete to execute the actual action).
  49. $textarea.on({
  50. 'textComplete:show'() {
  51. dropdownMenuIsOpened = true;
  52. },
  53. 'textComplete:hide'() {
  54. Tracker.afterFlush(() => {
  55. dropdownMenuIsOpened = false;
  56. });
  57. },
  58. });
  59. });
  60. EscapeActions.register('textcomplete',
  61. () => {},
  62. () => dropdownMenuIsOpened
  63. );
  64. // XXX I believe we should compute a HTML rendered field on the server that
  65. // would handle markdown, emojies and user mentions. We can simply have two
  66. // fields, one source, and one compiled version (in HTML) and send only the
  67. // compiled version to most users -- who don't need to edit.
  68. // In the meantime, all the transformation are done on the client using the
  69. // Blaze API.
  70. const at = HTML.CharRef({html: '&commat;', str: '@'});
  71. Blaze.Template.registerHelper('mentions', new Template('mentions', function() {
  72. const view = this;
  73. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  74. const knowedUsers = _.map(currentBoard.members, (member) => {
  75. member.username = Users.findOne(member.userId).username;
  76. return member;
  77. });
  78. const mentionRegex = /\B@(\w*)/gi;
  79. let content = Blaze.toHTML(view.templateContentBlock);
  80. let currentMention, knowedUser, linkClass, linkValue, link;
  81. while (Boolean(currentMention = mentionRegex.exec(content))) {
  82. knowedUser = _.findWhere(knowedUsers, { username: currentMention[1] });
  83. if (!knowedUser)
  84. continue;
  85. linkValue = [' ', at, knowedUser.username];
  86. linkClass = 'atMention js-open-member';
  87. if (knowedUser.userId === Meteor.userId())
  88. linkClass += ' me';
  89. link = HTML.A({
  90. 'class': linkClass,
  91. // XXX Hack. Since we stringify this render function result below with
  92. // `Blaze.toHTML` we can't rely on blaze data contexts to pass the
  93. // `userId` to the popup as usual, and we need to store it in the DOM
  94. // using a data attribute.
  95. 'data-userId': knowedUser.userId,
  96. }, linkValue);
  97. content = content.replace(currentMention[0], Blaze.toHTML(link));
  98. }
  99. return HTML.Raw(content);
  100. }));
  101. Template.viewer.events({
  102. 'click .js-open-member'(evt, tpl) {
  103. const userId = evt.currentTarget.dataset.userid;
  104. Popup.open('member').call({ userId }, evt, tpl);
  105. },
  106. // Viewer sometimes have click-able wrapper around them (for instance to edit
  107. // the corresponding text). Clicking a link shouldn't fire these actions, stop
  108. // we stop these event at the viewer component level.
  109. 'click a'(evt) {
  110. evt.stopPropagation();
  111. // XXX We hijack the build-in browser action because we currently don't have
  112. // `_blank` attributes in viewer links, and the transformer function is
  113. // handled by a third party package that we can't configure easily. Fix that
  114. // by using directly `_blank` attribute in the rendered HTML.
  115. evt.preventDefault();
  116. const href = evt.currentTarget.href;
  117. if (href) {
  118. window.open(href, '_blank');
  119. }
  120. },
  121. });