45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
|
import Editor from "../ace.mjs";
|
||
|
import { check_compose } from "/static/js/compose.mjs";
|
||
|
|
||
|
/**
|
||
|
* Makes the form require the stack to be checked before submitting
|
||
|
* @param {HTMLFormElement} form
|
||
|
* @param {Editor} editor
|
||
|
*/
|
||
|
export function add_check(form, editor) {
|
||
|
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
|
||
|
* @param {Editor} editor Editor instance
|
||
|
* @returns true if the stack is valid, false otherwise
|
||
|
*/
|
||
|
export async function check_stack(editor) {
|
||
|
const source = editor.editor.getValue();
|
||
|
const check_result = await check_compose(source);
|
||
|
|
||
|
const editorEl = document.querySelector(".ace_editor");
|
||
|
const editorErrorEl = document.querySelector("#editor-form .error");
|
||
|
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;
|
||
|
}
|