2023-11-30 09:14:32 +00:00
|
|
|
import { check_compose } from "/static/scripts/compose.js";
|
2023-11-24 23:45:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes the form require the stack to be checked before submitting
|
2023-11-30 09:14:32 +00:00
|
|
|
* @param form
|
|
|
|
* @param editor
|
2023-11-24 23:45:54 +00:00
|
|
|
*/
|
2023-11-30 09:14:32 +00:00
|
|
|
export function add_check(form: HTMLFormElement, editor: AceAjax.Editor) {
|
2023-11-24 23:45:54 +00:00
|
|
|
form.addEventListener("submit", (ev) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
check_stack(editor).then((result) => {
|
|
|
|
if (result) {
|
|
|
|
form.submit();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the check function and updates some DOM elements
|
2023-11-30 09:14:32 +00:00
|
|
|
* @param editor Editor instance
|
2023-11-24 23:45:54 +00:00
|
|
|
* @returns true if the stack is valid, false otherwise
|
|
|
|
*/
|
2023-11-30 09:14:32 +00:00
|
|
|
export async function check_stack(editor: AceAjax.Editor) {
|
|
|
|
const source = editor.getValue();
|
2023-11-24 23:45:54 +00:00
|
|
|
const check_result = await check_compose(source);
|
|
|
|
|
2023-11-30 09:14:32 +00:00
|
|
|
const editorEl = document.querySelector(".ace_editor")!;
|
|
|
|
const editorErrorEl = document.querySelector<HTMLDivElement>("#editor-form div.error")!;
|
2023-11-24 23:45:54 +00:00
|
|
|
editorEl.classList.remove("err", "checked");
|
|
|
|
editorErrorEl.style.display = "block";
|
|
|
|
editorErrorEl.classList.add("pending");
|
|
|
|
editorErrorEl.innerHTML = "Checking...";
|
|
|
|
if (check_result.ok) {
|
|
|
|
editorEl.classList.add("checked");
|
|
|
|
editorErrorEl.style.display = "";
|
|
|
|
} else {
|
|
|
|
editorEl.classList.add("err");
|
|
|
|
editorErrorEl.classList.remove("pending");
|
|
|
|
editorErrorEl.innerHTML = check_result.error.message;
|
|
|
|
}
|
|
|
|
return check_result.ok;
|
|
|
|
}
|