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!
This commit is contained in:
Tobias Kunze 2019-10-14 05:46:12 +02:00 committed by Simon Willison
commit af2e6a5cf1
6 changed files with 50 additions and 9 deletions

View file

@ -166,22 +166,32 @@ form input[type=search] {
width: 95%; width: 95%;
} }
} }
form input[type=submit] { form input[type=submit], form button[type=button] {
color: #fff;
background-color: #007bff;
border-color: #007bff;
font-weight: 400; font-weight: 400;
cursor: pointer; cursor: pointer;
text-align: center; text-align: center;
vertical-align: middle; vertical-align: middle;
border: 1px solid blue; border-width: 1px;
border-style: solid;
padding: .5em 0.8em; padding: .5em 0.8em;
font-size: 0.9rem; font-size: 0.9rem;
line-height: 1; line-height: 1;
border-radius: .25rem; border-radius: .25rem;
}
form input[type=submit] {
color: #fff;
background-color: #007bff;
border-color: #007bff;
-webkit-appearance: button; -webkit-appearance: button;
} }
form button[type=button] {
color: #007bff;
background-color: #fff;
border-color: #007bff;
}
.filter-row { .filter-row {
margin-bottom: 0.6em; margin-bottom: 0.6em;
} }

File diff suppressed because one or more lines are too long

View file

@ -1,3 +1,4 @@
<script src="/-/static/sql-formatter-2.3.3.min.js" defer></script>
<script src="/-/static/codemirror-5.31.0.js"></script> <script src="/-/static/codemirror-5.31.0.js"></script>
<link rel="stylesheet" href="/-/static/codemirror-5.31.0-min.css" /> <link rel="stylesheet" href="/-/static/codemirror-5.31.0-min.css" />
<script src="/-/static/codemirror-5.31.0-sql.min.js"></script> <script src="/-/static/codemirror-5.31.0-sql.min.js"></script>

View file

@ -1,5 +1,18 @@
<script> <script>
var editor = CodeMirror.fromTextArea(document.getElementById("sql-editor"), { 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, lineNumbers: true,
mode: "text/x-sql", mode: "text/x-sql",
lineWrapping: true, lineWrapping: true,
@ -10,4 +23,10 @@
}, },
Tab: false Tab: false
}); });
if (sqlInput && sqlFormat) {
sqlFormat.addEventListener("click", ev => {
editor.setValue(sqlFormatter.format(editor.getValue()));
})
}
}
</script> </script>

View file

@ -26,7 +26,10 @@
<form class="sql" action="{{ database_url(database) }}" method="get"> <form class="sql" action="{{ database_url(database) }}" method="get">
<h3>Custom SQL query</h3> <h3>Custom SQL query</h3>
<p><textarea id="sql-editor" name="sql">{% if tables %}select * from {{ tables[0].name|escape_sqlite }}{% else %}select sqlite_version(){% endif %}</textarea></p> <p><textarea id="sql-editor" name="sql">{% if tables %}select * from {{ tables[0].name|escape_sqlite }}{% else %}select sqlite_version(){% endif %}</textarea></p>
<p><input type="submit" value="Run SQL"></p> <p>
<button id="sql-format" type="button" hidden>Format SQL</button>
<input type="submit" value="Run SQL">
</p>
</form> </form>
{% endif %} {% endif %}

View file

@ -37,7 +37,7 @@
{% if editable and config.allow_sql %} {% if editable and config.allow_sql %}
<p><textarea id="sql-editor" name="sql">{% if query and query.sql %}{{ query.sql }}{% else %}select * from {{ tables[0].name|escape_sqlite }}{% endif %}</textarea></p> <p><textarea id="sql-editor" name="sql">{% if query and query.sql %}{{ query.sql }}{% else %}select * from {{ tables[0].name|escape_sqlite }}{% endif %}</textarea></p>
{% else %} {% else %}
<pre>{% if query %}{{ query.sql }}{% endif %}</pre> <pre id="sql-query">{% if query %}{{ query.sql }}{% endif %}</pre>
{% endif %} {% endif %}
{% else %} {% else %}
<input type="hidden" name="sql" value="{% if query and query.sql %}{{ query.sql }}{% else %}select * from {{ tables[0].name|escape_sqlite }}{% endif %}"> <input type="hidden" name="sql" value="{% if query and query.sql %}{{ query.sql }}{% else %}select * from {{ tables[0].name|escape_sqlite }}{% endif %}">
@ -49,7 +49,10 @@
<p><label for="qp{{ loop.index }}">{{ name }}</label> <input type="text" id="qp{{ loop.index }}" name="{{ name }}" value="{{ value }}"></p> <p><label for="qp{{ loop.index }}">{{ name }}</label> <input type="text" id="qp{{ loop.index }}" name="{{ name }}" value="{{ value }}"></p>
{% endfor %} {% endfor %}
{% endif %} {% endif %}
<p><input type="submit" value="Run SQL"></p> <p>
<button id="sql-format" type="button" hidden>Format SQL</button>
<input type="submit" value="Run SQL">
</p>
</form> </form>
{% if display_rows %} {% if display_rows %}