Add clickable Markdown checkboxes (#182)

This commit is contained in:
Thomas Miceli
2023-12-26 03:23:47 +01:00
parent 0753c5cb54
commit 85e2da054b
11 changed files with 177 additions and 9 deletions

View File

@ -33,6 +33,42 @@ document.querySelectorAll('.md-code-copy-btn').forEach(button => {
});
});
let checkboxes = document.querySelectorAll('li[data-checkbox-nb] input[type=checkbox]');
document.querySelectorAll<HTMLElement>('li[data-checkbox-nb]').forEach((el) => {
let input = el.firstElementChild;
(input as HTMLButtonElement).disabled = false;
let checkboxNb = (el as HTMLElement).dataset.checkboxNb;
let filename = input.parentElement.parentElement.parentElement.parentElement.parentElement.dataset.file;
input.addEventListener('change', function () {
const data = new URLSearchParams();
data.append('checkbox', checkboxNb);
data.append('file', filename);
if (document.getElementsByName('_csrf').length !== 0) {
data.append('_csrf', ((document.getElementsByName('_csrf')[0] as HTMLInputElement).value));
}
checkboxes.forEach((el: HTMLButtonElement) => {
el.disabled = true;
el.classList.add('text-gray-400')
});
fetch(window.location.href.split('#')[0] + '/checkbox', {
method: 'PUT',
credentials: 'same-origin',
body: data,
}).then((response) => {
if (response.status === 200) {
checkboxes.forEach((el: HTMLButtonElement) => {
el.disabled = false;
el.classList.remove('text-gray-400')
});
}
});
})
})