fix imported

This commit is contained in:
Alexander Wang 2024-01-04 11:46:36 -08:00
parent db1155ebcd
commit a6bfd1b974
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
3 changed files with 9 additions and 11 deletions

View file

@ -207,10 +207,6 @@ func (r Reference) InEdge() bool {
return r.Key != r.MapKey.Key return r.Key != r.MapKey.Key
} }
// func (r Reference) IsSameBoard() bool {
// return r.Key != r.MapKey.Key
// }
type Style struct { type Style struct {
Opacity *Scalar `json:"opacity,omitempty"` Opacity *Scalar `json:"opacity,omitempty"`
Stroke *Scalar `json:"stroke,omitempty"` Stroke *Scalar `json:"stroke,omitempty"`

View file

@ -349,6 +349,7 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
toSkip := 1 toSkip := 1
reserved := false reserved := false
imported := false
// If you're setting `(x -> y)[0].style.opacity` // If you're setting `(x -> y)[0].style.opacity`
// There's 3 cases you need to handle: // There's 3 cases you need to handle:
@ -382,9 +383,10 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
break break
} }
obj = o obj = o
imported = IsImported(baseAST, obj)
var maybeNewScope *d2ast.Map var maybeNewScope *d2ast.Map
if baseAST != g.AST { if baseAST != g.AST || imported {
writeableRefs := getWriteableRefs(obj, baseAST) writeableRefs := getWriteableRefs(obj, baseAST)
for _, ref := range writeableRefs { for _, ref := range writeableRefs {
if ref.MapKey != nil && ref.MapKey.Value.Map != nil { if ref.MapKey != nil && ref.MapKey.Value.Map != nil {
@ -411,7 +413,7 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
writeableLabelMK := true writeableLabelMK := true
var objK *d2ast.Key var objK *d2ast.Key
if baseAST != g.AST { if baseAST != g.AST || imported {
writeableRefs := getWriteableRefs(obj, baseAST) writeableRefs := getWriteableRefs(obj, baseAST)
if len(writeableRefs) > 0 { if len(writeableRefs) > 0 {
objK = writeableRefs[0].MapKey objK = writeableRefs[0].MapKey
@ -3112,7 +3114,7 @@ func filterReservedPath(path []*d2ast.StringBox) (filtered []*d2ast.StringBox) {
func getWriteableRefs(obj *d2graph.Object, writeableAST *d2ast.Map) (out []d2graph.Reference) { func getWriteableRefs(obj *d2graph.Object, writeableAST *d2ast.Map) (out []d2graph.Reference) {
for i, ref := range obj.References { for i, ref := range obj.References {
if ref.ScopeAST == writeableAST { if ref.ScopeAST == writeableAST && ref.Key.Range.Path == writeableAST.Range.Path {
out = append(out, obj.References[i]) out = append(out, obj.References[i])
} }
} }

View file

@ -140,14 +140,14 @@ func GetParentID(g *d2graph.Graph, boardPath []string, absID string) (string, er
return obj.Parent.AbsID(), nil return obj.Parent.AbsID(), nil
} }
func IsImported(g *d2graph.Graph, obj *d2graph.Object) bool { func IsImported(ast *d2ast.Map, obj *d2graph.Object) bool {
for _, ref := range obj.References { for _, ref := range obj.References {
if ref.MapKey.Range.Path == g.AST.Range.Path { if ref.Key.Range.Path != ast.Range.Path {
return false return true
} }
} }
return true return false
} }
func GetObj(g *d2graph.Graph, boardPath []string, absID string) *d2graph.Object { func GetObj(g *d2graph.Graph, boardPath []string, absID string) *d2graph.Object {