classes
This commit is contained in:
parent
2f545e3f61
commit
c828d2c825
5 changed files with 193 additions and 32 deletions
|
|
@ -328,6 +328,149 @@ func Table(r *Runner, shape d2target.Shape) (string, error) {
|
|||
return output, nil
|
||||
}
|
||||
|
||||
func Class(r *Runner, shape d2target.Shape) (string, error) {
|
||||
output := ""
|
||||
js := fmt.Sprintf(`node = rc.rectangle(0, 0, %d, %d, {
|
||||
fill: "%s",
|
||||
stroke: "%s",
|
||||
strokeWidth: %d,
|
||||
%s
|
||||
});`, shape.Width, shape.Height, shape.Fill, shape.Stroke, shape.StrokeWidth, baseRoughProps)
|
||||
paths, err := computeRoughPaths(r, js)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, p := range paths {
|
||||
output += fmt.Sprintf(
|
||||
`<path class="shape" transform="translate(%d %d)" d="%s" style="%s" />`,
|
||||
shape.Pos.X, shape.Pos.Y, p, shapeStyle(shape),
|
||||
)
|
||||
}
|
||||
|
||||
box := geo.NewBox(
|
||||
geo.NewPoint(float64(shape.Pos.X), float64(shape.Pos.Y)),
|
||||
float64(shape.Width),
|
||||
float64(shape.Height),
|
||||
)
|
||||
|
||||
rowHeight := box.Height / float64(2+len(shape.Class.Fields)+len(shape.Class.Methods))
|
||||
headerBox := geo.NewBox(box.TopLeft, box.Width, 2*rowHeight)
|
||||
|
||||
js = fmt.Sprintf(`node = rc.rectangle(0, 0, %d, %f, {
|
||||
fill: "%s",
|
||||
%s
|
||||
});`, shape.Width, headerBox.Height, shape.Fill, baseRoughProps)
|
||||
paths, err = computeRoughPaths(r, js)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, p := range paths {
|
||||
// TODO header fill
|
||||
output += fmt.Sprintf(
|
||||
`<path class="class_header" transform="translate(%d %d)" d="%s" style="fill:%s" />`,
|
||||
shape.Pos.X, shape.Pos.Y, p, "#0a0f25",
|
||||
)
|
||||
}
|
||||
|
||||
output += fmt.Sprintf(
|
||||
`<rect class="sketch-overlay" transform="translate(%d %d)" width="%d" height="%f" />`,
|
||||
shape.Pos.X, shape.Pos.Y, shape.Width, headerBox.Height,
|
||||
)
|
||||
|
||||
if shape.Label != "" {
|
||||
tl := label.InsideMiddleLeft.GetPointOnBox(
|
||||
headerBox,
|
||||
0,
|
||||
float64(shape.LabelWidth),
|
||||
float64(shape.LabelHeight),
|
||||
)
|
||||
|
||||
// TODO header font color
|
||||
output += fmt.Sprintf(`<text class="%s" x="%f" y="%f" style="%s">%s</text>`,
|
||||
"text",
|
||||
tl.X+float64(shape.LabelWidth)/2,
|
||||
tl.Y+float64(shape.LabelHeight)*3/4,
|
||||
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s",
|
||||
"middle",
|
||||
4+shape.FontSize,
|
||||
"white",
|
||||
),
|
||||
svg.EscapeText(shape.Label),
|
||||
)
|
||||
}
|
||||
|
||||
rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)
|
||||
rowBox.TopLeft.Y += headerBox.Height
|
||||
for _, f := range shape.Fields {
|
||||
output += classRow(rowBox, f.VisibilityToken(), f.Name, f.Type, float64(shape.FontSize))
|
||||
rowBox.TopLeft.Y += rowHeight
|
||||
}
|
||||
|
||||
js = fmt.Sprintf(`node = rc.line(%f, %f, %f, %f, {
|
||||
%s
|
||||
});`, rowBox.TopLeft.X, rowBox.TopLeft.Y, rowBox.TopLeft.X+rowBox.Width, rowBox.TopLeft.Y, baseRoughProps)
|
||||
paths, err = computeRoughPaths(r, js)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, p := range paths {
|
||||
output += fmt.Sprintf(
|
||||
`<path class="class_header" d="%s" style="fill:%s" />`,
|
||||
p, "#0a0f25",
|
||||
)
|
||||
}
|
||||
|
||||
for _, m := range shape.Methods {
|
||||
output += classRow(rowBox, m.VisibilityToken(), m.Name, m.Return, float64(shape.FontSize))
|
||||
rowBox.TopLeft.Y += rowHeight
|
||||
}
|
||||
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func classRow(box *geo.Box, prefix, nameText, typeText string, fontSize float64) string {
|
||||
output := ""
|
||||
prefixTL := label.InsideMiddleLeft.GetPointOnBox(
|
||||
box,
|
||||
d2target.PrefixPadding,
|
||||
box.Width,
|
||||
fontSize,
|
||||
)
|
||||
typeTR := label.InsideMiddleRight.GetPointOnBox(
|
||||
box,
|
||||
d2target.TypePadding,
|
||||
0,
|
||||
fontSize,
|
||||
)
|
||||
|
||||
// TODO theme based
|
||||
accentColor := "rgb(13, 50, 178)"
|
||||
|
||||
output += strings.Join([]string{
|
||||
fmt.Sprintf(`<text class="text" x="%f" y="%f" style="%s">%s</text>`,
|
||||
prefixTL.X,
|
||||
prefixTL.Y+fontSize*3/4,
|
||||
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "start", fontSize, accentColor),
|
||||
prefix,
|
||||
),
|
||||
|
||||
fmt.Sprintf(`<text class="text" x="%f" y="%f" style="%s">%s</text>`,
|
||||
prefixTL.X+d2target.PrefixWidth,
|
||||
prefixTL.Y+fontSize*3/4,
|
||||
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "start", fontSize, "black"),
|
||||
svg.EscapeText(nameText),
|
||||
),
|
||||
|
||||
fmt.Sprintf(`<text class="text" x="%f" y="%f" style="%s">%s</text>`,
|
||||
typeTR.X,
|
||||
typeTR.Y+fontSize*3/4,
|
||||
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s;", "end", fontSize, accentColor),
|
||||
svg.EscapeText(typeText),
|
||||
),
|
||||
}, "\n")
|
||||
return output
|
||||
}
|
||||
|
||||
func computeRoughPaths(r *Runner, js string) ([]string, error) {
|
||||
if _, err := r.run(js); err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
Before Width: | Height: | Size: 62 KiB After Width: | Height: | Size: 65 KiB |
|
|
@ -39,24 +39,18 @@ func classHeader(box *geo.Box, text string, textWidth, textHeight, fontSize floa
|
|||
return str
|
||||
}
|
||||
|
||||
const (
|
||||
prefixPadding = 10
|
||||
prefixWidth = 20
|
||||
typePadding = 20
|
||||
)
|
||||
|
||||
func classRow(box *geo.Box, prefix, nameText, typeText string, fontSize float64) string {
|
||||
// Row is made up of prefix, name, and type
|
||||
// e.g. | + firstName string |
|
||||
prefixTL := label.InsideMiddleLeft.GetPointOnBox(
|
||||
box,
|
||||
prefixPadding,
|
||||
d2target.PrefixPadding,
|
||||
box.Width,
|
||||
fontSize,
|
||||
)
|
||||
typeTR := label.InsideMiddleRight.GetPointOnBox(
|
||||
box,
|
||||
typePadding,
|
||||
d2target.TypePadding,
|
||||
0,
|
||||
fontSize,
|
||||
)
|
||||
|
|
@ -71,7 +65,7 @@ func classRow(box *geo.Box, prefix, nameText, typeText string, fontSize float64)
|
|||
),
|
||||
|
||||
fmt.Sprintf(`<text class="text" x="%f" y="%f" style="%s">%s</text>`,
|
||||
prefixTL.X+prefixWidth,
|
||||
prefixTL.X+d2target.PrefixWidth,
|
||||
prefixTL.Y+fontSize*3/4,
|
||||
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "start", fontSize, "black"),
|
||||
svg.EscapeText(nameText),
|
||||
|
|
@ -86,17 +80,6 @@ func classRow(box *geo.Box, prefix, nameText, typeText string, fontSize float64)
|
|||
}, "\n")
|
||||
}
|
||||
|
||||
func visibilityToken(visibility string) string {
|
||||
switch visibility {
|
||||
case "protected":
|
||||
return "#"
|
||||
case "private":
|
||||
return "-"
|
||||
default:
|
||||
return "+"
|
||||
}
|
||||
}
|
||||
|
||||
func drawClass(writer io.Writer, targetShape d2target.Shape) {
|
||||
fmt.Fprintf(writer, `<rect class="shape" x="%d" y="%d" width="%d" height="%d" style="%s"/>`,
|
||||
targetShape.Pos.X, targetShape.Pos.Y, targetShape.Width, targetShape.Height, shapeStyle(targetShape))
|
||||
|
|
@ -115,9 +98,9 @@ func drawClass(writer io.Writer, targetShape d2target.Shape) {
|
|||
|
||||
rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)
|
||||
rowBox.TopLeft.Y += headerBox.Height
|
||||
for _, f := range targetShape.Class.Fields {
|
||||
for _, f := range targetShape.Fields {
|
||||
fmt.Fprint(writer,
|
||||
classRow(rowBox, visibilityToken(f.Visibility), f.Name, f.Type, float64(targetShape.FontSize)),
|
||||
classRow(rowBox, f.VisibilityToken(), f.Name, f.Type, float64(targetShape.FontSize)),
|
||||
)
|
||||
rowBox.TopLeft.Y += rowHeight
|
||||
}
|
||||
|
|
@ -127,9 +110,9 @@ func drawClass(writer io.Writer, targetShape d2target.Shape) {
|
|||
rowBox.TopLeft.X+rowBox.Width, rowBox.TopLeft.Y,
|
||||
fmt.Sprintf("stroke-width:1;stroke:%v", targetShape.Stroke))
|
||||
|
||||
for _, m := range targetShape.Class.Methods {
|
||||
for _, m := range targetShape.Methods {
|
||||
fmt.Fprint(writer,
|
||||
classRow(rowBox, visibilityToken(m.Visibility), m.Name, m.Return, float64(targetShape.FontSize)),
|
||||
classRow(rowBox, m.VisibilityToken(), m.Name, m.Return, float64(targetShape.FontSize)),
|
||||
)
|
||||
rowBox.TopLeft.Y += rowHeight
|
||||
}
|
||||
|
|
|
|||
|
|
@ -617,7 +617,15 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
|
|||
|
||||
switch targetShape.Type {
|
||||
case d2target.ShapeClass:
|
||||
drawClass(writer, targetShape)
|
||||
if sketchRunner != nil {
|
||||
out, err := d2sketch.Class(sketchRunner, targetShape)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
fmt.Fprintf(writer, out)
|
||||
} else {
|
||||
drawClass(writer, targetShape)
|
||||
}
|
||||
fmt.Fprintf(writer, `</g></g>`)
|
||||
return labelMask, nil
|
||||
case d2target.ShapeSQLTable:
|
||||
|
|
|
|||
|
|
@ -6,6 +6,11 @@ import (
|
|||
"oss.terrastruct.com/d2/d2renderers/d2fonts"
|
||||
)
|
||||
|
||||
const (
|
||||
PrefixPadding = 10
|
||||
PrefixWidth = 20
|
||||
)
|
||||
|
||||
type Class struct {
|
||||
Fields []ClassField `json:"fields"`
|
||||
Methods []ClassMethod `json:"methods"`
|
||||
|
|
@ -27,6 +32,17 @@ func (cf ClassField) Text() *MText {
|
|||
}
|
||||
}
|
||||
|
||||
func (cf ClassField) VisibilityToken() string {
|
||||
switch cf.Visibility {
|
||||
case "protected":
|
||||
return "#"
|
||||
case "private":
|
||||
return "-"
|
||||
default:
|
||||
return "+"
|
||||
}
|
||||
}
|
||||
|
||||
type ClassMethod struct {
|
||||
Name string `json:"name"`
|
||||
Return string `json:"return"`
|
||||
|
|
@ -42,3 +58,14 @@ func (cm ClassMethod) Text() *MText {
|
|||
Shape: "class",
|
||||
}
|
||||
}
|
||||
|
||||
func (cm ClassMethod) VisibilityToken() string {
|
||||
switch cm.Visibility {
|
||||
case "protected":
|
||||
return "#"
|
||||
case "private":
|
||||
return "-"
|
||||
default:
|
||||
return "+"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue