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.

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