Skip to content

Commit

Permalink
sorting fields; fixing http
Browse files Browse the repository at this point in the history
  • Loading branch information
cneill committed Sep 17, 2023
1 parent 130ad1f commit c5cfeb9
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 37 deletions.
4 changes: 2 additions & 2 deletions cmd/jsonstruct/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func GenerateHandler(w http.ResponseWriter, req *http.Request) {
}

formatter, err := jsonstruct.NewFormatter(&jsonstruct.FormatterOptions{
// SortFields: ctx.Bool("sort-fields"),
// ValueComments: ctx.Bool("value-comments"),
SortFields: req.PostForm.Get("sort_fields") == "on",
ValueComments: req.PostForm.Get("value_comments") == "on",
// InlineStructs: ctx.Bool("inline-structs"),
})
if err != nil {
Expand Down
56 changes: 40 additions & 16 deletions cmd/jsonstruct/http/static/index.css
Original file line number Diff line number Diff line change
@@ -1,31 +1,60 @@
.body {
background-color: #000000;
color: #ffffff;
padding: 0;
margin: 0;
}

.form {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}

.container {
display: grid;
grid-template-rows: 80% 20%;
grid-template-columns: 50% 50%;
height: 100vh;
width: 100vw;
padding: 0;
margin: 0;
}

.input-container {
grid-row: 1/2;
grid-column: 1/2;
}

.output-container {
grid-row: 1/2;
grid-column: 2/3;
}

.options-container {
padding-top: 10px;
grid-row: 2/3;
grid-column: 1/3;
}

.input, .output {
height: 100%;
width: 100%;
min-height: 100px;
max-height: 500px;
background-color: #000000;
color: #ffffff;
font-size: 1.5em;
/*font-size: 1.5em;*/
font-family: Lucida Console, monospace;
border-color: #333333;
}

.button {
font-size: 20pt;
width: 100%;
/*width: 100%;*/
border-width: 3px;
border-style: solid;
border-radius: 3px;
font-weight: bold;
margin-left: 0;
margin-right: 0;
margin-top: 10px;
margin-bottom: 10px;
padding: 10px;
cursor: pointer;
}

Expand All @@ -41,11 +70,6 @@
color: #ffffff;
}

.output.htmx-added {
opacity: 0;
}

.output {
opacity: 1;
transition: opacity 200ms ease-out;
.htmx-indicator {
display: none;
}
21 changes: 20 additions & 1 deletion cmd/jsonstruct/http/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ function clipboardCopy() {
var output = document.querySelector("#output");
output.select();
output.setSelectionRange(0, 9999999);
console.log(output.value);
navigator.clipboard.writeText(output.value);
}

Expand All @@ -14,3 +13,23 @@ window.onload = function() {
clipboardCopy();
});
}

document.body.addEventListener("htmx:beforeSwap", e => {
if (e.detail.xhr.status >= 400) {
e.detail.shouldSwap = false;
e.detail.isError = true;
}
});

document.body.addEventListener("htmx:beforeRequest", e => {
let inputElem = e.detail.elt.querySelector(".input");
let inputText = inputElem.value;

try {
JSON.parse(inputText);
} catch (err) {
if (inputText != "") {
e.preventDefault();
}
}
});
51 changes: 35 additions & 16 deletions cmd/jsonstruct/http/templates/templates.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,48 @@
<meta charset="UTF-8">
<title>Roam Quote Prettifier</title>
<link rel="stylesheet" href="/static/index.css" type="text/css"></link>

<script src="/static/htmx.min.js"></script>
<script src="/static/index.js"></script>
</head>
<body class="body">
<form hx-indicator="#indicator">
<textarea class="input" id="input" name="input" placeholder="Enter your JSON"
hx-post="/generate"
hx-swap="outerHTML settle:400ms"
hx-target="#output"
hx-trigger="keyup changed dely:500ms"></textarea>
<br />
<form class="form" hx-post="/generate"
hx-swap="innerHTML"
hx-target="#output-container"
hx-trigger="keyup changed delay:50ms from:.input"
focus-scroll:true>
<div class="container">
<div class="input-container">
<textarea class="input" id="input" name="input" placeholder="Enter your JSON"></textarea>
</div>
<div class="output-container" id="output-container">
<img class="htmx-indicator" id="indicator" src="/static/loading.webp" />
<textarea class="output" id="output" readonly></textarea>
</div>
<div class="options-container"
hx-post="/generate"
hx-swap="innerHTML"
hx-target="#output-container"
hx-trigger="change">
<fieldset name="options">
<legend>Options</legend>
<label for="value_comments">Include value comments</label>
<input type="checkbox" name="value_comments">
<label for="sort_fields">Sort fields</label>
<input type="checkbox" name="sort_fields">
</fieldset>
<br />
<button type="button" id="copy" class="button button--green">
Copy to clipboard
</button>
</div>
</div>
</form>
<br />
<textarea class="output" id="output"></textarea>
<button id="copy" class="button button--green">
Copy to clipboard
</button>
<img id="indicator" src="/static/loading.webp" class="htmx-indicator">

<script src="/static/htmx.min.js"></script>
<script src="/static/index.js"></script>
</body>
</html>
{{- end }}

{{- define "generate" }}
<img class="htmx-indicator" id="indicator" src="/static/loading.webp" />
<textarea id="output" class="output" readonly>{{ .Generated }}</textarea>
{{- end }}
8 changes: 6 additions & 2 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ func (f *Formatter) FormatStructs(inputs ...*JSONStruct) (string, error) {
preamble := "package temp\n"
structStr := preamble

for i, input := range inputs {
for inputNum, input := range inputs {
if f.SortFields {
input.fields.SortAlphabetically()
}

formatted, err := f.formatStructNesting(0, input)
if err != nil {
return "", fmt.Errorf("failed to format struct %d: %w", i, err)
return "", fmt.Errorf("failed to format struct %d: %w", inputNum, err)
}

structStr += formatted
Expand Down

0 comments on commit c5cfeb9

Please sign in to comment.