mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Offer to format readonly SQL (#602)
Following discussion in #601, this PR adds a "Format SQL" button to read-only SQL (if the SQL actually differs from the formatting result). It also removes a console error on readonly SQL queries. Thanks, @rixx!
This commit is contained in:
parent
5eaf398592
commit
fa4d77b01e
1 changed files with 26 additions and 15 deletions
|
|
@ -6,21 +6,32 @@ window.onload = () => {
|
||||||
if (sqlFormat && !readOnly) {
|
if (sqlFormat && !readOnly) {
|
||||||
sqlFormat.hidden = false;
|
sqlFormat.hidden = false;
|
||||||
}
|
}
|
||||||
var editor = CodeMirror.fromTextArea(sqlInput, {
|
if (sqlInput) {
|
||||||
lineNumbers: true,
|
var editor = CodeMirror.fromTextArea(sqlInput, {
|
||||||
mode: "text/x-sql",
|
lineNumbers: true,
|
||||||
lineWrapping: true,
|
mode: "text/x-sql",
|
||||||
});
|
lineWrapping: true,
|
||||||
editor.setOption("extraKeys", {
|
});
|
||||||
"Shift-Enter": function() {
|
editor.setOption("extraKeys", {
|
||||||
document.getElementsByClassName("sql")[0].submit();
|
"Shift-Enter": function() {
|
||||||
},
|
document.getElementsByClassName("sql")[0].submit();
|
||||||
Tab: false
|
},
|
||||||
});
|
Tab: false
|
||||||
if (sqlInput && sqlFormat) {
|
});
|
||||||
sqlFormat.addEventListener("click", ev => {
|
if (sqlFormat) {
|
||||||
editor.setValue(sqlFormatter.format(editor.getValue()));
|
sqlFormat.addEventListener("click", ev => {
|
||||||
})
|
editor.setValue(sqlFormatter.format(editor.getValue()));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sqlFormat && readOnly) {
|
||||||
|
const formatted = sqlFormatter.format(readOnly.innerHTML);
|
||||||
|
if (formatted != readOnly.innerHTML) {
|
||||||
|
sqlFormat.hidden = false;
|
||||||
|
sqlFormat.addEventListener("click", ev => {
|
||||||
|
readOnly.innerHTML = formatted;
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue