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.

182 lines
5.2 KiB

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