Browse Source

Add markdown helper buttons

master
Dylan Baker 3 years ago
parent
commit
e26f198a42
3 changed files with 61 additions and 0 deletions
  1. 46
    0
      static/script.js
  2. 9
    0
      static/style.css
  3. 6
    0
      templates/components.html

+ 46
- 0
static/script.js View File

@@ -73,3 +73,49 @@ if (previewButton) {
73 73
     );
74 74
   });
75 75
 }
76
+
77
+document.querySelectorAll('[data-editor-helper]').forEach((helperBtn) => {
78
+  helperBtn.addEventListener('click', (e) => {
79
+    e.preventDefault();
80
+
81
+    const textarea = document.querySelector('[name=body]');
82
+    const currentValue = textarea.value;
83
+
84
+    const start = textarea.selectionStart;
85
+    const end = textarea.selectionEnd;
86
+
87
+    const leftChunk = currentValue.substring(0, start);
88
+    const rightChunk = currentValue.substring(end, textarea.value.length);
89
+
90
+    let insertion = '';
91
+    let startOffset = 0;
92
+    let endOffset = 0;
93
+    switch (e.target.dataset.editorHelper) {
94
+      case 'bold':
95
+        insertion = '****';
96
+        startOffset = 2;
97
+        endOffset = 2;
98
+        break;
99
+      case 'italic':
100
+        insertion = '__';
101
+        startOffset = 1;
102
+        endOffset = 1;
103
+        break;
104
+      case 'link':
105
+        insertion = '[text](url)';
106
+        startOffset = 1;
107
+        endOffset = 5;
108
+        break;
109
+      case 'code':
110
+        insertion = '```js\n```'
111
+        startOffset = 3;
112
+        endOffset = 5;
113
+    }
114
+
115
+    textarea.value = leftChunk + insertion + rightChunk;
116
+
117
+    textarea.focus();
118
+    textarea.selectionStart = start + startOffset;
119
+    textarea.selectionEnd = end + endOffset;
120
+  })
121
+});

+ 9
- 0
static/style.css View File

@@ -171,6 +171,7 @@ a:hover {
171 171
 .post__body pre {
172 172
   background: var(--off-white);
173 173
   border: 1px solid var(--dark-accent);
174
+  margin: 1em 0;
174 175
   overflow: scroll;
175 176
   padding: 0.5em;
176 177
 }
@@ -211,6 +212,14 @@ a:hover {
211 212
   margin: 1em 0;
212 213
 }
213 214
 
215
+.editor-helpers {
216
+  margin-bottom: 0.5em;
217
+}
218
+
219
+.editor-helpers .btn {
220
+  font-size: 14px;
221
+}
222
+
214 223
 @media (max-width: 500px) {
215 224
   .form__button {
216 225
     width: 100%;

+ 6
- 0
templates/components.html View File

@@ -49,6 +49,12 @@
49 49
     </div>
50 50
     <div class="form__field">
51 51
       <label for="body" class="form__label">Body</label>
52
+      <div class="editor-helpers">
53
+        <button class="btn" data-editor-helper="bold">bold</button>
54
+        <button class="btn" data-editor-helper="italic">italic</button>
55
+        <button class="btn" data-editor-helper="link">link</button>
56
+        <button class="btn" data-editor-helper="code">code</button>
57
+      </div>
52 58
       <textarea
53 59
         class="form__textarea"
54 60
         name="body"

Loading…
Cancel
Save