editor.js 3.4 KB

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