editor.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. Template.editor.onRendered(() => {
  2. const $textarea = this.$('textarea');
  3. autosize($textarea);
  4. $textarea.escapeableTextComplete([
  5. // Emojies
  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. // XXX I believe we should compute a HTML rendered field on the server that
  44. // would handle markdown, emojies and user mentions. We can simply have two
  45. // fields, one source, and one compiled version (in HTML) and send only the
  46. // compiled version to most users -- who don't need to edit.
  47. // In the meantime, all the transformation are done on the client using the
  48. // Blaze API.
  49. const at = HTML.CharRef({html: '&commat;', str: '@'});
  50. Blaze.Template.registerHelper('mentions', new Template('mentions', function() {
  51. const view = this;
  52. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  53. const knowedUsers = currentBoard.members.map((member) => {
  54. member.username = Users.findOne(member.userId).username;
  55. return member;
  56. });
  57. const mentionRegex = /\B@(\w*)/gi;
  58. let content = Blaze.toHTML(view.templateContentBlock);
  59. let currentMention, knowedUser, linkClass, linkValue, link;
  60. while (Boolean(currentMention = mentionRegex.exec(content))) {
  61. knowedUser = _.findWhere(knowedUsers, { username: currentMention[1] });
  62. if (!knowedUser)
  63. continue;
  64. linkValue = [' ', at, knowedUser.username];
  65. linkClass = 'atMention js-open-member';
  66. if (knowedUser.userId === Meteor.userId())
  67. linkClass += ' me';
  68. link = HTML.A({
  69. 'class': linkClass,
  70. // XXX Hack. Since we stringify this render function result below with
  71. // `Blaze.toHTML` we can't rely on blaze data contexts to pass the
  72. // `userId` to the popup as usual, and we need to store it in the DOM
  73. // using a data attribute.
  74. 'data-userId': knowedUser.userId,
  75. }, linkValue);
  76. content = content.replace(currentMention[0], Blaze.toHTML(link));
  77. }
  78. return HTML.Raw(content);
  79. }));
  80. Template.viewer.events({
  81. 'click .js-open-member'(evt, tpl) {
  82. const userId = evt.currentTarget.dataset.userid;
  83. Popup.open('member').call({ userId }, evt, tpl);
  84. },
  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) {
  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 href = evt.currentTarget.href;
  96. if (href) {
  97. window.open(href, '_blank');
  98. }
  99. },
  100. });