editor.js 3.4 KB

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