You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.9 KiB

  1. var editor = new MediumEditor('.editable', {
  2. buttons: ['bold', 'italic', 'anchor', 'header1', 'header2', 'quote', 'unorderedlist', 'pre'],
  3. paste: {
  4. // This example includes the default options for paste, if nothing is passed this is what it used
  5. forcePlainText: false,
  6. cleanPastedHTML: true,
  7. cleanReplacements: [],
  8. cleanAttrs: ['class', 'style', 'dir'],
  9. cleanTags: ['meta']
  10. }
  11. });
  12. $(function () {
  13. $('.editable').mediumInsert({
  14. editor: editor,
  15. beginning: true,
  16. addons: {
  17. images: {
  18. deleteScript: '/editor/delete-file',
  19. fileUploadOptions: {
  20. url: '/editor/upload'
  21. }
  22. },
  23. embeds: {
  24. oembedProxy: '/editor/oembed'
  25. }
  26. }
  27. });
  28. $('.editable').focus(function(){
  29. $('.placeholder').removeClass('placeholder');
  30. });
  31. $.post('/editor/test-login', {}, function(response) {
  32. $('#publish_btn').text(response.logged_in ? 'Publish' : 'Sign In');
  33. });
  34. $('#publish_btn').click(function(){
  35. if($('#publish_btn').text() == 'Publish') {
  36. $.post('/editor/publish', {
  37. name: $("#post-name").val(),
  38. body: editor.serialize().content.value
  39. }, function(response) {
  40. if(response.location) {
  41. reset_page().then(function(){
  42. window.location = response.location;
  43. });
  44. }
  45. });
  46. } else {
  47. var url = prompt("Enter your URL");
  48. window.location = '/auth/start?me=' + encodeURIComponent(url) + '&redirect=/editor';
  49. }
  50. });
  51. $('#new_btn').click(function(){
  52. reset_page();
  53. });
  54. });
  55. function reset_page() {
  56. $("#post-name").val('');
  57. $("#content").html('<p class="placeholder">Write something nice...</p>');
  58. $("#draft-status").text("New");
  59. return localforage.setItem('currentdraft', {});
  60. }
  61. /* ************************************************ */
  62. /* autosave loop */
  63. var autosaveTimeout = false;
  64. function contentChanged() {
  65. clearTimeout(autosaveTimeout);
  66. $("#draft-status").text("Draft");
  67. autosaveTimeout = setTimeout(doAutoSave, 1000);
  68. }
  69. function doAutoSave() {
  70. autosaveTimeout = false;
  71. var savedData = {
  72. title: $("#post-name").val(),
  73. body: editor.serialize().content.value
  74. }
  75. localforage.setItem('currentdraft', savedData).then(function(){
  76. $("#draft-status").text("Saved");
  77. });
  78. }
  79. $(function(){
  80. // Restore draft if present
  81. localforage.getItem('currentdraft', function(err,val){
  82. if(val && val.body) {
  83. $("#post-name").val(val.title);
  84. $("#content").html(val.body);
  85. $("#draft-status").text("Restored");
  86. }
  87. });
  88. });
  89. /* ************************************************ */
  90. // Not sure why this isn't working
  91. // editor.subscribe('editableInput', function(ev, editable) {
  92. // console.log("stuff changed");
  93. // });
  94. // This one works okay tho, but misses changes from the image uploader
  95. editor.on(document.getElementById('content'), 'input', function(){
  96. contentChanged();
  97. });
  98. $(function(){
  99. $('#post-name').on('keyup', contentChanged);
  100. });