Merge branch 'code/test' into 'master'

feature: save options in the url

See merge request jiehong/jq_offline!15
This commit is contained in:
Jiehong 2023-11-21 16:05:16 +00:00
commit 9a84d039e9

View File

@ -32,41 +32,36 @@ jq in your browser, without sending anything to any backend.">
async function setup() {
// Populate the form from the url parameters
let params = (new URL(document.location)).searchParams;
let query = params.get('query');
let query = params.get('query');
if (query) {
document.getElementById("filter").value = query;
}
['compact-output', 'sort-keys', 'raw-input', 'raw-output', 'slurp'].forEach(option => {
let p = params.get(option);
if (p === 'true') {
document.getElementById(option).checked = true;
}
});
}
async function jq() {
let data = document.getElementById("input-json").value;
let query = document.getElementById("filter").value;
let compactOutput = document.getElementById("compact-output").checked;
let sortKeys = document.getElementById("sort-keys").checked;
let rawInput = document.getElementById("raw-input").checked;
let rawOutput = document.getElementById("raw-output").checked;
let slurp = document.getElementById("slurp").checked;
let params = (new URL(document.location)).searchParams;
let options = ["--monochrome-output"];
if (compactOutput) {
options.push("--compact-output");
}
if (sortKeys) {
options.push("--sort-keys");
}
if (rawInput) {
options.push("--raw-input");
}
if (rawOutput) {
options.push("--raw-output");
}
if (slurp) {
options.push("--slurp");
}
['compact-output', 'sort-keys', 'raw-input', 'raw-output', 'slurp'].forEach(option => {
let part = document.getElementById(option).checked;
if (part) {
params.set(option, true);
options.push('--' + option);
} else {
params.delete(option);
}
});
// Update url query params with current query
let params = (new URL(document.location)).searchParams;
params.set('query', query);
const url = new URL(document.location);
url.search = params.toString();