editor.js 3.7 KB

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