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:
Tobias Kunze 2019-11-04 03:39:55 +01:00 committed by Simon Willison
commit fa4d77b01e

View file

@ -6,6 +6,7 @@ window.onload = () => {
if (sqlFormat && !readOnly) { if (sqlFormat && !readOnly) {
sqlFormat.hidden = false; sqlFormat.hidden = false;
} }
if (sqlInput) {
var editor = CodeMirror.fromTextArea(sqlInput, { var editor = CodeMirror.fromTextArea(sqlInput, {
lineNumbers: true, lineNumbers: true,
mode: "text/x-sql", mode: "text/x-sql",
@ -17,10 +18,20 @@ window.onload = () => {
}, },
Tab: false Tab: false
}); });
if (sqlInput && sqlFormat) { if (sqlFormat) {
sqlFormat.addEventListener("click", ev => { sqlFormat.addEventListener("click", ev => {
editor.setValue(sqlFormatter.format(editor.getValue())); 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>