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.

214 lines
6.4 KiB

6 years ago
6 years ago
6 years ago
  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: null
  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. var category = $("#post-tags").tokenfield("getTokens").map(function(t){ return t.value});
  70. $.post('/editor/publish', {
  71. name: $("#post-name").val(),
  72. body: editor.serialize().content.value,
  73. category: category,
  74. slug: $("#post-slug").val(),
  75. status: $("#post-status").val(),
  76. publish: $("#post-publish-date").val()
  77. }, function(response) {
  78. if(response.location) {
  79. reset_page().then(function(){
  80. $('#publish-success-url').attr('href', response.location);
  81. $('#publish-in-progress').addClass('hidden');
  82. $('#publish-error-debug').html('').addClass('hidden');
  83. $('#publish-error').addClass('hidden');
  84. $('#publish-success').removeClass('hidden');
  85. });
  86. } else {
  87. $('#publish-in-progress').addClass('hidden');
  88. $('#publish-error-debug').html(response.response).removeClass('hidden');
  89. $('#publish-error').removeClass('hidden');
  90. $('#publish-success').addClass('hidden');
  91. $('#publish-fields').removeClass('hidden');
  92. }
  93. });
  94. });
  95. $("#micropub-html-btn").click(function(){
  96. $.post('/settings/html-content', {
  97. html: 1
  98. }, function(data){
  99. $('.micropub-html-warning').hide();
  100. });
  101. });
  102. $("#post-status").change(function(){
  103. $("#published-status-warning").removeClass("hidden");
  104. });
  105. $("#post-publish-date").change(function(){
  106. if($("#post-publish-date").val() == "") {
  107. $("#post-publish-date").val("now");
  108. } else {
  109. // Parse and verify the publish date when it's changed
  110. $.post('/editor/parse-date', {
  111. date: $("#post-publish-date").val(),
  112. tzoffset: (new Date().getTimezoneOffset())
  113. }, function(response) {
  114. if(response.date) {
  115. $("#post-publish-date").val(response.date);
  116. } else {
  117. $("#post-publish-date").val("now");
  118. }
  119. });
  120. }
  121. });
  122. $.getJSON('/settings/html-content', function(data){
  123. if(data.html == '0') {
  124. $('.micropub-html-warning').show();
  125. }
  126. });
  127. });
  128. function reset_page() {
  129. $("#post-name").val('');
  130. $("#post-slug").val('');
  131. $("#post-tags").tokenfield('setTokens',[]);
  132. $("#post-status").val('published');
  133. $("#post-publish-date").val('now');
  134. $("#content").html('');
  135. $("#draft-status").text("New");
  136. $("#publish-confirm").hide();
  137. return localforage.setItem('currentdraft', {});
  138. }
  139. /* ************************************************ */
  140. /* autosave loop */
  141. var autosaveTimeout = false;
  142. function contentChanged() {
  143. clearTimeout(autosaveTimeout);
  144. $("#draft-status").text("Draft");
  145. autosaveTimeout = setTimeout(doAutoSave, 1000);
  146. }
  147. function doAutoSave() {
  148. autosaveTimeout = false;
  149. var savedData = {
  150. title: $("#post-name").val(),
  151. body: editor.serialize().content.value,
  152. tags: $("#post-tags").tokenfield('getTokensList'),
  153. slug: $("#post-slug").val(),
  154. status: $("#post-status").val(),
  155. publish: $("#post-publish-date").val()
  156. }
  157. localforage.setItem('currentdraft', savedData).then(function(){
  158. $("#draft-status").text("Saved");
  159. });
  160. }
  161. function activateTokenField() {
  162. $("#post-tags").tokenfield({
  163. createTokensOnBlur: true,
  164. beautify: true
  165. }).on('tokenfield:createdtoken', contentChanged)
  166. .on('tokenfield:removedtoken', contentChanged);
  167. }
  168. $(function(){
  169. // Restore draft if present
  170. localforage.getItem('currentdraft', function(err,val){
  171. if(val && val.body) {
  172. $("#post-name").val(val.title);
  173. $("#content").html(val.body);
  174. $("#draft-status").text("Restored");
  175. $("#post-tags").val(val.tags);
  176. $("#post-slug").val(val.slug);
  177. $("#post-status").val(val.status);
  178. $("#post-publish-date").val(val.publish);
  179. // drop the cursor into the editor which clears the placeholder text
  180. $("#content").focus().click();
  181. activateTokenField();
  182. } else {
  183. activateTokenField();
  184. }
  185. });
  186. });
  187. /* ************************************************ */
  188. // Not sure why this isn't working
  189. // editor.subscribe('editableInput', function(ev, editable) {
  190. // console.log("stuff changed");
  191. // });
  192. // This one works okay tho, but misses changes from the image uploader
  193. editor.on(document.getElementById('content'), 'input', function(){
  194. contentChanged();
  195. });
  196. $(function(){
  197. $('#post-name, #post-tags, #post-slug, #post-publish-date').on('keyup', contentChanged);
  198. });