summaryrefslogtreecommitdiff
path: root/public/editor-files/editor.js
blob: 516e16c15cd2b6ba17b9621d7f9df24cd96f8cba (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var editor = new MediumEditor('.editable', {
  toolbar: {
    buttons: ['bold', 'italic', 'anchor', 'h2', 'h3', 'quote', 'pre', 'unorderedlist']
  },
  placeholder: {text: 'Write something nice...'},
  paste: {
    // This example includes the default options for paste, if nothing is passed this is what it used
    forcePlainText: false,
    cleanPastedHTML: true,
    cleanReplacements: [],
    cleanAttrs: ['class', 'style', 'dir'],
    cleanTags: ['meta']
  }
});

$(function() {
  $('.editable').mediumInsert({
    editor: editor,
    beginning: true,
    addons: {
      images: {
        deleteScript: '/editor/delete-file',
        fileUploadOptions: {
          url: '/editor/upload'
        }
      },
      embeds: {
        oembedProxy: null
      }
    }
  });

  $.post('/editor/test-login', {}, function(response) {
    if(response.logged_in) {
      $('.publish-dropdown .action-publish').removeClass('hidden');
      $('.publish-dropdown .action-signin').addClass('hidden');
    } else {
      $('.publish-dropdown .action-publish').addClass('hidden');
      $('.publish-dropdown .action-signin').removeClass('hidden');
    }
  });

  $('#publish_btn').click(function(){
    if($('.publish-dropdown').hasClass('hidden')) {
      $('.publish-dropdown').removeClass('hidden');
      $('#publish-confirm').show();
      $('#publish-success').addClass('hidden');
      $('#publish-error').addClass('hidden');
      $('#publish-help').removeClass('hidden');
      $('#publish-fields').removeClass('hidden');
    } else {
      $('.publish-dropdown').addClass('hidden');
    }
  });

  $('#new_btn').click(function(){
    if(confirm('This will discard your current post. Are you sure?')) {
      reset_page();
    }
  });

  $('#signin-domain').on('keydown', function(e){
    if(e.keyCode == 13) {
      $('#signin-btn').click();
    }
  });
  $('#signin-btn').click(function(){
    window.location = '/auth/start?me=' + encodeURIComponent($('#signin-domain').val()) + '&redirect=/editor';
  });
  $('#publish-confirm').click(function(){
    $('#publish-help').addClass('hidden');
    $('#publish-in-progress').removeClass('hidden');
    $('#publish-fields').addClass('hidden');

    var category = $("#note_category").tokenfield("getTokens").map(function(t){ return t.value});

    $.post('/editor/publish', {
      name: $("#post-name").val(),
      body: editor.serialize().content.value,
      category: category,
      slug: $("#post-slug").val(),
      status: $("#post-status").val(),
      publish: $("#post-publish-date").val()
    }, function(response) {
      if(response.location) {
        reset_page().then(function(){
          $('#publish-success-url').attr('href', response.location);
          $('#publish-in-progress').addClass('hidden');
          $('#publish-error-debug').html('').addClass('hidden');
          $('#publish-error').addClass('hidden');
          $('#publish-success').removeClass('hidden');
        });
      } else {
        $('#publish-in-progress').addClass('hidden');
        $('#publish-error-debug').html(response.response).removeClass('hidden');
        $('#publish-error').removeClass('hidden');
        $('#publish-success').addClass('hidden');
        $('#publish-fields').removeClass('hidden');
      }
    });
  });

  $("#micropub-html-btn").click(function(){
    $.post('/settings/html-content', {
      html: 1
    }, function(data){
      $('.micropub-html-warning').hide();
    });
  });

  $("#post-status").change(function(){
    $("#published-status-warning").removeClass("hidden");
  });

  $("#post-publish-date").change(function(){
    if($("#post-publish-date").val() == "") {
      $("#post-publish-date").val("now");
    } else {
      // Parse and verify the publish date when it's changed
      $.post('/editor/parse-date', {
        date: $("#post-publish-date").val(),
        tzoffset: (new Date().getTimezoneOffset())
      }, function(response) {
        if(response.date) {
          $("#post-publish-date").val(response.date);
        } else {
          $("#post-publish-date").val("now");
        }
      });
    }
  });

  $.getJSON('/settings/html-content', function(data){
    if(data.html == '0') {
      $('.micropub-html-warning').show();
    }
  });
});

function reset_page() {
  $("#post-name").val('');
  $("#post-slug").val('');
  $("#post-tags").tokenfield('setTokens',[]);
  $("#post-status").val('published');
  $("#post-publish-date").val('now');
  $("#content").html('');
  $("#draft-status").text("New");
  $("#publish-confirm").hide();
  return localforage.setItem('currentdraft', {});
}

/* ************************************************ */
/* autosave loop */
var autosaveTimeout = false;
function contentChanged() {
  clearTimeout(autosaveTimeout);
  $("#draft-status").text("Draft");
  autosaveTimeout = setTimeout(doAutoSave, 1000);
}
function doAutoSave() {
  autosaveTimeout = false;
  var savedData = {
    title: $("#post-name").val(),
    body: editor.serialize().content.value,
    tags: $("#post-tags").tokenfield('getTokensList'),
    slug: $("#post-slug").val(),
    status: $("#post-status").val(),
    publish: $("#post-publish-date").val()
  }
  localforage.setItem('currentdraft', savedData).then(function(){
    $("#draft-status").text("Saved");
  });
}
function activateTokenField() {
  $("#post-tags").tokenfield({
    createTokensOnBlur: true,
    beautify: true
  }).on('tokenfield:createdtoken', contentChanged)
    .on('tokenfield:removedtoken', contentChanged);
}
$(function(){
  // Restore draft if present
  localforage.getItem('currentdraft', function(err,val){
    if(val && val.body) {
      $("#post-name").val(val.title);
      $("#content").html(val.body);
      $("#draft-status").text("Restored");
      $("#post-tags").val(val.tags);
      $("#post-slug").val(val.slug);
      $("#post-status").val(val.status);
      $("#post-publish-date").val(val.publish);
      // drop the cursor into the editor which clears the placeholder text
      $("#content").focus().click();
      activateTokenField();
    } else {
      activateTokenField();
    }
  });
});
/* ************************************************ */


// Not sure why this isn't working
// editor.subscribe('editableInput', function(ev, editable) {
//   console.log("stuff changed");
// });

// This one works okay tho, but misses changes from the image uploader
editor.on(document.getElementById('content'), 'input', function(){
  contentChanged();
});
$(function(){
  $('#post-name, #post-tags, #post-slug, #post-publish-date').on('keyup', contentChanged);
});