htmgo/htmgo-site/partials/html-to-go.go

61 lines
1.3 KiB
Go
Raw Normal View History

2024-10-11 16:19:32 +00:00
package partials
import (
"github.com/maddalax/htmgo/framework/h"
"github.com/maddalax/htmgo/tools/html-to-htmgo/htmltogo"
2024-10-13 11:35:27 +00:00
"htmgo-site/ui"
2024-10-11 16:19:32 +00:00
)
func ConvertHtmlToGo(ctx *h.RequestContext) *h.Partial {
value := ctx.FormValue("html-input")
2024-10-28 23:47:00 +00:00
parsed := string(htmltogo.Parse([]byte(value)))
2024-10-11 16:19:32 +00:00
2024-10-28 23:47:00 +00:00
formatted := ui.FormatCode(parsed, "height: 100%;")
2024-10-11 16:19:32 +00:00
2024-10-28 23:47:00 +00:00
return h.SwapManyPartial(ctx,
GoOutput(formatted),
HiddenCopyOutput(parsed),
)
2024-10-11 16:19:32 +00:00
}
func HtmlInput() *h.Element {
return h.Div(
2024-10-23 15:34:59 +00:00
h.Class("h-full w-1/2 min-w-1/2"),
2024-10-11 16:19:32 +00:00
h.TextArea(
h.Name("html-input"),
2024-10-13 11:35:27 +00:00
h.MaxLength(500*1000),
2024-10-11 16:19:32 +00:00
h.PostPartial(ConvertHtmlToGo, "keyup delay:300ms"),
h.Class("h-[90%] w-full p-4 rounded border border-slate-200"),
h.Placeholder("Paste your HTML here"),
h.Rows(10),
),
)
}
2024-10-28 23:47:00 +00:00
func HiddenCopyOutput(content string) *h.Element {
return h.Div(
h.Class("hidden"),
h.Id("go-output-raw"),
h.UnsafeRaw(content),
)
}
2024-10-11 16:19:32 +00:00
func GoOutput(content string) *h.Element {
return h.Div(
2024-10-23 15:34:59 +00:00
h.Class("h-full w-1/2 min-w-1/2"),
2024-10-11 16:19:32 +00:00
h.Id("go-output"),
h.Div(
2024-10-25 13:12:11 +00:00
h.Class("h-[90%] w-full rounded border border-slate-200 relative"),
h.Div(
h.Class("h-full"),
h.Id("go-output-content"),
h.UnsafeRaw(content),
),
h.If(
content != "",
2024-10-28 23:47:00 +00:00
ui.CopyButton("#go-output-raw"),
),
2024-10-11 16:19:32 +00:00
),
)
}