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.

226 lines
6.9 KiB

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