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.

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