summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/editor.js
blob: 4b35aeac3c6a998cb1b16305dc77b3be8e005668 (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
86
87
88
89
90
91
92
$(function() {

  $('.md_editor .field_container .preview-button').click(function() {
    if ($(this).data('preview') == 'true') {
      edit($(this));
    } else {
      preview($(this));
    }
  });

  function edit(target) {
    target.data('preview', 'false');
    target.text('Preview');
    target.parent().find('.preview').hide();
    target.parent().find('.editor_field').show();
  }

  function preview(target) {
    target.data('preview', 'true');
    target.text('Edit');
    var prev   = target.parent().find('.preview');
    var editor = target.parent().find('.editor_field');
    prev.html("<i>(Loading ...)</i>");
    prev.show();
    editor.hide();
    var url = '/tools/render_markdown';
    $.ajax(url, {
      type: 'post',
      data: {text: editor.val()},
      dataType: 'html',
      headers: {
        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
      },
      success: function(data) {
        prev.html(data);
        prev.find('pre code').each(function(i, e) {
          if ($(this).attr("class")) {
            $(this).parent().attr("lang", $(this).attr("class").replace("hljs", "").trim());
          } else {
            $(this).parent().attr("lang", "(language unknown)");
          }
          hljs.highlightBlock(e);
        });
      },
      error: function(xhr, status, err) {
        prev.html('<i>(Error: ' + status + ')</i><br>' + err);
      }
    });
  }

  $('.md_editor .editor_field').textcomplete([{
    // match up to 2 words (everything except some special characters)
    // each word can have up to 16 characters (up to 32 total)
    // words must be separated by a single space
    match: /(^|\s)@(([^!"§$%&\/()=?.,;+*@\s]{1,16} ?){0,1}[^!"§$%&\/()=?.,;+*@\s]{1,16})$/,
    search: function (text, callback, match) {
      console.log("Searching " + text);
      text = text.toLowerCase();
      $.ajax("/users/suggestions", {
        type: "post",
        data: {name: text},
        dataType: "json",
        headers: {
          "X-CSRF-Token": $('meta[name="csrf-token"]').attr("content")
        },
        success: function(data) {
          callback(data);
        },
        error: function(xhr, status, err) {
          console.error(err);
          callback([]);
        }
      });
    },
    template: function(user) {
      var name = user[0];
      var ign  = user[1];
      if (name != ign) {
        return name + " <small>(" + ign + ")</small>";
      } else {
        return ign;
      }
    },
    cache: true,
    replace: function (word) {
      return "$1@" + word[1] + " ";
    }
  }], {
    debounce: 300
  });

});