-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
789 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/euforic/exp | ||
|
||
go 1.21.1 | ||
|
||
require github.com/davecgh/go-spew v1.1.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Go Template Renderer</title> | ||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.63.3/codemirror.min.css"> | ||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.63.3/theme/material-darker.min.css"> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.63.3/codemirror.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.63.3/mode/go/go.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.63.3/mode/javascript/javascript.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.63.3/keymap/vim.js"></script> | ||
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet"> | ||
<style> | ||
#output>.CodeMirror { | ||
height: 80vh; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body class="font-sans bg-gray-100 text-gray-800 p-5 h-screen overflow-hidden"> | ||
<div class="flex h-full"> | ||
<div class="w-1/2 h-screen p-2"> | ||
<div class="p-4 mb-4"> | ||
<h2 class="font-bold mb-2">Go Template</h2> | ||
<div id="template" class="h-64"></div> | ||
</div> | ||
<div class="p-4"> | ||
<h2 class="font-bold mb-2">JSON Data</h2> | ||
<div id="json" class="h-64"></div> | ||
</div> | ||
<div class="flex mt-10 mb-10 ml-5"> | ||
<input type="checkbox" id="vim-toggle" class="form-checkbox mr-2"> | ||
<label for="vim-toggle" class="select-none cursor-pointer">Enable Vim Keybindings</label> | ||
</div> | ||
|
||
</div> | ||
|
||
<div class="w-1/2 h-screen p-2"> | ||
<div class="p-4 h-screen"> | ||
<h2 class="font-bold mb-2">Output</h2> | ||
<div id="output" class="h-screen pb-4"></div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<script src="wasm_exec.js"></script> | ||
<script type="text/javascript">function fetchAndInstantiate(url, importObject) { | ||
return fetch(url).then(response => response.arrayBuffer()).then(bytes => WebAssembly.instantiate(bytes, importObject)).then(results => results.instance); | ||
} | ||
|
||
var go = new Go(); | ||
var mod = fetchAndInstantiate("/main.wasm ", go.importObject); | ||
|
||
window.onload = function () { | ||
mod.then(function (instance) { | ||
go.run(instance); | ||
}); | ||
} | ||
</script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
//go:build js && wasm | ||
// +build js,wasm | ||
|
||
//go:generate cp $GOROOT/misc/wasm/wasm_exec.js . | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"syscall/js" | ||
"text/template" | ||
) | ||
|
||
type App struct { | ||
templateEditor js.Value | ||
jsonEditor js.Value | ||
outputEditor js.Value | ||
} | ||
|
||
func main() { | ||
app := &App{} | ||
app.init() | ||
|
||
// Block forever to keep the Go program running | ||
select {} | ||
} | ||
|
||
func (app *App) init() { | ||
app.initExports() | ||
app.initCodeMirror() | ||
app.initVimToggle() | ||
app.initRender() | ||
} | ||
|
||
func (app *App) initExports() { | ||
js.Global().Set("renderTemplate", js.FuncOf(app.renderTemplate)) | ||
} | ||
|
||
func (app *App) initCodeMirror() { | ||
cm := js.Global().Get("CodeMirror") | ||
app.templateEditor = cm.New(js.Global().Get("document").Call("getElementById", "template"), js.ValueOf(map[string]interface{}{ | ||
"mode": "go", | ||
"theme": "material-darker", | ||
"lineNumbers": true, | ||
"value": "Hello, {{.Name}}", | ||
})) | ||
|
||
app.jsonEditor = cm.New(js.Global().Get("document").Call("getElementById", "json"), js.ValueOf(map[string]interface{}{ | ||
"mode": "javascript", | ||
"theme": "material-darker", | ||
"lineNumbers": true, | ||
"value": `{ | ||
"Name": "World" | ||
}`, | ||
})) | ||
|
||
app.outputEditor = cm.New(js.Global().Get("document").Call("getElementById", "output"), js.ValueOf(map[string]interface{}{ | ||
"lineNumbers": true, | ||
"readOnly": true, | ||
"theme": "material-darker", | ||
"value": "Hello, World", | ||
})) | ||
} | ||
|
||
func (app *App) initVimToggle() { | ||
vimToggle := js.Global().Get("document").Call("getElementById", "vim-toggle") | ||
isVimEnabled := js.Global().Get("localStorage").Call("getItem", "vimEnabled").String() == "true" | ||
app.setKeyMap(isVimEnabled) | ||
vimToggle.Set("checked", isVimEnabled) | ||
vimToggle.Call("addEventListener", "change", js.FuncOf(func(this js.Value, args []js.Value) interface{} { | ||
app.setKeyMap(vimToggle.Get("checked").Bool()) | ||
js.Global().Get("localStorage").Call("setItem", "vimEnabled", vimToggle.Get("checked").Bool()) | ||
return nil | ||
})) | ||
} | ||
|
||
func (app *App) setKeyMap(vimEnabled bool) { | ||
keyMap := "default" | ||
if vimEnabled { | ||
keyMap = "vim" | ||
} | ||
app.templateEditor.Call("setOption", "keyMap", keyMap) | ||
app.jsonEditor.Call("setOption", "keyMap", keyMap) | ||
app.outputEditor.Call("setOption", "keyMap", keyMap) | ||
} | ||
|
||
func (app *App) initRender() { | ||
saveFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} { | ||
app.saveCurrentData() | ||
return nil | ||
}) | ||
renderFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} { | ||
go app.render() | ||
return nil | ||
}) | ||
app.templateEditor.Call("on", "change", saveFunc) | ||
app.jsonEditor.Call("on", "change", saveFunc) | ||
app.templateEditor.Call("on", "change", renderFunc) | ||
app.jsonEditor.Call("on", "change", renderFunc) | ||
app.loadPersistedData() | ||
} | ||
|
||
func (app *App) render() { | ||
template := app.templateEditor.Call("getValue").String() | ||
json := app.jsonEditor.Call("getValue").String() | ||
result := app.renderTemplate(js.Null(), []js.Value{js.ValueOf(template), js.ValueOf(json)}).(string) | ||
app.outputEditor.Call("setValue", result) | ||
} | ||
|
||
func (app *App) renderTemplate(this js.Value, args []js.Value) interface{} { | ||
tmplStr := args[0].String() | ||
jsonStr := args[1].String() | ||
|
||
tmpl, err := template.New("tmpl").Parse(tmplStr) | ||
if err != nil { | ||
return fmt.Sprintf("Template error: %v", err) | ||
} | ||
|
||
var data interface{} | ||
err = json.Unmarshal([]byte(jsonStr), &data) | ||
if err != nil { | ||
return fmt.Sprintf("JSON error: %v", err) | ||
} | ||
|
||
var buf bytes.Buffer | ||
err = tmpl.Execute(&buf, data) | ||
if err != nil { | ||
return fmt.Sprintf("Execution error: %v", err) | ||
} | ||
|
||
return buf.String() | ||
} | ||
|
||
func (app *App) saveCurrentData() { | ||
js.Global().Get("localStorage").Call("setItem", "templateData", app.templateEditor.Call("getValue").String()) | ||
js.Global().Get("localStorage").Call("setItem", "jsonData", app.jsonEditor.Call("getValue").String()) | ||
} | ||
|
||
func (app *App) loadPersistedData() { | ||
persistedTemplateData := js.Global().Get("localStorage").Call("getItem", "templateData").String() | ||
persistedJsonData := js.Global().Get("localStorage").Call("getItem", "jsonData").String() | ||
|
||
if persistedTemplateData != "<null>" { | ||
app.templateEditor.Call("setValue", persistedTemplateData) | ||
} | ||
|
||
if persistedJsonData != "<null>" { | ||
app.jsonEditor.Call("setValue", persistedJsonData) | ||
} | ||
|
||
if persistedTemplateData != "<null>" || persistedJsonData != "<null>" { | ||
app.render() | ||
} | ||
} |
Binary file not shown.
Oops, something went wrong.