diff --git a/d2layouts/d2dagrelayout/layout.go b/d2layouts/d2dagrelayout/layout.go index ee86e9599..6d2a5f91b 100644 --- a/d2layouts/d2dagrelayout/layout.go +++ b/d2layouts/d2dagrelayout/layout.go @@ -30,12 +30,12 @@ var setupJS string //go:embed dagre.js var dagreJS string -type Opts struct { +type ConfigurableOpts struct { NodeSep int EdgeSep int } -var DefaultOpts = Opts{ +var DefaultOpts = ConfigurableOpts{ NodeSep: 60, EdgeSep: 40, } @@ -52,16 +52,16 @@ type DagreEdge struct { Points []*geo.Point `json:"points"` } -type dagreGraphAttrs struct { +type dagreOpts struct { // for a top to bottom graph: ranksep is y spacing, nodesep is x spacing, edgesep is x spacing ranksep int - edgesep int - nodesep int // graph direction: tb (top to bottom)| bt | lr | rl rankdir string + + ConfigurableOpts } -func Layout(ctx context.Context, g *d2graph.Graph, opts *Opts) (err error) { +func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err error) { if opts == nil { opts = &DefaultOpts } @@ -76,9 +76,11 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *Opts) (err error) { return err } - rootAttrs := dagreGraphAttrs{ - edgesep: opts.EdgeSep, - nodesep: opts.NodeSep, + rootAttrs := dagreOpts{ + ConfigurableOpts: ConfigurableOpts{ + EdgeSep: opts.EdgeSep, + NodeSep: opts.NodeSep, + }, } isHorizontal := false switch g.Root.Attributes.Direction.Value { @@ -279,7 +281,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *Opts) (err error) { return nil } -func setGraphAttrs(attrs dagreGraphAttrs) string { +func setGraphAttrs(attrs dagreOpts) string { return fmt.Sprintf(`g.setGraph({ ranksep: %d, edgesep: %d, @@ -288,8 +290,8 @@ func setGraphAttrs(attrs dagreGraphAttrs) string { }); `, attrs.ranksep, - attrs.edgesep, - attrs.nodesep, + attrs.ConfigurableOpts.EdgeSep, + attrs.ConfigurableOpts.NodeSep, attrs.rankdir, ) } diff --git a/d2layouts/d2elklayout/layout.go b/d2layouts/d2elklayout/layout.go index f46e19004..384d877e5 100644 --- a/d2layouts/d2elklayout/layout.go +++ b/d2layouts/d2elklayout/layout.go @@ -31,23 +31,23 @@ var elkJS string var setupJS string type ELKNode struct { - ID string `json:"id"` - X float64 `json:"x"` - Y float64 `json:"y"` - Width float64 `json:"width"` - Height float64 `json:"height"` - Children []*ELKNode `json:"children,omitempty"` - Labels []*ELKLabel `json:"labels,omitempty"` - LayoutOptions *ELKLayoutOptions `json:"layoutOptions,omitempty"` + ID string `json:"id"` + X float64 `json:"x"` + Y float64 `json:"y"` + Width float64 `json:"width"` + Height float64 `json:"height"` + Children []*ELKNode `json:"children,omitempty"` + Labels []*ELKLabel `json:"labels,omitempty"` + LayoutOptions *elkOpts `json:"layoutOptions,omitempty"` } type ELKLabel struct { - Text string `json:"text"` - X float64 `json:"x"` - Y float64 `json:"y"` - Width float64 `json:"width"` - Height float64 `json:"height"` - LayoutOptions *ELKLayoutOptions `json:"layoutOptions,omitempty"` + Text string `json:"text"` + X float64 `json:"x"` + Y float64 `json:"y"` + Width float64 `json:"width"` + Height float64 `json:"height"` + LayoutOptions *elkOpts `json:"layoutOptions,omitempty"` } type ELKPoint struct { @@ -71,18 +71,10 @@ type ELKEdge struct { } type ELKGraph struct { - ID string `json:"id"` - LayoutOptions *ELKLayoutOptions `json:"layoutOptions"` - Children []*ELKNode `json:"children,omitempty"` - Edges []*ELKEdge `json:"edges,omitempty"` -} - -var DefaultOpts = ConfigurableOpts{ - Algorithm: "layered", - NodeSpacing: 100.0, - Padding: "[top=75,left=75,bottom=75,right=75]", - EdgeNodeSpacing: 50.0, - SelfLoopSpacing: 50.0, + ID string `json:"id"` + LayoutOptions *elkOpts `json:"layoutOptions"` + Children []*ELKNode `json:"children,omitempty"` + Edges []*ELKEdge `json:"edges,omitempty"` } type ConfigurableOpts struct { @@ -93,7 +85,15 @@ type ConfigurableOpts struct { SelfLoopSpacing int `json:"elk.spacing.nodeSelfLoop"` } -type ELKLayoutOptions struct { +var DefaultOpts = ConfigurableOpts{ + Algorithm: "layered", + NodeSpacing: 100.0, + Padding: "[top=75,left=75,bottom=75,right=75]", + EdgeNodeSpacing: 50.0, + SelfLoopSpacing: 50.0, +} + +type elkOpts struct { Direction string `json:"elk.direction"` HierarchyHandling string `json:"elk.hierarchyHandling,omitempty"` InlineEdgeLabels bool `json:"elk.edgeLabels.inline,omitempty"` @@ -125,7 +125,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err elkGraph := &ELKGraph{ ID: "root", - LayoutOptions: &ELKLayoutOptions{ + LayoutOptions: &elkOpts{ HierarchyHandling: "INCLUDE_CHILDREN", ConsiderModelOrder: "NODES_AND_EDGES", ConfigurableOpts: *opts, @@ -171,7 +171,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err } if len(obj.ChildrenArray) > 0 { - n.LayoutOptions = &ELKLayoutOptions{ + n.LayoutOptions = &elkOpts{ ForceNodeModelOrder: true, ConfigurableOpts: ConfigurableOpts{ Padding: opts.Padding, @@ -206,7 +206,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err Text: edge.Attributes.Label.Value, Width: float64(edge.LabelDimensions.Width), Height: float64(edge.LabelDimensions.Height), - LayoutOptions: &ELKLayoutOptions{ + LayoutOptions: &elkOpts{ InlineEdgeLabels: true, }, })