PR comments
This commit is contained in:
parent
5c9f87f64a
commit
347a6b263d
3 changed files with 11 additions and 11 deletions
|
|
@ -984,7 +984,7 @@ func Render(diagram *d2target.Diagram) ([]byte, error) {
|
||||||
// SVG has no notion of z-index. The z-index is effectively the order it's drawn.
|
// SVG has no notion of z-index. The z-index is effectively the order it's drawn.
|
||||||
// So draw from the least nested to most nested
|
// So draw from the least nested to most nested
|
||||||
idToShape := make(map[string]d2target.Shape)
|
idToShape := make(map[string]d2target.Shape)
|
||||||
allObjects := make([]d2target.DiagramObject, 0, len(diagram.Shapes)+len(diagram.Connections))
|
allObjects := make([]DiagramObject, 0, len(diagram.Shapes)+len(diagram.Connections))
|
||||||
for _, s := range diagram.Shapes {
|
for _, s := range diagram.Shapes {
|
||||||
idToShape[s.ID] = s
|
idToShape[s.ID] = s
|
||||||
allObjects = append(allObjects, s)
|
allObjects = append(allObjects, s)
|
||||||
|
|
@ -1005,7 +1005,7 @@ func Render(diagram *d2target.Diagram) ([]byte, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("unknow object of type %t", obj)
|
return nil, fmt.Errorf("unknow object of type %T", obj)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1015,13 +1015,18 @@ func Render(diagram *d2target.Diagram) ([]byte, error) {
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DiagramObject interface {
|
||||||
|
GetID() string
|
||||||
|
GetZIndex() int
|
||||||
|
}
|
||||||
|
|
||||||
// sortObjects sorts all diagrams objects (shapes and connections) in the desired drawing order
|
// sortObjects sorts all diagrams objects (shapes and connections) in the desired drawing order
|
||||||
// the sorting criteria is:
|
// the sorting criteria is:
|
||||||
// 1. zIndex, lower comes first
|
// 1. zIndex, lower comes first
|
||||||
// 2. two shapes with the same zIndex are sorted by their level (container nesting), containers come first
|
// 2. two shapes with the same zIndex are sorted by their level (container nesting), containers come first
|
||||||
// 3. two shapes with the same zIndex and same level, are sorted in the order they were exported
|
// 3. two shapes with the same zIndex and same level, are sorted in the order they were exported
|
||||||
// 4. shape and edge, shapes come first
|
// 4. shape and edge, shapes come first
|
||||||
func sortObjects(allObjects []d2target.DiagramObject) {
|
func sortObjects(allObjects []DiagramObject) {
|
||||||
sort.SliceStable(allObjects, func(i, j int) bool {
|
sort.SliceStable(allObjects, func(i, j int) bool {
|
||||||
// first sort by zIndex
|
// first sort by zIndex
|
||||||
iZIndex := allObjects[i].GetZIndex()
|
iZIndex := allObjects[i].GetZIndex()
|
||||||
|
|
@ -1030,7 +1035,7 @@ func sortObjects(allObjects []d2target.DiagramObject) {
|
||||||
return iZIndex < jZIndex
|
return iZIndex < jZIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
// then, if both are shapes, the containers come first
|
// then, if both are shapes, parents come before their children
|
||||||
iShape, iIsShape := allObjects[i].(d2target.Shape)
|
iShape, iIsShape := allObjects[i].(d2target.Shape)
|
||||||
jShape, jIsShape := allObjects[j].(d2target.Shape)
|
jShape, jIsShape := allObjects[j].(d2target.Shape)
|
||||||
if iIsShape && jIsShape {
|
if iIsShape && jIsShape {
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSortObjects(t *testing.T) {
|
func TestSortObjects(t *testing.T) {
|
||||||
allObjects := []d2target.DiagramObject{
|
allObjects := []DiagramObject{
|
||||||
// same zIndex and level, should keep in this order
|
// same zIndex and level, should keep in this order
|
||||||
d2target.Shape{
|
d2target.Shape{
|
||||||
ID: "0",
|
ID: "0",
|
||||||
|
|
@ -57,7 +57,7 @@ func TestSortObjects(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedOrder := []d2target.DiagramObject{
|
expectedOrder := []DiagramObject{
|
||||||
allObjects[8],
|
allObjects[8],
|
||||||
allObjects[0],
|
allObjects[0],
|
||||||
allObjects[1],
|
allObjects[1],
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,6 @@ const (
|
||||||
MAX_ICON_SIZE = 64
|
MAX_ICON_SIZE = 64
|
||||||
)
|
)
|
||||||
|
|
||||||
type DiagramObject interface {
|
|
||||||
GetID() string
|
|
||||||
GetZIndex() int
|
|
||||||
}
|
|
||||||
|
|
||||||
type Diagram struct {
|
type Diagram struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Description string `json:"description,omitempty"`
|
Description string `json:"description,omitempty"`
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue