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.

script.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. function createCloseButton() {
  2. const container = document.createElement('div');
  3. container.style.marginBottom = '1em';
  4. container.style.display = 'flex';
  5. container.style.justifyContent = 'flex-end';
  6. const closeButton = document.createElement('a');
  7. closeButton.classList.add('btn');
  8. closeButton.classList.add('btn--close');
  9. closeButton.href = '#';
  10. closeButton.innerText = 'x close';
  11. closeButton.style.marginLeft = 'auto';
  12. closeButton.addEventListener('click', (e) => {
  13. e.preventDefault();
  14. container.parentElement.remove();
  15. });
  16. container.append(closeButton);
  17. return container;
  18. }
  19. document.querySelectorAll('[data-delete]').forEach((el) => {
  20. el.addEventListener('click', (e) => {
  21. if (confirm('Are you sure? This cannot be undone.')) {
  22. let slug = e.target.dataset.delete;
  23. fetch(`/posts/${slug}`, {
  24. method: 'DELETE',
  25. }).then((r) => {
  26. r.json().then((data) => {
  27. if (data.success) {
  28. window.location = '/';
  29. }
  30. });
  31. });
  32. }
  33. });
  34. });
  35. const previewButton = document.querySelector('[data-preview]');
  36. if (previewButton) {
  37. previewButton.addEventListener('click', (e) => {
  38. e.preventDefault();
  39. const currentPreview = document.querySelector('#preview');
  40. if (currentPreview) {
  41. currentPreview.remove();
  42. }
  43. const form = document.querySelector('#post-form');
  44. const textarea = form.querySelector('[name=body]');
  45. const previewContainer = document.createElement('div');
  46. const contentContainer = document.createElement('div');
  47. const content = textarea.value.trim();
  48. previewContainer.id = 'preview';
  49. if (content.length === 0) {
  50. return;
  51. }
  52. const closeButton = createCloseButton();
  53. fetch('/preview', {
  54. method: 'POST',
  55. body: content,
  56. }).then((r) =>
  57. r.json().then((r) => {
  58. contentContainer.innerHTML = r.body;
  59. previewContainer.append(closeButton);
  60. previewContainer.append(contentContainer);
  61. form.append(previewContainer);
  62. }),
  63. );
  64. });
  65. }
  66. document.querySelectorAll('[data-editor-helper]').forEach((helperBtn) => {
  67. helperBtn.addEventListener('click', (e) => {
  68. e.preventDefault();
  69. const textarea = document.querySelector('[name=body]');
  70. const currentValue = textarea.value;
  71. const start = textarea.selectionStart;
  72. const end = textarea.selectionEnd;
  73. const leftChunk = currentValue.substring(0, start);
  74. const rightChunk = currentValue.substring(end, textarea.value.length);
  75. let insertion = '';
  76. let startOffset = 0;
  77. let endOffset = 0;
  78. switch (e.target.dataset.editorHelper) {
  79. case 'bold':
  80. insertion = '****';
  81. startOffset = 2;
  82. endOffset = 2;
  83. break;
  84. case 'italic':
  85. insertion = '__';
  86. startOffset = 1;
  87. endOffset = 1;
  88. break;
  89. case 'link':
  90. insertion = '[text](url)';
  91. startOffset = 1;
  92. endOffset = 5;
  93. break;
  94. case 'code':
  95. insertion = '```js\n```'
  96. startOffset = 3;
  97. endOffset = 5;
  98. }
  99. textarea.value = leftChunk + insertion + rightChunk;
  100. textarea.focus();
  101. textarea.selectionStart = start + startOffset;
  102. textarea.selectionEnd = end + endOffset;
  103. })
  104. });