editor.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. Template.editor.onRendered(() => {
  2. const $textarea = this.$('textarea');
  3. autosize($textarea);
  4. $textarea.escapeableTextComplete([
  5. // User mentions
  6. {
  7. match: /\B@([\w.]*)$/,
  8. search(term, callback) {
  9. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  10. callback(
  11. currentBoard
  12. .activeMembers()
  13. .map(member => {
  14. const username = Users.findOne(member.userId).username;
  15. return username.includes(term) ? username : null;
  16. })
  17. .filter(Boolean),
  18. );
  19. },
  20. template(value) {
  21. return value;
  22. },
  23. replace(username) {
  24. return `@${username} `;
  25. },
  26. index: 1,
  27. },
  28. ]);
  29. });
  30. import sanitizeXss from 'xss';
  31. // XXX I believe we should compute a HTML rendered field on the server that
  32. // would handle markdown and user mentions. We can simply have two
  33. // fields, one source, and one compiled version (in HTML) and send only the
  34. // compiled version to most users -- who don't need to edit.
  35. // In the meantime, all the transformation are done on the client using the
  36. // Blaze API.
  37. const at = HTML.CharRef({ html: '@', str: '@' });
  38. Blaze.Template.registerHelper(
  39. 'mentions',
  40. new Template('mentions', function() {
  41. const view = this;
  42. let content = Blaze.toHTML(view.templateContentBlock);
  43. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  44. if (!currentBoard) return HTML.Raw(sanitizeXss(content));
  45. const knowedUsers = currentBoard.members.map(member => {
  46. const u = Users.findOne(member.userId);
  47. if (u) {
  48. member.username = u.username;
  49. }
  50. return member;
  51. });
  52. const mentionRegex = /\B@([\w.]*)/gi;
  53. let currentMention;
  54. while ((currentMention = mentionRegex.exec(content)) !== null) {
  55. const [fullMention, username] = currentMention;
  56. const knowedUser = _.findWhere(knowedUsers, { username });
  57. if (!knowedUser) {
  58. continue;
  59. }
  60. const linkValue = [' ', at, knowedUser.username];
  61. let linkClass = 'atMention js-open-member';
  62. if (knowedUser.userId === Meteor.userId()) {
  63. linkClass += ' me';
  64. }
  65. const link = HTML.A(
  66. {
  67. class: linkClass,
  68. // XXX Hack. Since we stringify this render function result below with
  69. // `Blaze.toHTML` we can't rely on blaze data contexts to pass the
  70. // `userId` to the popup as usual, and we need to store it in the DOM
  71. // using a data attribute.
  72. 'data-userId': knowedUser.userId,
  73. },
  74. linkValue,
  75. );
  76. content = content.replace(fullMention, Blaze.toHTML(link));
  77. }
  78. return HTML.Raw(sanitizeXss(content));
  79. }),
  80. );
  81. Template.viewer.events({
  82. // Viewer sometimes have click-able wrapper around them (for instance to edit
  83. // the corresponding text). Clicking a link shouldn't fire these actions, stop
  84. // we stop these event at the viewer component level.
  85. 'click a'(event, templateInstance) {
  86. event.stopPropagation();
  87. // XXX We hijack the build-in browser action because we currently don't have
  88. // `_blank` attributes in viewer links, and the transformer function is
  89. // handled by a third party package that we can't configure easily. Fix that
  90. // by using directly `_blank` attribute in the rendered HTML.
  91. event.preventDefault();
  92. const userId = event.currentTarget.dataset.userid;
  93. if (userId) {
  94. Popup.open('member').call({ userId }, event, templateInstance);
  95. } else {
  96. const href = event.currentTarget.href;
  97. if (href) {
  98. window.open(href, '_blank');
  99. }
  100. }
  101. },
  102. });