datasette/datasette/templates/_codemirror_foot.html
Tobias Kunze af2e6a5cf1 Button to format SQL, closes #136
SQL code will be formatted on page load, and can additionally
be formatted by clicking the "Format SQL" button.

Thanks, @rixx!
2019-10-13 20:46:12 -07:00

32 lines
973 B
HTML

<script>
window.onload = () => {
const sqlFormat = document.querySelector("button#sql-format");
const readOnly = document.querySelector("pre#sql-query");
const sqlInput = document.querySelector("textarea#sql-editor");
if (sqlFormat && !readOnly) {
sqlFormat.hidden = false;
}
if (readOnly) {
readOnly.innerHTML = sqlFormatter.format(readOnly.innerHTML);
}
if (sqlInput) {
sqlInput.value = sqlFormatter.format(sqlInput.value);
}
var editor = CodeMirror.fromTextArea(sqlInput, {
lineNumbers: true,
mode: "text/x-sql",
lineWrapping: true,
});
editor.setOption("extraKeys", {
"Shift-Enter": function() {
document.getElementsByClassName("sql")[0].submit();
},
Tab: false
});
if (sqlInput && sqlFormat) {
sqlFormat.addEventListener("click", ev => {
editor.setValue(sqlFormatter.format(editor.getValue()));
})
}
}
</script>