summaryrefslogtreecommitdiffstats
path: root/client/components/main/editor.js
blob: 7966ff600518e4fbb86fa666cbd55bff2760d1fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var dropdownMenuIsOpened = false;

Template.editor.onRendered(function() {
  var $textarea = this.$('textarea');

  autosize($textarea);

  $textarea.textcomplete([
    // Emojies
    {
      match: /\B:([\-+\w]*)$/,
      search: function(term, callback) {
        callback($.map(Emoji.values, function(emoji) {
          return emoji.indexOf(term) === 0 ? emoji : null;
        }));
      },
      template: function(value) {
        var image = '<img src="' + Emoji.baseImagePath + value + '.png"></img>';
        return image + value;
      },
      replace: function(value) {
        return ':' + value + ':';
      },
      index: 1
    },

    // User mentions
    {
      match: /\B@(\w*)$/,
      search: function(term, callback) {
        var currentBoard = Boards.findOne(Session.get('currentBoard'));
        callback($.map(currentBoard.members, function(member) {
          var username = Users.findOne(member.userId).username;
          return username.indexOf(term) === 0 ? username : null;
        }));
      },
      template: function(value) {
        return value;
      },
      replace: function(username) {
        return '@' + username + ' ';
      },
      index: 1
    }
  ]);

  // Since commit d474017 jquery-textComplete automatically closes a potential
  // opened dropdown menu when the user press Escape. This behavior conflicts
  // with our EscapeActions system, but it's too complicated and hacky to
  // monkey-pach textComplete to disable it -- I tried. Instead we listen to
  // 'open' and 'hide' events, and create a ghost escapeAction when the dropdown
  // is opened (and rely on textComplete to execute the actual action).
  $textarea.on({
    'textComplete:show': function() {
      dropdownMenuIsOpened = true;
    },
    'textComplete:hide': function() {
      Tracker.afterFlush(function() {
        dropdownMenuIsOpened = false;
      });
    }
  });
});

EscapeActions.register('textcomplete',
  function() {},
  function() { return dropdownMenuIsOpened; }
);

Template.viewer.events({
  // Viewer sometimes have click-able wrapper around them (for instance to edit
  // the corresponding text). Clicking a link shouldn't fire these actions, stop
  // we stop these event at the viewer component level.
  'click a': function(evt) {
    evt.stopPropagation();

    // XXX We hijack the build-in browser action because we currently don't have
    // `_blank` attributes in viewer links, and the transformer function is
    // handled by a third party package that we can't configure easily. Fix that
    // by using directly `_blank` attribute in the rendered HTML.
    evt.preventDefault();
    let href = evt.currentTarget.href;
    window.open(href, '_blank');
  }
});