Merge pull request #503 from alixander/sketch-tables-classes
sketch: tables and classes
This commit is contained in:
commit
55ad60ba34
10 changed files with 572 additions and 76 deletions
|
|
@ -3,13 +3,17 @@ package d2sketch
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
|
||||||
"github.com/dop251/goja"
|
"github.com/dop251/goja"
|
||||||
|
|
||||||
"oss.terrastruct.com/d2/d2target"
|
"oss.terrastruct.com/d2/d2target"
|
||||||
|
"oss.terrastruct.com/d2/lib/geo"
|
||||||
|
"oss.terrastruct.com/d2/lib/label"
|
||||||
"oss.terrastruct.com/d2/lib/svg"
|
"oss.terrastruct.com/d2/lib/svg"
|
||||||
|
"oss.terrastruct.com/util-go/go2"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed fillpattern.svg
|
//go:embed fillpattern.svg
|
||||||
|
|
@ -81,10 +85,7 @@ func Rect(r *Runner, shape d2target.Shape) (string, error) {
|
||||||
strokeWidth: %d,
|
strokeWidth: %d,
|
||||||
%s
|
%s
|
||||||
});`, shape.Width, shape.Height, shape.Fill, shape.Stroke, shape.StrokeWidth, baseRoughProps)
|
});`, shape.Width, shape.Height, shape.Fill, shape.Stroke, shape.StrokeWidth, baseRoughProps)
|
||||||
if _, err := r.run(js); err != nil {
|
paths, err := computeRoughPaths(r, js)
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
paths, err := extractPaths(r)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
@ -109,10 +110,7 @@ func Oval(r *Runner, shape d2target.Shape) (string, error) {
|
||||||
strokeWidth: %d,
|
strokeWidth: %d,
|
||||||
%s
|
%s
|
||||||
});`, shape.Width/2, shape.Height/2, shape.Width, shape.Height, shape.Fill, shape.Stroke, shape.StrokeWidth, baseRoughProps)
|
});`, shape.Width/2, shape.Height/2, shape.Width, shape.Height, shape.Fill, shape.Stroke, shape.StrokeWidth, baseRoughProps)
|
||||||
if _, err := r.run(js); err != nil {
|
paths, err := computeRoughPaths(r, js)
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
paths, err := extractPaths(r)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
@ -140,10 +138,7 @@ func Paths(r *Runner, shape d2target.Shape, paths []string) (string, error) {
|
||||||
strokeWidth: %d,
|
strokeWidth: %d,
|
||||||
%s
|
%s
|
||||||
});`, path, shape.Fill, shape.Stroke, shape.StrokeWidth, baseRoughProps)
|
});`, path, shape.Fill, shape.Stroke, shape.StrokeWidth, baseRoughProps)
|
||||||
if _, err := r.run(js); err != nil {
|
sketchPaths, err := computeRoughPaths(r, js)
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
sketchPaths, err := extractPaths(r)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
@ -180,10 +175,7 @@ func connectionStyle(connection d2target.Connection) string {
|
||||||
func Connection(r *Runner, connection d2target.Connection, path, attrs string) (string, error) {
|
func Connection(r *Runner, connection d2target.Connection, path, attrs string) (string, error) {
|
||||||
roughness := 1.0
|
roughness := 1.0
|
||||||
js := fmt.Sprintf(`node = rc.path("%s", {roughness: %f, seed: 1});`, path, roughness)
|
js := fmt.Sprintf(`node = rc.path("%s", {roughness: %f, seed: 1});`, path, roughness)
|
||||||
if _, err := r.run(js); err != nil {
|
paths, err := computeRoughPaths(r, js)
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
paths, err := extractPaths(r)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
@ -197,6 +189,295 @@ func Connection(r *Runner, connection d2target.Connection, path, attrs string) (
|
||||||
return output, nil
|
return output, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO cleanup
|
||||||
|
func Table(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(1+len(shape.SQLTable.Columns))
|
||||||
|
headerBox := geo.NewBox(box.TopLeft, box.Width, rowHeight)
|
||||||
|
|
||||||
|
js = fmt.Sprintf(`node = rc.rectangle(0, 0, %d, %f, {
|
||||||
|
fill: "%s",
|
||||||
|
%s
|
||||||
|
});`, shape.Width, rowHeight, 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",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if shape.Label != "" {
|
||||||
|
tl := label.InsideMiddleLeft.GetPointOnBox(
|
||||||
|
headerBox,
|
||||||
|
20,
|
||||||
|
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,
|
||||||
|
tl.Y+float64(shape.LabelHeight)*3/4,
|
||||||
|
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s",
|
||||||
|
"start",
|
||||||
|
4+shape.FontSize,
|
||||||
|
"white",
|
||||||
|
),
|
||||||
|
svg.EscapeText(shape.Label),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
var longestNameWidth int
|
||||||
|
for _, f := range shape.Columns {
|
||||||
|
longestNameWidth = go2.Max(longestNameWidth, f.Name.LabelWidth)
|
||||||
|
}
|
||||||
|
|
||||||
|
rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)
|
||||||
|
rowBox.TopLeft.Y += headerBox.Height
|
||||||
|
for _, f := range shape.Columns {
|
||||||
|
nameTL := label.InsideMiddleLeft.GetPointOnBox(
|
||||||
|
rowBox,
|
||||||
|
d2target.NamePadding,
|
||||||
|
rowBox.Width,
|
||||||
|
float64(shape.FontSize),
|
||||||
|
)
|
||||||
|
constraintTR := label.InsideMiddleRight.GetPointOnBox(
|
||||||
|
rowBox,
|
||||||
|
d2target.TypePadding,
|
||||||
|
0,
|
||||||
|
float64(shape.FontSize),
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO theme based
|
||||||
|
primaryColor := "rgb(13, 50, 178)"
|
||||||
|
accentColor := "rgb(74, 111, 243)"
|
||||||
|
neutralColor := "rgb(103, 108, 126)"
|
||||||
|
|
||||||
|
output += strings.Join([]string{
|
||||||
|
fmt.Sprintf(`<text class="text" x="%f" y="%f" style="%s">%s</text>`,
|
||||||
|
nameTL.X,
|
||||||
|
nameTL.Y+float64(shape.FontSize)*3/4,
|
||||||
|
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "start", float64(shape.FontSize), primaryColor),
|
||||||
|
svg.EscapeText(f.Name.Label),
|
||||||
|
),
|
||||||
|
|
||||||
|
// TODO light font
|
||||||
|
fmt.Sprintf(`<text class="text" x="%f" y="%f" style="%s">%s</text>`,
|
||||||
|
nameTL.X+float64(longestNameWidth)+2*d2target.NamePadding,
|
||||||
|
nameTL.Y+float64(shape.FontSize)*3/4,
|
||||||
|
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "start", float64(shape.FontSize), neutralColor),
|
||||||
|
svg.EscapeText(f.Type.Label),
|
||||||
|
),
|
||||||
|
|
||||||
|
fmt.Sprintf(`<text class="text" x="%f" y="%f" style="%s">%s</text>`,
|
||||||
|
constraintTR.X,
|
||||||
|
constraintTR.Y+float64(shape.FontSize)*3/4,
|
||||||
|
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s;letter-spacing:2px;", "end", float64(shape.FontSize), accentColor),
|
||||||
|
f.ConstraintAbbr(),
|
||||||
|
),
|
||||||
|
}, "\n")
|
||||||
|
|
||||||
|
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",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output += fmt.Sprintf(
|
||||||
|
`<rect class="sketch-overlay" transform="translate(%d %d)" width="%d" height="%d" />`,
|
||||||
|
shape.Pos.X, shape.Pos.Y, shape.Width, shape.Height,
|
||||||
|
)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
return extractPaths(r)
|
||||||
|
}
|
||||||
|
|
||||||
type attrs struct {
|
type attrs struct {
|
||||||
D string `json:"d"`
|
D string `json:"d"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -223,6 +223,58 @@ queue -> package -> step
|
||||||
callout -> stored_data -> person
|
callout -> stored_data -> person
|
||||||
diamond -> oval -> circle
|
diamond -> oval -> circle
|
||||||
hexagon -> cloud
|
hexagon -> cloud
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "sql_tables",
|
||||||
|
script: `users: {
|
||||||
|
shape: sql_table
|
||||||
|
id: int
|
||||||
|
name: string
|
||||||
|
email: string
|
||||||
|
password: string
|
||||||
|
last_login: datetime
|
||||||
|
}
|
||||||
|
|
||||||
|
products: {
|
||||||
|
shape: sql_table
|
||||||
|
id: int
|
||||||
|
price: decimal
|
||||||
|
sku: string
|
||||||
|
name: string
|
||||||
|
}
|
||||||
|
|
||||||
|
orders: {
|
||||||
|
shape: sql_table
|
||||||
|
id: int
|
||||||
|
user_id: int
|
||||||
|
product_id: int
|
||||||
|
}
|
||||||
|
|
||||||
|
shipments: {
|
||||||
|
shape: sql_table
|
||||||
|
id: int
|
||||||
|
order_id: int
|
||||||
|
tracking_number: string
|
||||||
|
status: string
|
||||||
|
}
|
||||||
|
|
||||||
|
users.id <-> orders.user_id
|
||||||
|
products.id <-> orders.product_id
|
||||||
|
shipments.order_id <-> orders.id`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "class",
|
||||||
|
script: `manager: BatchManager {
|
||||||
|
shape: class
|
||||||
|
-num: int
|
||||||
|
-timeout: int
|
||||||
|
-pid
|
||||||
|
|
||||||
|
+getStatus(): Enum
|
||||||
|
+getJobs(): "Job[]"
|
||||||
|
+setTimeout(seconds int)
|
||||||
|
}
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
55
d2renderers/d2sketch/testdata/class/sketch.exp.svg
vendored
Normal file
55
d2renderers/d2sketch/testdata/class/sketch.exp.svg
vendored
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 65 KiB |
75
d2renderers/d2sketch/testdata/sql_tables/sketch.exp.svg
vendored
Normal file
75
d2renderers/d2sketch/testdata/sql_tables/sketch.exp.svg
vendored
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 87 KiB |
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"oss.terrastruct.com/d2/d2target"
|
"oss.terrastruct.com/d2/d2target"
|
||||||
"oss.terrastruct.com/d2/lib/geo"
|
"oss.terrastruct.com/d2/lib/geo"
|
||||||
"oss.terrastruct.com/d2/lib/label"
|
"oss.terrastruct.com/d2/lib/label"
|
||||||
|
"oss.terrastruct.com/d2/lib/svg"
|
||||||
)
|
)
|
||||||
|
|
||||||
func classHeader(box *geo.Box, text string, textWidth, textHeight, fontSize float64) string {
|
func classHeader(box *geo.Box, text string, textWidth, textHeight, fontSize float64) string {
|
||||||
|
|
@ -32,30 +33,24 @@ func classHeader(box *geo.Box, text string, textWidth, textHeight, fontSize floa
|
||||||
4+fontSize,
|
4+fontSize,
|
||||||
"white",
|
"white",
|
||||||
),
|
),
|
||||||
escapeText(text),
|
svg.EscapeText(text),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
prefixPadding = 10
|
|
||||||
prefixWidth = 20
|
|
||||||
typePadding = 20
|
|
||||||
)
|
|
||||||
|
|
||||||
func classRow(box *geo.Box, prefix, nameText, typeText string, fontSize float64) string {
|
func classRow(box *geo.Box, prefix, nameText, typeText string, fontSize float64) string {
|
||||||
// Row is made up of prefix, name, and type
|
// Row is made up of prefix, name, and type
|
||||||
// e.g. | + firstName string |
|
// e.g. | + firstName string |
|
||||||
prefixTL := label.InsideMiddleLeft.GetPointOnBox(
|
prefixTL := label.InsideMiddleLeft.GetPointOnBox(
|
||||||
box,
|
box,
|
||||||
prefixPadding,
|
d2target.PrefixPadding,
|
||||||
box.Width,
|
box.Width,
|
||||||
fontSize,
|
fontSize,
|
||||||
)
|
)
|
||||||
typeTR := label.InsideMiddleRight.GetPointOnBox(
|
typeTR := label.InsideMiddleRight.GetPointOnBox(
|
||||||
box,
|
box,
|
||||||
typePadding,
|
d2target.TypePadding,
|
||||||
0,
|
0,
|
||||||
fontSize,
|
fontSize,
|
||||||
)
|
)
|
||||||
|
|
@ -70,32 +65,21 @@ func classRow(box *geo.Box, prefix, nameText, typeText string, fontSize float64)
|
||||||
),
|
),
|
||||||
|
|
||||||
fmt.Sprintf(`<text class="text" x="%f" y="%f" style="%s">%s</text>`,
|
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,
|
prefixTL.Y+fontSize*3/4,
|
||||||
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "start", fontSize, "black"),
|
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "start", fontSize, "black"),
|
||||||
escapeText(nameText),
|
svg.EscapeText(nameText),
|
||||||
),
|
),
|
||||||
|
|
||||||
fmt.Sprintf(`<text class="text" x="%f" y="%f" style="%s">%s</text>`,
|
fmt.Sprintf(`<text class="text" x="%f" y="%f" style="%s">%s</text>`,
|
||||||
typeTR.X,
|
typeTR.X,
|
||||||
typeTR.Y+fontSize*3/4,
|
typeTR.Y+fontSize*3/4,
|
||||||
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "end", fontSize, accentColor),
|
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "end", fontSize, accentColor),
|
||||||
escapeText(typeText),
|
svg.EscapeText(typeText),
|
||||||
),
|
),
|
||||||
}, "\n")
|
}, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func visibilityToken(visibility string) string {
|
|
||||||
switch visibility {
|
|
||||||
case "protected":
|
|
||||||
return "#"
|
|
||||||
case "private":
|
|
||||||
return "-"
|
|
||||||
default:
|
|
||||||
return "+"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func drawClass(writer io.Writer, targetShape d2target.Shape) {
|
func drawClass(writer io.Writer, targetShape d2target.Shape) {
|
||||||
fmt.Fprintf(writer, `<rect class="shape" x="%d" y="%d" width="%d" height="%d" style="%s"/>`,
|
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))
|
targetShape.Pos.X, targetShape.Pos.Y, targetShape.Width, targetShape.Height, shapeStyle(targetShape))
|
||||||
|
|
@ -114,9 +98,9 @@ func drawClass(writer io.Writer, targetShape d2target.Shape) {
|
||||||
|
|
||||||
rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)
|
rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)
|
||||||
rowBox.TopLeft.Y += headerBox.Height
|
rowBox.TopLeft.Y += headerBox.Height
|
||||||
for _, f := range targetShape.Class.Fields {
|
for _, f := range targetShape.Fields {
|
||||||
fmt.Fprint(writer,
|
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
|
rowBox.TopLeft.Y += rowHeight
|
||||||
}
|
}
|
||||||
|
|
@ -126,9 +110,9 @@ func drawClass(writer io.Writer, targetShape d2target.Shape) {
|
||||||
rowBox.TopLeft.X+rowBox.Width, rowBox.TopLeft.Y,
|
rowBox.TopLeft.X+rowBox.Width, rowBox.TopLeft.Y,
|
||||||
fmt.Sprintf("stroke-width:1;stroke:%v", targetShape.Stroke))
|
fmt.Sprintf("stroke-width:1;stroke:%v", targetShape.Stroke))
|
||||||
|
|
||||||
for _, m := range targetShape.Class.Methods {
|
for _, m := range targetShape.Methods {
|
||||||
fmt.Fprint(writer,
|
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
|
rowBox.TopLeft.Y += rowHeight
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ package d2svg
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"encoding/xml"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
|
|
@ -357,7 +356,7 @@ func makeLabelMask(labelTL *geo.Point, width, height int) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawConnection(writer io.Writer, labelMaskID string, connection d2target.Connection, markers map[string]struct{}, idToShape map[string]d2target.Shape, sketchRunner *d2sketch.Runner) (labelMask string, _ error) {
|
func drawConnection(writer io.Writer, labelMaskID string, connection d2target.Connection, markers map[string]struct{}, idToShape map[string]d2target.Shape, sketchRunner *d2sketch.Runner) (labelMask string, _ error) {
|
||||||
fmt.Fprintf(writer, `<g id="%s">`, escapeText(connection.ID))
|
fmt.Fprintf(writer, `<g id="%s">`, svg.EscapeText(connection.ID))
|
||||||
var markerStart string
|
var markerStart string
|
||||||
if connection.SrcArrow != d2target.NoArrowhead {
|
if connection.SrcArrow != d2target.NoArrowhead {
|
||||||
id := arrowheadMarkerID(false, connection)
|
id := arrowheadMarkerID(false, connection)
|
||||||
|
|
@ -536,7 +535,7 @@ func render3dRect(targetShape d2target.Shape) string {
|
||||||
strings.Join(borderSegments, " "), borderStyle)
|
strings.Join(borderSegments, " "), borderStyle)
|
||||||
|
|
||||||
// create mask from border stroke, to cut away from the shape fills
|
// create mask from border stroke, to cut away from the shape fills
|
||||||
maskID := fmt.Sprintf("border-mask-%v", escapeText(targetShape.ID))
|
maskID := fmt.Sprintf("border-mask-%v", svg.EscapeText(targetShape.ID))
|
||||||
borderMask := strings.Join([]string{
|
borderMask := strings.Join([]string{
|
||||||
fmt.Sprintf(`<defs><mask id="%s" maskUnits="userSpaceOnUse" x="%d" y="%d" width="%d" height="%d">`,
|
fmt.Sprintf(`<defs><mask id="%s" maskUnits="userSpaceOnUse" x="%d" y="%d" width="%d" height="%d">`,
|
||||||
maskID, targetShape.Pos.X, targetShape.Pos.Y-threeDeeOffset, targetShape.Width+threeDeeOffset, targetShape.Height+threeDeeOffset,
|
maskID, targetShape.Pos.X, targetShape.Pos.Y-threeDeeOffset, targetShape.Width+threeDeeOffset, targetShape.Height+threeDeeOffset,
|
||||||
|
|
@ -583,7 +582,7 @@ func render3dRect(targetShape d2target.Shape) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2sketch.Runner) (labelMask string, err error) {
|
func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2sketch.Runner) (labelMask string, err error) {
|
||||||
fmt.Fprintf(writer, `<g id="%s">`, escapeText(targetShape.ID))
|
fmt.Fprintf(writer, `<g id="%s">`, svg.EscapeText(targetShape.ID))
|
||||||
tl := geo.NewPoint(float64(targetShape.Pos.X), float64(targetShape.Pos.Y))
|
tl := geo.NewPoint(float64(targetShape.Pos.X), float64(targetShape.Pos.Y))
|
||||||
width := float64(targetShape.Width)
|
width := float64(targetShape.Width)
|
||||||
height := float64(targetShape.Height)
|
height := float64(targetShape.Height)
|
||||||
|
|
@ -618,11 +617,27 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
|
||||||
|
|
||||||
switch targetShape.Type {
|
switch targetShape.Type {
|
||||||
case d2target.ShapeClass:
|
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>`)
|
fmt.Fprintf(writer, `</g></g>`)
|
||||||
return labelMask, nil
|
return labelMask, nil
|
||||||
case d2target.ShapeSQLTable:
|
case d2target.ShapeSQLTable:
|
||||||
drawTable(writer, targetShape)
|
if sketchRunner != nil {
|
||||||
|
out, err := d2sketch.Table(sketchRunner, targetShape)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
fmt.Fprintf(writer, out)
|
||||||
|
} else {
|
||||||
|
drawTable(writer, targetShape)
|
||||||
|
}
|
||||||
fmt.Fprintf(writer, `</g></g>`)
|
fmt.Fprintf(writer, `</g></g>`)
|
||||||
return labelMask, nil
|
return labelMask, nil
|
||||||
case d2target.ShapeOval:
|
case d2target.ShapeOval:
|
||||||
|
|
@ -823,15 +838,9 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
|
||||||
return labelMask, nil
|
return labelMask, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func escapeText(text string) string {
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
_ = xml.EscapeText(buf, []byte(text))
|
|
||||||
return buf.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func renderText(text string, x, height float64) string {
|
func renderText(text string, x, height float64) string {
|
||||||
if !strings.Contains(text, "\n") {
|
if !strings.Contains(text, "\n") {
|
||||||
return escapeText(text)
|
return svg.EscapeText(text)
|
||||||
}
|
}
|
||||||
rendered := []string{}
|
rendered := []string{}
|
||||||
lines := strings.Split(text, "\n")
|
lines := strings.Split(text, "\n")
|
||||||
|
|
@ -840,7 +849,7 @@ func renderText(text string, x, height float64) string {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
dy = 0
|
dy = 0
|
||||||
}
|
}
|
||||||
escaped := escapeText(line)
|
escaped := svg.EscapeText(line)
|
||||||
if escaped == "" {
|
if escaped == "" {
|
||||||
// if there are multiple newlines in a row we still need text for the tspan to render
|
// if there are multiple newlines in a row we still need text for the tspan to render
|
||||||
escaped = " "
|
escaped = " "
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"oss.terrastruct.com/d2/d2target"
|
"oss.terrastruct.com/d2/d2target"
|
||||||
"oss.terrastruct.com/d2/lib/geo"
|
"oss.terrastruct.com/d2/lib/geo"
|
||||||
"oss.terrastruct.com/d2/lib/label"
|
"oss.terrastruct.com/d2/lib/label"
|
||||||
|
"oss.terrastruct.com/d2/lib/svg"
|
||||||
"oss.terrastruct.com/util-go/go2"
|
"oss.terrastruct.com/util-go/go2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -32,7 +33,7 @@ func tableHeader(box *geo.Box, text string, textWidth, textHeight, fontSize floa
|
||||||
4+fontSize,
|
4+fontSize,
|
||||||
"white",
|
"white",
|
||||||
),
|
),
|
||||||
escapeText(text),
|
svg.EscapeText(text),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return str
|
return str
|
||||||
|
|
@ -64,7 +65,7 @@ func tableRow(box *geo.Box, nameText, typeText, constraintText string, fontSize,
|
||||||
nameTL.X,
|
nameTL.X,
|
||||||
nameTL.Y+fontSize*3/4,
|
nameTL.Y+fontSize*3/4,
|
||||||
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "start", fontSize, primaryColor),
|
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "start", fontSize, primaryColor),
|
||||||
escapeText(nameText),
|
svg.EscapeText(nameText),
|
||||||
),
|
),
|
||||||
|
|
||||||
// TODO light font
|
// TODO light font
|
||||||
|
|
@ -72,7 +73,7 @@ func tableRow(box *geo.Box, nameText, typeText, constraintText string, fontSize,
|
||||||
nameTL.X+longestNameWidth+2*d2target.NamePadding,
|
nameTL.X+longestNameWidth+2*d2target.NamePadding,
|
||||||
nameTL.Y+fontSize*3/4,
|
nameTL.Y+fontSize*3/4,
|
||||||
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "start", fontSize, neutralColor),
|
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "start", fontSize, neutralColor),
|
||||||
escapeText(typeText),
|
svg.EscapeText(typeText),
|
||||||
),
|
),
|
||||||
|
|
||||||
fmt.Sprintf(`<text class="text" x="%f" y="%f" style="%s">%s</text>`,
|
fmt.Sprintf(`<text class="text" x="%f" y="%f" style="%s">%s</text>`,
|
||||||
|
|
@ -84,19 +85,6 @@ func tableRow(box *geo.Box, nameText, typeText, constraintText string, fontSize,
|
||||||
}, "\n")
|
}, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func constraintAbbr(constraint string) string {
|
|
||||||
switch constraint {
|
|
||||||
case "primary_key":
|
|
||||||
return "PK"
|
|
||||||
case "foreign_key":
|
|
||||||
return "FK"
|
|
||||||
case "unique":
|
|
||||||
return "UNQ"
|
|
||||||
default:
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func drawTable(writer io.Writer, targetShape d2target.Shape) {
|
func drawTable(writer io.Writer, targetShape d2target.Shape) {
|
||||||
fmt.Fprintf(writer, `<rect class="shape" x="%d" y="%d" width="%d" height="%d" style="%s"/>`,
|
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))
|
targetShape.Pos.X, targetShape.Pos.Y, targetShape.Width, targetShape.Height, shapeStyle(targetShape))
|
||||||
|
|
@ -114,15 +102,15 @@ func drawTable(writer io.Writer, targetShape d2target.Shape) {
|
||||||
)
|
)
|
||||||
|
|
||||||
var longestNameWidth int
|
var longestNameWidth int
|
||||||
for _, f := range targetShape.SQLTable.Columns {
|
for _, f := range targetShape.Columns {
|
||||||
longestNameWidth = go2.Max(longestNameWidth, f.Name.LabelWidth)
|
longestNameWidth = go2.Max(longestNameWidth, f.Name.LabelWidth)
|
||||||
}
|
}
|
||||||
|
|
||||||
rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)
|
rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)
|
||||||
rowBox.TopLeft.Y += headerBox.Height
|
rowBox.TopLeft.Y += headerBox.Height
|
||||||
for _, f := range targetShape.SQLTable.Columns {
|
for _, f := range targetShape.Columns {
|
||||||
fmt.Fprint(writer,
|
fmt.Fprint(writer,
|
||||||
tableRow(rowBox, f.Name.Label, f.Type.Label, constraintAbbr(f.Constraint), float64(targetShape.FontSize), float64(longestNameWidth)),
|
tableRow(rowBox, f.Name.Label, f.Type.Label, f.ConstraintAbbr(), float64(targetShape.FontSize), float64(longestNameWidth)),
|
||||||
)
|
)
|
||||||
rowBox.TopLeft.Y += rowHeight
|
rowBox.TopLeft.Y += rowHeight
|
||||||
fmt.Fprintf(writer, `<line x1="%f" y1="%f" x2="%f" y2="%f" style="stroke-width:2;stroke:#0a0f25" />`,
|
fmt.Fprintf(writer, `<line x1="%f" y1="%f" x2="%f" y2="%f" style="stroke-width:2;stroke:#0a0f25" />`,
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,11 @@ import (
|
||||||
"oss.terrastruct.com/d2/d2renderers/d2fonts"
|
"oss.terrastruct.com/d2/d2renderers/d2fonts"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
PrefixPadding = 10
|
||||||
|
PrefixWidth = 20
|
||||||
|
)
|
||||||
|
|
||||||
type Class struct {
|
type Class struct {
|
||||||
Fields []ClassField `json:"fields"`
|
Fields []ClassField `json:"fields"`
|
||||||
Methods []ClassMethod `json:"methods"`
|
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 {
|
type ClassMethod struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Return string `json:"return"`
|
Return string `json:"return"`
|
||||||
|
|
@ -42,3 +58,14 @@ func (cm ClassMethod) Text() *MText {
|
||||||
Shape: "class",
|
Shape: "class",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cm ClassMethod) VisibilityToken() string {
|
||||||
|
switch cm.Visibility {
|
||||||
|
case "protected":
|
||||||
|
return "#"
|
||||||
|
case "private":
|
||||||
|
return "-"
|
||||||
|
default:
|
||||||
|
return "+"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,3 +31,16 @@ func (c SQLColumn) Text() *MText {
|
||||||
Shape: "sql_table",
|
Shape: "sql_table",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c SQLColumn) ConstraintAbbr() string {
|
||||||
|
switch c.Constraint {
|
||||||
|
case "primary_key":
|
||||||
|
return "PK"
|
||||||
|
case "foreign_key":
|
||||||
|
return "FK"
|
||||||
|
case "unique":
|
||||||
|
return "UNQ"
|
||||||
|
default:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
12
lib/svg/text.go
Normal file
12
lib/svg/text.go
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
package svg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/xml"
|
||||||
|
)
|
||||||
|
|
||||||
|
func EscapeText(text string) string {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
_ = xml.EscapeText(buf, []byte(text))
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue