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.

130 lines
3.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. $('#publish_btn span').text(response.logged_in ? 'Publish' : 'Sign In');
  33. });
  34. $('#publish_btn').click(function(){
  35. if($('.publish-dropdown').hasClass('hidden')) {
  36. $('.publish-dropdown').removeClass('hidden');
  37. } else {
  38. $('.publish-dropdown').addClass('hidden');
  39. }
  40. });
  41. $('#--publish_btn').click(function(){
  42. if($('#publish_btn span').text() == 'Publish') {
  43. $.post('/editor/publish', {
  44. name: $("#post-name").val(),
  45. body: editor.serialize().content.value
  46. }, function(response) {
  47. if(response.location) {
  48. reset_page().then(function(){
  49. window.location = response.location;
  50. });
  51. }
  52. });
  53. } else {
  54. var url = prompt("Enter your URL");
  55. window.location = '/auth/start?me=' + encodeURIComponent(url) + '&redirect=/editor';
  56. }
  57. });
  58. $('#new_btn').click(function(){
  59. reset_page();
  60. });
  61. });
  62. function reset_page() {
  63. $("#post-name").val('');
  64. $("#content").html('<p class="placeholder">Write something nice...</p>');
  65. $("#draft-status").text("New");
  66. return localforage.setItem('currentdraft', {});
  67. }
  68. function onUpdateReady() {
  69. // Show the notice that says there is a new version of the app
  70. $("#new_version_available").show();
  71. }
  72. window.applicationCache.addEventListener('updateready', onUpdateReady);
  73. if(window.applicationCache.status === window.applicationCache.UPDATEREADY) {
  74. onUpdateReady();
  75. }
  76. /* ************************************************ */
  77. /* autosave loop */
  78. var autosaveTimeout = false;
  79. function contentChanged() {
  80. clearTimeout(autosaveTimeout);
  81. $("#draft-status").text("Draft");
  82. autosaveTimeout = setTimeout(doAutoSave, 1000);
  83. }
  84. function doAutoSave() {
  85. autosaveTimeout = false;
  86. var savedData = {
  87. title: $("#post-name").val(),
  88. body: editor.serialize().content.value
  89. }
  90. localforage.setItem('currentdraft', savedData).then(function(){
  91. $("#draft-status").text("Saved");
  92. });
  93. }
  94. $(function(){
  95. // Restore draft if present
  96. localforage.getItem('currentdraft', function(err,val){
  97. if(val && val.body) {
  98. $("#post-name").val(val.title);
  99. $("#content").html(val.body);
  100. $("#draft-status").text("Restored");
  101. }
  102. });
  103. });
  104. /* ************************************************ */
  105. // Not sure why this isn't working
  106. // editor.subscribe('editableInput', function(ev, editable) {
  107. // console.log("stuff changed");
  108. // });
  109. // This one works okay tho, but misses changes from the image uploader
  110. editor.on(document.getElementById('content'), 'input', function(){
  111. contentChanged();
  112. });
  113. $(function(){
  114. $('#post-name').on('keyup', contentChanged);
  115. });