editor.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // XXX I believe we should compute a HTML rendered field on the server that
  44. // would handle markdown, emoji 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;
  60. while ((currentMention = mentionRegex.exec(content)) !== null) {
  61. const [fullMention, username] = currentMention;
  62. const knowedUser = _.findWhere(knowedUsers, { username });
  63. if (!knowedUser) {
  64. continue;
  65. }
  66. const linkValue = [' ', at, knowedUser.username];
  67. let linkClass = 'atMention js-open-member';
  68. if (knowedUser.userId === Meteor.userId()) {
  69. linkClass += ' me';
  70. }
  71. const link = HTML.A({
  72. 'class': linkClass,
  73. // XXX Hack. Since we stringify this render function result below with
  74. // `Blaze.toHTML` we can't rely on blaze data contexts to pass the
  75. // `userId` to the popup as usual, and we need to store it in the DOM
  76. // using a data attribute.
  77. 'data-userId': knowedUser.userId,
  78. }, linkValue);
  79. content = content.replace(fullMention, Blaze.toHTML(link));
  80. }
  81. return HTML.Raw(content);
  82. }));
  83. Template.viewer.events({
  84. // Viewer sometimes have click-able wrapper around them (for instance to edit
  85. // the corresponding text). Clicking a link shouldn't fire these actions, stop
  86. // we stop these event at the viewer component level.
  87. 'click a'(evt, tpl) {
  88. evt.stopPropagation();
  89. // XXX We hijack the build-in browser action because we currently don't have
  90. // `_blank` attributes in viewer links, and the transformer function is
  91. // handled by a third party package that we can't configure easily. Fix that
  92. // by using directly `_blank` attribute in the rendered HTML.
  93. evt.preventDefault();
  94. const userId = evt.currentTarget.dataset.userid;
  95. if (userId) {
  96. Popup.open('member').call({ userId }, evt, tpl);
  97. }
  98. else {
  99. const href = evt.currentTarget.href;
  100. if (href) {
  101. window.open(href, '_blank');
  102. }
  103. }
  104. },
  105. });