editor.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  36. const knowedUsers = currentBoard.members.map((member) => {
  37. const u = Users.findOne(member.userId);
  38. if(u){
  39. member.username = u.username;
  40. }
  41. return member;
  42. });
  43. const mentionRegex = /\B@([\w.]*)/gi;
  44. let content = Blaze.toHTML(view.templateContentBlock);
  45. let currentMention;
  46. while ((currentMention = mentionRegex.exec(content)) !== null) {
  47. const [fullMention, username] = currentMention;
  48. const knowedUser = _.findWhere(knowedUsers, { username });
  49. if (!knowedUser) {
  50. continue;
  51. }
  52. const linkValue = [' ', at, knowedUser.username];
  53. let linkClass = 'atMention js-open-member';
  54. if (knowedUser.userId === Meteor.userId()) {
  55. linkClass += ' me';
  56. }
  57. const link = HTML.A({
  58. 'class': linkClass,
  59. // XXX Hack. Since we stringify this render function result below with
  60. // `Blaze.toHTML` we can't rely on blaze data contexts to pass the
  61. // `userId` to the popup as usual, and we need to store it in the DOM
  62. // using a data attribute.
  63. 'data-userId': knowedUser.userId,
  64. }, linkValue);
  65. content = content.replace(fullMention, Blaze.toHTML(link));
  66. }
  67. return HTML.Raw(sanitizeXss(content));
  68. }));
  69. Template.viewer.events({
  70. // Viewer sometimes have click-able wrapper around them (for instance to edit
  71. // the corresponding text). Clicking a link shouldn't fire these actions, stop
  72. // we stop these event at the viewer component level.
  73. 'click a'(evt, tpl) {
  74. evt.stopPropagation();
  75. // XXX We hijack the build-in browser action because we currently don't have
  76. // `_blank` attributes in viewer links, and the transformer function is
  77. // handled by a third party package that we can't configure easily. Fix that
  78. // by using directly `_blank` attribute in the rendered HTML.
  79. evt.preventDefault();
  80. const userId = evt.currentTarget.dataset.userid;
  81. if (userId) {
  82. Popup.open('member').call({ userId }, evt, tpl);
  83. }
  84. else {
  85. const href = evt.currentTarget.href;
  86. if (href) {
  87. window.open(href, '_blank');
  88. }
  89. }
  90. },
  91. });