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.

168 lines
4.9 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. $('#publish-confirm').show();
  42. $('#publish-success').addClass('hidden');
  43. $('#publish-error').addClass('hidden');
  44. $('#publish-help').removeClass('hidden');
  45. } else {
  46. $('.publish-dropdown').addClass('hidden');
  47. }
  48. });
  49. $('#new_btn').click(function(){
  50. if(confirm('This will discard your current post. Are you sure?')) {
  51. reset_page();
  52. }
  53. });
  54. $('#signin-domain').on('keydown', function(e){
  55. if(e.keyCode == 13) {
  56. $('#signin-btn').click();
  57. }
  58. });
  59. $('#signin-btn').click(function(){
  60. window.location = '/auth/start?me=' + encodeURIComponent($('#signin-domain').val()) + '&redirect=/editor';
  61. });
  62. $('#publish-confirm').click(function(){
  63. $('#publish-help').addClass('hidden');
  64. $('#publish-in-progress').removeClass('hidden');
  65. $.post('/editor/publish', {
  66. name: $("#post-name").val(),
  67. body: editor.serialize().content.value
  68. }, function(response) {
  69. if(response.location) {
  70. reset_page().then(function(){
  71. $('#publish-success-url').attr('href', response.location);
  72. $('#publish-in-progress').addClass('hidden');
  73. $('#publish-error-debug').html('').addClass('hidden');
  74. $('#publish-error').addClass('hidden');
  75. $('#publish-success').removeClass('hidden');
  76. });
  77. } else {
  78. $('#publish-in-progress').addClass('hidden');
  79. $('#publish-error-debug').html(response.response).removeClass('hidden');
  80. $('#publish-error').removeClass('hidden');
  81. $('#publish-success').addClass('hidden');
  82. }
  83. });
  84. });
  85. $("#micropub-html-btn").click(function(){
  86. $.post('/settings/html-content', {
  87. html: 1
  88. }, function(data){
  89. });
  90. });
  91. $.getJSON('/settings/html-content', function(data){
  92. if(data.html == '0') {
  93. $('.micropub-html-warning').show();
  94. }
  95. });
  96. });
  97. function reset_page() {
  98. $("#post-name").val('');
  99. $("#content").html('');
  100. $("#draft-status").text("New");
  101. $("#publish-confirm").hide();
  102. return localforage.setItem('currentdraft', {});
  103. }
  104. function onUpdateReady() {
  105. // Show the notice that says there is a new version of the app
  106. $("#new_version_available").show();
  107. }
  108. window.applicationCache.addEventListener('updateready', onUpdateReady);
  109. if(window.applicationCache.status === window.applicationCache.UPDATEREADY) {
  110. onUpdateReady();
  111. }
  112. /* ************************************************ */
  113. /* autosave loop */
  114. var autosaveTimeout = false;
  115. function contentChanged() {
  116. clearTimeout(autosaveTimeout);
  117. $("#draft-status").text("Draft");
  118. autosaveTimeout = setTimeout(doAutoSave, 1000);
  119. }
  120. function doAutoSave() {
  121. autosaveTimeout = false;
  122. var savedData = {
  123. title: $("#post-name").val(),
  124. body: editor.serialize().content.value
  125. }
  126. localforage.setItem('currentdraft', savedData).then(function(){
  127. $("#draft-status").text("Saved");
  128. });
  129. }
  130. $(function(){
  131. // Restore draft if present
  132. localforage.getItem('currentdraft', function(err,val){
  133. if(val && val.body) {
  134. $("#post-name").val(val.title);
  135. $("#content").html(val.body);
  136. $("#draft-status").text("Restored");
  137. // drop the cursor into the editor which clears the placeholder text
  138. $("#content").focus().click();
  139. }
  140. });
  141. });
  142. /* ************************************************ */
  143. // Not sure why this isn't working
  144. // editor.subscribe('editableInput', function(ev, editable) {
  145. // console.log("stuff changed");
  146. // });
  147. // This one works okay tho, but misses changes from the image uploader
  148. editor.on(document.getElementById('content'), 'input', function(){
  149. contentChanged();
  150. });
  151. $(function(){
  152. $('#post-name').on('keyup', contentChanged);
  153. });