Mirror of Quill
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.

169 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. $('.micropub-html-warning').hide();
  90. });
  91. });
  92. $.getJSON('/settings/html-content', function(data){
  93. if(data.html == '0') {
  94. $('.micropub-html-warning').show();
  95. }
  96. });
  97. });
  98. function reset_page() {
  99. $("#post-name").val('');
  100. $("#content").html('');
  101. $("#draft-status").text("New");
  102. $("#publish-confirm").hide();
  103. return localforage.setItem('currentdraft', {});
  104. }
  105. function onUpdateReady() {
  106. // Show the notice that says there is a new version of the app
  107. $("#new_version_available").show();
  108. }
  109. window.applicationCache.addEventListener('updateready', onUpdateReady);
  110. if(window.applicationCache.status === window.applicationCache.UPDATEREADY) {
  111. onUpdateReady();
  112. }
  113. /* ************************************************ */
  114. /* autosave loop */
  115. var autosaveTimeout = false;
  116. function contentChanged() {
  117. clearTimeout(autosaveTimeout);
  118. $("#draft-status").text("Draft");
  119. autosaveTimeout = setTimeout(doAutoSave, 1000);
  120. }
  121. function doAutoSave() {
  122. autosaveTimeout = false;
  123. var savedData = {
  124. title: $("#post-name").val(),
  125. body: editor.serialize().content.value
  126. }
  127. localforage.setItem('currentdraft', savedData).then(function(){
  128. $("#draft-status").text("Saved");
  129. });
  130. }
  131. $(function(){
  132. // Restore draft if present
  133. localforage.getItem('currentdraft', function(err,val){
  134. if(val && val.body) {
  135. $("#post-name").val(val.title);
  136. $("#content").html(val.body);
  137. $("#draft-status").text("Restored");
  138. // drop the cursor into the editor which clears the placeholder text
  139. $("#content").focus().click();
  140. }
  141. });
  142. });
  143. /* ************************************************ */
  144. // Not sure why this isn't working
  145. // editor.subscribe('editableInput', function(ev, editable) {
  146. // console.log("stuff changed");
  147. // });
  148. // This one works okay tho, but misses changes from the image uploader
  149. editor.on(document.getElementById('content'), 'input', function(){
  150. contentChanged();
  151. });
  152. $(function(){
  153. $('#post-name').on('keyup', contentChanged);
  154. });