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.

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