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.

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