summaryrefslogtreecommitdiff
path: root/public/editor/editor.js
blob: 17cc24ed1f2ac12d370c774e83eb3a6d188829b1 (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
var editor = new MediumEditor('.editable', {
  buttons: ['bold', 'italic', 'anchor', 'header1', 'header2', 'quote', 'unorderedlist', 'pre'],
  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: '/editor/oembed'
      }
    }
  });
  $('.editable').focus(function(){
    $('.placeholder').removeClass('placeholder');
  });

  $.post('/editor/test-login', {}, function(response) {
    $('#publish_btn').text(response.logged_in ? 'Publish' : 'Sign In');
  });

  $('#publish_btn').click(function(){
    if($('#publish_btn').text() == 'Publish') {

      $.post('/editor/publish', {
        name: $("#post-name").val(),
        body: editor.serialize().content.value
      }, function(response) {
        if(response.location) {
          reset_page().then(function(){
            window.location = response.location;
          });
        }
      });

    } else {
      var url = prompt("Enter your URL");
      window.location = '/auth/start?me=' + encodeURIComponent(url) + '&redirect=/editor';
    }
  });

  $('#new_btn').click(function(){
    reset_page();
  });
});

function reset_page() {
  $("#post-name").val('');
  $("#content").html('<p class="placeholder">Write something nice...</p>');
  $("#draft-status").text("New");
  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
  }
  localforage.setItem('currentdraft', savedData).then(function(){
    $("#draft-status").text("Saved");
  });
}
$(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");
    }
  });
});
/* ************************************************ */


// 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').on('keyup', contentChanged);
});