blob: f3f4f71e5916404921e3648972ef8f0104124a3e (
plain) (
blame)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
const convertButton = document.getElementById("convert-button");
const uploadButton = document.getElementById("upload-button");
const markdownInput = document.getElementById("markdown");
function sourceForm(event) {
const sourceElement = event.detail?.elt;
if (!sourceElement) {
return null;
}
return sourceElement.closest("form");
}
document.body.addEventListener("htmx:beforeRequest", (event) => {
const form = sourceForm(event);
if (form?.id === "convert-form" && convertButton) {
convertButton.disabled = true;
convertButton.textContent = "generating...";
}
if (form?.id === "upload-form" && uploadButton) {
uploadButton.disabled = true;
uploadButton.textContent = "uploading...";
}
});
document.body.addEventListener("htmx:afterRequest", (event) => {
const form = sourceForm(event);
if (form?.id === "convert-form" && convertButton) {
convertButton.disabled = false;
convertButton.textContent = "generate pdf";
}
if (form?.id === "upload-form" && uploadButton) {
uploadButton.disabled = false;
uploadButton.textContent = "upload image";
}
});
document.body.addEventListener("click", (event) => {
const target = event.target;
if (!(target instanceof HTMLElement)) {
return;
}
const button = target.closest("[data-insert-markdown]");
if (!(button instanceof HTMLElement) || !markdownInput) {
return;
}
const snippet = button.dataset.insertMarkdown;
if (!snippet) {
return;
}
const needsLeadingNewline = markdownInput.value && !markdownInput.value.endsWith("\n");
const prefix = needsLeadingNewline ? "\n" : "";
markdownInput.value += `${prefix}${snippet}\n`;
markdownInput.focus();
});
|