From a6bfd1b9748f645477aed87c8146b1e59291cae4 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Thu, 4 Jan 2024 11:46:36 -0800 Subject: [PATCH] fix imported --- d2graph/d2graph.go | 4 ---- d2oracle/edit.go | 8 +++++--- d2oracle/get.go | 8 ++++---- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index 24749eaff1..b70c887bd4 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -207,10 +207,6 @@ func (r Reference) InEdge() bool { return r.Key != r.MapKey.Key } -// func (r Reference) IsSameBoard() bool { -// return r.Key != r.MapKey.Key -// } - type Style struct { Opacity *Scalar `json:"opacity,omitempty"` Stroke *Scalar `json:"stroke,omitempty"` diff --git a/d2oracle/edit.go b/d2oracle/edit.go index 8af8fc5506..d5afa5d6f6 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -349,6 +349,7 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string) toSkip := 1 reserved := false + imported := false // If you're setting `(x -> y)[0].style.opacity` // 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 } obj = o + imported = IsImported(baseAST, obj) var maybeNewScope *d2ast.Map - if baseAST != g.AST { + if baseAST != g.AST || imported { writeableRefs := getWriteableRefs(obj, baseAST) for _, ref := range writeableRefs { 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 var objK *d2ast.Key - if baseAST != g.AST { + if baseAST != g.AST || imported { writeableRefs := getWriteableRefs(obj, baseAST) if len(writeableRefs) > 0 { 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) { 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]) } } diff --git a/d2oracle/get.go b/d2oracle/get.go index b75ad12d1a..6e5721ee89 100644 --- a/d2oracle/get.go +++ b/d2oracle/get.go @@ -140,14 +140,14 @@ func GetParentID(g *d2graph.Graph, boardPath []string, absID string) (string, er 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 { - if ref.MapKey.Range.Path == g.AST.Range.Path { - return false + if ref.Key.Range.Path != ast.Range.Path { + return true } } - return true + return false } func GetObj(g *d2graph.Graph, boardPath []string, absID string) *d2graph.Object {