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.

222 lines
6.5 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. var category = csv_to_array($("#post-tags").tokenfield('getTokensList'));
  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. function csv_to_array(val) {
  140. if(val.length > 0) {
  141. return val.split(/[, ]+/);
  142. } else {
  143. return [];
  144. }
  145. }
  146. /* ************************************************ */
  147. /* autosave loop */
  148. var autosaveTimeout = false;
  149. function contentChanged() {
  150. clearTimeout(autosaveTimeout);
  151. $("#draft-status").text("Draft");
  152. autosaveTimeout = setTimeout(doAutoSave, 1000);
  153. }
  154. function doAutoSave() {
  155. autosaveTimeout = false;
  156. var savedData = {
  157. title: $("#post-name").val(),
  158. body: editor.serialize().content.value,
  159. tags: $("#post-tags").tokenfield('getTokensList'),
  160. slug: $("#post-slug").val(),
  161. status: $("#post-status").val(),
  162. publish: $("#post-publish-date").val()
  163. }
  164. localforage.setItem('currentdraft', savedData).then(function(){
  165. $("#draft-status").text("Saved");
  166. });
  167. }
  168. function activateTokenField() {
  169. $("#post-tags").tokenfield({
  170. createTokensOnBlur: true,
  171. beautify: true
  172. }).on('tokenfield:createdtoken', contentChanged)
  173. .on('tokenfield:removedtoken', contentChanged);
  174. }
  175. $(function(){
  176. // Restore draft if present
  177. localforage.getItem('currentdraft', function(err,val){
  178. if(val && val.body) {
  179. $("#post-name").val(val.title);
  180. $("#content").html(val.body);
  181. $("#draft-status").text("Restored");
  182. $("#post-tags").val(val.tags);
  183. $("#post-slug").val(val.slug);
  184. $("#post-status").val(val.status);
  185. $("#post-publish-date").val(val.publish);
  186. // drop the cursor into the editor which clears the placeholder text
  187. $("#content").focus().click();
  188. activateTokenField();
  189. } else {
  190. activateTokenField();
  191. }
  192. });
  193. });
  194. /* ************************************************ */
  195. // Not sure why this isn't working
  196. // editor.subscribe('editableInput', function(ev, editable) {
  197. // console.log("stuff changed");
  198. // });
  199. // This one works okay tho, but misses changes from the image uploader
  200. editor.on(document.getElementById('content'), 'input', function(){
  201. contentChanged();
  202. });
  203. $(function(){
  204. $('#post-name, #post-tags, #post-slug, #post-publish-date').on('keyup', contentChanged);
  205. });