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.

148 lines
4.3 KiB

  1. var editor = new MediumEditor('.editable', {
  2. buttons: ['bold', 'italic', 'anchor', 'header1', 'header2', 'quote', 'unorderedlist', 'pre'],
  3. placeholder: {text: 'Write something nice...'},
  4. paste: {
  5. // This example includes the default options for paste, if nothing is passed this is what it used
  6. forcePlainText: false,
  7. cleanPastedHTML: true,
  8. cleanReplacements: [],
  9. cleanAttrs: ['class', 'style', 'dir'],
  10. cleanTags: ['meta']
  11. }
  12. });
  13. $(function() {
  14. $('.editable').mediumInsert({
  15. editor: editor,
  16. beginning: true,
  17. addons: {
  18. images: {
  19. deleteScript: '/editor/delete-file',
  20. fileUploadOptions: {
  21. url: '/editor/upload'
  22. }
  23. },
  24. embeds: {
  25. oembedProxy: '/editor/oembed'
  26. }
  27. }
  28. });
  29. $.post('/editor/test-login', {}, function(response) {
  30. if(response.logged_in) {
  31. $('.publish-dropdown .action-publish').removeClass('hidden');
  32. $('.publish-dropdown .action-signin').addClass('hidden');
  33. } else {
  34. $('.publish-dropdown .action-publish').addClass('hidden');
  35. $('.publish-dropdown .action-signin').removeClass('hidden');
  36. }
  37. });
  38. $('#publish_btn').click(function(){
  39. if($('.publish-dropdown').hasClass('hidden')) {
  40. $('.publish-dropdown').removeClass('hidden');
  41. } else {
  42. $('.publish-dropdown').addClass('hidden');
  43. }
  44. });
  45. $('#new_btn').click(function(){
  46. if(confirm('This will discard your current post. Are you sure?')) {
  47. reset_page();
  48. }
  49. });
  50. $('#signin-domain').on('keydown', function(e){
  51. if(e.keyCode == 13) {
  52. $('#signin-btn').click();
  53. }
  54. });
  55. $('#signin-btn').click(function(){
  56. window.location = '/auth/start?me=' + encodeURIComponent($('#signin-domain').val()) + '&redirect=/editor';
  57. });
  58. $('#publish-confirm').click(function(){
  59. $('#publish-help').addClass('hidden');
  60. $('#publish-in-progress').removeClass('hidden');
  61. $.post('/editor/publish', {
  62. name: $("#post-name").val(),
  63. body: editor.serialize().content.value
  64. }, function(response) {
  65. if(response.location) {
  66. reset_page().then(function(){
  67. $('#publish-success-url').attr('href', response.location);
  68. $('#publish-in-progress').addClass('hidden');
  69. $('#publish-error-debug').html('').addClass('hidden');
  70. $('#publish-error').addClass('hidden');
  71. $('#publish-success').removeClass('hidden');
  72. });
  73. } else {
  74. $('#publish-in-progress').addClass('hidden');
  75. $('#publish-error-debug').html(response.response).removeClass('hidden');
  76. $('#publish-error').removeClass('hidden');
  77. $('#publish-success').addClass('hidden');
  78. }
  79. });
  80. });
  81. });
  82. function reset_page() {
  83. $("#post-name").val('');
  84. $("#draft-status").text("New");
  85. return localforage.setItem('currentdraft', {});
  86. }
  87. function onUpdateReady() {
  88. // Show the notice that says there is a new version of the app
  89. $("#new_version_available").show();
  90. }
  91. window.applicationCache.addEventListener('updateready', onUpdateReady);
  92. if(window.applicationCache.status === window.applicationCache.UPDATEREADY) {
  93. onUpdateReady();
  94. }
  95. /* ************************************************ */
  96. /* autosave loop */
  97. var autosaveTimeout = false;
  98. function contentChanged() {
  99. clearTimeout(autosaveTimeout);
  100. $("#draft-status").text("Draft");
  101. autosaveTimeout = setTimeout(doAutoSave, 1000);
  102. }
  103. function doAutoSave() {
  104. autosaveTimeout = false;
  105. var savedData = {
  106. title: $("#post-name").val(),
  107. body: editor.serialize().content.value
  108. }
  109. localforage.setItem('currentdraft', savedData).then(function(){
  110. $("#draft-status").text("Saved");
  111. });
  112. }
  113. $(function(){
  114. // Restore draft if present
  115. localforage.getItem('currentdraft', function(err,val){
  116. if(val && val.body) {
  117. $("#post-name").val(val.title);
  118. $("#content").html(val.body);
  119. $("#draft-status").text("Restored");
  120. }
  121. });
  122. });
  123. /* ************************************************ */
  124. // Not sure why this isn't working
  125. // editor.subscribe('editableInput', function(ev, editable) {
  126. // console.log("stuff changed");
  127. // });
  128. // This one works okay tho, but misses changes from the image uploader
  129. editor.on(document.getElementById('content'), 'input', function(){
  130. contentChanged();
  131. });
  132. $(function(){
  133. $('#post-name').on('keyup', contentChanged);
  134. });