|
| 1 | +//go:build js && wasm |
| 2 | + |
| 3 | +package d2wasm |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "strings" |
| 8 | + "syscall/js" |
| 9 | + |
| 10 | + "oss.terrastruct.com/d2/d2ast" |
| 11 | + "oss.terrastruct.com/d2/d2compiler" |
| 12 | + "oss.terrastruct.com/d2/d2format" |
| 13 | + "oss.terrastruct.com/d2/d2lsp" |
| 14 | + "oss.terrastruct.com/d2/d2oracle" |
| 15 | + "oss.terrastruct.com/d2/d2parser" |
| 16 | + "oss.terrastruct.com/d2/lib/version" |
| 17 | +) |
| 18 | + |
| 19 | +func GetParentID(args []js.Value) (interface{}, error) { |
| 20 | + if len(args) < 1 { |
| 21 | + return nil, &WASMError{Message: "missing id argument", Code: 400} |
| 22 | + } |
| 23 | + |
| 24 | + id := args[0].String() |
| 25 | + mk, err := d2parser.ParseMapKey(id) |
| 26 | + if err != nil { |
| 27 | + return nil, &WASMError{Message: err.Error(), Code: 400} |
| 28 | + } |
| 29 | + |
| 30 | + if len(mk.Edges) > 0 { |
| 31 | + return "", nil |
| 32 | + } |
| 33 | + |
| 34 | + if mk.Key != nil { |
| 35 | + if len(mk.Key.Path) == 1 { |
| 36 | + return "root", nil |
| 37 | + } |
| 38 | + mk.Key.Path = mk.Key.Path[:len(mk.Key.Path)-1] |
| 39 | + return strings.Join(mk.Key.StringIDA(), "."), nil |
| 40 | + } |
| 41 | + |
| 42 | + return "", nil |
| 43 | +} |
| 44 | + |
| 45 | +func GetObjOrder(args []js.Value) (interface{}, error) { |
| 46 | + if len(args) < 1 { |
| 47 | + return nil, &WASMError{Message: "missing dsl argument", Code: 400} |
| 48 | + } |
| 49 | + |
| 50 | + dsl := args[0].String() |
| 51 | + g, _, err := d2compiler.Compile("", strings.NewReader(dsl), &d2compiler.CompileOptions{ |
| 52 | + UTF16Pos: true, |
| 53 | + }) |
| 54 | + if err != nil { |
| 55 | + return nil, &WASMError{Message: err.Error(), Code: 400} |
| 56 | + } |
| 57 | + |
| 58 | + objOrder, err := d2oracle.GetObjOrder(g, nil) |
| 59 | + if err != nil { |
| 60 | + return nil, &WASMError{Message: err.Error(), Code: 500} |
| 61 | + } |
| 62 | + |
| 63 | + return map[string]interface{}{ |
| 64 | + "order": objOrder, |
| 65 | + }, nil |
| 66 | +} |
| 67 | + |
| 68 | +func GetRefRanges(args []js.Value) (interface{}, error) { |
| 69 | + if len(args) < 4 { |
| 70 | + return nil, &WASMError{Message: "missing required arguments", Code: 400} |
| 71 | + } |
| 72 | + |
| 73 | + var fs map[string]string |
| 74 | + if err := json.Unmarshal([]byte(args[0].String()), &fs); err != nil { |
| 75 | + return nil, &WASMError{Message: "invalid fs argument", Code: 400} |
| 76 | + } |
| 77 | + |
| 78 | + file := args[1].String() |
| 79 | + key := args[2].String() |
| 80 | + |
| 81 | + var boardPath []string |
| 82 | + if err := json.Unmarshal([]byte(args[3].String()), &boardPath); err != nil { |
| 83 | + return nil, &WASMError{Message: "invalid boardPath argument", Code: 400} |
| 84 | + } |
| 85 | + |
| 86 | + ranges, importRanges, err := d2lsp.GetRefRanges(file, fs, boardPath, key) |
| 87 | + if err != nil { |
| 88 | + return nil, &WASMError{Message: err.Error(), Code: 500} |
| 89 | + } |
| 90 | + |
| 91 | + return RefRangesResponse{ |
| 92 | + Ranges: ranges, |
| 93 | + ImportRanges: importRanges, |
| 94 | + }, nil |
| 95 | +} |
| 96 | + |
| 97 | +func Compile(args []js.Value) (interface{}, error) { |
| 98 | + if len(args) < 1 { |
| 99 | + return nil, &WASMError{Message: "missing script argument", Code: 400} |
| 100 | + } |
| 101 | + |
| 102 | + script := args[0].String() |
| 103 | + g, _, err := d2compiler.Compile("", strings.NewReader(script), &d2compiler.CompileOptions{ |
| 104 | + UTF16Pos: true, |
| 105 | + }) |
| 106 | + if err != nil { |
| 107 | + if pe, ok := err.(*d2parser.ParseError); ok { |
| 108 | + return nil, &WASMError{Message: pe.Error(), Code: 400} |
| 109 | + } |
| 110 | + return nil, &WASMError{Message: err.Error(), Code: 500} |
| 111 | + } |
| 112 | + |
| 113 | + newScript := d2format.Format(g.AST) |
| 114 | + if script != newScript { |
| 115 | + return map[string]string{"result": newScript}, nil |
| 116 | + } |
| 117 | + |
| 118 | + return nil, nil |
| 119 | +} |
| 120 | + |
| 121 | +func GetBoardAtPosition(args []js.Value) (interface{}, error) { |
| 122 | + if len(args) < 3 { |
| 123 | + return nil, &WASMError{Message: "missing required arguments", Code: 400} |
| 124 | + } |
| 125 | + |
| 126 | + dsl := args[0].String() |
| 127 | + line := args[1].Int() |
| 128 | + column := args[2].Int() |
| 129 | + |
| 130 | + boardPath, err := d2lsp.GetBoardAtPosition(dsl, d2ast.Position{ |
| 131 | + Line: line, |
| 132 | + Column: column, |
| 133 | + }) |
| 134 | + if err != nil { |
| 135 | + return nil, &WASMError{Message: err.Error(), Code: 500} |
| 136 | + } |
| 137 | + |
| 138 | + return BoardPositionResponse{BoardPath: boardPath}, nil |
| 139 | +} |
| 140 | + |
| 141 | +func Encode(args []js.Value) (interface{}, error) { |
| 142 | + if len(args) < 1 { |
| 143 | + return nil, &WASMError{Message: "missing script argument", Code: 400} |
| 144 | + } |
| 145 | + |
| 146 | + script := args[0].String() |
| 147 | + return map[string]string{"result": script}, nil |
| 148 | +} |
| 149 | + |
| 150 | +func Decode(args []js.Value) (interface{}, error) { |
| 151 | + if len(args) < 1 { |
| 152 | + return nil, &WASMError{Message: "missing script argument", Code: 400} |
| 153 | + } |
| 154 | + |
| 155 | + script := args[0].String() |
| 156 | + return map[string]string{"result": script}, nil |
| 157 | +} |
| 158 | + |
| 159 | +func GetVersion(args []js.Value) (interface{}, error) { |
| 160 | + return version.Version, nil |
| 161 | +} |
0 commit comments