Code
·
45 lines
·
1573 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45// Simple Markdown toolbar voor issue editor
(function () {
const textareas = document.querySelectorAll('.editor-textarea');
textareas.forEach(function (textarea) {
const toolbar = document.createElement('div');
toolbar.className = 'editor-toolbar';
const buttons = [
{ label: 'B', title: 'Vet', before: '**', after: '**' },
{ label: 'I', title: 'Cursief', before: '*', after: '*' },
{ label: '`', title: 'Code', before: '`', after: '`' },
{ label: '```', title: 'Codeblok', before: '```\n', after: '\n```' },
{ label: 'H2', title: 'Kop 2', before: '## ', after: '' },
{ label: '> ', title: 'Citaat', before: '> ', after: '' },
];
buttons.forEach(function (def) {
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'btn btn-sm btn-secondary editor-btn';
btn.textContent = def.label;
btn.title = def.title;
btn.addEventListener('click', function () {
const start = textarea.selectionStart;
const end = textarea.selectionEnd;
const selected = textarea.value.substring(start, end);
const replacement = def.before + selected + def.after;
textarea.focus();
document.execCommand('insertText', false, replacement);
if (selected.length === 0) {
const cursorPos = start + def.before.length;
textarea.setSelectionRange(cursorPos, cursorPos);
}
});
toolbar.appendChild(btn);
});
textarea.parentNode.insertBefore(toolbar, textarea);
});
})();