getparentid

This commit is contained in:
Alexander Wang 2024-01-20 11:13:34 -08:00 committed by Maricaya
parent 21afb4861d
commit 7dea1db40b
No known key found for this signature in database
GPG key ID: 91DA6031E30DE02F

View file

@ -20,6 +20,7 @@ import (
)
func main() {
js.Global().Set("d2GetParentID", js.FuncOf(jsGetParentID))
js.Global().Set("d2GetObjOrder", js.FuncOf(jsGetObjOrder))
js.Global().Set("d2GetRefRanges", js.FuncOf(jsGetRefRanges))
js.Global().Set("d2Compile", js.FuncOf(jsCompile))
@ -64,6 +65,26 @@ func jsGetObjOrder(this js.Value, args []js.Value) interface{} {
return string(str)
}
func jsGetParentID(this js.Value, args []js.Value) interface{} {
id := args[0].String()
mk, _ := d2parser.ParseMapKey(id)
if len(mk.Edges) > 0 {
return ""
}
if mk.Key != nil {
if len(mk.Key.Path) == 1 {
return "root"
}
mk.Key.Path = mk.Key.Path[:len(mk.Key.Path)-1]
return strings.Join(mk.Key.IDA(), ".")
}
return ""
}
type jsRefRanges struct {
Ranges []d2ast.Range `json:"ranges"`
ParseError string `json:"parseError"`