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.

210 lines
6.2 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. $('#publish-fields').removeClass('hidden');
  48. } else {
  49. $('.publish-dropdown').addClass('hidden');
  50. }
  51. });
  52. $('#new_btn').click(function(){
  53. if(confirm('This will discard your current post. Are you sure?')) {
  54. reset_page();
  55. }
  56. });
  57. $('#signin-domain').on('keydown', function(e){
  58. if(e.keyCode == 13) {
  59. $('#signin-btn').click();
  60. }
  61. });
  62. $('#signin-btn').click(function(){
  63. window.location = '/auth/start?me=' + encodeURIComponent($('#signin-domain').val()) + '&redirect=/editor';
  64. });
  65. $('#publish-confirm').click(function(){
  66. $('#publish-help').addClass('hidden');
  67. $('#publish-in-progress').removeClass('hidden');
  68. $('#publish-fields').addClass('hidden');
  69. $.post('/editor/publish', {
  70. name: $("#post-name").val(),
  71. body: editor.serialize().content.value,
  72. category: csv_to_array($("#post-tags").val()),
  73. slug: $("#post-slug").val(),
  74. status: $("#post-status").val(),
  75. publish: $("#post-publish-date").val()
  76. }, function(response) {
  77. if(response.location) {
  78. reset_page().then(function(){
  79. $('#publish-success-url').attr('href', response.location);
  80. $('#publish-in-progress').addClass('hidden');
  81. $('#publish-error-debug').html('').addClass('hidden');
  82. $('#publish-error').addClass('hidden');
  83. $('#publish-success').removeClass('hidden');
  84. });
  85. } else {
  86. $('#publish-in-progress').addClass('hidden');
  87. $('#publish-error-debug').html(response.response).removeClass('hidden');
  88. $('#publish-error').removeClass('hidden');
  89. $('#publish-success').addClass('hidden');
  90. $('#publish-fields').removeClass('hidden');
  91. }
  92. });
  93. });
  94. $("#micropub-html-btn").click(function(){
  95. $.post('/settings/html-content', {
  96. html: 1
  97. }, function(data){
  98. $('.micropub-html-warning').hide();
  99. });
  100. });
  101. $("#post-status").change(function(){
  102. $("#published-status-warning").removeClass("hidden");
  103. });
  104. $("#post-publish-date").change(function(){
  105. if($("#post-publish-date").val() == "") {
  106. $("#post-publish-date").val("now");
  107. } else {
  108. // Parse and verify the publish date when it's changed
  109. $.post('/editor/parse-date', {
  110. date: $("#post-publish-date").val(),
  111. tzoffset: (new Date().getTimezoneOffset())
  112. }, function(response) {
  113. if(response.date) {
  114. $("#post-publish-date").val(response.date);
  115. } else {
  116. $("#post-publish-date").val("now");
  117. }
  118. });
  119. }
  120. });
  121. $.getJSON('/settings/html-content', function(data){
  122. if(data.html == '0') {
  123. $('.micropub-html-warning').show();
  124. }
  125. });
  126. });
  127. function reset_page() {
  128. $("#post-name").val('');
  129. $("#post-slug").val('');
  130. $("#post-tags").val('');
  131. $("#post-status").val('published');
  132. $("#post-publish-date").val('now');
  133. $("#content").html('');
  134. $("#draft-status").text("New");
  135. $("#publish-confirm").hide();
  136. return localforage.setItem('currentdraft', {});
  137. }
  138. function csv_to_array(val) {
  139. if(val.length > 0) {
  140. return val.split(/[, ]+/);
  141. } else {
  142. return [];
  143. }
  144. }
  145. /* ************************************************ */
  146. /* autosave loop */
  147. var autosaveTimeout = false;
  148. function contentChanged() {
  149. clearTimeout(autosaveTimeout);
  150. $("#draft-status").text("Draft");
  151. autosaveTimeout = setTimeout(doAutoSave, 1000);
  152. }
  153. function doAutoSave() {
  154. autosaveTimeout = false;
  155. var savedData = {
  156. title: $("#post-name").val(),
  157. body: editor.serialize().content.value,
  158. tags: $("#post-tags").val(),
  159. slug: $("#post-slug").val(),
  160. status: $("#post-status").val(),
  161. publish: $("#post-publish-date").val()
  162. }
  163. localforage.setItem('currentdraft', savedData).then(function(){
  164. $("#draft-status").text("Saved");
  165. });
  166. }
  167. $(function(){
  168. // Restore draft if present
  169. localforage.getItem('currentdraft', function(err,val){
  170. if(val && val.body) {
  171. $("#post-name").val(val.title);
  172. $("#content").html(val.body);
  173. $("#draft-status").text("Restored");
  174. $("#post-tags").val(val.tags);
  175. $("#post-slug").val(val.slug);
  176. $("#post-status").val(val.status);
  177. $("#post-publish-date").val(val.publish);
  178. // drop the cursor into the editor which clears the placeholder text
  179. $("#content").focus().click();
  180. }
  181. });
  182. });
  183. /* ************************************************ */
  184. // Not sure why this isn't working
  185. // editor.subscribe('editableInput', function(ev, editable) {
  186. // console.log("stuff changed");
  187. // });
  188. // This one works okay tho, but misses changes from the image uploader
  189. editor.on(document.getElementById('content'), 'input', function(){
  190. contentChanged();
  191. });
  192. $(function(){
  193. $('#post-name, #post-tags, #post-slug, #post-publish-date').on('keyup', contentChanged);
  194. });