Skip to content

Commit a9159fd

Browse files
committed
init
1 parent 87d5155 commit a9159fd

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

d2js/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
`GOOS=js GOARCH=wasm go build -ldflags='-s -w' -trimpath -o main.wasm ./d2js`

d2js/js.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//go:build wasm
2+
3+
package main
4+
5+
import (
6+
"encoding/json"
7+
"errors"
8+
"strings"
9+
"syscall/js"
10+
11+
"oss.terrastruct.com/d2/d2compiler"
12+
"oss.terrastruct.com/d2/d2format"
13+
"oss.terrastruct.com/d2/d2parser"
14+
"oss.terrastruct.com/d2/lib/urlenc"
15+
)
16+
17+
func main() {
18+
js.Global().Set("d2Compile", js.FuncOf(jsCompile))
19+
js.Global().Set("d2Encode", js.FuncOf(jsEncode))
20+
js.Global().Set("d2Decode", js.FuncOf(jsDecode))
21+
select {}
22+
}
23+
24+
type jsObject struct {
25+
Result string `json:"result"`
26+
UserError string `json:"userError"`
27+
D2Error string `json:"d2Error"`
28+
}
29+
30+
// TODO error passing
31+
// TODO recover panics
32+
func jsCompile(this js.Value, args []js.Value) interface{} {
33+
script := args[0].String()
34+
35+
g, err := d2compiler.Compile("", strings.NewReader(script), &d2compiler.CompileOptions{
36+
UTF16: true,
37+
})
38+
var pe d2parser.ParseError
39+
if err != nil {
40+
if errors.As(err, &pe) {
41+
serialized, _ := json.Marshal(err)
42+
ret := jsObject{UserError: string(serialized)}
43+
str, _ := json.Marshal(ret)
44+
return string(str)
45+
}
46+
ret := jsObject{D2Error: err.Error()}
47+
str, _ := json.Marshal(ret)
48+
return string(str)
49+
}
50+
51+
newScript := d2format.Format(g.AST)
52+
if script != newScript {
53+
ret := jsObject{Result: newScript}
54+
str, _ := json.Marshal(ret)
55+
return string(str)
56+
}
57+
58+
return nil
59+
}
60+
61+
func jsEncode(this js.Value, args []js.Value) interface{} {
62+
script := args[0].String()
63+
64+
encoded, err := urlenc.Encode(script)
65+
// should never happen
66+
if err != nil {
67+
ret := jsObject{D2Error: err.Error()}
68+
str, _ := json.Marshal(ret)
69+
return string(str)
70+
}
71+
72+
ret := jsObject{Result: encoded}
73+
str, _ := json.Marshal(ret)
74+
return string(str)
75+
}
76+
77+
func jsDecode(this js.Value, args []js.Value) interface{} {
78+
script := args[0].String()
79+
80+
script, err := urlenc.Decode(script)
81+
if err != nil {
82+
ret := jsObject{UserError: err.Error()}
83+
str, _ := json.Marshal(ret)
84+
return string(str)
85+
}
86+
87+
ret := jsObject{Result: script}
88+
str, _ := json.Marshal(ret)
89+
return string(str)
90+
}

d2renderers/d2latex/latex.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !wasm
2+
13
package d2latex
24

35
import (

d2renderers/d2latex/latex_stub.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build wasm
2+
3+
package d2latex
4+
5+
func Render(s string) (_ string, err error) {
6+
return "", nil
7+
}
8+
9+
func Measure(s string) (width, height int, err error) {
10+
return
11+
}

0 commit comments

Comments
 (0)