editor.js 2.7 KB

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