diff --git a/Justfile b/Justfile index 69dfd85..90d7955 100644 --- a/Justfile +++ b/Justfile @@ -7,12 +7,17 @@ htmx-sha256sum := "491955cd1810747d7d7b9ccb936400afb760e06d25d53e4572b64b6563b27 htmx-file := "htmx.min.js" htmx-url := "https://github.com/bigskysoftware/htmx/releases/download/v" + htmx-ver / htmx-file +van-ver := "1.5.2" +van-sha256sum := "cf0027d9061fb10dc3c7055e0d8e39f3f75a30ab0cf00c3f65933d3f7e3b08e6" +van-file := "van.min.js" +van-url := "https://vanjs.org/code/" + "van-" + van-ver + ".min.js" + [doc("Run the server")] -run: get-htmx +run: get-htmx get-van go run ./http-serve.go [doc("Build a static binary with content bundled")] -build: get-htmx +build: get-htmx get-van go build -o build/htmx-server ./http-serve.go # TODO: Maybe use shasum instead for FreeBSD support @@ -21,12 +26,17 @@ get-htmx: @test -f htmx.min.js || echo "Getting HTMX..." && curl --silent -O -lSSL {{htmx-url}} @sha256sum -c <<< "{{htmx-sha256sum}} {{htmx-file}}" || (echo "Checksum failed, check Justfile" && exit 1) +get-van: + @test -f {{van-file}} || echo "Getting Van..." && curl --silent -o {{van-file}} -lSSL {{van-url}} + @sha256sum -c <<< "{{van-sha256sum}} {{van-file}}" || (echo "Checksum failed, check Justfile" && exit 1) + [doc("Cleanup source tree")] [confirm("Are you sure? [y/N]:")] clean: clean-container rm -rf .go rm -rf build rm -f htmx.min.js + rm -f van-*.min.js # Build the container build-container: get-htmx diff --git a/http-serve.go b/http-serve.go index 0683088..fd7bdbe 100644 --- a/http-serve.go +++ b/http-serve.go @@ -21,6 +21,9 @@ var style string //go:embed htmx.min.js var htmx string +//go:embed van.min.js +var van string + func main() { // Parse the template from the string literal t := template.Must(template.New("webpage").Parse(tmpl)) @@ -50,6 +53,11 @@ func main() { w.Write([]byte(htmx)) }) + http.HandleFunc("/van.min.js", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/javascript") + w.Write([]byte(van)) + }) + // Start the server on port 8080 println("Listening on http://localhost:8080") if err := http.ListenAndServe(":8080", nil); err != nil { diff --git a/van.min.js b/van.min.js new file mode 100644 index 0000000..9780bbf --- /dev/null +++ b/van.min.js @@ -0,0 +1 @@ +let e,t,r,o,l,n,s=Object.getPrototypeOf,f={isConnected:1},i={},h=s(f),a=s(s),d=(e,t,r,o)=>(e??(setTimeout(r,o),new Set)).add(t),u=(e,t,o)=>{let l=r;r=t;try{return e(o)}catch(e){return console.error(e),o}finally{r=l}},w=e=>e.filter(e=>e.t?.isConnected),_=e=>l=d(l,e,()=>{for(let e of l)e.o=w(e.o),e.l=w(e.l);l=n},1e3),c={get val(){return r?.i?.add(this),this.rawVal},get oldVal(){return r?.i?.add(this),this.h},set val(o){r?.u?.add(this),o!==this.rawVal&&(this.rawVal=o,this.o.length+this.l.length?(t?.add(this),e=d(e,this,v)):this.h=o)}},S=e=>({__proto__:c,rawVal:e,h:e,o:[],l:[]}),g=(e,t)=>{let r={i:new Set,u:new Set},l={f:e},n=o;o=[];let s=u(e,r,t);s=(s??document).nodeType?s:new Text(s);for(let e of r.i)r.u.has(e)||(_(e),e.o.push(l));for(let e of o)e.t=s;return o=n,l.t=s},y=(e,t=S(),r)=>{let l={i:new Set,u:new Set},n={f:e,s:t};n.t=r??o?.push(n)??f,t.val=u(e,l,t.rawVal);for(let e of l.i)l.u.has(e)||(_(e),e.l.push(n));return t},b=(e,...t)=>{for(let r of t.flat(1/0)){let t=s(r??0),o=t===c?g(()=>r.val):t===a?g(r):r;o!=n&&e.append(o)}return e},m=(e,t,...r)=>{let[o,...l]=s(r[0]??0)===h?r:[{},...r],f=e?document.createElementNS(e,t):document.createElement(t);for(let[e,r]of Object.entries(o)){let o=t=>t?Object.getOwnPropertyDescriptor(t,e)??o(s(t)):n,l=t+","+e,h=i[l]??=o(s(f))?.set??0,d=e.startsWith("on")?(t,r)=>{let o=e.slice(2);f.removeEventListener(o,r),f.addEventListener(o,t)}:h?h.bind(f):f.setAttribute.bind(f,e),u=s(r??0);e.startsWith("on")||u===a&&(r=y(r),u=c),u===c?g(()=>(d(r.val,r.h),f)):d(r)}return b(f,l)},x=e=>({get:(t,r)=>m.bind(n,e,r)}),j=(e,t)=>t?t!==e&&e.replaceWith(t):e.remove(),v=()=>{let r=0,o=[...e].filter(e=>e.rawVal!==e.h);do{t=new Set;for(let e of new Set(o.flatMap(e=>e.l=w(e.l))))y(e.f,e.s,e.t),e.t=n}while(++r<100&&(o=[...t]).length);let l=[...e].filter(e=>e.rawVal!==e.h);e=n;for(let e of new Set(l.flatMap(e=>e.o=w(e.o))))j(e.t,g(e.f,e.t)),e.t=n;for(let e of l)e.h=e.rawVal};export default{tags:new Proxy(e=>new Proxy(m,x(e)),x()),hydrate:(e,t)=>j(e,g(t,e)),add:b,state:S,derive:y}; \ No newline at end of file diff --git a/views/index.html b/views/index.html index 0ddd73e..bd0b0ce 100644 --- a/views/index.html +++ b/views/index.html @@ -5,6 +5,7 @@ +