editor.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. let dropdownMenuIsOpened = false;
  2. Template.editor.onRendered(() => {
  3. const $textarea = this.$('textarea');
  4. autosize($textarea);
  5. $textarea.textcomplete([
  6. // Emojies
  7. {
  8. match: /\B:([\-+\w]*)$/,
  9. search(term, callback) {
  10. callback($.map(Emoji.values, (emoji) => {
  11. return emoji.indexOf(term) === 0 ? emoji : null;
  12. }));
  13. },
  14. template(value) {
  15. const imgSrc = Emoji.baseImagePath + value;
  16. const image = `<img src="${imgSrc}.png" />`;
  17. return image + value;
  18. },
  19. replace(value) {
  20. return `:${value}:`;
  21. },
  22. index: 1,
  23. },
  24. // User mentions
  25. {
  26. match: /\B@(\w*)$/,
  27. search(term, callback) {
  28. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  29. callback($.map(currentBoard.members, (member) => {
  30. const username = Users.findOne(member.userId).username;
  31. return username.indexOf(term) === 0 ? username : null;
  32. }));
  33. },
  34. template(value) {
  35. return value;
  36. },
  37. replace(username) {
  38. return `@${username} `;
  39. },
  40. index: 1,
  41. },
  42. ]);
  43. // Since commit d474017 jquery-textComplete automatically closes a potential
  44. // opened dropdown menu when the user press Escape. This behavior conflicts
  45. // with our EscapeActions system, but it's too complicated and hacky to
  46. // monkey-pach textComplete to disable it -- I tried. Instead we listen to
  47. // 'open' and 'hide' events, and create a ghost escapeAction when the dropdown
  48. // is opened (and rely on textComplete to execute the actual action).
  49. $textarea.on({
  50. 'textComplete:show'() {
  51. dropdownMenuIsOpened = true;
  52. },
  53. 'textComplete:hide'() {
  54. Tracker.afterFlush(() => {
  55. dropdownMenuIsOpened = false;
  56. });
  57. },
  58. });
  59. });
  60. EscapeActions.register('textcomplete',
  61. () => {},
  62. () => dropdownMenuIsOpened
  63. );
  64. Template.viewer.events({
  65. // Viewer sometimes have click-able wrapper around them (for instance to edit
  66. // the corresponding text). Clicking a link shouldn't fire these actions, stop
  67. // we stop these event at the viewer component level.
  68. 'click a'(evt) {
  69. evt.stopPropagation();
  70. // XXX We hijack the build-in browser action because we currently don't have
  71. // `_blank` attributes in viewer links, and the transformer function is
  72. // handled by a third party package that we can't configure easily. Fix that
  73. // by using directly `_blank` attribute in the rendered HTML.
  74. evt.preventDefault();
  75. const href = evt.currentTarget.href;
  76. window.open(href, '_blank');
  77. },
  78. });